Minecraft Bukkit插件,RunTask(插件插件)
我尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SPIGOT包括自己的计时器任务。您应该永远不要使用
thread.sleep()
在Spigot上,而在主线程中则更少。在这里,您只是完全阻止主线程。您应该使用
bukkit.getscheduler()
。使用
运行
而不是bukkitrunnable
使用文档。 In your case,
最后,这是您在
daynighttask
class中所拥有的。10在tick中。 20个滴答= 1秒,如果您需要每0.5秒,则需要每10个滴答。
现在,在您的命令中,您应该使用:
ps:插件实例应该扩展
javaplugin
的类的对象。如 eclipse , Intellij Idea ...)您应该拥有一个extendds javaplugin 。
您应该保存这样的实例,可以通过两种方式执行此操作:
new Daynight(this)
然后在构造函数中,做: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()
.Use
Runnable
instead ofBukkitRunnable
Use method showed in documentation. In your case,
runTaskLater
should answerFinally, this is what you will have in your
dayNightTask
class: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 :
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 thatextendds JavaPlugin
.You should save the instance of this, you can do this in 2 ways:
new dayNight(this)
then in the constructor, do :您可以尝试在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