WesJD / AnvilGUI

Capture user input in Minecraft through an anvil GUI in under 20 lines of code
MIT License
485 stars 114 forks source link

Issue with setting displaynames of items #317

Open Thorinwasher opened 9 months ago

Thorinwasher commented 9 months ago

Issue

I'm having problems with setting the displayname of the items. I want one of the displaynames to be "Cancel" in red, and another be "Accept" in green.

Behavior

When triggering the GUI, these items appear to have the default names. (Barrier and Green wool)

Versions

Server:

This server is running Paper version git-Paper-426 (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT) (Git: 7ccefdc) You are 9 version(s) behind Download the new version at: https://papermc.io/downloads/paper

AnvilGUI version 1.9.2-SNAPSHOT

Reproducible example

        AnvilGUI.Builder builder = new AnvilGUI.Builder();
        ItemStack itemLeft = new ItemStack(Material.BARRIER);
        ItemMeta itemMetaLeft = itemLeft.getItemMeta();
        itemMetaLeft.displayName(Component.text("Cancel").color(NamedTextColor.RED));
        itemLeft.setItemMeta(itemMetaLeft);
        ItemStack itemOutput = new ItemStack(Material.GREEN_WOOL);
        ItemMeta itemMetaOutput = itemOutput.getItemMeta();
        itemMetaOutput.displayName(Component.text("Accept").color(NamedTextColor.GREEN));
        itemOutput.setItemMeta(itemMetaOutput);
        builder.itemLeft(itemLeft).itemOutput(itemOutput).title("Type flag arguments").plugin(plugin).text("");
        builder.onClickAsync((slot, stateSnapshot) -> CompletableFuture.supplyAsync(() -> {
            if (slot == AnvilGUI.Slot.INPUT_RIGHT) {
                return Collections.emptyList();
            }
            if (slot == AnvilGUI.Slot.OUTPUT) {
                action.accept(stateSnapshot.getText()); // Do whatever action I want with the string I fetched from the anvil
            }
            return List.of(AnvilGUI.ResponseAction.close());
        }));
        builder.interactableSlots(AnvilGUI.Slot.INPUT_LEFT, AnvilGUI.Slot.INPUT_RIGHT, AnvilGUI.Slot.OUTPUT);
        builder.open(activator);
BoBkiNN commented 8 months ago

Same issue on 1.19.4 1.9.2-SNAPSHOT

zuukynn commented 4 months ago

Same. Let me know if a fix is found.

0dinD commented 3 months ago

I would assume this is because you are calling .text(""), the empty string means default name, i.e. "Barrier" or "Green Wool", if I remember correctly. Calling .text("") overrides the display name of the item, see:

https://github.com/WesJD/AnvilGUI/blob/a7cac4bc77cf399aba80c8c418aa335879e6b11f/api/src/main/java/net/wesjd/anvilgui/AnvilGUI.java#L657-L665

https://github.com/WesJD/AnvilGUI/blob/a7cac4bc77cf399aba80c8c418aa335879e6b11f/api/src/main/java/net/wesjd/anvilgui/AnvilGUI.java#L715-L734

https://github.com/WesJD/AnvilGUI/blob/master/README.md#textstring

So anyways, if you just remove the .text("") call then your custom name should probably work. But I'm not sure if that's what you want. The initial contents of the text field are linked to the name of the left item, and the name of the output item is linked to whatever you type into the text field. I haven't really looked into it but wouldn't be surprised if that's hardcoded behavior in the Minecraft client. I mean, that's how anvils work.

You might be able to work around this by using item lore to add your custom text though.