From ebe3708eca7e9dbe3f31dfb21d7cbb7d4284b087 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Thu, 25 Apr 2024 22:27:50 +0200 Subject: [PATCH] Finished the MaterialSelectSettingGui. --- .../gui/config/ask/ConfirmActionGui.java | 16 +- .../config/list/MaterialSelectSettingGui.java | 181 ----------- .../elements/GroupConfigSubSettingGui.java | 9 +- .../settings/MaterialSelectSettingGui.java | 304 ++++++++++++++++++ .../cuanvil/gui/util/GuiGlobalItems.java | 4 +- .../cuanvil/gui/util/GuiSharedConstant.java | 9 +- 6 files changed, 332 insertions(+), 191 deletions(-) delete mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MaterialSelectSettingGui.java create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ask/ConfirmActionGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ask/ConfirmActionGui.java index 5b962ed..d306ff1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ask/ConfirmActionGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ask/ConfirmActionGui.java @@ -19,11 +19,14 @@ import java.util.logging.Level; public class ConfirmActionGui extends AbstractAskGui { public ConfirmActionGui(@NotNull String title, String actionDescription, - Gui backOnCancel, Gui backOnConfirm, Supplier onConfirm) { + Gui backOnCancel, Gui backOnConfirm, Supplier onConfirm, + boolean permanent) { super(3, title, backOnCancel); // Save item - this.pane.bindItem('S', new GuiItem(GuiSharedConstant.CONFIRM_ITEM, event -> { + this.pane.bindItem('S', new GuiItem( + (permanent ? GuiSharedConstant.CONFIRM_PERMANENT_ITEM : GuiSharedConstant.CONFIRM_ITEM), + event -> { event.setCancelled(true); HumanEntity player = event.getWhoClicked(); @@ -53,12 +56,19 @@ public class ConfirmActionGui extends AbstractAskGui { ItemMeta infoMeta = infoItem.getItemMeta(); infoMeta.setDisplayName("\u00A7eAre you sure ?"); - infoMeta.setLore(Arrays.asList(actionDescription.split("\n"))); + if(actionDescription != null){ + infoMeta.setLore(Arrays.asList(actionDescription.split("\n"))); + } infoItem.setItemMeta(infoMeta); pane.bindItem('I', new GuiItem(infoItem, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); } + public ConfirmActionGui(@NotNull String title, String actionDescription, + Gui backOnCancel, Gui backOnConfirm, Supplier onConfirm){ + this(title, actionDescription, backOnCancel, backOnConfirm, onConfirm, true); + } + @Override protected Pattern getGuiPattern() { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MaterialSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MaterialSelectSettingGui.java deleted file mode 100644 index c7831ec..0000000 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/MaterialSelectSettingGui.java +++ /dev/null @@ -1,181 +0,0 @@ -package xyz.alexcrea.cuanvil.gui.config.list; - -import com.github.stefvanschie.inventoryframework.gui.GuiItem; -import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; -import com.github.stefvanschie.inventoryframework.pane.util.Pattern; -import io.delilaheve.CustomAnvil; -import io.delilaheve.util.ConfigOptions; -import org.bukkit.Material; -import org.bukkit.entity.HumanEntity; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.jetbrains.annotations.NotNull; -import xyz.alexcrea.cuanvil.gui.config.SelectMaterialContainer; -import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; -import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; -import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; -import java.util.logging.Level; - -public class MaterialSelectSettingGui extends MappedElementListConfigGui { - - private final SelectMaterialContainer selector; - private final List defaultMaterials; - private final int defaultMaterialHash; - private int nowMaterialHash; - - public MaterialSelectSettingGui( - @NotNull SelectMaterialContainer selector, - @NotNull String title, - @NotNull Gui backGui) { - super(title); - this.selector = selector; - this.defaultMaterials = new ArrayList<>(this.selector.getSelectedMaterials()); - - this.defaultMaterialHash = hashFromMaterialList(this.defaultMaterials); - this.nowMaterialHash = this.defaultMaterialHash; - - init(); - - // Change back item - this.backgroundPane.bindItem('B', GuiGlobalItems.backItem(backGui)); - } - - @Override - protected Pattern getBackgroundPattern(){ - return new Pattern( - GuiSharedConstant.EMPTY_GUI_FULL_LINE, - GuiSharedConstant.EMPTY_GUI_FULL_LINE, - GuiSharedConstant.EMPTY_GUI_FULL_LINE, - GuiSharedConstant.EMPTY_GUI_FULL_LINE, - GuiSharedConstant.EMPTY_GUI_FULL_LINE, - "BT1LAR11S" - ); - } - - private GuiItem saveItem; - private GuiItem noChangeItem; - - @Override - protected void prepareStaticValues() { - super.prepareStaticValues(); - - // Temporary leave item - GuiItem temporaryLeave = GuiGlobalItems.temporaryCloseGuiToSelectItem(Material.YELLOW_TERRACOTTA, this); - this.backgroundPane.bindItem('T', temporaryLeave); - - // Select new mat item - ItemStack selectItem = new ItemStack(Material.LIME_STAINED_GLASS_PANE); - ItemMeta selectMeta = selectItem.getItemMeta(); - - selectMeta.setDisplayName("aaaaaaa"); - - selectItem.setItemMeta(selectMeta); - - this.backgroundPane.bindItem('A', new GuiItem(selectItem, setItemAsCursor(), CustomAnvil.instance)); - - // Save item - this.saveItem = GuiGlobalItems.noChangeItem(); - - this.noChangeItem = GuiGlobalItems.noChangeItem(); - this.backgroundPane.bindItem('S', this.noChangeItem); - - } - - /** - * @return A consumer to update the current setting's value. - */ - protected Consumer setItemAsCursor() { - return event -> { - event.setCancelled(true); - - HumanEntity player = event.getWhoClicked(); - ItemStack cursor = player.getItemOnCursor(); - - if(cursor.getType().isAir()) return; - - Material cursorMat = cursor.getType(); - if(!this.elementGuiMap.containsKey(cursorMat)){ - updateValueForGeneric(cursorMat, true); - this.nowMaterialHash ^= cursorMat.hashCode(); - - testCanSave(); - } - - update(); - }; - } - - @Override - protected ItemStack createItemForGeneric(Material material) { - return new ItemStack(material); //this is temp TODO the function - } - - @Override - protected List getEveryDisplayableInstanceOfGeneric() { - return this.defaultMaterials; - } - - @Override - protected void updateElement(Material generic, GuiItem element) { - if(ConfigOptions.INSTANCE.getDebugLog()){ - CustomAnvil.instance.getLogger().log(Level.INFO, - "Call that should not happen happened...", - new IllegalStateException()); - } - } - - @Override - protected GuiItem newElementRequested(Material generic, GuiItem newItem) { - newItem.setAction(GuiGlobalActions.stayInPlace); //TODO ask to remove the item on click (or instant remove) - return newItem; - } - - @Override - protected GuiItem findItemFromElement(Material generic, GuiItem element) { - return element; - } - - @Override - protected GuiItem findGuiItemForRemoval(Material generic, GuiItem element) { - return element; - } - - private static int hashFromMaterialList(List materialList){ - int defaultMaterialHash = 0; - for (Material material : materialList) { - defaultMaterialHash ^= material.hashCode(); - } - return defaultMaterialHash; - } - - private void testCanSave() { - if(this.defaultMaterialHash == this.nowMaterialHash){ - this.backgroundPane.bindItem('S', this.noChangeItem); - }else{ - this.backgroundPane.bindItem('S', this.saveItem); - } - - } - - - - // Unused functions. - @Override - protected GuiItem prepareCreateNewItem() {// Not used - return null; - } - @Override - protected Consumer prepareCreateItemConsumer(HumanEntity player) {// Not used - return null; - } - - @Override - protected String genericDisplayedName() {// Not Used - return null; - } -} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/elements/GroupConfigSubSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/elements/GroupConfigSubSettingGui.java index e9499c4..8f1d905 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/elements/GroupConfigSubSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/list/elements/GroupConfigSubSettingGui.java @@ -17,8 +17,8 @@ import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; import xyz.alexcrea.cuanvil.gui.config.SelectMaterialContainer; import xyz.alexcrea.cuanvil.gui.config.ask.ConfirmActionGui; import xyz.alexcrea.cuanvil.gui.config.global.GroupConfigGui; -import xyz.alexcrea.cuanvil.gui.config.list.MaterialSelectSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.GroupSelectSettingGui; +import xyz.alexcrea.cuanvil.gui.config.settings.MaterialSelectSettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; @@ -233,7 +233,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen @Override public void cleanAndBeUnusable() { - + //TODO } // ---------------------------- @@ -294,6 +294,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen // ---------------------------- // End of SelectGroupContainer related methods + // ---------------------------- // SelectGroupContainer related methods // ---------------------------- @@ -320,14 +321,14 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen // Save file configuration to disk if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { - return ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); + return ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; } @Override public EnumSet illegalMaterials() { - return EnumSet.noneOf(Material.class); + return EnumSet.of(Material.AIR); } // ---------------------------- diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java new file mode 100644 index 0000000..5419b62 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java @@ -0,0 +1,304 @@ +package xyz.alexcrea.cuanvil.gui.config.settings; + +import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; +import com.github.stefvanschie.inventoryframework.pane.util.Pattern; +import io.delilaheve.CustomAnvil; +import org.bukkit.Material; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; +import xyz.alexcrea.cuanvil.gui.config.SelectMaterialContainer; +import xyz.alexcrea.cuanvil.gui.config.ask.ConfirmActionGui; +import xyz.alexcrea.cuanvil.gui.config.list.MappedElementListConfigGui; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; +import xyz.alexcrea.cuanvil.util.CasedStringUtil; + +import java.util.*; +import java.util.function.Consumer; + +public class MaterialSelectSettingGui extends MappedElementListConfigGui { + + private final SelectMaterialContainer selector; + private final Gui backGui; + private boolean instantRemove; + + private final List defaultMaterials; + private final EnumSet illegalMaterials; + private final int defaultMaterialHash; + private int nowMaterialHash; + + public MaterialSelectSettingGui( + @NotNull SelectMaterialContainer selector, + @NotNull String title, + @NotNull Gui backGui) { + super(title); + this.selector = selector; + this.backGui = backGui; + this.instantRemove = false; + + this.defaultMaterials = new ArrayList<>(this.selector.getSelectedMaterials()); + this.illegalMaterials = this.selector.illegalMaterials(); + + this.defaultMaterialHash = hashFromMaterialList(this.defaultMaterials); + this.nowMaterialHash = this.defaultMaterialHash; + + init(); + + // Change back item + this.backgroundPane.bindItem('B', GuiGlobalItems.backItem(backGui)); + } + + @Override + protected Pattern getBackgroundPattern(){ + return new Pattern( + GuiSharedConstant.EMPTY_GUI_FULL_LINE, + GuiSharedConstant.EMPTY_GUI_FULL_LINE, + GuiSharedConstant.EMPTY_GUI_FULL_LINE, + GuiSharedConstant.EMPTY_GUI_FULL_LINE, + GuiSharedConstant.EMPTY_GUI_FULL_LINE, + "BT1LAR1IS" + ); + } + + private GuiItem saveItem; + private GuiItem noChangeItem; + + private GuiItem instantRemoveOn; + private GuiItem instantRemoveOff; + + @Override + protected void prepareStaticValues() { + super.prepareStaticValues(); + + // Temporary leave item + GuiItem temporaryLeave = GuiGlobalItems.temporaryCloseGuiToSelectItem(Material.YELLOW_TERRACOTTA, this); + this.backgroundPane.bindItem('T', temporaryLeave); + + // Select new mat item + ItemStack selectItem = new ItemStack(Material.BLUE_STAINED_GLASS_PANE); + ItemMeta selectMeta = selectItem.getItemMeta(); + + selectMeta.setDisplayName("\u00A7aAdd Item"); + selectMeta.setLore(Arrays.asList( + "\u00A77Click here with an item to add", + "\u00A77it's Material to the list.")); + + selectItem.setItemMeta(selectMeta); + + this.backgroundPane.bindItem('A', new GuiItem(selectItem, setItemAsCursor(), CustomAnvil.instance)); + + // Save item + this.saveItem = prepareSaveItem(); + + this.noChangeItem = GuiGlobalItems.noChangeItem(); + this.backgroundPane.bindItem('S', this.noChangeItem); + + // Instant Remove On item + ItemStack instantRemoveOnItem = new ItemStack(Material.LIME_STAINED_GLASS_PANE); + ItemMeta instantRemoveOnMeta = instantRemoveOnItem.getItemMeta(); + + instantRemoveOnMeta.setDisplayName("\u00A7eInstant remove is \u00A7aEnabled \u00A7e!"); + instantRemoveOnMeta.setLore( + Collections.singletonList("\u00A77Click here to disable the instant remove")); + + instantRemoveOnItem.setItemMeta(instantRemoveOnMeta); + + // Instant Remove Off item + ItemStack instantRemoveOffItem = new ItemStack(Material.RED_STAINED_GLASS_PANE); + ItemMeta instantRemoveOffMeta = instantRemoveOffItem.getItemMeta(); + + instantRemoveOffMeta.setDisplayName("\u00A7eInstant remove is \u00A7cDisabled \u00A7e!"); + instantRemoveOffMeta.setLore( + Collections.singletonList("\u00A77Click here to enable the instant remove")); + + instantRemoveOffItem.setItemMeta(instantRemoveOffMeta); + + // Instant Remove gui items + this.instantRemoveOn = new GuiItem(instantRemoveOnItem, (event -> { + this.instantRemove = false; + this.backgroundPane.bindItem('I', this.instantRemoveOff); + update(); + }), CustomAnvil.instance); + + this.instantRemoveOff = new GuiItem(instantRemoveOffItem, (event -> { + this.instantRemove = true; + this.backgroundPane.bindItem('I', this.instantRemoveOn); + update(); + }), CustomAnvil.instance); + + this.backgroundPane.bindItem('I', this.instantRemoveOff); + } + + private GuiItem prepareSaveItem() { + ItemStack saveItemStack = new ItemStack(GuiGlobalItems.DEFAULT_SAVE_ITEM); + ItemMeta saveMeta = saveItemStack.getItemMeta(); + + saveMeta.setDisplayName("\u00A7aSave"); + + saveItemStack.setItemMeta(saveMeta); + + return new GuiItem(saveItemStack, event -> { + event.setCancelled(true); + + HumanEntity player = event.getWhoClicked(); + // Do not allow to save configuration if player do not have edit configuration permission + if (!player.hasPermission(CustomAnvil.editConfigPermission)) { + player.closeInventory(); + player.sendMessage(GuiGlobalActions.NO_EDIT_PERM); + return; + } + if(!testCanSave()) return; + + + // Save setting + EnumSet result = EnumSet.noneOf(Material.class); + result.addAll(this.elementGuiMap.keySet()); + + if(!this.selector.setSelectedMaterials(result)){ + player.sendMessage("\u00A7cSomething went wrong while saving the change of value."); + } + + // Return to parent + this.backGui.show(player); + + }, CustomAnvil.instance); + } + + /** + * @return A consumer to update the current setting's value. + */ + protected Consumer setItemAsCursor() { + return event -> { + event.setCancelled(true); + + HumanEntity player = event.getWhoClicked(); + ItemStack cursor = player.getItemOnCursor(); + + // Test if cursor material allowed + Material cursorMat = cursor.getType(); + if(cursorMat.isAir()) return; + if(this.illegalMaterials.contains(cursorMat)) return; + + // Update gui only if item did not exist before. + if(!this.elementGuiMap.containsKey(cursorMat)){ + updateValueForGeneric(cursorMat, true); + this.nowMaterialHash ^= cursorMat.hashCode(); + + setSaveItem(); + update(); + } + }; + } + + @Override + protected ItemStack createItemForGeneric(Material material) { + ItemStack item = new ItemStack(material); + ItemMeta meta = item.getItemMeta(); + + if(meta == null) return item; + meta.setDisplayName("\u00A7f" + CasedStringUtil.snakeToUpperSpacedCase(material.name())); + meta.setLore(Collections.singletonList("\u00A77Click here to remove this material from the list")); + meta.addItemFlags(ItemFlag.values()); + + item.setItemMeta(meta); + + return item; + } + + @Override + protected List getEveryDisplayableInstanceOfGeneric() { + return this.defaultMaterials; + } + + @Override + protected void updateElement(Material material, GuiItem element) { + // Nothing happen here I think + } + + @Override + protected GuiItem newElementRequested(Material material, GuiItem newItem) { + newItem.setAction(event -> { + if(this.instantRemove){ + removeMaterial(material); + }else { + String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()); + + // Create and show confirm remove gui. + ConfirmActionGui confirmGui = new ConfirmActionGui( + "Remove " + materialName, + "\u00A77Confirm Remove " + materialName.toLowerCase() + " from this list.", + this, this, + () -> { + removeMaterial(material); + return true; + }, false + ); + confirmGui.show(event.getWhoClicked()); + + } + }); + return newItem; + } + + private void removeMaterial(Material material) { + if(this.elementGuiMap.containsKey(material)){ + this.nowMaterialHash ^= material.hashCode(); + setSaveItem(); + removeGeneric(material); + } + + } + + @Override + protected GuiItem findItemFromElement(Material generic, GuiItem element) { + return element; + } + + @Override + protected GuiItem findGuiItemForRemoval(Material generic, GuiItem element) { + return element; + } + + private static int hashFromMaterialList(List materialList){ + int defaultMaterialHash = 0; + for (Material material : materialList) { + defaultMaterialHash ^= material.hashCode(); + } + return defaultMaterialHash; + } + + private void setSaveItem() { + if(!testCanSave()){ + this.backgroundPane.bindItem('S', this.noChangeItem); + }else{ + this.backgroundPane.bindItem('S', this.saveItem); + } + + } + + private boolean testCanSave() { + return this.defaultMaterialHash != this.nowMaterialHash; + } + + + // Unused functions. + @Override + protected GuiItem prepareCreateNewItem() {// Not used + return null; + } + @Override + protected Consumer prepareCreateItemConsumer(HumanEntity player) {// Not used + return null; + } + + @Override + protected String genericDisplayedName() {// Not Used + return null; + } +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java index f2e0563..d7fa41f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java @@ -117,8 +117,8 @@ public class GuiGlobalItems { addBackgroundItem(target, DEFAULT_BACKGROUND_MAT); } - private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE; - private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE; + public static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE; + public static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE; /** * Create a new save setting GuiItem. diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java index 0ad90a6..a2fa352 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -55,6 +55,7 @@ public class GuiSharedConstant { public static final ItemStack CANCEL_ITEM; public static final ItemStack CONFIRM_ITEM; + public static final ItemStack CONFIRM_PERMANENT_ITEM; static { CANCEL_ITEM = new ItemStack(Material.RED_TERRACOTTA); @@ -66,9 +67,15 @@ public class GuiSharedConstant { CONFIRM_ITEM = new ItemStack(Material.GREEN_TERRACOTTA); meta = CONFIRM_ITEM.getItemMeta(); meta.setDisplayName("\u00A7aConfirm"); + meta.setLore(Collections.singletonList("\u00A77Confirm current action.")); + CONFIRM_ITEM.setItemMeta(meta); + + CONFIRM_PERMANENT_ITEM = new ItemStack(Material.GREEN_TERRACOTTA); + meta = CONFIRM_PERMANENT_ITEM.getItemMeta(); + meta.setDisplayName("\u00A7aConfirm"); meta.setLore(Arrays.asList("\u00A77Confirm current action.", "\u00A74Cation: This action can't be canceled.")); - CONFIRM_ITEM.setItemMeta(meta); + CONFIRM_PERMANENT_ITEM.setItemMeta(meta); }