Solved Better event for this case?.

Discussion in 'Plugin Development' started by DeathWizsh, Mar 17, 2015.

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

    DeathWizsh

    Hey guys, I've run into a bit of a problem.
    I created a "lore" enchant for helmets which adds the potion effect Night vision when you equip it.
    Currently it works all fine to the exception that ppl won't be able to use the normal Night vision pots properly.

    Without actually realizing at first, the following code removes the Night vision effect when a player is not wearing the Night vision helmet, even that from the normal pots.

    Code:
        @EventHandler
        public void nightVision(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.YELLOW  + "Night vision 1");
           
            if(player.getEquipment().getHelmet() != null && player.getEquipment().getHelmet().getItemMeta().getLore() != null && player.getEquipment().getHelmet().getItemMeta().getLore().equals(lore)) {
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));           
            }
           
            if(player.getEquipment().getHelmet() == null || player.getEquipment().getHelmet().getItemMeta().getLore() == null) {
               
                player.removePotionEffect(PotionEffectType.NIGHT_VISION);
            }       
        }
    I'm wondering, isen't there a better EVENT option? or something to distinguish the 2 night vision effects?
    I'm kinda new to plugins.
    I read something about InventoryClickEvent but I don't know how to approach this event, never dealt with it before.
     
  2. Online

    timtower Administrator Administrator Moderator

    @DeathWizsh You could also use a runnable. Not saying that it is the best option, I am saying that it is a option.
     
  3. @DeathWizsh
    Well, you can check on inventory click if the item on cursor is that helmet. If it is, run a delayed task to check if the player is wearing or not the helmet. If it`s wearing it, add the potion effect and add the player to a list (if it isn`t already), and if it isn`t wearing it, check if the player is on that list. If it does, remove the night vision effect and remove the player from that list
     
  4. Offline

    DeathWizsh

    I have no idea what a "runnable" is. :S
     
  5. Online

    timtower Administrator Administrator Moderator

    @DeathWizsh Something that you can tell to run every ..... ticks
     
  6. Offline

    DeathWizsh

    I see, I've already been fiddeling with InventoryClickEvent.

    I got this sofar.
    Code:
       
      @EventHandler
       public void nightVisionDisequip(InventoryClickEvent event) {
         Player player = (Player) event.getWhoClicked();
         ArrayList<String> lore = new ArrayList<String>();
         lore.add(ChatColor.YELLOW  + "Night vision 1");
         
         if(event.getCursor().getItemMeta().getLore() == lore) {
           
           if(player.getEquipment().getHelmet() == null || player.getEquipment().getHelmet().getItemMeta().getLore() == null) {
             
             player.removePotionEffect(PotionEffectType.NIGHT_VISION);
           }
         }
       }
        }
    Currently gives me a NPE, I don't fully grasp the concept on how to check if something is empty or isen't there.
    How would I add this runnable to delay the check on if the Helmet is still there?
     
  7. Online

    timtower Administrator Administrator Moderator

    @DeathWizsh Check if something is on the cursor first. And also note that your method doesn't handle plugin related helmet changes.
     
  8. Offline

    DeathWizsh

    I've fiddled around some more, and this is what I got now.
    Currently in game it removes the night vision effect whenever I equip and disequip the helmet with that specific lore.
    While the PlayerMoveEvent adds it whenever a Player has it equiped.

    Now if I add an if statement to check wether the Player has the effect already I can switch between adding or removing the potion effect.
    Then I can remove the PlayerMoveEvent entirely.

    Code:
       
    @EventHandler
        public void nightVisionDisequip(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.YELLOW  + "Night vision 1");
            Material mat = event.getCursor().getType();
          
            if(mat == Material.DIAMOND_HELMET || mat == Material.IRON_HELMET || mat == Material.LEATHER_HELMET) {
              
                if(event.getCursor().getItemMeta().getLore() != null && event.getCursor().getItemMeta().getLore().equals(lore)){
                  
                    player.sendMessage("Found helmet");
                    player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                }
            }
        }
    Thx for the tips guys, sofar it helped me alot.
    I got it working to check wether the playing has the potioneffect to switch it on and off.
    However I've ran into the problem that where ever I drag the helmet it switches the effect.
    I tried adding if the player has the helmet equiped or not to the if statement and fiddled around but nothing worked sofar.

    Any ideas on how I should check it the right way?

    Code:
        @EventHandler
        public void nightVision(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.YELLOW  + "Night vision 1");
            Material mat = event.getCursor().getType();      
          
            if(mat == Material.DIAMOND_HELMET || mat == Material.IRON_HELMET || mat == Material.LEATHER_HELMET) {
              
                if(event.getCursor().getItemMeta().getLore() != null && event.getCursor().getItemMeta().getLore().equals(lore)){
                  
                    if(player.hasPotionEffect(PotionEffectType.NIGHT_VISION) && player.getEquipment().getHelmet() == null) {
                      
                        player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                        player.sendMessage(ChatColor.RED + "Effect OFF");
                    }
                  
                    else if(player.hasPotionEffect(null) && player.getEquipment().getHelmet() != null) {
                      
                        player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
                        player.sendMessage(ChatColor.GREEN + "Effect ON");
                    }
                }
            }
        }
    EDIT by Timtower: merged posts

    I've worked it out, by adding the SlotType where the helmet is placed.
    If the player has the potion effect and the item is placed in a container the effect is removed.
    If placed in an armor slot, the effect is enabled.
    I would like to thank you 2 who helped me point into the right direction, much appreciated.

    My current code.
    Code:
       
      @EventHandler
       public void nightVision(InventoryClickEvent event) {
         Player player = (Player) event.getWhoClicked();
         ArrayList<String> lore = new ArrayList<String>();
         lore.add(ChatColor.YELLOW  + "Night vision 1");
         Material mat = event.getCursor().getType();
            
         if(mat == Material.DIAMOND_HELMET || mat == Material.IRON_HELMET || mat == Material.LEATHER_HELMET) {
          
           if(event.getCursor().getItemMeta().getLore() != null && event.getCursor().getItemMeta().getLore().equals(lore)){
                    
             if(player.hasPotionEffect(PotionEffectType.NIGHT_VISION) && event.getSlotType() == SlotType.CONTAINER) {
                
                 player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                 player.sendMessage(ChatColor.RED + "Effect OFF");
             }
            
             else if(event.getSlotType() == SlotType.ARMOR) {
                    
               player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0));
               player.sendMessage(ChatColor.GREEN + "Effect ON");
              
             }
           }
         }
       }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page