4

我知道已经有其他主题,但他们的解决方案未能成功地帮助我。我正在创建一个插件,并且有一个名为 CommandErrors 的类,其中只有静态引用和方法。这是控制台中打印出的错误:

控制台错误

严重:无

org.bukkit.command.CommandException: Unhandled exception executing command 'capturetheflag' in plugin CaptureTheFlag v1.0<br />
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)<br />
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)<br />
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:543)<br />
    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:923)<br />
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:803)<br />
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28)<br />
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47)<br />
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146)<br />
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)<br />
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:645)<br />
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:243)<br />
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:535)<br />
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:447)<br />
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)<br />
Caused by: java.lang.NoSuchMethodError: <br />net.strikecraft.Commands.CommandErrors.throwError(Lorg/bukkit/command/CommandSender;Lnet/strikecraft/Commands/CTFCommandExecutor;Ljava/lang/String;)Ljava/lang/String;<br />
    at net.strikecraft.Commands.CTFCommand.onCommand(CTFCommand.java:28)<br />
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)<br />
    ... 13 more


CTFCommand.onCommand()

 package net.strikecraft.Commands;

 import java.util.ArrayList;

 import org.bukkit.command.Command;
 import org.bukkit.command.CommandExecutor;
 import org.bukkit.command.CommandSender;
 import org.bukkit.entity.Player;

 public class CTFCommand implements CommandExecutor {

private static ArrayList<CTFCommandExecutor> commands = new ArrayList<CTFCommandExecutor>();
public static CTFCommandExecutor registerCommand(CTFCommandExecutor cmd){
    commands.add(cmd);
    return cmd;
}
public static ArrayList<CTFCommandExecutor> getCommands(){
    return commands;
}

public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    if(!(sender instanceof Player)){
        CommandErrors.throwError(sender, null, CommandErrors.NOT_A_PLAYER);
        return true;
    }
    Player player = (Player)sender;
    if(args.length == 0){
        CommandErrors.throwError(player, null, CommandErrors.COMMAND_NOT_FOUND);
        return true;
    }
    CTFCommandExecutor ctfCommand = null;
    for(CTFCommandExecutor c : commands){
        if(c.command().equalsIgnoreCase(args[0]))
            ctfCommand = c;
    }
    if(ctfCommand == null){
        CommandErrors.throwError(player, null, CommandErrors.COMMAND_NOT_FOUND);
        return true;
    }
    if(ctfCommand.permission() != null && !player.hasPermission(ctfCommand.permission())){
        CommandErrors.throwError(sender, ctfCommand, CommandErrors.NO_PERMISSION);
        return true;
    }
    if(ctfCommand.validArgsCount() != -1 && args.length != ctfCommand.validArgsCount()){
        CommandErrors.throwError(sender, ctfCommand, CommandErrors.INCORRECT_USAGE);
        return true;
    }
    String[] ctfCommandArgs = new String[args.length-1];
    for(int x=1; x<args.length; x++){
        ctfCommandArgs[x-1] = args[x];
    }
    ctfCommand.onCommand(player, ctfCommandArgs);
    return true;
}

 }

命令错误

 package net.strikecraft.Commands;

 import net.strikecraft.CaptureTheFlag;

 import org.bukkit.ChatColor;
 import org.bukkit.command.CommandSender;

 public class CommandErrors {

public static String throwError(CommandSender sender, CTFCommandExecutor subCmd, String error){
    if(error.startsWith("#")){
        String hashError = null;
        if(error.equals(INCORRECT_USAGE)){
            hashError = "Incorrect usage. Type /ctf "+subCmd.usage();
            sender.sendMessage(CaptureTheFlag.PREFIX+ChatColor.RED+hashError);
        }
        return hashError;
    }

    String errorMsg = CaptureTheFlag.PREFIX+ChatColor.RED+error;
    sender.sendMessage(errorMsg);
    return error;
}

public static final String NOT_A_PLAYER = "You must be a player in order to perform this action.";

public static final String NO_PERMISSION = "You do not have permission to this command.";
public static final String COMMAND_NOT_FOUND = "Command not found. Type /ctf help";
public static final String GAME_NOT_FOUND = "Game not found.";
public static final String GAME_ALREADY_EXISTS = "A game with this name already exists.";

public static final String INCORRECT_USAGE = "#incorrect-usage";

 }

如何解决此 NoSuchMethod 错误?我确定方法和类确实存在。谢谢!

4

1 回答 1

0

我发现的不是问题,而是问题的解决方案。虽然我确信这绝对是更好的编程实践,而且通常更好地以正确的方式解决问题本身,但我发现创建一个具有不同名称但具有相同主体的类可以解决问题。

希望这对其他人有帮助!

于 2014-04-01T02:07:53.317 回答