Minecraft 插件 scheduleSyncDelayedTask 错误 [英] Minecraft Plugin scheduleSyncDelayedTask Error

查看:71
本文介绍了Minecraft 插件 scheduleSyncDelayedTask 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作 Minecraft 插件和编写一般代码方面遇到了问题.

I have a problem and am new at making Minecraft plugins and writing code in general.

我正在尝试制作一个在执行第二个命令之前等待大约 15 秒的插件,但是当我尝试执行(插件,新的 Runnable() 时,我现在拥有的代码出现错误.我已经做了一些研究,而且最人们说这是因为我的 Main 类中没有这个.问题是我不想在我的 Main 中使用它.所以我想知道我必须做些什么才能使这个工作正常进行.

I am trying to make a plugin that waits about 15 seconds before executing the second command however the code I have now has an error when I try to do (plugin, new Runnable(). I have done some research and it most people say that is because I don't have this in my Main class. The problem is that I don't want it in my Main. So I was wondering what I have to do to make this work.

代码如下.在此先感谢您提供的任何帮助.~石头

Code below. Thanks in advance for any help you can provide. ~Stone

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


    if (sender instanceof Player){
        //checks to see if player sent command
        Player player = (Player) sender;


        if (args.length >= 1) {
            //too many arguments message
            player.sendMessage(Utils.chat("&4There were too many arguments, I could not complete that command"));

        }


        if (player.hasPermission("reloadc.use")) {
            //reloads server, sends message, and stores variable value              
            Bukkit.broadcastMessage(Utils.chat("&6Server will be reloaded in 15 seconds by &5" + player.getDisplayName()));

            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage(Utils.chat("&6This works"));
                }
            }, 20L);

            Bukkit.broadcastMessage(Utils.chat("&6IT WORKED!!!!!"));                
        }

        else if (!player.hasPermission("reloadc.use")) {

            player.sendMessage(Utils.chat("&4You do not have permission to reload the server"));
            player.sendMessage(Utils.chat("&5If you belive this is a mistake please contact an admin"));

        }
    }
    return true;
}

}

给我带来问题的代码就在这里(插件这个词)

The Code that is giving me problems is right here (the word plugin)

                Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                public void run() {
                    Bukkit.broadcastMessage(Utils.chat("&6This works"));
                }
            }, 20L);

这里有 3 张图片,其中包含它给我的错误.我没有做的唯一更改是 getServer().因为它给了我更多的错误,并且至少从我所知道的情况来看并没有使任何事情变得更好.

Here are 3 images with the errors that it is giving me. The only change that i did not make was the getServer(). because it gave me more errors and did not change anything for the better at least from what I can tell.

[]

好的,所以我已经完成了更改,一切都表明它可以工作,但是现在当我运行我设置的命令时,它会做它应该做的一切,除了等待 15 秒.它一个接一个地执行文本,告诉我它将在 15 秒内重新加载,然后同时告诉我它起作用了.我现在似乎没有什么问题,它只是说它运行良好,我的等待时间是 300 升,这是服务器滴答声.那应该等于 15.

Ok So I have completed the changes, everything says that it works but now when I run the command that I setup it does everything it should except wait for 15 seconds. It executes the text one after the other telling me that it will be reloaded in 15 seconds and then at the same time it tells me it worked. Nothing seems wrong to me now, it just says that it is running fine and my wait time is 300L which is server ticks. That should equal 15.

以下完成代码的图像.

推荐答案

响应您的更新/

您的错误发生是因为您使用 plugin 对您的代码没有任何意义.您需要在使用之前将其声明为变量,或者假设您在一个类中为插件编写了所有代码,那么您可以轻松地将 plugin 替换为 this像这样 Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {.

Your error happens because you use plugin does not mean anything to your code. You need to declare it as a variable before you use in there, or assuming that you wrote all the code in one class for your plugin then you can easily replace plugin with this like so Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {.

如果它在另一个类中,那么要声明变量,您需要从另一个类传入它或从您的 Main 插件类调用它.下面将向您展示如何将它传递给您的侦听器类.

If it is in another class then to declare the variable you need to pass it in from another class or call it from your Main plugin class. The following will show you haw to pass it to your listener class.

在你的主插件类中你需要这样做,注意我们如何将 this 添加到调用你的命令类的函数 new CommandClass(this) 注意你的类将具有与 CommandClass 不同的名称:

In your main plugin class you need to do this, note how we add this to the function that is calling your command class new CommandClass(this) note that your class will have a different name than CommandClass:

public class Main extends JavaPlugin{
  @Override
  public void onEnable(){
    new CommandClass(this);
  }
}

然后在命令类中,我们修改它以接收变量public CommandClass(Main plugin):

And then in the command class, we modify it to receive the variable public CommandClass(Main plugin):

public class CommandClass implements CommandExecutor{
  private Main plugin;

  public CommandClass(Main plugin){
    this.plugin = plugin;
  }
}

现在您的 onCommand 方法将起作用,因为您在类中引用了 plugin:

Now your onCommand method will work because you have a reference to plugin in your class:

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
        @Override
        public void run() {
            Bukkit.broadcastMessage(Utils.chat("&6This works"));
        }
    }, 300L);
}

<小时>

对原始答案进行了一些编辑,以包含对您的屏幕截图的一些回复:


Original answer edited a little to include some of the response to your screenshots:

我可以看到四个问题:

  1. 发生错误是因为您没有引用实际的插件,而只是输入了plugin.
  2. 请注意,延迟以服务器滴答为单位,因此 20L 将只有延迟 1 秒.如果您想延迟 15 秒,请使用 300L.
  3. 您没有使用 @Override 注释,但它对于可运行任务非常重要.
  4. 您可以使用 getServer().getScheduler() 而不是 Bukkit.getScheduler(),以防万一您的代码发生了一些奇怪的事情并且您有设法启动了多个服务器实例.
  1. Your error happens because you have not referenced your actual plugin, but just typed plugin.
  2. Please note that the delay is in server ticks, so 20L will only have a delay of 1 second. If you want 15 seconds delay then use 300L.
  3. You didn't use the @Override annotation, but it is very important for the runnable task.
  4. You could use getServer().getScheduler() instead of Bukkit.getScheduler(), just in case there is something funky going on with your code and you have managed to start more than one instance of the server.

这是您的代码的更新版本,其中 1 和 3 已修复:

Here is an updated version of your code with 1 and 3 fixed:

        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            @Override
            public void run() {
                Bukkit.broadcastMessage(Utils.chat("&6This works"));
            }
        }, 300L);

这是包含建议 4 的代码的更新版本:

Here is an updated version of your code with suggestion 4 included:

        getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            @Override
            public void run() {
                Bukkit.broadcastMessage(Utils.chat("&6This works"));
            }
        }, 300L);

这篇关于Minecraft 插件 scheduleSyncDelayedTask 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆