Minecraft Bukkit插件,RunTask(插件插件)

发布于 2025-01-25 10:56:32 字数 1784 浏览 3 评论 0原文

我尝试使用Bukkit制作Minecraft插件,我将尝试每500ms更改时间,但是当我在主线程中启动该插件时,服务器会崩溃。所以我尝试了runtask();功能,但我不知道可以在哪里找到参数插件,因为它说它需要RunTask(插件插件);。 这是我的代码:

Daynight文件:

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.plugin.java.JavaPlugin;

public class dayNight implements CommandExecutor
{

    public void sleep(int time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }

    public static void changeTimeEvent(CommandSender sender, int time)
    {
        sender.getServer().getWorld("world").setTime(time);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
        Plugin plugin;
        new dayNightTask().runTask(plugin);

以及Daynighttask文件:

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;

public class dayNightTask extends BukkitRunnable
{

    public void sleep(int time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run()
    {
        World world = Bukkit.getWorld("world");
        for (int i = 0; i < 500000; i++)
        {
            world.setTime(0);
            sleep(500);
            world.setTime(16000);
            sleep(500);
        }
    }
}

我不知道如何找到RunTask()的插件参数;功能,请帮助:) 此致

i try to make a minecraft plugin with bukkit and i will try to change the time every 500ms but when i start that in the main thread, the server crashes. So i tried the runTask(); function but i do not know where i can find the parameter Plugin, because it says it needs runTask(Plugin plugin);.
Here is my code:

dayNight file:

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.plugin.java.JavaPlugin;

public class dayNight implements CommandExecutor
{

    public void sleep(int time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }

    public static void changeTimeEvent(CommandSender sender, int time)
    {
        sender.getServer().getWorld("world").setTime(time);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
        Plugin plugin;
        new dayNightTask().runTask(plugin);

and the dayNightTask file:

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;

public class dayNightTask extends BukkitRunnable
{

    public void sleep(int time)
    {
        try
        {
            Thread.sleep(time);
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run()
    {
        World world = Bukkit.getWorld("world");
        for (int i = 0; i < 500000; i++)
        {
            world.setTime(0);
            sleep(500);
            world.setTime(16000);
            sleep(500);
        }
    }
}

I do not know how i can find the plugin parameter for the runTask(); function, please help :)
best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑红尘 2025-02-01 10:56:32

SPIGOT包括自己的计时器任务。您应该永远不要使用thread.sleep()在Spigot上,而在主线程中则更少。在这里,您只是完全阻止主线程。

您应该使用bukkit.getscheduler()

  1. 使用运行而不是bukkitrunnable

  2. 使用文档。 In your case,

  3. 最后,这是您在daynighttask class中所拥有的。

public class dayNightTask implements Runnable {

    @Override
    public void run() {
        World world = Bukkit.getWorld("world");
        for (int i = 0; i < 500000; i++) {
            world.setTime(0);
            Bukkit.getScheduler().runTaskLater(MyPlugin.getInstance(), () ->  world.setTime(16000), 10);
        }
    }
}

10在tick中。 20个滴答= 1秒,如果您需要每0.5秒,则需要每10个滴答。

现在,在您的命令中,您应该使用:

Bukkit.getScheduler().runTaskTimer(plugin, new dayNightTask(), delay);

ps:插件实例应该扩展javaplugin的类的对象。如 eclipse Intellij Idea ...)您应该拥有一个extendds javaplugin 。

您应该保存这样的实例,可以通过两种方式执行此操作:

  1. 将对象传递到构造函数中,例如:new Daynight(this)然后在构造函数中,做:
private JavaPlugin plugin;

public dayNight(JavaPlugin pl) {
   this.plugin = pl;
}
  1. 创建一个静态类:
// declare object and method
private static JavaPlugin instance;
public static JavaPlugin getInstance() {
   return instance;
}

@Override
public void onEnable() { // this class should already exist
   instance = this; // set instance as current object
}

Spigot include own timer task. You should NEVER use Thread.sleep() on spigot, and less in main thread. Here you are just totally blocking the main thread.

You should use Bukkit.getScheduler().

  1. Use Runnable instead of BukkitRunnable

  2. Use method showed in documentation. In your case, runTaskLater should answer

  3. Finally, this is what you will have in your dayNightTask class:

public class dayNightTask implements Runnable {

    @Override
    public void run() {
        World world = Bukkit.getWorld("world");
        for (int i = 0; i < 500000; i++) {
            world.setTime(0);
            Bukkit.getScheduler().runTaskLater(MyPlugin.getInstance(), () ->  world.setTime(16000), 10);
        }
    }
}

10 is in tick. 20 ticks = 1 second, if you want each 0.5 second, you want each 10 ticks.

Now, in your command, you should use :

Bukkit.getScheduler().runTaskTimer(plugin, new dayNightTask(), delay);

PS: The plugin instance should the object of the class that extends JavaPlugin. Such as said in documentation (Eclipse, Intellij Idea ...) you should have a class that extendds JavaPlugin.

You should save the instance of this, you can do this in 2 ways:

  1. Pass object into constructor, for example: new dayNight(this) then in the constructor, do :
private JavaPlugin plugin;

public dayNight(JavaPlugin pl) {
   this.plugin = pl;
}
  1. Create a static class :
// declare object and method
private static JavaPlugin instance;
public static JavaPlugin getInstance() {
   return instance;
}

@Override
public void onEnable() { // this class should already exist
   instance = this; // set instance as current object
}
这个俗人 2025-02-01 10:56:32

您可以尝试在Java中使用执行者。它们异步且易于使用。

https://docs.oracle.com/ javase/7/docs/api/java/util/consurrent/opecutors.html

You can try using Executors in java. They're asynchron and easy to use.

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文