Minecraft Spigot命令在主类中无法启用

huangapple go评论82阅读模式
英文:

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。"));
    }
}

这只是控制台上显示的错误。

我的控制台:

Console

英文:

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 + &quot;\n\nTest Plugin has been enabled.\n\n&quot;);
/* 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 = &quot;getpickaxe&quot;;
@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(&quot;&amp;c&amp;lERROR &amp;cToo many arguments&amp;8.&quot;));
return true;
}
}
} else {
sender.sendMessage(Colors.chat(&quot;&amp;c&amp;lERROR &amp;cOnly players can use this command&amp;8.&quot;));
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&lt;String&gt; lore = new ArrayList&lt;String&gt;();
meta.setDisplayName(Colors.chat(&quot;&amp;3Wooden Pickaxe&quot;));
lore.add(Colors.chat(&quot;&amp;7&amp;oThe Starter Pickaxe&amp;8&amp;o.&quot;));
meta.setLore(lore);
item.setItemMeta(meta);
inv.addItem(new ItemStack(item));
player.sendMessage(Colors.chat(&quot;&amp;8&amp;l&#187; &amp;3You have received a Wooden Pickaxe&amp;8.&quot;));
}
}

This is only the display error on my console.

My Console:

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(&quot;cmd1 &quot; + commands.cmd1);
System.out.println(&quot;cmd2 &quot; + commands.cmd2);
System.out.println(&quot;cmdObj &quot; + 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!

huangapple
  • 本文由 发表于 2020年3月17日 03:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定