首页 > 解决方案 > Minecraft 插件加载但命令未知

问题描述

我一直在为 Eclipse 上的 Minecraft 创建一个插件,但我遇到了一个错误。插件加载正常;我可以在控制台中看到它,但是当我尝试运行命令时它会显示未知命令。

这是我的命令代码:

package me.TheThunder56.helloworld.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import me.TheThunder56.helloworld.Main;

@SuppressWarnings("unused")
public class HelloCommand implements CommandExecutor{

    private Main plugin;
    
    public HelloCommand(Main plugin) {
        this.plugin = plugin;
        plugin.getCommand("sword").setExecutor(this);
        
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(!(sender instanceof Player)) {
            sender.sendMessage("Only players may execute this command!");
            return true;
        }
        
        Player p = (Player) sender;
        
        if(p.hasPermission("sword.use")) {
            p.performCommand("/give TheThunder56 netherite_sword{Unbreakable:1,Enchantments:[{id:knockback,lvl:1000}]}");
            return true;
        }else {
            p.sendMessage("You do not have permission to execute this command.");
            
        }
        return false;
    }
}

这是我的 Main.java:

package me.TheThunder56.helloworld;

import org.bukkit.plugin.java.JavaPlugin;

import me.TheThunder56.helloworld.commands.HelloCommand;

public class Main extends JavaPlugin{

    @Override
    public void onEnable() {
        getCommand("sword").setExecutor(new HelloCommand(this));
    }
}

这是我的 plugin.yml 文件:

name: GodSword
main: me.TheThunder56.helloworld.Main
version: 1.0
author: TheThunder56
commands:
  sword:
    description: A command to give you a godly sword.
    usage: /sword

请帮忙!谢谢。

标签: minecraft

解决方案


private Main plugin;
    
    public HelloCommand(Main plugin) {
        this.plugin = plugin;
        plugin.getCommand("sword").setExecutor(this);

 

    public void onEnable() {
        getCommand("sword").setExecutor(new HelloCommand(this));
    } 

你为什么要注册你的命令两次?没必要。仅在您的 onenable 中注册该命令。此外,对于你给玩家剑的方法,你不应该使用 p.performCommand();,因为即使他们有权限,他们仍然需要 op 才能使用 /give。

尝试使用这个:

ItemStack sword = new ItemStack(Material.NETHERITE_SWORD));
ItemMeta meta = sword.getItemMeta();
meta.addUnsafeEnchantment(Enchantment.KNOCKBACK, 1000, true);
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
meta.setUnbreakable(true);
meta.setCustomName(ChatColor.GOLD + "GOD Sword"));
sword.setItemMeta(meta);

int i = 0;

while(true){
   for(ItemStack it = p.getInventory(){
        if(it == null){
            p.getInventory().setItem(i, sword);
            break;
        }else{
           i++;
        }
    }
}



推荐阅读