子命令Bukkit中的内部错误

时间:2017-12-07 15:25:05

标签: java minecraft bukkit

我最近在使用bukkit编码时遇到了问题。当我启动插件并输入/ akitz 命令标签或名称时,会弹出一个内部错误,但是当我键入/ akitz {subcommand}时它工作正常!这里的一切基本上都运行正常,但只是/ akitz< - 当我输入它时,会弹出一个错误!这是代码:

package me.frostgamersa;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {
@Override
public void onEnable() {

}

@Override
public void onDisable() {

}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (label.equalsIgnoreCase("akitz")) {
        Player player = (Player) sender;

        if (args.length == 0 || args.length < 2) {
            player.sendMessage("§cIncorrect Arguements");

            if (args[0].equalsIgnoreCase("legend")) {
                player.sendMessage("§bYou have been Granted §3Legend §bKit!");
                CustomItems.givela(player);
                CustomItems.givels(player);
                player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 64));
                return true;
            }
            if (args[0].equalsIgnoreCase("god")) {
                player.sendMessage("§bYou have been Granted §3God §bKit!");
                CustomItems.givega(player);
                CustomItems.givegs(player);
                player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 64));
                return true;
            } else {
                player.sendMessage("§cIncorrect Arguements!");
                return true;
            }
        }

2 个答案:

答案 0 :(得分:0)

Spoonfeed来了:

package me.frostgamersa;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {
@Override
public void onEnable() {

}

@Override
public void onDisable() {

}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (label.equalsIgnoreCase("akitz")) {
        Player player = (Player) sender;

        if (args.length < 2) {
            player.sendMessage("§cIncorrect Arguements");
        } else {
            if (args[0].equalsIgnoreCase("legend")) {
                player.sendMessage("§bYou have been Granted §3Legend §bKit!");
                CustomItems.givela(player);
                CustomItems.givels(player);
                player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 64));
                return true;
            }

            if (args[0].equalsIgnoreCase("god")) {
                player.sendMessage("§bYou have been Granted §3God §bKit!");
                CustomItems.givega(player);
                CustomItems.givegs(player);
                player.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 64));
                return true;
            } else {
                player.sendMessage("§cIncorrect Arguements!");
                return true;
            }
        }
    }
}

我还建议您删除onEnable和onDisable,因为这些不是必需的,因为它们中没有任何内容,它只是占用了它不需要的空间。

此外,您应该在投射之前检查发件人是否为播放器,因为这可能会在控制台执行命令时产生错误。

答案 1 :(得分:0)

这就是命令的样子......

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    Player p = (Player) sender;

    if (cmd.getName().equalsIgnoreCase("akitz")) {

        if (args.length != 1) {

            p.sendMessage("§cIncorrect argument count!");

        }

        if (args[0].equalsIgnoreCase("legend")) {

            p.sendMessage("§bYou got a kit §3Legend");
            CustomItems.givela(p);
            CustomItems.givels(p);
            p.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 64));
            return true;

        }

        if (args[0].equalsIgnoreCase("god")) {

            p.sendMessage("§bYou got a kit §3God");
            CustomItems.givega(p);
            CustomItems.givegs(p);
            p.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 64));
            return true;

        }

        if (!args[0].equalsIgnoreCase("legend") | !args[0].equalsIgnoreCase("god")) {

            p.sendMessage("§cIncorrect argument!");

        }

    }

    return false;

}

另外,让你的类实现CommandExecutor并添加到你的onEnable方法..

    public void onEnable() {

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

}

将注册命令,只是为了确定