java 如何使函数每 2 个滴答调用一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16126978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:59:17  来源:igfitidea点击:

How to make a function be called every 2 ticks

javarepeatbukkit

提问by Henry

I'm making a bukkit plugin for my friend, and I want to find out how to call a function every 2 ticks (1/10th of a second).

我正在为我的朋友制作一个 bukkit 插件,我想知道如何每 2 个滴答声(1/10 秒)调用一个函数。

回答by Tips48

Well, this probably isn't where you want to look. You should be using thisfor any of your development needs.
Either way, I can answer your question. You want to use the built in scheduler. You can access it using

嗯,这可能不是你想看的地方。您应该将用于您的任何开发需求。
不管怎样,我都可以回答你的问题。您想使用内置调度程序。您可以使用它访问它

server.getScheduler();

Specifically, your going to want to create a Runnable and make it call your method every 2 ticks.

具体来说,您将想要创建一个 Runnable 并让它每 2 个滴答调用一次您的方法。

int id = server.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    public void run() {
        myMethod();
    }}, 0, 2);

I'll explain the difference with sync and async in a second, but this should accomplish what you want. The first argument is just a reference to the plugin (Normally thisor plugin). The second is the runnable, and you should obviously replace myMethodwith the method you want to call. The third argument is the amount of delay (In ticks) until the method first runs. I assumed 0, but it might also be two depending on what you want to do. The last argument is the amount of time between runs, which you wanted to be two.
You'll notice that the method returns an integer, which you can use to cancel the task like so:

我将在一秒钟内解释同步和异步的区别,但这应该可以完成您想要的。第一个参数只是对插件的引用(通常thisplugin)。第二个是runnable,你显然应该myMethod用你想要调用的方法替换。第三个参数是方法第一次运行之前的延迟量(以滴答为单位)。我假设为 0,但也可能是两个,具体取决于您想要做什么。最后一个参数是运行之间的时间量,您希望为两次。
您会注意到该方法返回一个整数,您可以使用它来取消任务,如下所示:

server.getScheduler().cancelTask(id);

It's important to discuss the difference between Async and Sync. You'll notice the method we called in the beginning says scheduleSyncRepeatingTask. Sync means that the Runnable which we specified as the second argument will be run on the main thread of the server. ANY method that calls a Bukkit/CraftBukkit/Minecraft method MUST be run as sync. If you run something that say, modifies a block, as Async you could completely corrupt the server. Just don't do it ;) Async, on the other hand, creates a seperate thread for the Runnable, is used for background tasks, such as copying and pasting a file or checking what time it is. Once again, NEVERrun a method that will modify the world as Async.

讨论 Async 和 Sync 之间的区别很重要。您会注意到我们在开头调用的方法是 schedule SyncRepeatingTask。同步意味着我们指定为第二个参数的 Runnable 将在服务器的主线程上运行。任何调用 Bukkit/CraftBukkit/Minecraft 方法的方法都必须同步运行。如果你运行一些东西,修改一个块,因为 Async 你可能会完全破坏服务器。只是不要这样做;) 另一方面,异步为 Runnable 创建一个单独的线程,用于后台任务,例如复制和粘贴文件或检查时间。再一次,永远不要运行将世界修改为异步的方法。

Lastly, in the future if you only wanted to run a method once and then not have it repeat, you can just use the method int id = scheduleSyncDelayedTask(plugin, Runnable, 2), with the first two arguments being the same and the third being the delay until the runnable is run (Ticks)

最后,将来如果您只想运行一次方法然后不再重复,您可以只使用该方法int id = scheduleSyncDelayedTask(plugin, Runnable, 2),前两个参数相同,第三个参数是运行可运行的延迟(滴答声)

Good luck,
Tips

祝你好运,
小贴士