仅测试列表的第一个值

时间:2015-11-09 01:35:50

标签: java bukkit

我的问题是,只有我config.yml内的列表的第一个值才有效“才能进行测试。想象一个for循环,它测试一个包含三个值的列表: “FirstString”,“SecondString”和“ThirdString”。如果它找到一个设置String,比如“SecondString”,那么就做点什么。我的for循环只测试第一个值。 所以,在我的例子中,它永远不会找到“SecondString”,即使它在列表中也很难。我希望你理解我的问题,即使我很难解释我的问题。

Stym.java(主要课程):

import org.bukkit.plugin.java.JavaPlugin;

public class Stym extends JavaPlugin {

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

    }

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

    }

    public boolean isGroup(String groupName, String playerName) {
        if(groupName.equalsIgnoreCase("LEATHER")) {
            for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
                if(!ironPlayerName.equals(playerName)) {
                    for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                        if(!diamondPlayerName.equals(playerName)) {
                            return true;

                        } else {
                            return false;

                        }

                    }

                } else {
                    return false;

                }

            }

        } else if(groupName.equalsIgnoreCase("IRON")) {
            for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
                if(ironPlayerName.equals(playerName)) {
                    return true;

                } else {
                    return false;

                }

            }

        } else if(groupName.equalsIgnoreCase("DIAMOND")) {
            for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                if(diamondPlayerName.equals(playerName)) {
                    return true;

                } else {
                    return false;

                }

            }

        }

        return false;

    }

}

GroupCommand(执行课程):

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

public class GroupCommand implements CommandExecutor {

    Stym stym;

    GroupCommand(Stym stymClass) {
        stym = stymClass;

    }

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

                    } else if(stym.isGroup("IRON", sender.getName())) {
                        sender.sendMessage("You are made out of iron!");
                        return true;

                    } else if(stym.isGroup("DIAMOND", sender.getName())) {
                        sender.sendMessage("You are made out of diamond!");
                        return true;

                    } else {
                        sender.sendMessage("You are a cyborg!");
                        return true;

                    }

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

                }

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

            }

        } else if(arguments.length == 1) {
            if(stym.isGroup("LEATHER", arguments[0])) {
                sender.sendMessage(arguments[0] + " " + "is made out of leather!");
                return true;

            } else if(stym.isGroup("IRON", arguments[0])) {
                sender.sendMessage(arguments[0] + " " + "is made out of iron!");
                return true;

            } else if(stym.isGroup("DIAMOND", arguments[0])) {
                sender.sendMessage(arguments[0] + " " + "is made out of diamond!");
                return true;

            } else {
                sender.sendMessage(arguments[0] + " " + "is a cyborg!");
                return true;

            }

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

        }

    }

}

plugin.yml

name: Stym
main: Stym
version: 1.3.3.7
commands:
  group:
    description: Some fancy description
    usage: /group or /group <player_name>

config.yml

groups:
  iron:
    - Player //Works well and will find
    - Another_Player // Won't find
  diamond:
    - Rich_Player //Works well and will find

1 个答案:

答案 0 :(得分:0)

我认为这只是归结为一些逻辑错误。据我所知,你的代码中,只有当他既不是铁牌手也不是钻石球员名单时,玩家都属于“皮革”组。如果铁玩家列表包含他们的名字,则玩家在“铁”组中;如果钻石玩家列表包含他们的名字,则在“钻石”组中。所有这些都需要进行一些检查,以查看是否可以在特定的数组/列表中找到给定的字符串。

你的for循环检查数组中包含的任何String是否与playerName匹配,这是正确的方向,但你的方法返回false,如果有的话,甚至遇到的元素不等于playerName

如果我们有一个列表:{“Player”,“Notch”,“Jeb_”},我们想知道其中是否包含String“Notch”,您的代码将返回false,因为遇到的第一个元素(“玩家”)不等于“Notch”。对于应该查看列表中任何元素是否与给定值匹配的代码,这没有意义。

如果您正在处理数组,您可以编写自己的一般方法来检查给定的String是否在数组中,如下所示:

public static boolean contains(String value, String[] array) {
    for (String element : array) { // For each element in array
        if (element.equals(value)) { // If we have found the matching element -> return true (we don't need to keep looking)
            return false;
        }
    }
    return false; // No match was found -> return false
}

在这段代码中,如果我们遍历的当前元素与我们正在寻找的字符串匹配,我们可以立即返回true,从而退出循环。如果已遍历所有元素,则该方法仅返回false,并且我们找不到单个匹配项。

由于您在配置文件的上下文中处理List<String>,您只需使用contains(String)方法检查播放器名称列表是否包含某个名称,同时缩短您的代码并使其更易于阅读。

你的方法可能看起来像这样,应该有效:

public boolean isGroup(String groupName, String playerName) {

    List<String> ironPlayers = getConfig().getStringList("groups.iron"); // Get list of names in iron group
    List<String> diamondPlayers = getConfig().getStringList("groups.diamond"); // Get list of names in diamond group

    boolean isIron = ironPlayers.contains(playerName); // Boolean value whether playerName is in iron group
    boolean isDiamond = diamondPlayers.contains(playerName); // Boolean value whether playerName is in diamond group

    if (groupName.equalsIgnoreCase("LEATHER") && !isIron && !isDiamond) {
        // Return true for leather if the player name is not in the iron and diamond group
        return true; 
    } else if (groupName.equalsIgnoreCase("IRON") && isIron && !isDiamond) {
        // Return true for iron if the player name is in the iron group but not in the diamond group
        return true;
    } else if (groupName.equalsIgnoreCase("DIAMOND") && !isIron && isDiamond) {
        // Return true for diamond if the player name is in the diamond group but not in the iron group
        return true;
    }
    return false; // In any other case return false (player could be in both lists, or invalid groupName)
}