Getting the Data of A Clicked Item in Inventory

Discussion in 'Plugin Development' started by mamifsidtect, Dec 27, 2013.

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

    mamifsidtect

    Hey you guys, I would really appreciate if you guys could help me out with something.

    Code:java
    1. public static org.bukkit.inventory.Inventory classSelect;
    2. public static org.bukkit.inventory.Inventory unranked;
    3. public static org.bukkit.inventory.Inventory vip;
    4.  
    5. public ItemStack white = new ItemStack(Material.INK_SACK, 1, (short)15);{
    6. ItemMeta wmeta = white.getItemMeta();
    7. ArrayList<String> w = new ArrayList<String>();
    8. wmeta.setDisplayName(ChatColor.YELLOW + "Default Classes");
    9. w.add(ChatColor.GOLD + "Everyone can access these classes!");
    10. wmeta.setLore(w);
    11. white.setItemMeta(wmeta);
    12. }

    This works just fine, but the next snippet of code is where it starts to mess up...
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent event) {
    4. HumanEntity player = event.getWhoClicked();
    5. @SuppressWarnings("unused")
    6. MaterialData data = player.getItemInHand().getData();
    7. ItemMeta wmeta = white.getItemMeta();
    8. if(event.getInventory().getName().equals(classSelect.getName())) {
    9. if(event.getCurrentItem().getItemMeta() == wmeta){
    10. event.setCancelled(true);
    11. ((Player) player).openInventory(unranked);
    12. ((Player) player).updateInventory();
    13. ((Player) player).playSound(player.getLocation(), Sound.LEVEL_UP, 2F, 1F);
    14. }
    15. }
    16. }

    And as you can see, what I am trying to do is open up another inventory, when you click inside of the "classSelect" inventory. What Problem I am running into though is that the items inside of "classSelect" are the same item but with a short of 15, and I need help differentiating those so it can open up the correct inventory when I click on it.

    Here is an example of of it: Right-click stick and it opens up classSelect(Which works) click the bonemeal in that inventory(this is where it doesn't work), it cancels and opens up the unranked inventory(That's the part that is the kicker)

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    Deleted user

    mamifsidtect
    Don't get the item...get the physical items

    if one equals the other, do somehting
     
  3. Offline

    mamifsidtect

    JHG0 And how would I go about doing that? Example code if you could?
     
  4. Offline

    mamifsidtect

  5. Offline

    NathanWolf

    You won't be able to compare two item meta objects, what exactly are you trying to do? ItemMeta contains lore, display name, enchantments, etc. you would need to compare the piece you care about directly,
    This is also completely different from data. If you want to compare data, the easiest way is probably just comparing durability, though it's deprecated.
     
  6. Offline

    mamifsidtect

    So I would maybe do something like this? NathanWolf
    Edit: Tested...doesn't work. Help?

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent event) {
    4. HumanEntity player = event.getWhoClicked();
    5. @SuppressWarnings("unused")
    6. MaterialData data = player.getItemInHand().getData();
    7. if(event.getInventory().getName().equals(classSelect.getName())) {
    8. if(event.getCurrentItem().getItemMeta().equals(white)){
    9. event.setCancelled(true);
    10. ((Player) player).openInventory(unranked);
    11. ((Player) player).updateInventory();
    12. ((Player) player).playSound(player.getLocation(), Sound.LEVEL_UP, 2F, 1F);
    13. }
    14. }
    15. }
     
  7. Offline

    JRL1004

    mamifsidtect A quick thing you should probably know when working with inventory click event:
    Event#getCurrentItem() returns the ItemStack within the slot that the player's mouse if hovering over. This means that, with the way you are currently doing this, your method has a chance to return as null. This chance is increase greatly if the size of the ItemStack clicked on is 1 because it will mean that even a right click (which is meant to just half the stack) will grab the entire slot's contents and make getCurrentItems() return value null.
     
  8. Offline

    NathanWolf

    What exactly are you trying to compare?
     
  9. Offline

    mamifsidtect

    Thank you for that JRL1004 and NathanWolf I'm trying to see if they grabbed bonemeal or if they grabbed the orange or green dye, this might help [pic]
     
  10. Offline

    JRL1004

    mamifsidtect Event#getCursor().getItemMeta().getDisplayname() and compare the strings?
     
  11. Offline

    mamifsidtect

    JRL1004 Let me test that really quick

    Alright so I did this instead
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
          public void onInventoryClick(InventoryClickEvent event) {
            HumanEntity player = event.getWhoClicked();
            if(event.getInventory().getName().equals(classSelect.getName())) {
                if(event.getCursor().getItemMeta().getDisplayName() == white.getItemMeta().getDisplayName()){
                  event.setCancelled(true);
                  ((Player) player).openInventory(unranked);
                  ((Player) player).updateInventory();
                  ((Player) player).playSound(player.getLocation(), Sound.LEVEL_UP, 2F, 1F);
              }
            }
        }
    And it turns out, my Eclipse is all screwed up for some reason, and sometimes the class file doesn't get changed to the current, saved .class that I edit. So it is way out of sync, it works now. The only problem is, is that when you click on the bonemeal, it lets you put it in your inventory, but then it gets bounced out and then drops on the ground, and then it opens the unranked inventory JRL1004

    JRL1004 NathanWolf JHG0 Thanks for all of your guys' help, I eventually got it xD And it is functioning now! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. Offline

    xTigerRebornx

    mamifsidtect Are you running 1.7.2? There was a bug in 1.7.2 about that. Solution is to close the inventory then open the new one
     
  13. Offline

    mamifsidtect

    xTigerRebornx Actually instead of using "event.getCursor()" I used "event.getCurrentItem()" lol
     
  14. Offline

    xTigerRebornx

Thread Status:
Not open for further replies.

Share This Page