我在编写一个简单的Minecraft 1.10测试插件时在Eclipse Java Mars上遇到以下错误:"无法从CommandSender类型对非静态方法sendMessage(String)进行静态引用。"除了主类之外,这是一个单独的类,仅用于命令。这是整个班级:
package io.github.ultraMLGcode.TestPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class TestPluginCommandExecutor implements CommandExecutor {
public TestPlugin plugin;
public TestPluginCommandExecutor(TestPlugin instance) {
plugin = instance;
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("basic") && sender instanceof Player) { //If the player typed /basic then do the following...
if (sender instanceof Player) {
Player player = (Player) sender;
//do something
Player.sendMessage("Hello, it is a nice day, isn't it?");
} else {
sender.sendMessage("You must be a player!");
}
//doSomething
return true;
}
return false;
}
}
答案 0 :(得分:1)
我不知道Minecraft api,但我认为这有助于:
更改此行:
Player.sendMessage("Hello, it is a nice day, isn't it?");
到这一行
player.sendMessage("Hello, it is a nice day, isn't it?");
我认为“sendMessage”是一种非静态方法。
我希望它有所帮助。