简体   繁体   English

ArrayList无法正常工作

[英]ArrayList is not working as expected

(I'm still pretty new to coding) I'm trying to make a Minecraft plugin that has a command so that a player freezes when the command is typed, but my ArrayList is acting very weirdly. (我对编码还是很陌生的)我正在尝试制作一个具有命令的Minecraft插件,以便在键入命令时播放器冻结,但我的ArrayList行为异常。 When I type the command (/freeze) the plugin sends a message to me saying that the target has been frozen. 当我键入命令(/ freeze)时,插件会向我发送一条消息,说明目标已冻结。 When I check the list with a piece of code that says it in chat (code below) it says it is empty. 当我用一段在聊天中说出它的代码(下面的代码)检查列表时,它说它是空的。 But when I do the command again, the function unFreeze, that requires the ArrayList has the name, still, works. 但是,当我再次执行命令时,需要ArrayList名称的unFreeze函数仍然有效。 But if I move and freeze myself that way, the name is added to the list and it works, even the code that checks the list and says it in chat says it is there, but even if I walk and the playername gets added to the list, the text that says that I'm frozen or unfrozen doesn't seem to get affected, it just toggles between the two, and the command doesn't do anything else. 但是,如果我以这种方式移动并冻结自己,该名称将被添加到列表中并且可以使用,即使是检查该列表并在聊天中说出该代码的代码也表明该名称存在,但是即使我走了并且将玩家名称添加到了列表中,显示我被冻结或未冻结的文本似乎没有受到影响,只是在两者之间切换而已,该命令没有执行任何其他操作。

Code for checking the list: 检查列表的代码:

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (label.equalsIgnoreCase("seefrozenlist")){
        for (String s : freezeListener.frozen){
            sender.sendMessage(s);
        }
    }
    return true;
}

Code for adding the player: 添加播放器的代码:

if (freezeListener.frozen.contains(player.getName())){ //normally freezeListener.getFrozen.contains(...)
    freezeListener.unFreeze(player.getName());
    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',player.getDisplayName() + "&e is no longer &b&lFROZEN"));
    player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&eYou are no longer &b&lFROZEN"));
    return true;
}
else {
    //freezeListener.freeze(player.getName());
    freezeListener.freeze(player.getName());
    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',player.getDisplayName() + "&e is now &b&lFROZEN"));
    player.sendMessage(ChatColor.translateAlternateColorCodes('&',"&eYou are now &b&lFROZEN"));
    return true;
}

Class that has the ArrayList: 具有ArrayList的类:

public class FreezeListener implements Listener {
// was private, is public for testing
public ArrayList<String> frozen = new ArrayList<>();

public ArrayList<String> getFrozen(){
    return frozen;
}

public void freeze(String name){
    //if (!(frozen.contains(name))){
        frozen.add(name.trim());
        Bukkit.getServer().getPlayer(name).sendMessage("\"" + name + "\" Freeze");
    //}
}

public void unFreeze(String name){
    if (frozen.contains(name)){
        frozen.remove(name);
        Bukkit.getServer().getPlayer(name).sendMessage("\"" + name + "\" Unfreeze");
    }
}

@EventHandler
public void onPlayerMove(PlayerMoveEvent event){
    if (getFrozen().contains(event.getPlayer().getName().trim())){
        event.getPlayer().sendMessage("2");
        Location to = event.getFrom();
        to.setPitch(event.getTo().getPitch());
        to.setYaw(event.getTo().getYaw());
        event.setTo(to);
    }
    //For testing
    else {
        freeze(event.getPlayer().getName());
    }
}

Your problem is that you have few different instances of FreezeListener . 您的问题是FreezeListener实例FreezeListener The one which is used for adding/removing player is not the same that is read in chat with onCommand . 用于添加/删除播放器的播放器与与onCommand聊天时读取的播放器不同。 You should create new FreezeListener once and use it everywhere. 您应该一次创建一个新的FreezeListener并在所有地方使用它。 You should do something like that probably in main class: 您应该在主类中做类似的事情:

FreezeListener freezeListener = new FreezeListener();
FreezeCommand freezeCommand = new FreezeCommand( freezeListener); //<- This listener...

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (label.equalsIgnoreCase("seefrozenlist")){
        for (String s : freezeListener.frozen){ // <-...and this one are now the same listener and will share the list of frozen players
            sender.sendMessage(s);
        }
    }
    return true;
}

I can try to be more accurate if you show your main class. 如果您主修班级,我可以尝试更加准确。

The problem is with the object reference. 问题出在对象引用上。 Every time you create a new freezeListener , a new frozen list is initialised. 每次创建新的freezeListener ,都会初始化一个新的冻结列表。

In your class FreezeListener try this: 在您的FreezeListener类中,尝试以下操作:

public static ArrayList<String> frozen = new ArrayList<>();

Or you can use a single instance method as suggested by @Egan Wolf. 或者,您可以使用@Egan Wolf建议的单实例方法。

Whichever suits your need. 满足您的需求。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM