Solved Event Help ( InventoryClick event)

Discussion in 'Plugin Development' started by RudyBx, Jan 16, 2014.

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

    RudyBx

    Im trying to make, when the player tries to place an item on the Inventory it gets the eventcancelled, so here is the error and the code:

    PS: All the code works, except when the player tries to place an item on the inventory, it gets the following error.

    Error:
    Code:
    Caused by: java.lang.NullPointerException
        at me.rudy.com.ItemEvent.onInventoryClick(ItemEvent.java:139) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_25]
    
    Code:
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            World world = e.getWhoClicked().getWorld();
            Location location = e.getWhoClicked().getLocation();
            Player player = (Player) e.getWhoClicked();
           
            if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Item1")) {
                e.setCancelled(true);
     
  2. Offline

    NoLiver92

    which line is 139?
     
  3. Offline

    RudyBx

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Item1")) {
     
  4. Offline

    NoLiver92

    does the item have meta data? i believe you would need to check that before running this as some items dont have meta data.
     
  5. Offline

    RudyBx

    It does have the meta data:
    Code:
            @SuppressWarnings("deprecation")
            ItemStack is1 = new ItemStack(383, 1, (byte)92);
            ItemMeta im1 = is1.getItemMeta();
            List<String> loreList = new ArrayList<String>();
            loreList.add("\247aPurchase!");
            loreList.add("\247ePrice: \2476Free");
            im1.setLore(loreList);
            im1.setDisplayName("\2477Item1");
            is1.setItemMeta(im1);
    I've also set the item on inv..
    Code:
            inv.setItem(0, is1);
    The only problem I have is that when they player tries to place any item on the inv, it gets this error. ._.
     
  6. Offline

    SacredWaste

    RudyBx Looks like
    Code:java
    1. if (e.getCurrentItem().getItemMeta() == null) return;

    is most likely the culprit. If any item does not have meta data, cancel. You should probably change that to be more appropriate for the situation?
     
  7. Offline

    NoLiver92

    RudyBx
    This is what i mean - the item the user wants to place in the inventory. im happy with your code but its the item the user tries to put in, does it have metadata?
     
  8. Offline

    RudyBx

    And how can I set the meta data in that situation?
     
  9. Offline

    NoLiver92

    you dont set meta data, check to see if it has any metadata like SacredWaste said :
    Code:java
    1. if (e.getCurrentItem().getItemMeta() == null) {//code here
    2. };


    when there is no meta data just cancel the event. is this like a menu? why do items need metadata?
     
  10. Offline

    RudyBx

    Thanks, and they need the metadata, because the item I've set has 3 different custom names (metadata).
     
  11. Offline

    NoLiver92

    ok but you need to check if there is metadata or not as thats what will be giving you the error.
     
  12. Offline

    RudyBx

    Okey thanks ;D
     
Thread Status:
Not open for further replies.

Share This Page