IDE没有错误,但ArrayList无法正常工作

时间:2013-06-22 11:14:21

标签: java arrays list arraylist bukkit

我在包RuneCraft中名为com.mcvigor的主类中有这个:

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

我在包MiningListener中名为com.mcvigor.listeners的另一个类中有这个:

`   public void playerRClickOre(PlayerInteractEvent event) { //Ore Interactions

    final Player p = event.getPlayer();

    if(event.getPlayer().isOp()) return;

    if(RuneCraft.prospPlayers.contains(p.getName())) return;

    if(isClickedBlockMineral(event) && event.getAction() == Action.LEFT_CLICK_BLOCK && !hasPickaxe(p)) { //block is mineral, player left clicked it, no pickaxe
        p.sendMessage(ChatColor.DARK_RED + "You need a pickaxe to mine this."); // ADD CHECKS FOR PICKAXE REQUIRED LEVEL 2 USE
    }

    if(isClickedBlockMineral(event) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {

        switch(event.getClickedBlock().getType()) {

        case CLAY: //clay
            RuneCraft.prospPlayers.add(p.getName());
            p.sendMessage(ChatColor.DARK_RED + "You examine the rock for ores...");
            Bukkit.getScheduler().runTaskLater(plugin, new Runnable(){
                public void run() {
                    p.sendMessage(ChatColor.GRAY + "This rock contains" + ChatColor.DARK_RED + " clay" + ChatColor.GRAY + ".");
                }
            }, 3*20L); // TODO: Change time
            RuneCraft.prospPlayers.remove(p.getName());
            break;`

还有其他东西,但这并不重要。问题是,当我为矿石勘探或检查岩石时,它应该将我添加到列表中,如果我再次尝试,它将不会让我直到我完成探矿(它从ArrayList中删除我的名字)

为什么会这样?

问题是if(RuneCraft.prospPlayers.contains(p.getName())) return;吗?

1 个答案:

答案 0 :(得分:2)

看看这段代码:

RuneCraft.prospPlayers.add(p.getName());
p.sendMessage(ChatColor.DARK_RED + "You examine the rock for ores...");
Bukkit.getScheduler().runTaskLater(plugin, new Runnable(){
    public void run() {
        p.sendMessage(ChatColor.GRAY + "This rock contains" + ChatColor.DARK_RED + " clay" + ChatColor.GRAY + ".");
    }
}, 3*20L); // TODO: Change time
RuneCraft.prospPlayers.remove(p.getName());

您在添加后再次删除名称​​ 并说您已开始检查它。您只想在完成检查后将其删除,对吗?因此,当“后续任务”执行时,您需要移动remove调用:

RuneCraft.prospPlayers.add(p.getName());
p.sendMessage(ChatColor.DARK_RED + "You examine the rock for ores...");
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    public void run() {
        p.sendMessage(ChatColor.GRAY + "This rock contains" + 
                      ChatColor.DARK_RED + " clay" + ChatColor.GRAY + ".");
        RuneCraft.prospPlayers.remove(p.getName());
    }
}, 3*20L); // TODO: Change time

不可否认,我会尽量避免使用静态变量,但这是另一回事。