Solved Simple event problem

Discussion in 'Plugin Development' started by AleilDr4go, Feb 4, 2016.

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

    AleilDr4go

    Hello, what i want to do is a simple cooldown on right click event (on a iron ingot)

    Im watching this tutorial : https://bukkit.org/threads/creating-cooldown-in-event.377111/


    My problem is here
    Code:
    Projectile pro = e.getEntity();
            if(pro.getShooter() instanceof Player)
    i dont understand how to convert it for the right block click event
    Please help me i really need this
    Thanks in advance
     
  2. Offline

    JoaoBM

    @AleilDr4go Do you want to add the shooter (from EntityShootEvent) in the PlayerInteractEvent?
     
  3. Offline

    AleilDr4go

    No, i want to change this cope from EntityShootEvent from PlayerInteractEvent but i cant understand how to do this part...

    Edit: Like how i can define the item "i"
     
  4. Offline

    JoaoBM

    @AleilDr4go Then you want to add this:
    to PlayerInteractEvent? What are you trying to do in-game?
     
  5. Offline

    AleilDr4go

    Im trying to do that when a player right click an iron ingot, it give to him resistance but i need to cooldown the event
     
  6. Offline

    JoaoBM

    @AleilDr4go Basically you don't need to check if the "shooter" is a Player.
    • Use just PlayerInteractEvent
    • Check if they have an item in their hand
    • Check if it's an Iron Ingot
    • Check the Action
    • Proceed with the Cooldown Code
     
  7. Offline

    AleilDr4go

    Umh...i did this, but its not working
    Code:
    package AleDEV;
    
    import java.util.HashMap;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class Cooldown implements Listener {
       
        public HashMap<String, Long> cooldown;
        Main plugin;
        public Cooldown(Main passedPlugin)
        {
            this.plugin = passedPlugin;
            cooldown = new HashMap<String, Long>();
        }
       
        @EventHandler
        public void onClickEvent(PlayerInteractEvent e)
        {
            int seconds = 10;
            Player p = e.getPlayer();
            if((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() ==Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.IRON_INGOT)
            {
                {
                    if(cooldown.containsKey(p.getName()))
                    {
                        long timer = (cooldown.get(p.getName())/1000 + seconds) - System.currentTimeMillis()/1000;
                        if(!(timer < 0))
                        {
                            p.sendMessage("§cYou have to wait for" + timer + "s!");
                            e.setCancelled(true);
                        }
                        else
                        {
                            cooldown.remove(p.getName());
                        }
                    }
                    else {
                        cooldown.put(p.getName(), System.currentTimeMillis());
                    }
                }
            }
           
        }
    
    }
     
  8. Offline

    JoaoBM

    @AleilDr4go
    Don't forget to tahg someone to reply.
    Now, you should check first if the item in hand is not null before checking if it's an Iron Ingot.
    Did you register you events in Main class?
    --EDIT--
    Also you have a floating { below If statement.
     
  9. Offline

    AleilDr4go

    Is this right?...it give to me an error
    Code:
    if(p.getItemInHand == null){
                  e.setCancelled(false);
                }
            if((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() ==Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.IRON_INGOT){
    And, yes i registered the event, basically now it works and show the cooldown, but this dont cancel the event..
     
  10. Offline

    JoaoBM

    @AleilDr4go You dont neet to check if it's null and then not cancel the event. Do:
    Code:
    if(p.getItemInHand() != null)
    {
    //do the rest
    --EDIT--
    Also doesn't cancel the event? What you are cancelling here is the RightClick.
     
    Last edited: Feb 4, 2016
  11. Offline

    AleilDr4go

    @JoaoBM
    Still not working... (i do the right click event of the iron in another class)
    Basically it say "You have to wait %x% s!"
    but it give both message, like
    You cant do this for x s!
    You right-clicked etc...
    Help D:


    I've seen your edit later
    It give anyway the effects..
     
    Last edited: Feb 4, 2016
  12. Offline

    JoaoBM

    @AleilDr4gon Post your classes here :p
     
  13. Offline

    AleilDr4go

    @JoaoBM
    Here's the class :p
    How can i implement the cooldown?
    Code:
    package AleDEV;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    public class ItemListener implements Listener {
    
      
        private Main plugin;
       
        public ItemListener(Main plugin)
        {
            this.plugin = plugin;
        }
      
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerClick(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() ==Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.IRON_INGOT){
                p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, Integer.MAX_VALUE, 2));
                p.getInventory().removeItem(new ItemStack[] {
                        new ItemStack(Material.IRON_INGOT, 1) });
                String IronIngot = plugin.getConfig().getString("IronIngot");
                p.sendMessage(IronIngot.replaceAll("&", "§"));
            }
        }
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerClick1(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if((e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() ==Action.RIGHT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.IRON_AXE){
                 Vector v = p.getLocation().getDirection().multiply(2);
                    p.setVelocity(v);
              
            }
        }
      
    }
    (Just watch the first event)
     
    Last edited: Feb 4, 2016
  14. Offline

    Xerox262

    @AleilDr4go You need to use a nested if for that first if, because right now what you're check is
    Code:
    IF
    It was a right click in the air
    OR
    It was a right click on a block
       With an iron ingot in hand
    You want
    If it was a right click either on block or in air with an iron ingot in hand.

    So either wrap the ors in brackets so it will check for an iron ingot no matter what.

    Edit: Ignore, I missed the brackets


    As for implementing the cooldown you nest an if inside your current one, check if the cooldown doesn't contain the player, or it contains the player and their cooldown is less than the system.current whatever, if so give them their effects and remove the items, else show them a message.
     
  15. Offline

    AleilDr4go

    @Xerox262 Can you show me how? i dont understand how to do it, thanks (just an example)
     
  16. Offline

    AleilDr4go

  17. Offline

    JoaoBM

    @AleilDr4go Check first if the Item is not null, then in another line check if the Item Type is an IronIngot, then in another line check the Action. Remove 1 IronIngot, add the potioneffect and send them a message.
     
  18. Offline

    Xerox262

    @AleilDr4go
    HashMap#containsKey(Key); To check if the player is in the cooldown map, if they are then
    HashMap#get(Key); To get their cooldown time, if using System.currentTimeMillis(); then compare it to that

    If either they're not on cooldown or their long is less than the criteria then apply it to them, otherwise show a message that they're still on cooldown.
     
  19. Offline

    AleilDr4go

Thread Status:
Not open for further replies.

Share This Page