简体   繁体   English

Bukkit 插件:如何使用命令停止计时器

[英]Bukkit Plugin: How to stop timer using command

So basically I have所以基本上我有

timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    }
}

that runs when the command "play" is sent by a player, and I want to stop and reset the timer when a player sends the command "cancel".当玩家发送命令“播放”时运行,我想在玩家发送命令“取消”时停止并重置计时器。 How can I do this?我怎样才能做到这一点?

By using Bukkit's scheduler API, it's better.使用 Bukkit 的调度器 API 会更好。

Firstly, start your server:首先,启动你的服务器:

BukkitTask task = Bukkit.getScheduler().runTaskTimer(myPlugin, () -> {
   // do something
}, 20, 20);

Then, with the task variable, you can cancel it:然后,使用任务变量,您可以取消它:

task.cancel();

Finally, the next time, you will be able to just create a new timer that will replace the task variable.最后,下一次,您将能够创建一个新的计时器来替换任务变量。

You could create a static Timer object and cancel it in your cancel command class.您可以创建一个 static 计时器object 并在您的取消命令 class 中取消它。

/play (PlayCommand) /play(播放命令)

public static Timer timer = null;


timer.scheduleAtFixedRate(new TimerTask() {
   public void run() {
      //Run your Timer code
   }
}

/cancel (CancelCommand) /cancel(取消命令)

PlayCommand.timer.cancel();

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

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