Util AnvilGUI - Use the Anvil GUI to retrieve Strings!

Discussion in 'Resources' started by chasechocolate, Dec 28, 2013.

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

    coleypoly

  2. Offline

    mug561

    @ShadowWizardMC thanks :)

    I've been fooling around with it a bit. I found out you dont NEED xp to use it, but it takes x levels if you do have it. Would there be a efficient way to cancelling/setting the xp cost to 0 whilst letting players use regular anvils normally?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 23, 2015
  3. Offline

    cococow123

    @mugmc561 maybe store the xp and set the player's xp to the stored xp once they click it.
     
  4. Hey, did anyone manage to make it work to change the Title of the AnvilGUI? I managed to make it work on the latest 1.10 Minecraft, but changing the title either works not or it works but the actual Anvil GUI disappears.
     
  5. Offline

    I Al Istannen

    @Lionhard
    I can't play with it right now, but from the "documentation" it says, the title needs to be a chat component. Try using ChatSerializer.a(String title) instead of "Repairing".

    The arguments appear to be:
    int windowId
    String type
    ChatComponent title (use the ChatSerializer as I wrote above)
    int numberOfSlots
    int entityId (seems to only be used for EntityHorse. Else just leave it out.)

    This is just from looking at wiki.vg, I haven't tested it.
     
  6. Oh, I will try it out, all I used was ChatMessage(String str). I will inform you if I have any success. Thanks.
     
  7. Offline

    Muffin2Go

    Hey Guys!

    Don't know if someone is still here but maybe you can help me.

    If got a problem with opening an inventory after useing the AnvilGUI.
    Little Information: - The Inventory needs the output of the AnvilGUI
    - It seems like the Inventory opens but directly closes again
    - No Errors
    My Use of AnvilGUI (open)
    [​IMG]


    My AnvilGUI File (open)
    Code:
    package muffin2go.playermanager.Util;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_8_R3.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 de.muffin2go.playermanager.Main;
    import net.minecraft.server.v1_8_R3.BlockPosition;
    import net.minecraft.server.v1_8_R3.ChatMessage;
    import net.minecraft.server.v1_8_R3.ContainerAnvil;
    import net.minecraft.server.v1_8_R3.EntityHuman;
    import net.minecraft.server.v1_8_R3.EntityPlayer;
    import net.minecraft.server.v1_8_R3.PacketPlayOutOpenWindow;
    
    /**
    * Created by chasechocolate.
    */
    public class AnvilGUI {
    
        private class AnvilContainer extends ContainerAnvil {
            public AnvilContainer(EntityHuman entity) {
                super(entity.inventory, entity.world,new BlockPosition(0, 0, 0), entity);
            }
    
            @Override
            public boolean a(EntityHuman entityhuman) {
                return true;
            }
        }
    
        public enum AnvilSlot {
            INPUT_LEFT(0),
            INPUT_RIGHT(1),
            OUTPUT(2);
    
            private int slot;
    
            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 {
            void onAnvilClick(AnvilClickEvent event);
        }
    
        private Player player;
    
        @SuppressWarnings("unused")
        private AnvilClickEventHandler handler;
    
        private HashMap<AnvilSlot, ItemStack> items = new HashMap<>();
    
        private Inventory inv;
    
        private Listener listener;
    
        public AnvilGUI(Player player, final AnvilClickEventHandler handler) {
            this.player = player;
            this.handler = handler;
    
            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();
                            }
                        }
                    }
                }
    
                @SuppressWarnings("unused")
                @EventHandler
                public void onInventoryClose(InventoryCloseEvent event) {
                    if (event.getPlayer() instanceof Player) {
                        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.getInstance()); //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(c, "minecraft:anvil", new ChatMessage("Repairing"), 0));
            //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;
        }
    }


    Thanks in Advance
    Muffin2Go

    Ps.: If you need other Files please reply.
     
    Last edited: Sep 12, 2016
  8. Offline

    chasechocolate

    Updated the code :)
     
Thread Status:
Not open for further replies.

Share This Page