Solved Bukkit custom inventory

Discussion in 'Spigot Plugin Development' started by starmixcraft, Mar 8, 2018.

Thread Status:
Not open for further replies.
  1. Hello,

    im am trying to create a custom inventory class and a tryed to extend CraftInventoryCustom and the inventory works fine but if a want to get my inventory in a InventoryClickEvent it is never a instance of my inventory, wy is this and is ther a way arround it? below is my code for the inventory and the eventhandler

    Code (Text):
    import java.util.HashMap;

    import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;

    import de.starmixcraft.guiapi.gui.callbacks.GuiCallback;


    public class ApiInventoryCustom extends CraftInventoryCustom {
        private HashMap<Integer, GuiCallback> callbacks;

        public ApiInventoryCustom(InventoryHolder owner, int size, String title) {
            super(owner, size, title);
            callbacks = new HashMap<>();
        }

        public ApiInventoryCustom(InventoryHolder owner, int size) {
            super(owner, size);
            callbacks = new HashMap<>();
        }

        public ApiInventoryCustom(InventoryHolder owner, InventoryType type, String title) {
            super(owner, type, title);
            callbacks = new HashMap<>();
        }

        public ApiInventoryCustom(InventoryHolder owner, InventoryType type) {
            super(owner, type);
            callbacks = new HashMap<>();
        }

        public HashMap<Integer, GuiCallback> getCallbacks() {
            return callbacks;
        }
       
        public void setItem(int index, ItemStack item, GuiCallback callback) {
            super.setItem(index, item);
            callbacks.put(index, callback);
        }
       
       
        GuiCallback getCallback(int index) {
            return callbacks.get(index);
        }
       
       
       

    }
     
    And the listener:
    Code (Text):
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;

    import de.starmixcraft.guiapi.gui.callbacks.GuiCallback;

    public class GuiListener implements Listener{
       
        @EventHandler
        public void onKlick(InventoryClickEvent e) {
            if(e.getClickedInventory() == null) {
                System.out.println("Inventory null!");
                return;
            }
           
            if(!(e.getClickedInventory() instanceof ApiInventoryCustom)) {
                System.out.println("Inventory not ApiInventoryCustom");
                return;
            }
            ApiInventoryCustom inv = (ApiInventoryCustom) e.getClickedInventory();
            System.out.println("Slot: " + e.getSlot());
            GuiCallback callback = inv.getCallback(e.getSlot());
            if(callback == null) {
                System.out.println("Callback null!");
                return;
            }
            callback.clicked((Player)e.getWhoClicked(), e.getClickedInventory().getItem(e.getSlot()), e.getSlot(), e.getClickedInventory().getName());
            System.out.println("Called callback!!");
        }

    }
     
    PS: Im german so my englisch is not the best, if you are german to you can anser in german, no problem for my :)
     
  2. Hm, not sure if you want to extend CraftInventoryCustom to create a custom inventory. I have never really seen anyone having to do that to create a custom inventory. You are better off creating an inventory through Bukkit's API instead of through that version dependent code using Bukkit.createInventory(). You can give the inventory a title that you can identify it with, for instance, in your listener class. I don't even think that the InventoryClickEvent will be capable to process your custom inventory to begin with.
     
    • Agree Agree x 2
  3. The problem was i user p.openinventory() but i have to create the inventory view my self the all works ;)
    Thanks for the help
     
Thread Status:
Not open for further replies.

Share This Page