【发布时间】:2019-08-02 10:55:46
【问题描述】:
我的问题是我有一个简单的 Minecraft 插件,我只想让它执行一个命令。我以前做过命令,所以这些步骤对我来说很清楚。我的 plugin.yml 设置正确(服务器检测到我添加的命令并显示它的帮助页面等),并且 onCommand() 函数的设置方式也与我在所有其他插件中的设置方式相同。插件本身可以工作(主要是我测试的 onEnable() 函数)但是 onCommand() 只是没有被调用。
我已经尝试过不同的 plugin.yml 格式以及将 @Override 注释添加到 onCommand() 中,Eclipse 并不真正希望我这样做。我也知道我使用的 API (com.PluginBase) 可以在其他项目中使用它。执行命令时不会产生异常,在聊天中只显示我输入的命令。
这是 Main.java:
package org.Professions;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.material.Command;
import org.bukkit.plugin.java.JavaPlugin;
import com.PluginBase.Chat;
public class Main extends JavaPlugin {
public void onEnable() {
Chat.getInstance().sendErrorMessageToConsole("Professions enabled");
Bukkit.getPluginCommand("profession").setExecutor((CommandExecutor) this);
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Chat.getInstance().sendErrorMessageToConsole("Got the command: " + label);
/*
* Check if command sender is a player
* and if the command is for this plugin
*/
if
(
sender instanceof Player == false
|| !label.toLowerCase().equals("profession")
) {
return true;
}
// Get the player who send the command
Player player = (Player) sender;
// Check if the player has given the right amount of arguments
if (args.length != 1) {
// Notify the player of invalid argument use
Chat.getInstance().sendMessageToPlayer(player, ChatColor.RED + "Invalid arguments. Usage: /profession <name>");
// Stop executing code after we've determined an incorrect amount of arguments
return true;
}
// Get the players new profession from the first argument he gave for the command
String profession = args[1];
// Set the players name in the playerlist to feature his professions
player.setPlayerListName
(
ChatColor.GREEN + "[" + profession + "] " // the players' profession
+ ChatColor.WHITE + player.getName() // the actual player name
);
// Always return true since if the command wasn't for this plugin we return false earlier
return true;
}
}
这是我的 plugin.yml:
name: Professions
main: org.Professions.Main
version: 1.0
api-version: 1.13
depend: [PluginBase]
commands:
profession:
description: Change your profession
usage: /<command>
aliases: [p]
【问题讨论】:
-
在 IntelliJ 中,您甚至可以通过单击生成重载方法。只需扩展一个类并按 Alt + Insert,然后选择“覆盖方法”,intellij 将采用所有正确的导入和方法签名。如果需要,我相信 Eclipse 会通过插件提供相同的功能