0

RuneCraft在包中命名的主类中有这个com.mcvigor

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

我在另一个名为MiningListenerpackage的类中有这个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;问题吗?

4

1 回答 1

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

诚然,我会尽量避免为此使用静态变量,但这是另一回事。

于 2013-06-22T11:19:50.700 回答