Solved Problem with Scheduler

Discussion in 'Plugin Development' started by monkafynix, Dec 14, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    monkafynix

    If I execute this with more than one Players on the server, then Scheduler add's the amount of Player to the timer, so if ther're two player on the server it counts 0, 2, 4 and so on. So that's is my problem it's probably easy to fix but idk how to.
    Code:
    package de.monkafynix.playthrough.commands;
    
    public class TimerResume implements CommandExecutor{
       
       
        int pog2 = 0;
        int pog1 = 0;
        protected int pog = 2147483647;
       
        int ss = 0;
        int mm = 0;
        int hh = 0;
       
        private static int yeet = 20;
       
        boolean Running;
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player) {
                if(args[0].equalsIgnoreCase("resume")) {
                    for(Player player: Bukkit.getOnlinePlayers()) {
                        Bukkit.getScheduler().cancelTask(pog2);
                    pog1 = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {
                       
                        @Override
                        public void run() {
                           
                            String timeString = String.format("%02d:%02d:%02d", hh, mm, ss);
                            PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.ACTIONBAR,
                                    ChatSerializer.a("{\"text\":\"§7Challenge§2§l "+ timeString +"\"}"),pog,pog,pog);
                            ((CraftPlayer)player).getHandle().playerConnection.sendPacket(title);
                            switch(ss) {
                            default:
                            if(ss >= 59) {
                                ss = -1;
                                mm++;
                                if(mm >= 59) {
                                    mm = -0;
                                    hh++;
                                    if(hh >= 99) {
                                        return;
                                       
                                                }
                                   
                                   
                                   
                                        }
                               
                               
                               
                                    }
                           
                                }ss++;
                           
                        }
                    }, 0, yeet);
                           
                       
                    }
                   
                }else if(args[0].equalsIgnoreCase("reset")) {
                    Bukkit.getScheduler().cancelTask(pog1);
                    for(Player player : Bukkit.getOnlinePlayers()) {
                        hh = 0;
                        mm = 0;
                        ss = 0;
                   
                    Bukkit.reload();
                   
    
                    PacketPlayOutTitle title = new PacketPlayOutTitle(EnumTitleAction.ACTIONBAR,
                            ChatSerializer.a("{\"text\":\"§6§oDer Timer ist gestoppt\"}"),pog,pog,pog);
                    ((CraftPlayer)player).getHandle().playerConnection.sendPacket(title);
    
                }
                   
                }
               
            }
           
           
            return false;
        }
    }
     
  2. Offline

    DerDonut

    In the 'resume' part, use the for-loop only to send the packets and not for creating and cancelling the schedulers

    Also, I don't know what you are trying to achieve but are you sure you want to reload the whole server when resetting the timer?
     
  3. Offline

    monkafynix

    But it doesn't update with the ticks.
     
  4. Offline

    DerDonut

    @monkafynix what do you mean? The scheduler runs every second as it's supposed to, but there is a scheduler for every player because you start them inside the for loop
     
  5. Offline

    monkafynix

    Just can you give me an example how you would do because im to stupid.

    I'm like kinda to coding LULW.
     
  6. Offline

    DerDonut

    You only need the for loop (which loops over all the online players) to send the action bar packets. So you remove the existing one and add it only around these lines:
    So the scheduler only gets started once and in every run it loops over all the player. Right now you loop over every player and start a new scheduler for everyone. That's not what we want

    And also remove the switch if you only have a default case
     
  7. Offline

    monkafynix

    Ok thank you it work.


    But one question, if I execute the command ingame twice the scheduler also execute twice how can I fix this?
     
  8. Offline

    DerDonut

    If the timer is running you shouldn't be able to resume it again right? So just use your boolean 'running'and return if you try to resume twice
     
  9. Offline

    monkafynix

    I don't know how to do that auf deutsch ich bin ein noob lul

    so I know that I'm checking whether the Boolean Running = true but I cannot cancel the scheduler because it would stop the whole process and yeah
     
  10. Offline

    DerDonut

    I think I should talk in English here, pm me if you want a german explanation

    So you have to detect if the scheduler is running. You do that either with a variable which is set everytime you start/stop the timer or you use Bukkit.getScheduler().isCurrentlyRunning(pog1)

    You add one of these checks after the command is detected and you return if it is already running
     
Thread Status:
Not open for further replies.

Share This Page