Multiple cooldowns for kits

Discussion in 'Plugin Development' started by KoalaOnCaffeine, Oct 5, 2018.

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

    KoalaOnCaffeine

    Hi i was wondering how i can make cooldowns for DIFFERENT kits, I have a theory about using the time a player got the kit and then checking the time when they try and use it again, but i have absoluely no clue how.
    And it would be helpful if you can help me store the kit items in a HashMap or something, so im not just getting it from files all the time.

    Mucho appriciatos :)
     
  2. Offline

    knokko

    @KoalaOnCaffeine

    If you would like to modify your own plug-in, this should be an easy task.

    Create a small class that contains a player id and kit id/name.
    Then create a Map that uses the class above as key and Long as value.
    Whenever a player uses a kit, use his UUID and kit to create a Key instance and set the value for that key to System.currentTimeMillis();

    Notice:

    Your class should override hashCode() and equals(Object)!

    If the plug-in is not yours, you will have to find the source codes (probably on github.com).
    If you can't find the source, you could try decompiling, but that won't be easy.

    If you can find the source, you will have to search for the part where the kit is given and modify it with the idea I gave above.
     
  3. Offline

    DanielTheDev

    I wrote you an util to have kit cooldowns.

    KitCooldown class:

    Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class KitCooldown extends BukkitRunnable {
    
        private final int period = 20*60;
        private final Map<UUID, List<Object[]>> cooldowns = new LinkedHashMap<>();
      
        public KitCooldown(Plugin plugin) {
            this.runTaskTimer(plugin, 0, period);
        }
      
        public boolean getKit(Player player, Kit kit) {
            if(!this.cooldowns.containsKey(player.getUniqueId())) {
                this.cooldowns.put(player.getUniqueId(), new ArrayList<Object[]>());
              
            }
          
            final List<Object[]> kits = this.cooldowns.get(player.getUniqueId());
          
            Object[] item;
            for(Iterator<Object[]> it = kits.iterator(); it.hasNext();) {
                item = it.next();
              
                if(((Kit)item[0]).getKitId() == kit.getKitId()) {
                    if((long)item[1]+kit.getCooldownSeconds() <= (System.currentTimeMillis()/1000)) {
                        item[1] = (System.currentTimeMillis()/1000);
                        kit.giveItems(player);
                        return true;
                    } else {
                        kit.onCooldown(player, ((long)item[1]+kit.getCooldownSeconds()-(System.currentTimeMillis()/1000)));
                        return false;
                    }
                }
            }
            kits.add(new Object[] {kit, (System.currentTimeMillis()/1000)});
            kit.giveItems(player);
            return true;
        }
      
    
        @Override
        public void run() {
          
            UUID uuid;
            for(Iterator<UUID> it = cooldowns.keySet().iterator(); it.hasNext();) {
                uuid = it.next();
              
              
                final List<Object[]> kits = this.cooldowns.get(uuid);
                Object[] item;
                for(Iterator<Object[]> it_2 = kits.iterator(); it_2.hasNext();) {
                    item = it_2.next();
                    if((long)item[1]+((Kit)item[0]).getCooldownSeconds() <= (System.currentTimeMillis()/1000)) {
                        it_2.remove();
                    }
                  
                }
                if(kits.isEmpty()) {
                    it.remove();
                }
              
            }
        }
      
    }
    You must create the instance in the onEnable method of the main class:

    Code:
    @Override
        public void onEnable() {
            this.cooldown = new KitCooldown(this);
        }
    The Kit interface:

    Code:
    import org.bukkit.entity.Player;
    
    public interface Kit {
      
        long getKitId();
      
        long getCooldownSeconds();
      
        void giveItems(Player player);
      
        void onCooldown(Player player, long seconds_left);
      
        String getName();
    }
    
    Kit example class:

    Code:
    import org.bukkit.entity.Player;
    
    public class GodKit implements Kit {
    
        @Override
        public long getKitId() {
            return 32764;
        }
    
        @Override
        public long getCooldownSeconds() {
            return 10;
        }
    
        @Override
        public void giveItems(Player player) {
            player.sendMessage("successfully given kit to you.");
        }
    
        @Override
        public void onCooldown(Player player, long seconds_left) {
            player.sendMessage("wait " + seconds_left + " seconds to claim the kit");
        }
      
        public String getName() {
            return "God Kit";
        }
    }
    In the example kit, you can edit the messages when the kit is given, or when the cooldown is still active.
    To give the kit and check if the kit is given from the called method you can use
    Code:
    boolean success = cooldown.getKit(player, kit);
    Tip: don't create multiple instances of the same kit. you don't need to do that.
    Info: the timer will automaticly check every 30 seconds if the playerdata cooldowns contain unnecessary cooldowns that can be thrown away, and then it will clean the data, so it prevents using to much memory.

    And here is my example main class:

    Code:
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        private KitCooldown cooldown;
        private GodKit godkit;
       
        @Override
        public void onEnable() {
            this.cooldown = new KitCooldown(this);
            this.godkit = new GodKit();
            this.getCommand("godkit").setExecutor(this);
        }
       
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            cooldown.getKit((Player) sender, this.godkit);
            return false;
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page