我的 bukkitplugin 中的命令不起作用

Command in my bukkitplugin not working

现在我已将 plugin.yml 中的 'Commands' 更改为 'commands',当我 运行 我的服务器时,我的 cmd 中出现另一个错误。错误显示“.Jar 文件不包含 plugin.yml”。

这是我现在的 plugin.yml:

name: Wand
version: 1.0
main: me.Pixel.Main
commands:
 wand:

这是我当前的主文件:

package me.Pixel;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {
    public Main plugin;
    public List<String> spells = new ArrayList<String>();
    public getTargets getTargets = new getTargets();
    public Spark spark = new Spark(this);
    public PoisonWave poisonwave = new PoisonWave(this);
    public DarkSpark darkSpark = new DarkSpark(this);

    @Override
    public void onEnable() {
        plugin = this;
        getServer().getPluginManager().registerEvents(this, this);
        spells.add("Spark");
        spells.add("PoisonWave");
        spells.add("DarkSpark");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command,
            String label, String[] args) {
        if(label.equalsIgnoreCase("wand")) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be an in-game player to perform this action!");
            } else {
                Player p = (Player) sender;
                if(sender.hasPermission("wand.wand")) {
                    ItemStack stack = new ItemStack(Material.BLAZE_ROD);
                    ItemMeta stackMeta = stack.getItemMeta();
                    stackMeta.setDisplayName(ChatColor.RED + "Empire Wand");
                    stack.setItemMeta(stackMeta);
                    p.getInventory().addItem(stack);
                    ChatUtilities.sendMessage(p, "You have got yourself a powerful Empire Wand!");
                } else {
                    ChatUtilities.sendMessage(p, ChatColor.RED + "ERROR: No Permission!");
                }
            }
        }
        return false;
    }

    @EventHandler
    public void onClick(PlayerInteractEvent e) {
        if((e.getAction() == Action.RIGHT_CLICK_AIR) || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Player p = e.getPlayer();
            ItemStack stack = p.getItemInHand();
            if(stack != null && stack.getType() == Material.BLAZE_ROD && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Empire Wand")) {
                int SpellSelected = stack.getDurability();
                if(SpellSelected < 2) {
                    stack.setDurability((short) (SpellSelected + 1));
                    p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, 119, 30);
                } else {
                    stack.setDurability((short) 0);
                }
                ChatUtilities.sendMessage(p, "Selected: " + spells.get(SpellSelected));
            }
        }
        if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
            Player p = e.getPlayer();
            ItemStack stack = p.getItemInHand();
            if(stack != null && stack.getType() == Material.BLAZE_ROD && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Empire Wand")) {
                int SpellSelected = stack.getDurability();
                if(SpellSelected == 1) {
                    this.spark.onCast(p);  
                } else if (SpellSelected == 0) {
                   this.poisonwave.onCast(p);
                }
            }
        }
    }
}

添加 p.updateInventory() 物品后,您可能需要更新玩家的物品栏。在您的情况下,它将是这样的:

p.getInventory().addItem(stack);
p.updateInventory();

plugin.yml 文件区分大小写,命令列表的名称需要全部小写(commands 而不是 Commands)。由于您当前已将其大写,因此 Bukkit/Spigot 不会注册任何命令,因此如果您测试 /wand 命令会导致“Unknown command. Type "/help" for help.”消息(我假设这是您的错误得到,因为你没有 describe the problem nor the expected behavior,但这是我测试代码时发生的事情,更正命令列表的名称使命令执行)。

name: Wand
version: 1.0
main: me.Pixel.Main
commands:
  wand:

您的 plugin.yml 文件无效。 'commands'必须全部小写,wand前必须有2个空格

此外,您应该那样发出命令。您应该使外部 类 实现 CommandExecutor 接口。

我想,如果你这样做,我认为你必须注册命令。 像这样:

getCommand("wand").setExecutor(this);