英文:
Minecraft Spigot Command Not Enabling in Main Class
问题
以下是您要翻译的内容:
我的命令(具体仅限于我的cmd2命令)无法注册,并且在我启动服务器时控制台显示错误。另一个命令cmd1有效,但cmd2无效。我真的不知道为什么,所以我来这里寻求帮助。
一些主类的代码:
package me.Vamp.Test;
import me.Vamp.Test.Events.EventsClass;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private Commands commands = new Commands();
@Override
public void onEnable() {
/* 启用插件 */
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nTest插件已启用。\n\n");
/* 事件注册 */
getServer().getPluginManager().registerEvents(new EventsClass(), this);
/* 命令注册 */
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
}
}
以下类(Commands)仅显示错误的命令(cmd2)。如果需要cmd1的代码,我会展示它。
一些命令类的代码:
package me.Vamp.Test;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
public class Commands implements Listener, CommandExecutor {
public String cmd2 = "getpickaxe";
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
/*
/getPickaxe 命令
*/
if (cmd.getName().equalsIgnoreCase(cmd2)) {
Player player = (Player) sender;
if (args.length == 0) {
commandGetPickaxe(player);
return true;
} else {
player.sendMessage(Colors.chat("&c&lERROR &c太多参数&8。"));
return true;
}
}
} else {
sender.sendMessage(Colors.chat("&c&lERROR &c只有玩家可以使用此命令&8。"));
return true;
}
return false;
}
public void commandGetPickaxe(Player player){
Inventory inv = player.getInventory();
ItemStack item = new ItemStack(Material.WOOD_PICKAXE, 1);
ItemMeta meta = item.getItemMeta();
ArrayList<String> lore = new ArrayList<String>();
meta.setDisplayName(Colors.chat("&3木镐"));
lore.add(Colors.chat("&7&o入门用的镐子&8&o。"));
meta.setLore(lore);
item.setItemMeta(meta);
inv.addItem(new ItemStack(item));
player.sendMessage(Colors.chat("&8&l» &3你获得了一个木镐&8。"));
}
}
这只是控制台上显示的错误。
我的控制台:
英文:
My Command (Specifically only my cmd2 command) doesn't register, and the console displays an error when I start the server. The other command, cmd1, works, but cmd2 doesn't. I'm really not sure why, so I came here for help.
Some of my Main Class:
package me.Vamp.Test;
import me.Vamp.Test.Events.EventsClass;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private Commands commands = new Commands();
@Override
public void onEnable() {
/* Enabler */
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\nTest Plugin has been enabled.\n\n");
/* Events Register */
getServer().getPluginManager().registerEvents(new EventsClass(), this);
/* Commands Register */
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
}
}
The following Class (Commands) only show for the errored command (cmd2). If the code for cmd1 is needed, I will show it.
Some of My Command Class:
package me.Vamp.Test;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
public class Commands implements Listener, CommandExecutor {
public String cmd2 = "getpickaxe";
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
/*
/getPickaxe Command
*/
if (cmd.getName().equalsIgnoreCase(cmd2)) {
Player player = (Player) sender;
if (args.length == 0) {
commandGetPickaxe(player);
return true;
} else {
player.sendMessage(Colors.chat("&c&lERROR &cToo many arguments&8."));
return true;
}
}
} else {
sender.sendMessage(Colors.chat("&c&lERROR &cOnly players can use this command&8."));
return true;
}
return false;
}
public void commandGetPickaxe(Player player){
Inventory inv = player.getInventory();
ItemStack item = new ItemStack(Material.WOOD_PICKAXE, 1);
ItemMeta meta = item.getItemMeta();
ArrayList<String> lore = new ArrayList<String>();
meta.setDisplayName(Colors.chat("&3Wooden Pickaxe"));
lore.add(Colors.chat("&7&oThe Starter Pickaxe&8&o."));
meta.setLore(lore);
item.setItemMeta(meta);
inv.addItem(new ItemStack(item));
player.sendMessage(Colors.chat("&8&l» &3You have received a Wooden Pickaxe&8."));
}
}
This is only the display error on my console.
My Console:
答案1
得分: 0
我可以建议您添加大量的打印语句来查看哪些是空的吗?
/* 命令注册 */
System.out.println("cmd1 " + commands.cmd1);
System.out.println("cmd2 " + commands.cmd2);
System.out.println("cmdObj " + commands);
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
编辑 1:看起来您的 plugin.yml 文件中缺少该命令?
有可能是拼写错误,请仔细检查。如果您认为一切都完全正常,但错误仍然发生,请编辑您的原始帖子并包含 plugin.yml 文件。谢谢!
英文:
Can I suggest you add a multitude of print statements to see what is null?
/* Commands Register */
System.out.println("cmd1 " + commands.cmd1);
System.out.println("cmd2 " + commands.cmd2);
System.out.println("cmdObj " + commands);
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd2).setExecutor(commands);
EDIT 1: It seems as though you are missing the command in your plugin.yml?
It's possible that it is a typo, look carefully. If you think everything is perfectly fine, and the error still occurs, please edit you original post and include the plugin.yml file. Thanks!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论