Minecraft Plugin scheduleSyncDelayedTask error

Stonegoblin9:

Tengo un problema y soy nuevo en la fabricación de complementos de Minecraft y la escritura de código en general.

Estoy tratando de hacer un plugin que espera unos 15 segundos antes de ejecutar el segundo comando sin embargo, el código que tengo ahora tiene un error cuando trato de hacerlo (plug-in, nuevo Ejecutable (). He hecho un poco de investigación y de TI mayoría de la gente decir que es porque no tengo esto en mi clase principal. El problema es que no quiero que en mi principal. Así que me preguntaba lo que tengo que hacer para que esto funcione.

Código de abajo. Gracias de antemano por cualquier ayuda que puede proporcionar. Piedra del ~

@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;
}

}

El código que está dando problemas me está aquí (la palabra plugin)

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

Aquí hay 3 imágenes con los errores que me está dando. El único cambio que no hice fue el getServer (). porque me dio más errores y no cambió nada para mejor, al menos, de lo que puedo contar.

1 [ Imagen 1: Captura de pantalla de error Eclipse me dio] 2 [ Imagen 2: Captura de pantalla de error Eclipse me dio (plug-in está subrayado en rojo)]Imagen 3: Captura de pantalla del CMD de mi servidor cuando traté de excecute mi mando reloadc

Ok, así que he completado los cambios, todo lo que se dice que funciona, pero ahora cuando ejecute el comando de configuración que lo que hace todo lo que debería excepto esperar durante 15 segundos. Ejecuta el texto después de la otra que me dice que se volverá a cargar en 15 segundos y, a continuación, al mismo tiempo que me dice que funcionó. Nada parece mal a mí ahora, sólo dice que está funcionando muy bien y mi tiempo de espera es 300L que es garrapatas servidor. Eso debería ser igual a 15.

Imágenes de código completo a continuación.

Mi Clase PrincipalImagen de mi clase Recargar C con la espera de 15 segundos método en el interior.

sorifiend:

En respuesta a su actualización / editar:

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() {.

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.

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);
  }
}

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;
  }
}

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:

I can see four problems:

  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.

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);

Aquí es una versión actualizada de su código con la sugerencia 4 incluye:

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

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=221278&siteId=1
Recomendado
Clasificación