NoSuchMethod错误Java但存在方法

时间:2014-03-31 01:17:00

标签: java plugins bukkit nosuchmethod

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

控制台错误

SEVERE:null

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;
}

 }

CommandErrors

 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错误?我知道方法和类确实存在。 谢谢!

1 个答案:

答案 0 :(得分:0)

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

希望这有助于其他人!