Solved Cooldown for kits?

Discussion in 'Spigot Plugin Development' started by imfeared, Jun 1, 2017.

  1. Basically, I want the cooldown to countdown and show up as shown in the screenshot.
    I have tried multiple times but can't seem to figure it out, also I need it to be in a different class.
     

    Attached Files:

  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You can save a HashMap for each player along with a System millisecond timestamp. Then, when the inventory opens, you can set the display name by using the last time it was used. If you need to use it across restarts, save it in a config.
     
  3. I wouldn't save the name, better save the uuid instead of the name
     

  4. Not entirely what I meant but that is extremely helpful!
    The thing Is that I don't have that much experience with BukkitRunnable in another class to make it able to type a thing like addCD to add them and then remove them when the time is done.

    Agreed, to save the UUID then the name.
     
  5. use system time(s) and compare the amount of time that has passed.
     
  6. You don't need to save the name
     
  7. I said, "Agreed, to save the UUID than the name." (well I put then but I meant than).
     
  8. Oh okay thats better :D
     
  9. Code (Text):

    public class ListenerClass implements Listener {

        public static HashMap<UUID, Long> cooldown = new HashMap<>();

        public void addCD(Player p) {

            if (cooldown.containsKey(p.getUniqueId()) && cooldown.get(p.getUniqueId()) > System.currentTimeMillis()) {
                long remaining = cooldown.get(p.getUniqueId()) - System.currentTimeMillis();
                p.sendMessage(ChatColor.RED + "Wait " + remaining/1000 + " seconds!");
            } else {
                cooldown.put(p.getUniqueId(), System.currentTimeMillis() + (15 * 1000));
            }

        }




        @EventHandler
        public void onClick(InventoryClickEvent e) {

            Player p = (Player) e.getWhoClicked();
            if (e.getInventory().getTitle().equals(ChatColor.RED + "Test Inv")) {

                e.setCancelled(true);

                if (e.getSlot() == 0) {

                    if (!e.getClickedInventory().getTitle().equals(ChatColor.RED + "Test Inv")) {

                    } else {
                        p.playSound(p.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 25, 4);
                        addCD(p);
                    }

                }

            }

        }

    }
     

    That is what I got and I dont know how to put it and format it.... also, put it as the lore

    It puts it in chat because I wanted to see if it would work the way I would like it to and it does now I need to put it in days/hours/minutes/seconds.......
     
  10. Make a method. Something along the lines of this:
    Code (Text):
    public static String convert(int i) {
    If you parse in the System time in the param...
    int secs = i/1000;
    //convert methods

    Return string;

    }

    Sent from my iPhone using Tapatalk
     
  11. Would really suggest using longs here instead of ints.