Custom chest Inventory

Discussion in 'Plugin Development' started by OLEGSHA, Dec 25, 2014.

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

    OLEGSHA

    Hello everyone,

    I'm making a plugin in which I need to add a custom inventory. So I made a class implementing Inventory, but when I use player.openInventory(myInventory) it throws ClassCastException saying myInventory can't be casted to org.bukkit.craftbukkit.v1_7_R1.inventory.CraftInventory. So I tried making my class extend CraftInventory but got overconfused with constructors, so I realized I'm doing it wrong.
    Any idea how to make it work? I mean, how to create an inventory class that I can use as an Inventory?

    Thank you,
    OLEGSHA

    P.S. Here's the beginning of my class (imports, class, constructor):
    Code:
    package ru.iwcs.smallChests.chest;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.ListIterator;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;
    
    public class InventorySmallChest implements Inventory {
       
        public static final ItemStack EMPTY_SLOT = new ItemStack(Material.AIR);
        public static final String DEF_NAME = "Small Chest";
        public static final int SIZE = 18;
       
        private final BlockSmallChest owner;
        private ItemStack[] inv;
        private String name = DEF_NAME;
       
        public InventorySmallChest(BlockSmallChest blockSmallChest) {
            this.owner = blockSmallChest;
            {
                this.inv = new ItemStack[] {
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT,
                    EMPTY_SLOT, EMPTY_SLOT
                };
            }
        }
    And here's the exception I catch:
    Code:
    [04:25:36 ERROR]: Could not pass event PlayerInteractEvent to SmallChests  0.1 org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInteractManager.java:374) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.ClassCastException: ru.iwcs.smallChests.chest.InventorySmallChest cannot be cast to org.bukkit.craftbukkit.v1_7_R1.inventory.CraftInventory
    
            at org.bukkit.craftbukkit.v1_7_R1.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:188) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at ru.iwcs.smallChests.EventListener.onClick(EventListener.java:45) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 15 more
    And, finally, the entire class on pastebin
     
  2. @OLEGSHA ... If you're trying to make a custom inventory, there are many more ways which are much simpler than implementing Inventory or using CraftInventory.
    Code:
    Bukkit.createInventory(InventoryHolder owner, int size, String title)
     
  3. Offline

    OLEGSHA

    But how can I actually link it to non-default InventoryHolder? I mean, to a skull, for example? Because when I register an inventory it asks for an InventoryHolder that is only implemented by Chest-alike stuff that has inventory in vanilla MC, and I just don't see how to make some other block behave like a chest. And still I will need my own Inventory class as I need to make some methods reguarding the object itself. So how can create an Inventory class here?

    Thank you anyway, it's just that I need a bit more.
     
  4. Offline

    BlazingBroGamer

    @OLEGSHA
    Maybe check for player interact event, and open it to the player there? I dunno, just a suggestion.
     
  5. Offline

    OLEGSHA

    That's where I got that error the first time =\
     
  6. Offline

    BlazingBroGamer

    @OLEGSHA
    Erm... Create a new inventory, not making a class implement inventory, but doing the post that djskepter used above, and making the player open the inventory you created.
     
  7. Offline

    OLEGSHA

    Again: I can't assign it to a non-chest block + I need my own methods to work with the inventory. How do I assign it to dirt, for example?
     
  8. Offline

    BlazingBroGamer

    @OLEGSHA
    Again, on interact, open the inv to the PLAYER. Remember, it's PlayerInteractEvent, and check for right click, then open chest to the player, using player.openinventory(inventory). No need for a custom inventory class. Just make one with bukkit.createinventory.
     
  9. Offline

    OLEGSHA

    Look. Here's what I have right now.
    EventHandler:

    Code:
    @EventHandler public void onClick(PlayerInteractEvent event) {
            try {
                if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))
                    if (event.getClickedBlock().getType().equals(Material.SKULL))
                        smallChests.c.info(event.getClickedBlock().getType().toString());
                        if (((Skull) event.getClickedBlock().getState()).hasOwner())
                            if (((Skull) event.getClickedBlock().getState()).getOwner().equals(SmallChests.CHEST_OWNER))
                                if (smallChests.hasPerm(event.getPlayer(), SmallChests.MAIN_PERM_NODE + ".open", true)) {
                                    try {
                                        event.getPlayer().openInventory(smallChests.getByLoc(event.getClickedBlock().getLocation()).getInventory());
                                    } catch (Exception e) {
                                        SmallChest chest = new BlockSmallChest((Skull) event.getClickedBlock().getState());
                                        smallChests.registerChest(chest, event.getClickedBlock());
                                        event.getPlayer().openInventory((Inventory) chest.getInventory());
                                    }
                                }
            } catch (PluginNotInitException e) {e.printStackTrace();}
        }
    And SmallChests.registerChest method:
    Code:
    public void registerChest(SmallChest chest, Block holder) {
            if (chest != null) {
                chestList.add(chest);
                Bukkit.createInventory((InventoryHolder) holder, InventoryType.CHEST); //Throws here
            }
        }
    It says org.block.Skull can't be cast to InventoryHolder as it's not extending it. However I know some plugin that does it with bookshelves, so it's possible somehow.
     
  10. Offline

    BurnerDiamond

    This is what I do to make a custom inventory

    Inventory chest = Bukkit.createInventory(null, 27, "Kit Selector");

    You would keep it (null, the number of spaces which is a multiple of 9, the name)

    Whole code would be like this

    Code:
    Inventory chest = Bukkit.createInventory(null, 9, "SmallChest");       
    chest.setItem(4, new Itemstack(Material.DIRT)
    
    If you want to add a name and a custom lore to the item then use this

    Code:
    ItemStack smallchest = new ItemStack(Material.SUGAR);
            ItemMeta smallim = smallchest.getItemMeta();
            smallchest.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "SMALL CHEST");
            ArrayList<String> smalllore = new ArrayList<String>();
            smalllore.add(ChatColor.YELLOW + ("Small chest inventory")
            smallim.setLore(smalllore);
            smallchest.setItemMeta(smallim);
     
Thread Status:
Not open for further replies.

Share This Page