Cooldown

Discussion in 'Spigot Plugin Development' started by harveycat1, Jun 3, 2017.

  1. What is the most effective way to run multiple cooldowns im making a fully customisable gui plugin so the user could basically have up to 54 cooldowns
     
  2. make array list that contain BukkitRunnble
     
  3. Map<Player, Map<SomethingToIdentifyTheCooldown, Date>>

    And the date will be the last time they used the cooldown so that you can calculate how much time they have remaining etc
     
  4. i know that way but that means making lots i need something to run around 54 different cooldowns just for 1 player
     
  5. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

  6. Choco

    Moderator

    If you have to put a Map within a Map, you're doing something horribly wrong... let's just ignore the fact that you're storing Player instances, as well as unnecessarily identifying a Date object.

    @OP Read through @ScarabCoder's link
     
  7. what would you say is most effective for max of 54 cooldowns
     
  8. Well there is 0 things wrong with nesting maps + you told him to read a reply of someone who nested maps..?

    I agree on player instances though, I'd use UUIDs and what do you even mean by "identifying a Date object"
     
  9. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    It's much cleaner to use a millisecond timestamp rather then a Date object.
     
  10. Code (Text):
    Map<UUID, Cooldown[]> cooldowns;

    cooldowns.put(uuid, new Cooldown[54])

    addCooldown(UUID uuid, long expire):
      array = cooldowns.get(uuid)
      for (i : array.length):
          if (array[i] == null):
               array[i] = new Cooldown(expire)
               break
       }


    class Cooldown

    private long expiryEpochMs;
    Pseudoocode

    Only using array here because you wanted limit of 54 - could easily just use List<Cooldown>
     
  11. Choco

    Moderator

    Correct. There is nothing wrong with nesting maps, though if you're nesting collections of any kind, it's time you make an Object. Scarab happened to edit his post shortly after I recommended reading through the link that he provided. The link in his thread exemplified exactly what I would have said as well, and that's a Map<UUID, Long> to hold the expiration or start time (preference).

    If you're only storing 1 primitive field in your object, there's no reason to have it. Just store the Long
     
  12. Yes because I didn't want to spend next 10 minutes coming up with methods/variables for it that's his job
     
  13. It's not really cleaner, if you want to represent a date then just use a Date object...
     
  14. I think you should make a map for bukkitrunnable id-s!
     
  15. Choco

    Moderator

    Would you rather use a 4 byte primitive data type, or a 32 byte Date object? I'd personally prefer the value that takes up 1/8th of the space ;) Date wraps a long value, so I see no reason to have it if you've got a primitive alternative
     
  16. Well 28 bytes per object is a great price for nicer and easier to maintain code, don't you think? After all I don't imagine millions of players joining his server so it shouldn't really matter...