TextGUI

You want the player to input a single line of text, but chat is either too busy or not an interesting medium. How about through an Anvil interface? Creating one is simple - you may either instantiate a TextGUI object or extend the class, but with the added step of calling the #build method.

This inventory type uses the AnvilGUI library, which in turn uses NMS code. You may need to update the MilkGUI version for your plugin after each Minecraft update if using a TextGUI.

Option 1: Object

Quick and easy, this method is ideal for infrequent use of your GUI:

// Create the object with title, size is ignored
TextGUI tg = new TextGUI(new GUI("Submit answer", Rows.ONE));
// Only first slot can set configured
tg.setItem(0, new Item(Material.BOOK));
// Pass settings to builder
tg.build(null, true, false);
// Open the inventory for a Player
tg.openInventory(player);

If your resource supports versions of Minecraft both before and after 1.13, you'll need to be careful with Materials. Passing as a String instead ("APPLE") may give you better results with some items, but we recommend using a library like XMaterial.

Option 2: Extended Class

This method is preferred if you intend to reuse your GUI frequently and/or show it to multiple players at once. For example:

public class MyCustomGUI extends TextGUI {
    public MyCustomGUI(Player player) {
        super(new GUI("Submit answer"), Rows.ONE);
    
        setItem(0, new Item(Material.BOOK));
        
        final int id = this.getId();
        final boolean preventClose = false;
        build(new BukkitRunnable() {
            final TextGUI tg = (TextGUI)MilkGUI.INSTANCE.getGUI(id);
            
            @Override
            public void run() {
                final String output = tg.getOutput().trim();
                player.sendMessage("You wrote " + output);
            }
        }, preventClose);
    }
}

// Call #openInventory from another class or listener

If your resource supports versions of Minecraft both before and after 1.13, you'll need to be careful with Materials. Passing as a String instead ("APPLE") may give you better results with some items, but we recommend using a library like XMaterial.

Last updated