Help with ItemMeta

Discussion in 'Plugin Development' started by 9xc, Apr 2, 2021.

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

    9xc

    I'm trying to make my plugin show up with a menu that has four items with knockback 1,000, and so far, I've gotten that, but I also want to make it so that when I click on the item, it stays in the menu, but I get the item. The problem is that these are in different files, and I don't know how to import an ItemStack/ItemMeta through files. For example, lets say you have defined some items in items.java, but then you want to give them to the player on an event, so you make a switch statement on that event, but now when you try to add the item to the player's inventory, you don't know how to reference the item.
    Code:
    package me.ninexc.firstplugin.events;
    
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class guiHandler implements Listener {
    
    
        @EventHandler
        public void guiHandlerClickEvent(InventoryClickEvent e){
    
            Player player = (Player) e.getWhoClicked();
            if(e.getView().getTitle().equalsIgnoreCase("stick with kb 1000")){
    
                switch(e.getCurrentItem().getType()){
                    case STICK:
                    player.getInventory().addItem();
                }
    
                e.setCancelled(true);
            }
            if(e.getView().getTitle().equalsIgnoreCase("fish with kb 1000")){
                e.setCancelled(true);
            }
            if(e.getView().getTitle().equalsIgnoreCase("poisonous potato with kb 1000")){
                e.setCancelled(true);
            }
            if(e.getView().getTitle().equalsIgnoreCase("mushroom with kb 1000")){
                e.setCancelled(true);
            }
            if(e.getView().getTitle().equalsIgnoreCase("every single knockback item")){
                e.setCancelled(true);
            }
        }
    }
    
    I am new to this type of thing, please keep this in a language that I can understand.
    Thanks!
     
  2. Offline

    FlorianBK

    Hey.
    1. I would put the "e.setCancelled(true);" before the switch-statement "switch(e.getCurrentItem().getType()"
    2. Why do you only have one case "case STICK:"?
    3. I would put a "break;" after every Case
    4. You have no default-Case (if you click on an item that is not a case) -> "default: break;"




    My Code would be:
    Code:
    package me.ninexc.firstplugin.events;
    
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    public class guiHandler implements Listener {
    public void guiHandlerClickEvent(InventoryClickEvent e){
    
    @Eventhandler
    Player player = (Player) e.getWhoClicked();
    if(e.getView().getTitle().equalsIgnoreCase("stick with kb 1000")){
    e.setCancelled(true);
    switch(e.getCurrentItem().getType()){
    
    case ITEM1:
    player.getInventory().addItem(Item1);
    break;
    
    case ITEM2:
    player.getInventory().addItem(Item2);
    break;
    
    case ITEM3:
    player.getInventory().addItem(Item3);
    break;
    
    case ITEM4:
    player.getInventory().addItem(Item4);
    break;
    
    default:
    break
    }
    }
    }
    }
    ...sorry I can not tell you, how to import ItemMeta from another file. I think there ist something called ItemManager, that you could use.
     
    Last edited by a moderator: Apr 2, 2021
  3. Offline

    Newdel

    That's not another file, it's another class. That's a whole difference in coding. Also, you don't need to access these items from another class.
    This method gets the name of the inventory, not the item you clicked or anything else. Use e.getCurrentItem() to get the clicked item. Remember to check if it's null (represents an empty slot)
    Then add it to the players inventory with Player#getInventory() Inventory#addItem(Itemstack)
     
  4. Offline

    KarimAKL

    They would be different if we were not talking about Java. :p Java uses 1 file for 1 class. Even inner classes get compiled into separate files. However, access modifiers are something to take into consideration.

    @9xc, you should be following the Java naming conventions. Also, are you trying to compare item names or inventory names?
     
    Newdel likes this.
Thread Status:
Not open for further replies.

Share This Page