Need some help :)

Discussion in 'Plugin Development' started by bossoblak, Feb 6, 2020.

  1. Offline

    bossoblak

    So i've been working on a plugin for some time now, and i stumbeled upon a "bug" or error. I tried to make an Inventory with a InventoryClickEvent to open another inventory. It worked with the one item called "Particals" but the other one seemed to not go off. I tried to put System.out.println, to check what happend but nothing seemed to happen. Got an error on line 17 with java.lang.NullPointerException.

    Anyways, the problem is that when I click on the "Pets" item it dosent do anything and just throws the error above.

    Line 17:
    if (event.getCurrentItem().getItemMeta().getDisplayName().equals("§6§lParticals")) {

    full code:
    Code:
    package core.bossoblak.minecraft.hubfun;
    
    import core.bossoblak.minecraft.Inventorys;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    
    public class CosmeticsEvents implements Listener {
    
    
    @EventHandlerpublic void onCosmetic(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();
    if (event.getView().getTitle().equals(" §8§lCosmetics Menu")) {
    if (event.getCurrentItem().getItemMeta().getDisplayName().equals("§6§lParticals")) {
    event.setCancelled(true);
    player.updateInventory();
    player.openInventory(Inventorys.particalsInv);
    
    } else if (event.getCurrentItem().getItemMeta().getDisplayName().equals("§f§lPets")) {
    event.setCancelled(true);
    player.updateInventory();
    player.openInventory(Inventorys.petsInv);
    
    } else if (event.getCurrentItem() == null || event.getCurrentItem().getItemMeta().getDisplayName().equals(" ")) {
    event.setCancelled(true);
    player.updateInventory();
    
    }
    }
    }
    }
     
    Last edited by a moderator: Feb 6, 2020
  2. Offline

    timtower Moderator Moderator

    @bossoblak Item can be null, displayname can be null.
     
  3. Offline

    bossoblak

    What? The item isnt null, and the displayname is not null either
     
  4. Offline

    timtower Moderator Moderator

    Not all items have a displayname. Only if they are renamed.
     
  5. Offline

    EvilestVirus7

    Try to use the block.getType() if you have diferent block. Can you give us all the code? I want to see how did you create you ItemMeta / Item in your menu.

    This is a example of how you can do what you want:

    Code:
    public void onComestic(InventoryClickEvent e){
    
    //Get the player who clicked on the block in the inventory
    Player player = (Player) e.getWhoClicked();
    
    //Be sure that the inventory where the player clicked is the same then your GUI/MENU
    if(e.getView().getTitle().equalsIgnoreCase("§8§lCosmetics Menu")
    
    event.setCancelled(true);
    
    if(e.getCurrentItem().getType() == Material.GOLD_BLOCK){
    player.closeInventory();
    //YOU CODE HERE FOR YOUR FISRT ITEM
      }
    
    if(e.getCurrentItem().getType() == Material.GOLD_DIAMOND){
    player.closeInventory();
    //YOU CODE HERE FOR YOUR SECOND ITEM
       }
    
    if(e.getCurrentItem().getType() == Material.GOLD_IRON){
    player.closeInventory();
    //YOU CODE HERE FOR YOUR THIRD ITEM
       }
    }
    Have a great day!
    EvilestVirus7
     
  6. Offline

    Sw_aG

    Better to post the full error log but it sounds like you just need to check if the item doesn't equal null.
    I think itemMeta can be null too btw, minecraft says on items lore "tag:1" (or higher)
    Just check if the item & the itemMeta both not equal to null
     
  7. Offline

    KarimAKL

    I'm pretty sure the ItemMeta is only null if the Material is AIR, which it won't be in the InventoryClickEvent.

    So there's only a need to check for the ItemStack, and the ItemMeta's values (name, lore, etc.).
     

Share This Page