执行命令“ / group”和“ / group example_player”时,出现NullPointerException。
它没有给我任何更多有关问题所在(或位置)的信息。
我希望StackOverflow可以提供帮助。

Stym.java:

import org.bukkit.plugin.java.JavaPlugin;

public class Stym extends JavaPlugin {

    @Override
    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
        registerCommands();
        registerListeners();

    }

    public void registerCommands() {
        GroupCommand groupCommandClass = new GroupCommand(this);
        getCommand("GROUP").setExecutor(groupCommandClass);

    }

    public void registerListeners() {

    }

    public boolean isLeather(String playerName) {
        for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
            if(!ironPlayerName.equals(playerName)) {
                for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                    return !diamondPlayerName.equals(playerName);

                }

            } else {
                return false;

            }

        }

        return false;

    }

    public boolean isIron(String playerName) {
        for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
            if(ironPlayerName.equals(playerName)) {
                for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                    return !diamondPlayerName.equals(playerName);

                }

            } else {
                return false;

            }

        }

        return false;

    }

    public boolean isDiamond(String playerName) {
        for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
            if(!ironPlayerName.equals(playerName)) {
                for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                    return diamondPlayerName.equals(playerName);

                }

            } else {
                return false;

            }

        }

        return false;

    }

}


GroupCommand.java:

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

public class GroupCommand implements CommandExecutor {

    Stym stym;

    GroupCommand(Stym stymClass) {

    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] arguments) {
        try {
            if(arguments.length == 0) {
                if(sender instanceof Player) {
                    if(!sender.isOp()) {
                        if(stym.isLeather(sender.getName())) {
                            sender.sendMessage("You are made out of leather!");

                        } else if(stym.isIron(sender.getName())) {
                            sender.sendMessage("You are made out of iron!");

                        } else {
                            sender.sendMessage("You are made out of diamond!");

                        }

                    } else {
                        sender.sendMessage("You are an operator!");

                    }

                } else {
                    sender.sendMessage("You are a god!");

                }

            } else if(arguments.length == 1) {
                if(!arguments[0].equals("Krischon")) {
                    if(stym.isLeather(sender.getName())) {
                        sender.sendMessage(arguments[0] + " " + "is made out of leather!");

                    } else if(stym.isIron(sender.getName())) {
                        sender.sendMessage(arguments[0] + " " + "is made out of iron!");

                    } else {
                        sender.sendMessage(arguments[0] + " " + "is made out of diamond!");

                    }

                } else {
                    sender.sendMessage("Krischon is an operator!");

                }

            } else {
                sender.sendMessage("Not supported yet!");

            }

        } catch(Exception exception) {
            System.out.println(exception);

        }

        return true;

    }

}


plugin.yml:

name: Stym
main: Stym
version: 1.3.3.7
commands:
  group:


config.yml:

groups:
  iron:
    - Player
    - Another_Player
  diamond:
    - Rich_Player

最佳答案

Stym stym;

GroupCommand(Stym stymClass) {

}


您从未在构造函数中初始化Stym stym。因此,当您尝试在方法中使用它时,它是null

Stym stym;

GroupCommand(Stym stymClass) {
    stym = stymClass;
}


对于循环

for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
    return !diamondPlayerName.equals(playerName);

}


这些循环的设置方式,它们只会评估列表中的名字并返回相应的布尔值。当您return一个值时,该方法将终止。您最好使用这样的方法:

public boolean isLeather(String playerName) {
    for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
        if(ironPlayerName.equals(playerName)) {
            return false;
        }
    }
    for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
        if(diamondPlayerName.equals(playerName)) {
            return false;
        }
    }
    return true;
}


这样,它将遍历铁杆和菱形列表,并且如果在这些列表之一中找到了玩家名称,它将立即返回false。如果它在两个列表中都没有找到播放器名称,它将返回true。您还需要对isIronisDiamond做类似的事情。

如果您还有"groups.leather",则可以更简单地完成此操作。

public boolean isLeather(String playerName) {
    for(String leatherPlayerName : getConfig().getStringList("groups.leather")) {
        if(leatherPlayerName.equals(playerName)) {
            return true;
        }
    }
    return false;
}

public boolean isIron(String playerName) {
    for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
        if(ironPlayerName.equals(playerName)) {
            return true;
        }
    }
    return false;
}

public boolean isDiamond(String playerName) {
    for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
        if(diamondPlayerName.equals(playerName)) {
            return true;
        }
    }
    return false;
}


这种方法只会在相应的列表中搜索皮革/铁/钻石。由于无论如何都在Stym中调用所有3个方法,所以这是理想的选择。

08-24 17:34