ItemHeldEvent

Discussion in 'Plugin Development' started by xelatercero, Oct 18, 2016.

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

    xelatercero

    my itemHeldEvent works but not fine i mean the potionEffect is apllied when the player have had the item in hand and the switch to other item: i will explain

    player with no item in hand:

    [​IMG]

    player switched to the item, nothing happens:

    [​IMG]


    player switch to another slot of inventory and then the potionEffect is applied, why?

    [​IMG]
     
  2. Offline

    ShaneCraftDev

    @xelatercero
    Either your order of checks in your eventhandler is wrong or you check on previously held item, but without you posting your source we can't help you.
     
  3. Offline

    xelatercero

  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    xelatercero

    OMG my stupidity is increasing

    @timtower @ShaneCraftDev

    PHP:
    @EventHandler
       
        
    public void onItemHandChangeEvent(PlayerItemHeldEvent e) {
           
            
    Player player e.getPlayer();

             
    ItemStack item player.getItemInHand();
           
            
    ItemStack item2 Main.plumaVelocidad();
           
           
               
            if(!(
    item.equals(null))) {
                if(
    item.isSimilar(item2)) {
                
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED5001));
                }
                else {
                    return;
                }
            }
               
           
            else {
                return;
            }
     
  6. Offline

    Zombie_Striker

    Stop abusing static. If you want to access something from the Main class, pass the instance of the Main class through this class's constructor.

    This is both useless and will break your plugin. If item is null, then using the .equals method on the null object will throw an NPE. Use == instead of equals.

    This does nothing, so remove it. There is no other code past this point, so there is no reason to return prematurely.

    All this does is check if the Materials and Durability are the same. If you want to check for lore and displaynames, then you will have to add another line to check specifically for those things. If not, why do you need to build a new itemstack to check for materials?
     
  7. Online

    timtower Administrator Administrator Moderator

  8. Offline

    ShaneCraftDev

    @xelatercero
    Yes, this event is deceiving if you do not use the docs, it reads: "Fired when a player changes their currently held item".

    However this event returns a reference to the new inventory slot id, you can use the following:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerItemHeldEvent(PlayerItemHeldEvent event) {
    4. Inventory inv = event.getPlayer().getInventory();
    5.  
    6. int slotId = event.getNewSlot();
    7. if (slotId >= 0 && slotId < inv.getSize()) {
    8. ItemStack stack = inv.getItem(slotId);
    9.  
    10. if (stack != null) {
    11. // your items comparison check
    12. }
    13. }
    14. }
    15.  
    16.  
     
  9. Offline

    xelatercero

    @Zombie_Striker

    PHP:
    @EventHandler
       
        
    public void onItemHandChangeEvent(PlayerItemHeldEvent e) {
           
            
    Player player e.getPlayer();

             
    String item player.getItemInHand().getItemMeta().getDisplayName();
             
    ItemStack item2 player.getItemInHand();
           
           
           
           
               
            if(!(
    item2 == null)) {
                if(
    item == "Pluma de Velocidad") {
                
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED5001));
                }
               
               
            }
               
           
            else {
                return;
            }
               
       
           
       
        }
     
  10. Online

    timtower Administrator Administrator Moderator

    @xelatercero Now you have the item before the event actually occurs.
    Get the new slot, get that item.
     
  11. Offline

    Zombie_Striker

    Also, this will never be true. == compares memory space. This means it checks if the instances are the same, which they wont since you are creating a new string each time. What you are looking for is ".equals" because that compares values of instances.
     
  12. Offline

    xelatercero

    @timtower i dont reaaly understand how to aplly the getNewSlot to my code because returns a int
     
  13. Online

    timtower Administrator Administrator Moderator

  14. Offline

    xelatercero

    ok i have tried somthing different but doesnt work, i mean, when i right click in cobblestone with the hand throw me nullPointerException , but if i right click the cobblestone with the item nothing happens no potion effect no error:

    PHP:
    @EventHandler
      
        
    public void onRightClick(PlayerInteractEvent e) {
            
    Player player e.getPlayer();
            
    ItemStack item player.getItemInHand();
            
    Material block =  Material.COBBLESTONE;
          
          
            if(!(
    e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
          
            if(
    e.getClickedBlock().getType().equals(block)) {
    /*the error is in this line*/   if(item.getItemMeta().getDisplayName() != null && item.getItemMeta().getDisplayName().equals("Pluma de Velocidad")) {
                    
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED5001));
          
              
                }
              
          
            }
          
            
     
  15. Online

    timtower Administrator Administrator Moderator

    @xelatercero Why are you still using getItemInHand?
    And item could be null
    Metadata could be null
    Displayname could be null
     
  16. Offline

    xelatercero

    i check if the DisplayName is null
     
  17. Online

    timtower Administrator Administrator Moderator

    @xelatercero Oww, you changed events. Didn't notice that.
    The 3 other points still stand though.
     
  18. Offline

    xelatercero

    @timtower

    but now i am checking if the item is null and the detItemMeta().getDisplayName() is null

    PHP:
    if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
         
            if(
    e.getClickedBlock().getType().equals(block)) {
                if(!(
    item == null)) {
                if(
    item.getItemMeta().getDisplayName() != null && item.getItemMeta().getDisplayName().equals("Pluma de Velocidad")) {
                    
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED5001));
         
             
                }
             
                } else return;
             
             
         
            }
    @timtower
    ok no i dont get any error but i pu some debug messages but only works the first why?

    PHP:
    @EventHandler
       
        
    public void onRightClick(PlayerInteractEvent e) {
            
    Player player e.getPlayer();
            
    ItemStack item player.getItemInHand();
           
           
           
            if(!(
    e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
           
                
    boolean meta item.hasItemMeta();
                if(
    meta == true) {
                    
    System.out.println("tiene meta");
               
                   
                if(
    item.getItemMeta().getDisplayName() != null  &&  item.getItemMeta().getDisplayName().equalsIgnoreCase("Pluma de Velocidad")) {
                    
    System.out.println("passa el null");
                    
    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED5001));
           
               
                } else {return;}
               
               
               
        }
               
           
           
        }
        
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Oct 20, 2016
  19. Online

    timtower Administrator Administrator Moderator

    @xelatercero As said:
     
Thread Status:
Not open for further replies.

Share This Page