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

    chasechocolate

    Hey guys,

    I was playing around with various NMS-related things and long story short, I ended up coming up with this. It basically allows you to get user-inputted text by using the anvil GUI. Here is the class:

    View the gist here: https://gist.github.com/chasechocolate/a9eff3d0cd577e6c098ae59286da3961

    NOTE: This requires the use of CraftBukkit, but I may come up with a reflection version later.

    If you guys come up with any cool usage ideas for this, then post them here! I'm interested in what you come up with. Dippoakabob was talking about storing passwords, but I'm sure there are many other usages!

    Enjoy!
     
    Last edited: Nov 29, 2016
  2. Offline

    BungeeTheCookie

    chasechocolate
    You have done it again. You have amazed everyone with your creation. No wonder you are a BukkitDev staff.
     
  3. Offline

    Dippoakabob

    Such amaze!
    Thanks for your awesome work Chase!
     
    njb_said and _Dashy like this.
  4. Offline

    Ba7marker

    Nice idea, could this also be done with the command block GUI then?
     
  5. Offline

    darkness1999

  6. Offline

    Ba7marker

  7. Offline

    Garris0n

    Looks nice :)
     
  8. Offline

    _Dashy

    I don't understand coding but I love the colors!
     
  9. Offline

    NathanWolf

    This is very cool! I love how it's self-contained (versus InputLib).

    I've been wanting a text-entry GUI for a while, here are a few ideas I had (since you asked!)

    - Send message to targeted player
    - Edit target command block (.. I'd love to be able to do this without creative mode ..)
    - I dunno... other stuff? I just love the idea of players not having to use server commands for gamey things...

    The 2nd one I may try out soon. Admittedly, I'd much rather pop up a command block GUI for that (er, obviously?) but I don't want protocollib so I think I'll try your anvilgui first :)

    Thanks for another great nugget of code and wisdom, chasechocolate!
     
  10. Offline

    PieMan456

    chasechocolate
    How can I get what the player types in the typing slot? Or is that what your example is doing because I don't really understand what is happening in it.
     
  11. Offline

    chasechocolate

    PieMan456 I suppose you could just pull the ItemMeta from the item in the result slot, but the way I made it calls that method when the player clicks on the item in the result slot.
     
  12. Offline

    DevRosemberg

    PieMan456 Its what he is doing, i think i have an example somewhere, let me find it.

    //Edit: Here, found it:

    Code:java
    1. renamePet(event.getPlayer(), anvilClickEvent.getName());


    Pretty easy to get the String inputed as long as he clicks the output item.
     
  13. Offline

    Pik0

    chasechocolate I have a problem, i use everything like you did in the resource, here's my code:
    Code:java
    1. public void openAnvil(final Player p, final int amount) {
    2. AnvilGUI gui = new AnvilGUI(p, new AnvilGUI.AnvilClickEventHandler() {
    3. @Override
    4. public void onAnvilClick(final AnvilGUI.AnvilClickEvent event) {
    5. if (event.getSlot() == AnvilGUI.AnvilSlot.OUTPUT) {
    6. event.setWillClose(true);
    7. event.setWillDestroy(true);
    8. ItemStack skull = new ItemStack(Material.SKULL_ITEM,
    9. amount, (short) 3);
    10. SkullMeta im = (SkullMeta) skull.getItemMeta();
    11. im.setOwner(event.getName());
    12. skull.setItemMeta(im);
    13. p.getInventory().addItem(skull);
    14. takeHeadCoins(p, amount);
    15. p.sendMessage("§2[JacintoCabeças] §6Compraste §r"
    16. + amount
    17. + " §6cabeças de §b"
    18. + event.getName()
    19. + "§6. §c(Isto não pode ser devolvido)");
    20. }else{
    21. event.setWillClose(true);
    22. event.setWillDestroy(true);
    23. }
    24. }
    25. });
    26. ItemStack tag = new ItemStack(Material.NAME_TAG);
    27. ItemMeta im = tag.getItemMeta();
    28. im.setDisplayName("Escolhe o nome");
    29. tag.setItemMeta(im);
    30. gui.setSlot(AnvilGUI.AnvilSlot.INPUT_LEFT, tag);
    31.  
    32. gui.open();
    33. }


    But when i click the name tag to close the inventory this bug prints out:
    Code:
    Could not pass event InventoryCloseEvent to JacintoHeads v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.craftbukkit.v1_7_R2.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:700) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.EntityPlayer.closeInventory(EntityPlayer.java:784) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.craftbukkit.v1_7_R2.entity.CraftHumanEntity.closeInventory(CraftHumanEntity.java:323) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at com.jacintomc.dev.piko.jacintocabecas.AnvilGUI$1.onInventoryClick(AnvilGUI.java:144) [JacintoCabeças%20v1.0.0.jar:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.PlayerConnection.a(PlayerConnection.java:1361) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.PacketPlayInWindowClick.a(SourceFile:32) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.PacketPlayInWindowClick.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.NetworkManager.a(NetworkManager.java:147) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:657) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:548) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:459) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
        at net.minecraft.server.v1_7_R2.InventorySubcontainer.setItem(InventorySubcontainer.java:72) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.craftbukkit.v1_7_R2.inventory.CraftInventory.setItem(CraftInventory.java:82) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.craftbukkit.v1_7_R2.inventory.CraftInventory.clear(CraftInventory.java:399) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at org.bukkit.craftbukkit.v1_7_R2.inventory.CraftInventory.clear(CraftInventory.java:404) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        at com.jacintomc.dev.piko.jacintocabecas.AnvilGUI$1.onInventoryClose(AnvilGUI.java:161) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-14-g8f8716c-b3042jnks]
        ... 25 more
    [18:34:10] [Server thread/INFO]: [WorldEdit] Using inbuilt NmsBlock for this version.
    
    The line 161 of AnvilGUI is:

    Code:java
    1. @EventHandler
    2. public void onInventoryClose(InventoryCloseEvent event) {
    3. if (event.getPlayer() instanceof Player) {
    4. Player player = (Player) event.getPlayer();
    5. Inventory inv = event.getInventory();
    6.  
    7. if (inv.equals(AnvilGUI.this.inv)) {
    8. //this one is the line 161(inv.clear();)
    9. inv.clear();
    10. destroy();
    11. }
    12. }
    13. }


    Could someone help me fixing this? chasechocolate
     
  14. Offline

    Chlorek

    Just tried this little-lib and I get same error Pik0 has, but it looks like we're not able to fix it?. Wondering if I can just put it into try-catch to remove this awful stacktrace. Won't there be any side-effects?
    #Edit
    Another problem is when I try to set output by myself - I get same exception. It looks like Anvil inventory has only 2 slots, like if there is no output for server... F**k logic!
     
  15. Offline

    Pik0

    Chlorek I just put a catch in the method and it worked
     
  16. chasechocolate First off, this looks amazing! Second, does it work for 1.7.9 R.02? And lastly, if it does, how exactly would I open the anvil for someone and get the text inputted? Sorry, but I'm still rather nooby with Java, and I don't see an open method to open the anvil for a player, other than the open() one, which doesn't take a player. How would I go about this?
     
  17. Offline

    chasechocolate

    ZonalYewHD
    1. Yes, it should still work, however you may need to fix the NMS imports to take your version of CraftBukkit.
    2. In the constructor of the class, it takes a Player parameter, and when you call open() it will open the GUI for the player that was passed in the constructor.
     
  18. chasechocolate Ah, didn't see that. Thanks so much! Just one more thing: where am I supposed to get the player from? Wow, I'm stupid. Please ignore this whole post.
     
  19. Offline

    123ang

    chasechocolate I've done exactly what you've said to do. Everything works good but how do you open the gui?
     
  20. Offline

    Guichaguri

  21. Offline

    MineStein

  22. Offline

    iBecameALoaf

    chasechocolate, it doesn't seem to be working for me... It'll open for a split second and immediately close again ;/

    EDIT: Hmm... It seems that it only does this when I open with my click gui...

    EDIT #2: Sigh, I was closing the players current inventory after they opened the inventory instead of before xD
     
  23. Offline

    xXSniperzzXx_SD

    So has anyone got around to creating a version of this class using Reflections? I would but I have no clue how to use reflection when i need to extend something, or even if reflection can do that...
     
  24. Offline

    CheesyFreezy

    Nothing works
     
  25. OK this whole post of me was just stupid, I have forgotten, what I was doing is already possible by getting the name of the output item since it is the same as the text written in the anvil...
     
    Last edited: Apr 8, 2015
  26. Is there a version for 1.8 available?
     
  27. Offline

    VictoryShot

    Seems to be broken for 1.8, is there a fix?
     
  28. Offline

    lucasdidur

    There is any way to get more characters? When i click on item, the string is cut by 30 characters.
     
  29. Offline

    CyranoTrinity

    Using this on 1.8.3. Got some errors.
    How would i fix undefined errors?
     
  30. Offline

    ShadowWizardMC

Thread Status:
Not open for further replies.

Share This Page