IF icon indicating copy to clipboard operation
IF copied to clipboard

Weird behaviors with AnvilGUI

Open LonLoF opened this issue 3 years ago • 4 comments

I had weird behaviors with AnvilGUI, what I haven't notice with ChestGUI. Tried to create name changing GUI, weird things what I found:

  1. When creating AnvilGUI you are able to click-delete items in your inventory, even if setOnGlobalClick(event -> event.setCancelled(true)) is used. All items still exist when clicked GUI items what opens last GUI where were you, but when closed via ESC then you lose your items, what you clicked in your inventory.
  2. When clicked item in first slot in my GUI, what brings you back to last opened GUI, it also copys first and result item to last opende GUI. These items disabers when clicked any item in inventory or GUI. Result slot item event handeler is similar but do not act this way. Didn't figured that out how to fix it. image image
  3. Had a problem when having item in second slot, couldn't get out from gui when clicking result item. Solution was to set new StaticPane to second slot.
  4. I was unable to change item meta or put the new item on the same slot with new info. So I opened the dublicated gui with new item in place.
Snipet of my GUI system, where point 3 & 4 are somehow fixed but problems 1 & 2 still exist.
private void buildNameChangerGUI(String name, ChestGui backGUI, WarpObject warp, String newN, List <Component> errLore, int errCount){
        AnvilGui changeName = new AnvilGui(guiHeading(name));
        changeName.setOnGlobalClick(event -> event.setCancelled(true));
        changeName.setOnOutsideClick(event -> event.getWhoClicked().closeInventory());

        Label cancel = new Label(0, 0, 1, 1, Font.RED);
        List <Component> cancelLore = new ArrayList<>();
        cancelLore.add(Component.text("Back to settings", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
        cancel.setText("X", (character, item) -> buttonGuiItem(warp.getName(), cancelLore, item));
        cancel.setOnClick(event -> backGUI.show(event.getWhoClicked())); // Somehow changeName gui elements appears in backGUI, updating the gui does not help, setting labels to hidden, does not help, hidding lable and updating does not help.
        changeName.getFirstItemComponent().addPane(cancel);

        StaticPane secondItem = new StaticPane(0,0,1,1);
        if (errCount > 0){
            secondItem.addItem(new GuiItem(creatInfoItem("Errors in the name of \"" + newN + "\"", errLore, errCount)),0,0);
            changeName.getSecondItemComponent().addPane(secondItem);
        }

        Label confirmation = new Label(0, 0, 1, 1, Font.LIME);
        List <Component> confirmationLore = new ArrayList<>();
        if (!player.hasPermission("pws.admin")){
            confirmationLore.add(Component.text("Min. characters:", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
            confirmationLore.add(Component.text(6, TextColor.color(0xeaeaea)));
            confirmationLore.add(Component.text(""));
        }
        confirmationLore.add(Component.text("Max. characters:", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
        confirmationLore.add(Component.text(16, TextColor.color(0xeaeaea)));
        confirmationLore.add(Component.text(""));
        confirmationLore.add(Component.text("Contain only letters:", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
        confirmationLore.add(Component.text("a-zA-ZöäõüÖÜÕÄ", TextColor.color(0xeaeaea)));
        confirmationLore.add(Component.text(""));
        confirmationLore.add(Component.text("Start with a capital letter", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
        confirmation.setText("✓", (character, item) -> buttonGuiItem("Confirm", confirmationLore, item));
        confirmation.setOnClick(event -> {
            String newName = changeName.getRenameText();
            List <Component> errorLore = new ArrayList<>();
            int errorCount = 0;

            if (newName.length() < 6){
                if (!player.hasPermission("pws.admin")){
                    errorCount ++;
                    errorLore.add(Component.text("Name has to be longer than " + 6 + " characters!", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
                    errorLore.add(Component.text(""));
                }
            }
            if (newName.length() > 16){
                errorCount ++;
                errorLore.add(Component.text("Name has to be shorter than " + 16 + " characters!", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
                errorLore.add(Component.text(""));
            }
            if (!newName.matches("[a-zA-ZöäõüÖÜÕÄ]+")){
                errorCount ++;
                errorLore.add(Component.text("Name has to contain only letters!", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
                errorLore.add(Component.text(""));
            }
            if (!Character.toString(newName.charAt(0)).matches("[A-ZÖÜÕÄ]+")){
                errorCount ++;
                errorLore.add(Component.text("Name has to start with a capital letter!", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
                errorLore.add(Component.text(""));
            }
            if (queryDatabase.nameInDatabase(newName)){
                errorCount ++;
                errorLore.add(Component.text("Name already in use!", TextColor.color(0xeaeaea)).decoration(TextDecoration.ITALIC, false));
            }

            if (errorCount > 0){
                if (errorLore.get(errorLore.size()-1).equals(Component.text(""))) errorLore.remove(errorLore.size()-1);
                buildNameChangerGUI(name, backGUI,warp, newName, errorLore, errorCount);
            }
            else{
                changeName.getSecondItemComponent().addPane(secondItem); //because otherwise it does not work
                player.sendMessage(newName); //TODO Change the name in the db
                backGUI.show(event.getWhoClicked());
            }
        });
        changeName.getResultComponent().addPane(confirmation);

        changeName.show(player);
    }

LonLoF avatar Aug 21 '22 20:08 LonLoF

Thank you for the report. I'm uncertain what you mean with point 3. Do you mean that when opening a new gui by clicking on the result item, having an item in the second slot prevents this gui from opening? As for point 4, make sure to update the gui with Gui#update after changing the item's lore. I will look into points 1, and 2.

stefvanschie avatar Aug 22 '22 13:08 stefvanschie

I'm uncertain what you mean with point 3. Do you mean that when opening a new gui by clicking on the result item, having an item in the second slot prevents this gui from opening?

Yes, exactly what I meant.

As for point 4, make sure to update the gui with Gui#update after changing the item's lore.

I am pretty sure I tested that and it didn't work, but to be sure I gave it a try. And looks like my problems 3 and 4 are solved. Before I created that issue, in my code I created secondItem pane inside the event handlerer. Code what I posted here, it is moved out and seems it solved these 2 problems without knowing it. So I removed my workaround, what created new gui every time. Problems 1 & 2 still exists.

Extra idee with AnvilGUI That would be awesome if you could change AnvilGUI Enchantment Cost also. In my case Enchantment Cost 1 is miss leading, it shows it but luckily it does no take any levels. That would be nice if I can set it to 0.

LonLoF avatar Aug 22 '22 17:08 LonLoF

I tried reproducing point 3, but was unable to do so. The gui opened fine for me.

I'll look into changing the enchantment cost for the anvil as well.

stefvanschie avatar Aug 24 '22 13:08 stefvanschie

Points 1 and 2 will be fixed in the next version.

stefvanschie avatar Sep 17 '22 18:09 stefvanschie

The ability to change the cost will be added in 0.10.8.

stefvanschie avatar Nov 06 '22 19:11 stefvanschie