Arraylist和if语句不起作用

时间:2014-12-30 22:07:35

标签: java if-statement arraylist bukkit

我正在制作一个bukkit插件,它有一个切换选项。这是我用来进行切换的代码。在游戏中,唯一的响应是OFF,它应该在ON和OFF之间切换。请帮忙!

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    ArrayList<String> players = new ArrayList<String>();

    if (cmd.getName().equalsIgnoreCase("togglegift") && sender instanceof Player){

        Player Tplayer = (Player) sender;

        if(players.contains(Tplayer.getName())) {
            Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.GREEN + " ON");
            players.remove(Tplayer.getName());

        } else {
        players.add(Tplayer.getName());
        Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.RED + " OFF");
        }
    }

3 个答案:

答案 0 :(得分:2)

与评论中提到的@ZouZou一样,每次重新调用方法时,都会列出players列表。

因此,它总是空的,玩家的名字永远不会包含在其中。

要解决此问题,请将列表players的声明移出您的方法。

public class ThaClassInWhichYourMethodIsDeclared {
    private ArrayList<String> players = new ArrayList<String>();


    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


        if (cmd.getName().equalsIgnoreCase("togglegift") && sender instanceof Player) {

            Player Tplayer = (Player) sender;

            if (players.contains(Tplayer.getName())) {
                Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.GREEN + " ON");
                players.remove(Tplayer.getName());

            } else {
                players.add(Tplayer.getName());
                Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.RED + " OFF");
            }
        }

}

包括@LuiggiMendoza的评论,它看起来像这样:

public class ThaClassInWhichYourMethodIsDeclared {
    private Set<String> players = new HashSet<String>();


    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (cmd.getName().equalsIgnoreCase("togglegift") && sender instanceof Player) {

            Player Tplayer = (Player) sender;

            if (players.contains(Tplayer.getName())) {
                Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.GREEN + " ON");
                players.remove(Tplayer.getName());

            } else {
                players.add(Tplayer.getName());
                Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.RED + " OFF");
            }
        }

}

答案 1 :(得分:0)

它完全取决于您创建的类实例。 如果只创建一次类对象,则可以在函数外部创建arraylist实例变量。如果多次创建类对象,则再次将arraylist初始化两次。

答案 2 :(得分:-3)

您必须将列表移动为静态列表。在您的类声明中 class 范围(或实例范围仅限私有)。如果您没有,如@ZouZou所说,该列表仅在本地使用,因此在方法范围内创建。

public class <YOUR_CLASS_NAME> extends JavaPlugin {

    /**
     * Declare your list as class scope.
     */
    private static final List<String> PLAYERS;

    // Static block is used to do not instanciate variable as attribute declaration.
    static {
        PLAYERS = new ArrayList<>();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("togglegift") && sender instanceof Player) {

            Player Tplayer = (Player) sender;

            if (PLAYERS.contains(Tplayer.getName())) {
                Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.GREEN + " ON");
                PLAYERS.remove(Tplayer.getName());

            } else {
                PLAYERS.add(Tplayer.getName());
                Tplayer.sendMessage(ChatColor.GOLD + "[BapGift] " + ChatColor.LIGHT_PURPLE + "Gifts Toggled" + ChatColor.RED + " OFF");
            }
        }
        return true;
    }