Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with setting displaynames of items #317

Open
Thorinwasher opened this issue Feb 24, 2024 · 3 comments
Open

Issue with setting displaynames of items #317

Thorinwasher opened this issue Feb 24, 2024 · 3 comments
Labels

Comments

@Thorinwasher
Copy link

Thorinwasher commented Feb 24, 2024

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
Copy link

BoBkiNN commented Mar 19, 2024

Same issue on 1.19.4 1.9.2-SNAPSHOT

@zuukynn
Copy link

zuukynn commented Jul 25, 2024

Same. Let me know if a fix is found.

@0dinD
Copy link
Collaborator

0dinD commented Aug 5, 2024

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:

if (itemText != null) {
if (itemLeft == null) {
itemLeft = new ItemStack(Material.PAPER);
}
ItemMeta paperMeta = itemLeft.getItemMeta();
paperMeta.setDisplayName(itemText);
itemLeft.setItemMeta(paperMeta);
}

static ResponseAction replaceInputText(String text) {
Validate.notNull(text, "text cannot be null");
return (anvilgui, player) -> {
ItemStack item = anvilgui.getInventory().getItem(Slot.OUTPUT);
if (item == null) {
// Fallback on left input slot if player hasn't typed anything yet
item = anvilgui.getInventory().getItem(Slot.INPUT_LEFT);
}
if (item == null) {
throw new IllegalStateException(
"replaceInputText can only be used if slots OUTPUT or INPUT_LEFT are not empty");
}
final ItemStack cloned = item.clone();
final ItemMeta meta = cloned.getItemMeta();
meta.setDisplayName(text);
cloned.setItemMeta(meta);
anvilgui.getInventory().setItem(Slot.INPUT_LEFT, cloned);
};
}

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.

@0dinD 0dinD added the question label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants