Solved Anvil GUI not working

Discussion in 'Plugin Development' started by eliteprogamer100, Oct 17, 2015.

Thread Status:
Not open for further replies.
  1. So my dev is having a problem with a adding an item stack inside of an anvil. We dont know how to fix it. Can someone teach us or explain how to add them in 1.8?
     
  2. Offline

    mine-care

  3. Offline

    mine-care

    @eliteprogamer100
    so what is your problem with it?
    Can you show us your code?
    Does it error?
    Does it have a not normal behaviour or something?
     
  4. Offline

    hammy2899

    @mine-care

    Hey, im the dev btw

    This is the error i am getting while using that code

    Code:
    [21:04:23] [Server thread/INFO]: hammy2899 issued server command: /l
    [21:04:26] [Server thread/ERROR]: Could not pass event InventoryClickEvent to VividCraftLounge v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1586) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:31) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.PacketPlayInWindowClick.a(SourceFile:9) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NoClassDefFoundError: net/minecraft/server/v1_7_R4/EntityHuman
        at com.HamiStudios.VividCraftLounge.Events.ItemClickEvent.itemClick(ItemClickEvent.java:54) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        ... 14 more
    Caused by: java.lang.ClassNotFoundException: net.minecraft.server.v1_7_R4.EntityHuman
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:91) ~[spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_51]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_51]
        at com.HamiStudios.VividCraftLounge.Events.ItemClickEvent.itemClick(ItemClickEvent.java:54) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot_(1.8).jar:git-Spigot-612de46-07c2162]
        ... 14 more
    
    this is the AnvilGUI class:

    Code:
    package com.HamiStudios.VividCraftLounge.Util;
    
    import java.util.HashMap;
    
    import net.minecraft.server.v1_7_R4.ContainerAnvil;
    import net.minecraft.server.v1_7_R4.EntityHuman;
    import net.minecraft.server.v1_7_R4.EntityPlayer;
    import net.minecraft.server.v1_7_R4.PacketPlayOutOpenWindow;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.HamiStudios.VividCraftLounge.Main;
    
    class AnvilContainer extends ContainerAnvil {
        public AnvilContainer(EntityHuman entity){
            super(entity.inventory, entity.world, 0, 0, 0, entity);
        }
    
        @Override
        public boolean a(EntityHuman entityhuman){
            return true;
        }
    }
    
    public class AnvilGUI {
        public enum AnvilSlot {
            INPUT_LEFT(0),
            INPUT_RIGHT(1),
            OUTPUT(2);
            private int slot;
            private AnvilSlot(int slot){
                this.slot = slot;
            }
            public int getSlot(){
                return slot;
            }
            public static AnvilSlot bySlot(int slot){
                for(AnvilSlot anvilSlot : values()){
                    if(anvilSlot.getSlot() == slot){
                        return anvilSlot;
                    }
                }
                return null;
            }
        }
        public class AnvilClickEvent {
            private AnvilSlot slot;
            private String name;
            private boolean close = true;
            private boolean destroy = true;
            public AnvilClickEvent(AnvilSlot slot, String name){
                this.slot = slot;
                this.name = name;
            }
            public AnvilSlot getSlot(){
                return slot;
            }
            public String getName(){
                return name;
            }
            public boolean getWillClose(){
                return close;
            }
            public void setWillClose(boolean close){
                this.close = close;
            }
            public boolean getWillDestroy(){
                return destroy;
            }
            public void setWillDestroy(boolean destroy){
                this.destroy = destroy;
            }
        }
        public interface AnvilClickEventHandler {
            public void onAnvilClick(AnvilClickEvent event);
        }
        private Player player;
        private AnvilClickEventHandler handler;
        private HashMap<AnvilSlot, ItemStack> items = new HashMap<AnvilSlot, ItemStack>();
        private Inventory inv;
        private Listener listener;
        public AnvilGUI(Player player, Main main){
            this.player = player;
            this.listener = new Listener(){
                @SuppressWarnings("unused")
                @EventHandler
                public void onInventoryClick(InventoryClickEvent event){
                    if(event.getWhoClicked() instanceof Player){
                        Player clicker = (Player) event.getWhoClicked();
                        if(event.getInventory().equals(inv)){
                            event.setCancelled(true);
                            ItemStack item = event.getCurrentItem();
                            int slot = event.getRawSlot();
                            String name = "";
                            if(item != null){
                                if(item.hasItemMeta()){
                                    ItemMeta meta = item.getItemMeta();
                                    if(meta.hasDisplayName()){
                                        name = meta.getDisplayName();
                                    }
                                }
                            }
                            AnvilClickEvent clickEvent = new AnvilClickEvent(AnvilSlot.bySlot(slot), name);
                            handler.onAnvilClick(clickEvent);
                            if(clickEvent.getWillClose()){
                                event.getWhoClicked().closeInventory();
                            }
                            if(clickEvent.getWillDestroy()){
                                destroy();
                            }
                        }
                    }
                }
                @EventHandler
                public void onInventoryClose(InventoryCloseEvent event){
                    if(event.getPlayer() instanceof Player){
                        @SuppressWarnings("unused")
                        Player player = (Player) event.getPlayer();
                        Inventory inv = event.getInventory();
                        if(inv.equals(AnvilGUI.this.inv)){
                            inv.clear();
                            destroy();
                        }
                    }
                }
                @EventHandler
                public void onPlayerQuit(PlayerQuitEvent event){
                    if(event.getPlayer().equals(getPlayer())){
                        destroy();
                    }
                }
            };
            Bukkit.getPluginManager().registerEvents(listener, main); //Replace with instance of main class
        }
        public Player getPlayer(){
            return player;
        }
        public void setSlot(AnvilSlot slot, ItemStack item){
            items.put(slot, item);
        }
        public void open(){
            EntityPlayer p = ((CraftPlayer) player).getHandle();
            AnvilContainer container = new AnvilContainer(p);
            //Set the items to the items from the inventory given
            inv = container.getBukkitView().getTopInventory();
            for(AnvilSlot slot : items.keySet()){
                inv.setItem(slot.getSlot(), items.get(slot));
            }
            //Counter stuff that the game uses to keep track of inventories
            int c = p.nextContainerCounter();
            //Send the packet
            p.playerConnection.sendPacket(new PacketPlayOutOpenWindow());
            //Set their active container to the container
            p.activeContainer = container;
            //Set their active container window id to that counter stuff
            p.activeContainer.windowId = c;
            //Add the slot listener
            p.activeContainer.addSlotListener(p);
        }
        public void destroy(){
            player = null;
            handler = null;
            items = null;
            HandlerList.unregisterAll(listener);
            listener = null;
        }
    }
    
    and this is what i am using to open it:

    Code:
                    AnvilGUI anvilGUI = new AnvilGUI(player, main);
                    anvilGUI.open();
     
  5. Offline

    rbrick

  6. Offline

    hammy2899

    Iv tried the 1.8 imports too still doesnt work
     
  7. Offline

    rbrick

    @hammy2899
    You imports will need to match with the version of Spigot/Bukkit you are on, in this case it will be
    and
    If you are still getting errors, post the new stacktrace
     
  8. Offline

    mine-care

    That makes sense...
    The code used from the resource section is version depended and that means that each time you update the server to the latest build and the packages change, you will have to change this code as well ^...
     
  9. Offline

    hammy2899

    i am using the 1.8 imports

    this is the error from console:

    Code:
    [21:23:59] [Netty Server IO #1/ERROR]: java.lang.NullPointerException
    this is an error you get in game

    [​IMG]
     
  10. Offline

    Scimiguy

  11. Offline

    hammy2899

    @Scimiguy

    That is the full error stack and we have solved it
     
  12. Offline

    Scimiguy

  13. Offline

    hammy2899

    im the thread owner
     
Thread Status:
Not open for further replies.

Share This Page