Cooldowns In Events, can't do, tried loads ¬_¬

Discussion in 'Plugin Development' started by yewtree8, May 16, 2014.

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

    yewtree8

    So i want to make it so there is a cooldown on the interact event but nothing works? I've tried using bukkit scheduler and also adding them to hashmaps and getting millis, but i just can't figure it out... so i have turned to you lovely and patronizing people to help :) here is my code:

    Code:java
    1. @EventHandler
    2. public void ThunderShock(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. Action action = event.getAction();
    5. if(player.getItemInHand().equals(ThunderShock) && action.equals(Action.RIGHT_CLICK_AIR)) {
    6.  
    7.  
    8.  
    9. } return;
    10. }


    Code:java
    1. public ItemStack setMeta(ItemStack material, String name , List<String> lore, String string) {
    2. if(((material == null) || material.getType() == Material.AIR) || (name == null && lore == null))
    3.  
    4. return null;
    5.  
    6. ItemMeta im = material.getItemMeta();
    7. if (name != null)
    8. im.setDisplayName(name);
    9. if(lore != null) {
    10. im.setLore(lore);
    11.  
    12. material.setItemMeta(im);
    13. return material;
    14.  
    15. }
    16. return material;
    17. }


    Code:java
    1. public ItemStack ThunderShock = setMeta(new ItemStack(Material.BLAZE_ROD), ChatColor.BOLD + "THUNDERSHOCK",
    2. Arrays.asList(ChatColor.GOLD + "Right Click To Use Thundershock!"), (ChatColor.ITALIC + "Can Be Used Every 3 Seconds"));



    ok so that is the method, now can someone tell me how to implement a cooldown into that? thanks. :)
     
  2. Offline

    Code0

    You never defined the thundershock thing to start off..
     
  3. Offline

    yewtree8


    that isn't all the code.... i have edited it now.... :p
     
  4. Offline

    Code0

    1) Create a hashmap called cooldown or something like this
    Code:
    private HashMap<String,Boolean> cooldown = new HashMap<String,Boolean>();
    
    2) Add the to the hashmap after the event fires
    Code:
    cooldown.put(player.getName(),false);
    3) Create a schedular that removes them after some time (note that 20 ticks = 1 second)
    Code:java
    1. Bukkit.getServer().getSchedular().scheduleSyncDelayedTask(this,new Runnable(){
    2.  
    3. public void run(){
    4. cooldown.put(player.getName(),true);
    5. }
    6. }
    7. ,200);


    4) Make sure to check, if they're boolean value = false, to cancel the event.

    If this doesn't work, just tag me and I'll respond

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    yewtree8

    Code0

    so just get a scheduled task from bukkit or what?

    Code0

    It doesn't work :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    Code0

    What exactly makes it not work?

    Is it an error in the console or what...


    Post full code please
     
  7. Offline

    DoctorDark

    yewtree8

    For cooldowns, I would personally compare against a Long time as it won't need a task to remove the player from the collection, it will be comparing against the current system time in milliseconds.

    First declare your variables:
    private Map<UUID, Long> cooldownMap = new HashMap<UUID, Long>();
    private int cooldownDuration = 180 //(3 minutes - 180 second cooldown)


    Setting a player on the cooldown:
    Code:
    public void setCooldown(Player player) {
     
          cooldownMap.put(player.getUniqueId(), System.currentTimeMillis());
     
    }

    When you want to get the remaining cooldown time:
    Code:
    public double getCooldownRemaining(Player player) {
       
         long timeLeft= cooldownMap.get(player.getUniqueId());
         double cooldownTime = cooldownDuration + 0.0;
         double secondsLeft = ((timeLeft / 1000.0) + cooldownTime) - (System.currentTimeMillis() / 1000.0);
       
    }

    To check if the player is on the cooldown:
    Code:
    public boolean hasCooldown(Player player) {
     
         return getCooldownRemaining(player) > 0;
     
    }
     
  8. Offline

    yewtree8

    Code0

    ok so this is my code, it just doesn't work, there will be no cooldown of 3 seconds (60 ticks)

    for the casting of thundershock, asks for a player to do the thundershock, maybe that is interfearing?

    Code:java
    1. @EventHandler
    2. public void onThunderShock(EntityDamageByEntityEvent event) {
    3. Player player = (Player) event.getEntity();
    4.  
    5. if (event.getDamager() instanceof Snowball) {
    6. Snowball s = (Snowball) event.getDamager();
    7. if (s.getShooter() instanceof Player) {
    8. Player shooter = (Player) s.getShooter();
    9. if (shooter.getItemInHand().equals(ThunderShock)) {
    10. if(player instanceof Player) {
    11.  
    12. event.getEntity().getLocation().getWorld().strikeLightning(event.getEntity().getLocation());
    13. event.setDamage(3.0);
    14. shooter.sendMessage(ChatColor.GOLD + "PikaChu Used ThunderShock ");
    15.  
    16. } else {
    17.  
    18. player.sendMessage(ChatColor.GOLD + "ThunderShock " + ChatColor.RED + "Missed!" );
    19.  
    20. }
    21.  
    22. }
    23. }
    24. }
    25.  
    26. }



    Now for the event with the cooldown

    Code:java
    1. @EventHandler
    2. public void ThunderShock(PlayerInteractEvent event) {
    3. final Player player = event.getPlayer();
    4. Action action = event.getAction();
    5. if(player.getItemInHand().equals(ThunderShock) && action.equals(Action.LEFT_CLICK_AIR)) {
    6. if(ThunderCD.containsValue(player)) {
    7.  
    8. player.sendMessage(ChatColor.RED + "You Cant Use That Yet!");
    9. return;
    10.  
    11. }
    12.  
    13.  
    14.  
    15. Projectile snowball = player.launchProjectile(Snowball.class);
    16. snowball.setVelocity(player.getEyeLocation().getDirection().multiply(4));
    17.  
    18. ThunderCD.put(player.getName(),false);
    19.  
    20. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this,new Runnable(){
    21.  
    22. public void run(){
    23. ThunderCD.put(player.getName(),true);
    24. }
    25. }
    26. ,60);
    27.  
    28.  
    29. } return;
    30.  
    31.  
    32.  
    33. }
     
Thread Status:
Not open for further replies.

Share This Page