ArrayList未按预期工作

时间:2017-08-08 09:02:06

标签: java

(我还是很擅长编码) 我正在尝试创建一个具有命令的Minecraft插件,以便在键入命令时玩家冻结,但我的ArrayList表现得非常奇怪。 当我输入命令(/ freeze)时,插件会向我发送一条消息,说明目标已被冻结。当我用一段在聊天中显示它的代码(下面的代码)检查列表时,它表示它是空的。但是当我再次执行命令时,需要ArrayList的函数unFreeze仍然有效。但是,如果我以这种方式移动并冻结自己,名称将被添加到列表中并且它可以工作,甚至是检查列表并在聊天中说它的代码说它存在,但即使我走路并且玩家名称被添加到列表,说我冻结或解冻的文本似乎没有受到影响,它只是在两者之间切换,命令没有做任何其他事情。

检查列表的代码:

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

添加播放器的代码:

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

具有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());
    }
}

2 个答案:

答案 0 :(得分:0)

您的问题是您有FreezeListener的几个不同实例。用于添加/删除播放器的播放器与在onCommand聊天时读取的播放器不同。您应该创建一次新的FreezeListener并在任何地方使用它。您应该在主要类中执行类似的操作:

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

如果你展示你的主要课程,我可以尝试更准确。

答案 1 :(得分:0)

问题在于对象引用。每次创建新的if (!Array.from) { console.error("Your browser doesn't support Array.from yet."); } else { const htmlArray = Array.from(document.querySelectorAll("div.menu-item"), e => e.innerHTML); console.log(htmlArray); }时,都会初始化新的冻结列表。

在班级<div>I'm not included in the result because I don't have the class</div> <div class="menu-item">Menu item #1</div> <div class="menu-item">Menu item #2</div> <div class="menu-item">Menu item #3</div>中试试这个:

curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/my_collection/update' --data-binary '
[
 {
"id" : "978-0641723445",
 "cat" : ["book","hardcover"],
 "name" : "The Lightning Thief",
 "author" : "Rick Riordan",
 "series_t" : "Percy Jackson and the Olympians", 
 "sequence_i" : 1,
 "genre_s" : "fantasy",
 "inStock" : true,
 "price" : 12.50,
 "pages_i" : 384
  },
  {
  ...
  }
]'

或者您可以使用@Egan Wolf建议的单实例方法。

适合您的需要。

感谢。