Cooldown for an event

Discussion in 'Spigot Plugin Development' started by Froyzz, May 25, 2016.

  1. Hi guys,
    I'm trying to make a plugin for respawn an specific entity in 4 random positions that i created from MythicsMobs when this entity die. I did this but i don't know how add 1 day beetween the entity respawn.
    Code:
    Code (Text):
     @EventHandler
            public void onmort(EntityDeathEvent event){
                Entity p = event.getEntity();
                if (p instanceof PigZombie){
                    if (p.isCustomNameVisible()){
                        if (p.getWorld().getName().equals("world")){
                            Random r = new Random();
                            int alĂ©atoire = r.nextInt(4);                      
                            switch(alĂ©atoire){
                            case 0:
                                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3775");
                            break;

                            case 1:
                                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3778");
                            break;
                           
                            case 2:
                                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3778");
                            break;
                           
                            case 3:
                                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                               
                            break;
                           
                            case 4:
                                Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                               
                            break;
                           
                            default:;
                            break;                      
                             
                            }
                                               
                        }
                    }
                }
          }
    }
               
    May you help me to add a cooldown at every Event please :)
     
  2. There's a video on YouTube that is very detailed and uses arraylist to stop it from happening for X tick (20 ticks = 1 second) search how to create a cool down in spigot"
     
  3. Okay, thank you
     
  4. Not the recommended way, as Minecraft doesn't always guarantee you 20 ticks per second. What if your server restarts? What your tps goes down? Instead, it's recommended to add the player to a HashMap and compare to the current system time. To do so:

    • Create a HashMap with a UUID as the key, and a Long as the value.
    • Add the player to the HashMap with their unique id as the key, and the current time + cooldown in seconds (multiplied by 1000, so 7 second cooldown would be 7000)
    • Check if the current system time is greater than or equal to the time of the player, and if so, their cooldown expired, and you can remove them from the HashMap.
     
    • Agree Agree x 1
  5. You can do that too btw
     
  6. Because everyone knows hashmaps magically survive a program restarting? :rolleyes:

    If you mean a real day, it would be best to just store the current time in a plain text file or the plugin config, and retrieve it when needed. Very easy to do.
     
  7. Config files are recommended.
     
  8. Doesn't really matter how it's stored. What he's trying to say is that if you're making a longer cooldown, it's recommended to store it someplace where you can access it and get the data later on.
     
  9. add Bukkit runnable
     
    • Funny Funny x 1
  10. Did you even read the other replies on this thread?

     
  11. HashMaps implement serializable (So does UUID, and Long), so you could easily dump it to a file upon restart and it'd be stored as binary rather then writing a text file (Idk why you'd use a text file to begin with..?).
     
  12. THANKS for the help i also needed this <3
     
    • Friendly Friendly x 1