Timer Script Not Working

Discussion in 'Plugin Development' started by WolfTamer, Jan 14, 2022 at 6:00 PM.

  1. Offline

    WolfTamer

    I made a timer that is supposed to countdown from args[0] however is not working. Please don't just fix my code but tell me how to. Also if there are any other methods please run them by me, thank you in advance!

    Code:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import net.md_5.bungee.api.ChatColor;
    import net.md_5.bungee.api.ChatMessageType;
    import net.md_5.bungee.api.chat.TextComponent;
    
    public class TimerCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
          
            if(sender instanceof Player)
            {
              
                if(args.length > 0)
                {
                  
                    new BukkitRunnable()
                    {
    
                        int TimerNum = (int) Integer.parseInt(args[0]);
                      
                        @Override
                        public void run() {                 
                          
                            if(TimerNum > 0)
                            {                 
                                for(Player player : Bukkit.getServer().getOnlinePlayers())
                                {                 
                                    player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(TimerNum + " Second Left"));
                                }
                              
                                TimerNum--;
                              
                            }
                            if(TimerNum <= 0)
                            {
                              
                                for(int i = 0; i >= 10; i++)
                                {
                                    Bukkit.broadcastMessage(ChatColor.RED + "" + ChatColor.BOLD + "Games Starting!"); 
                                }
                              
                              
                            }
                          
                          
                        }
                      
                    }.runTaskTimer((Plugin) this, 0, 20);
                  
                  
                } else
                {
                    sender.sendMessage(ChatColor.RED + "You Need A Number!");
                }
              
            }
          
            return false;
        }
      
    }
    Error:
    Code:
    [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'timer' in plugin JungleTestPlugin v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchCommand(CraftServer.java:790) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.network.PlayerConnection.handleCommand(PlayerConnection.java:1931) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1770) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1751) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:46) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:30) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.TickTask.run(SourceFile:18) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(SourceFile:151) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(SourceFile:125) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.MinecraftServer.bf(MinecraftServer.java:1148) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.MinecraftServer.executeNext(MinecraftServer.java:1141) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(SourceFile:134) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.MinecraftServer.sleepForTick(MinecraftServer.java:1125) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1054) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:305) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        at java.lang.Thread.run(Thread.java:833) [?:?]
    Caused by: java.lang.ClassCastException: class me.wolftamer.plugin.Commands.TimerCommand cannot be cast to class org.bukkit.plugin.Plugin (me.wolftamer.plugin.Commands.TimerCommand is in unnamed module of loader org.bukkit.plugin.java.PluginClassLoader @35755a94; org.bukkit.plugin.Plugin is in unnamed module of loader 'app')
        at me.wolftamer.plugin.Commands.TimerCommand.onCommand(TimerCommand.java:46) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[server.jar:3284a-Spigot-3892929-0ab8487]
        ... 19 more
     
  2. Offline

    KarimAKL

    @WolfTamer You can't just cast anything to Plugin; you have to actually pass your plugin instance.
     
  3. Offline

    WolfTamer

    Could you please tell me how, I am relatively new.
     
  4. Offline

    KarimAKL

    Code:Java
    1. // TimerCommand class
    2.  
    3. // We define an instance variable of type Plugin
    4. private final Plugin plugin;
    5.  
    6. // We define a constructor with a parameter of type Plugin
    7. public TimerCommand(Plugin plugin) {
    8. // We then assign the plugin instance passed to our instance variable
    9. this.plugin = plugin;
    10. }
    11.  
    12. // We then pass that variable to the method
    13. ...runTaskTimer(plugin, 0, 20);
    14.  
    15.  
    16.  
    17. // Main class (extends JavaPlugin, which implements Plugin)
    18.  
    19. // 'this' refers to this instance of the class, so we pass this instance of the plugin to the new TimerCommand instance.
    20. ...setExecutor(new TimerCommand(this));
     

Share This Page