Making a cooldown for golden apples

Discussion in 'Plugin Development' started by MacDev, Nov 29, 2014.

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

    MacDev

    I wanted to make a cooldown of 30 seconds for eating a golden apple. How would I go about doing this? I'm a little confused on what events to use and how to go about it. I am not experienced with coding, so this is why I am asking for the help of a more experienced person.
    So say when they eat a golden apple they get the message 'you have eaten a golden apple! wait {cooldown remaining} seconds to eat another!' and every time they try to eat it while the cooldown is activated they get the message 'please wait {cooldown remaining} seconds to eat another golden apple!' and when the cooldown is expired it tells them 'you can eat another golden apple!'
    Thanks
     
  2. Offline

    Skionz

    MacDev Save the player and the current time in milliseconds to some kind of Map. Then when the consume event is called check if the itemstack is a golden apple. if it is a golden apple check if the players name is in the map and the time < the new current time in milliseconds. You get the idea.
     
  3. Offline

    MacDev

    So make a cooldown like this:
    Code:
      public static HashMap<String, Boolean> cooldown = new HashMap();
    And something like this?
    Code:
     @EventHandler
      public void onInteract(final PlayerInteractEvent e)
      {
        if ((e.getAction().equals(Action.RIGHT_CLICK_AIR) | e.getAction().equals(Action.RIGHT_CLICK_BLOCK)))
        {
          final Player player = e.getPlayer();
            ItemStack i = player.getItemInHand();
            if (i.getType().equals(Material.GOLDEN_APPLE)) {
              if (cooldown.containsKey(e.getPlayer().getName()))
              {
                e.setCancelled(true);
                  e.getPlayer().sendMessage(ChatColor.RED + "Please wait until you can eat a golden apple again.");
                }
              }
    I haven't tested this out and don't know if it will work, would it?
     
  4. Offline

    Skionz

    MacDev likes this.
  5. Offline

    Googlelover1234

    MacDev
    I think the map would be more like
    Code:java
    1. public HashMap<String, Long> = new HashMap<>();
     
    MacDev likes this.
  6. Offline

    BrentHogeling

    MacDev Skionz Instead you could always make a set, and then on event do a check to see if their name is in a set, and if it isn't allow the event to fire, make a new runnable and add their name to the set, and at the end of the runnable it will be removed, and each time they eat an apple and their name is in the set, just do event.setCancelled(true)?
     
Thread Status:
Not open for further replies.

Share This Page