Solved Problems with delays.

Discussion in 'Plugin Development' started by iLucasPvP, Jan 11, 2017 at 1:10 PM.

  1. Offline

    iLucasPvP

    Hello.

    So I got a problem (on console) when I type the command "/delay".

    I'm trying to create something like a command that sends the player a message after a certain amout of time; and this is the code:

    Code:java
    1.  
    2. package Comandos;
    3.  
    4.  
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.plugin.Plugin;
    11.  
    12. public class Delay implements CommandExecutor {
    13.  
    14.  
    15. @Override
    16. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    17. if(cmd.getName().equalsIgnoreCase("delay")) {
    18. final CommandSender s = sender;
    19. Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    20. @Override
    21. public void run() {
    22. s.sendMessage("┬žaFoi");
    23.  
    24. }
    25. }, 100L);
    26. }
    27. return false;
    28. }
    29. }
    30.  


    The error I get on the console is:

    Code:
    [19:08:20 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'dela
    y' in plugin VittaUtils v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    1) ~[spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:767) ~[spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerCon
    nection.java:1043) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :880) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat
    .java:65) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186
    ) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java
    :81) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:7
    34) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    Caused by: java.lang.ClassCastException: Comandos.Delay cannot be cast to org.bu
    kkit.plugin.Plugin
            at Comandos.Delay.onCommand(Delay.java:17) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            ... 13 more
    >
    However, I believe that my problem is that "((Plugin) this, new Runnable()" at line 19 of my Java code, and I need to solve it as well.

    Thank you :*
     
  2. Offline

    timtower Ninja on the waves Moderator

    @iLucasPvP Add a constructor to the class, pass the main instance to it. Put that instead of "(Plugin) this"
     
  3. Offline

    iLucasPvP

    Thank you, I'll try it out.

    It worked, thanks Tim
     
    Last edited: Jan 11, 2017 at 1:33 PM

Share This Page