每次重新加载时命令只能运行一次

时间:2016-11-17 10:48:20

标签: java minecraft bukkit

所以我刚刚学会了使用计划,我想做一个重复的任务,但是当我完成时,我注意到该命令仅在每次重新加载时才起作用。我该如何解决这个问题?

代码:

public class SpawnCommand implements CommandExecutor {

public int i = 5;

@SuppressWarnings("deprecation")
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    Player p = (Player) sender;
    if (cmd.getName().equalsIgnoreCase("spawn")) {
        Bukkit.getScheduler().scheduleAsyncRepeatingTask(JavaPlugin.getProvidingPlugin(Teams.class), new Runnable(){
                    public void run() {
                        if (i != -1) {
                            if (i != 0) {
                                p.sendMessage("§8§l┃ §3Revenant §8┃  §eTeleporting in §c(" + i + "§c)");
                                 p.playSound(p.getLocation(), Sound.ORB_PICKUP, 1, 0 + i);
                                i--;
                            } else {
                                p.sendMessage("§8§l┃ §3Revenant §8┃  §eTeleporting...");
                                Location centerblock = new Location(p.getWorld(),
                                        p.getWorld().getSpawnLocation().getX() + 0.5,
                                        p.getWorld().getSpawnLocation().getY(),
                                        p.getWorld().getSpawnLocation().getZ() + 0.5);
                                p.teleport(centerblock);
                                p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0 );
                                p.playEffect(p.getPlayer().getLocation(centerblock), Effect.ENDER_SIGNAL, 1);
                                p.playEffect(p.getPlayer().getLocation(centerblock), Effect.MOBSPAWNER_FLAMES, 1);
                                i--;
                            }
                        }
                    }
                }, 0L, 20L);
    }
    return true;
}
 }

1 个答案:

答案 0 :(得分:2)

您的任务中的i在所有任务之间共享,因为它在您的命令类中声明。

此外,您的任务需要同步。异步使用Bukkit API无效,导致问题(尽管不是您遇到的问题)。

这是一个固定版本,使用BukkitRunnable代替Runnable,以便在不再需要时取消任务(现在,即使i 1}}是-1,任务仍然运行每个滴答,这意味着如果命令被使用了一百次,那么每个滴答都有一百个任务...不理想)。 BukkitRunnable可以方便地启动和取消任务。

import org.bukkit.scheduler.BukkitRunnable;
// ... other imports ...

public class SpawnCommand implements CommandExecutor {
    @SuppressWarnings("deprecation")
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("§cYou must be a player to run use command.");
            return true;
        }
        Player p = (Player) sender;
        if (cmd.getName().equalsIgnoreCase("spawn")) {
            BukkitRunnable task = new BukkitRunnable() {
                    /**
                     * Counter until teleport time
                     */
                    int i = 5;

                    @Override
                    public void run() {
                        if (i != 0) {
                            p.sendMessage("§8§l┃ §3Revenant §8┃  §eTeleporting in §c(" + i + "§c)");
                            p.playSound(p.getLocation(), Sound.ORB_PICKUP, 1, 0 + i);
                            i--;
                        } else {
                            p.sendMessage("§8§l┃ §3Revenant §8┃  §eTeleporting...");
                            Location centerblock = new Location(p.getWorld(),
                                    p.getWorld().getSpawnLocation().getX() + 0.5,
                                    p.getWorld().getSpawnLocation().getY(),
                                    p.getWorld().getSpawnLocation().getZ() + 0.5);
                            p.teleport(centerblock);
                            p.playSound(p.getLocation(), Sound.CAT_MEOW, 1, 0 );
                            p.playEffect(p.getPlayer().getLocation(centerblock), Effect.ENDER_SIGNAL, 1);
                            p.playEffect(p.getPlayer().getLocation(centerblock), Effect.MOBSPAWNER_FLAMES, 1);

                            // This task is done; we can terminate it now
                            this.cancel();
                        }
                    }
            });
            task.runTaskTimer(JavaPlugin.getProvidingPlugin(Teams.class), 0L, 20L);
        }
        return true;
    }
}

由于new BukkitRunnable() {}实际上是在创建一个内联BukkitRunnable的新类,因此可以在那里添加成员变量。