diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java index 9112a83..614f052 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java @@ -18,13 +18,12 @@ import java.util.HashMap; public class EnchantConflictGui extends ChestGui { public final static EnchantConflictGui INSTANCE = new EnchantConflictGui(); - - - private final HashMap conflictGuiMap; static { INSTANCE.init(); } + private final HashMap conflictGuiMap; + private EnchantConflictGui() { super(6, "§eConflict Config", CustomAnvil.instance); this.conflictGuiMap = new HashMap<>(); @@ -50,24 +49,54 @@ public class EnchantConflictGui extends ChestGui { this.conflictGuiMap.clear(); this.filledEnchant.clear(); - // Create new sub setting gui for (EnchantConflictGroup conflict : ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList()) { - EnchantConflictSubSettingGui conflictGui = new EnchantConflictSubSettingGui(this, conflict); + updateValueForConflict(conflict, false); + } - // Temporaire, il faut faire un item avec le conflict et donc un generateur arbitraire + update(); + } - ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); - filledEnchant.addItem(new GuiItem(item, GuiGlobalActions.openGuiAction(conflictGui), CustomAnvil.instance)); + public ItemStack createItemForConflict(EnchantConflictGroup conflict){ + ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); + //TODO item + + return item; + } + + public void updateValueForConflict(EnchantConflictGroup conflict, boolean shouldUpdate){ + EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict); + ItemStack item = createItemForConflict(conflict); + + GuiItem guiItem; + if(gui == null){ + // Create new sub setting gui + guiItem = new GuiItem(item, CustomAnvil.instance); + gui = new EnchantConflictSubSettingGui(this, conflict, guiItem); + + guiItem.setAction(GuiGlobalActions.openGuiAction(gui)); + + this.conflictGuiMap.put(conflict, gui); + this.filledEnchant.addItem(guiItem); + }else{ + // replace item with the updated one + guiItem = gui.getParentItemForThisGui(); + guiItem.setItem(item); + } + + gui.updateLocal(); + if(shouldUpdate){ + update(); } } - public void updateValueForConflict(EnchantConflictGroup conflict){ - - } - public void removeConflict(EnchantConflictGroup conflict){ + EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict); + if(gui == null) return; + this.filledEnchant.removeItem(gui.getParentItemForThisGui()); + this.conflictGuiMap.remove(conflict); + update(); } } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/subsetting/EnchantConflictSubSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/subsetting/EnchantConflictSubSettingGui.java index 730423d..1122d80 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/subsetting/EnchantConflictSubSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/subsetting/EnchantConflictSubSettingGui.java @@ -14,7 +14,6 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import xyz.alexcrea.cuanvil.group.EnchantConflictManager; -import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.ConfirmActionGui; import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer; @@ -28,22 +27,31 @@ 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.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; import java.util.function.Supplier; +import java.util.logging.Level; public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements SelectEnchantmentContainer, SelectGroupContainer { private final EnchantConflictGui parent; private final EnchantConflictGroup enchantConflict; + private final GuiItem parentItemForThisGui; private final PatternPane pane; - private boolean canOpen = true; + private boolean shouldWorld = true; public EnchantConflictSubSettingGui( @NotNull EnchantConflictGui parent, - @NotNull EnchantConflictGroup enchantConflict) { - super(3, "\u00A72Config for \u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), CustomAnvil.instance); + @NotNull EnchantConflictGroup enchantConflict, + @NotNull GuiItem parentItemForThisGui) { + super(3, + "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A78Config", + CustomAnvil.instance); this.parent = parent; this.enchantConflict = enchantConflict; + this.parentItemForThisGui = parentItemForThisGui; Pattern pattern = new Pattern( GuiSharedConstant.EMPTY_GUI_FULL_LINE, @@ -62,8 +70,8 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S private void prepareStaticValues() { - GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE); - GuiGlobalItems.addBackgroundItem(pane); + GuiGlobalItems.addBackItem(this.pane, this.parent); + GuiGlobalItems.addBackgroundItem(this.pane); // Delete item ItemStack deleteItem = new ItemStack(Material.RED_TERRACOTTA); @@ -73,33 +81,34 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S deleteMeta.setLore(Collections.singletonList("\u00A7cCaution with this button !")); deleteItem.setItemMeta(deleteMeta); - pane.bindItem('D', new GuiItem(deleteItem, GuiGlobalActions.openGuiAction(createDeleteGui()), CustomAnvil.instance)); + this.pane.bindItem('D', new GuiItem(deleteItem, GuiGlobalActions.openGuiAction(createDeleteGui()), CustomAnvil.instance)); // Displayed item will be updated later - enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event)->{ + this.enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event)->{ event.setCancelled(true); EnchantSelectSettingGui enchantGui = new EnchantSelectSettingGui( - "\u00A7eEnchantments for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), + "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A75Enchantments", this, this, 0); enchantGui.show(event.getWhoClicked()); }, CustomAnvil.instance); - groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{ + this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{ event.setCancelled(true); GroupSelectSettingGui enchantGui = new GroupSelectSettingGui( - "\u00A7eGroups for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), + "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(this.enchantConflict.getName()) + " \u00A73Groups", this, this, 0); enchantGui.show(event.getWhoClicked()); }, CustomAnvil.instance); - minBeforeActiveSettingFactory = IntSettingsGui.intFactory("\u00A7eMinimum enchantment before conflict is active", this, - enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER, + this.minBeforeActiveSettingFactory = IntSettingsGui.intFactory( + "\u00A78Minimum enchantment count", + this, this.enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER, 0, 255, 0, 1 ); - pane.bindItem('E', enchantSettingItem); - pane.bindItem('G', groupSettingItem); + this.pane.bindItem('E', this.enchantSettingItem); + this.pane.bindItem('G', this.groupSettingItem); // Now we update the items updateLocal(); @@ -111,13 +120,19 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); // Remove from manager - for (Enchantment enchantment : enchantConflict.getEnchants()) { - manager.removeConflictFromMap(enchantment, enchantConflict); + for (Enchantment enchantment : this.enchantConflict.getEnchants()) { + manager.removeConflictFromMap(enchantment, this.enchantConflict); } - manager.conflictList.remove(enchantConflict); + manager.conflictList.remove(this.enchantConflict); + + // Remove from parent + this.parent.removeConflict(this.enchantConflict); + + // Remove self + cleanUnused(); // Update config file storage - ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName(), null); + ConfigHolder.CONFLICT_HOLDER.getConfig().set(this.enchantConflict.getName(), null); // Save boolean success = true; @@ -128,7 +143,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S return success; }; - return new ConfirmActionGui("\u00A7cDelete \u00A7e"+CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName())+"\u00A7c ?", + return new ConfirmActionGui("\u00A7cDelete \u00A7e"+CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName())+"\u00A7c?", "\u00A77Confirm that you want to delete this conflict.", this, this.parent, deleteSupplier ); @@ -136,46 +151,83 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S @Override public void updateGuiValues() { - this.parent.updateValueForConflict(this.enchantConflict); + this.parent.updateValueForConflict(this.enchantConflict, true); // Parent should call updateLocal } public void updateLocal(){ - if(!this.canOpen) return; + if(!this.shouldWorld) return; // Prepare enchantment lore ArrayList enchantLore = new ArrayList<>(); enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include"); Set enchants = getSelectedEnchantments(); if(enchants.isEmpty()){ - enchantLore.add("\u00A77There is no enchantment for this conflict."); + enchantLore.add("\u00A77There is no included enchantment for this conflict."); }else{ enchantLore.add("\u00A77List of included enchantment for this conflict:"); Iterator enchantIterator = enchants.iterator(); - int maxindex = (enchants.size() > 5 ? 4 : enchants.size()); + boolean greaterThanMax = enchants.size() > 5; + int maxindex = (greaterThanMax ? 4 : enchants.size()); for (int i = 0; i < maxindex; i++) { // format string like "- Fire Protection" - enchantLore.add("\u00A77- \u00A75"+CasedStringUtil.snakeToUpperSpacedCase(enchantIterator.next().getKey().getKey())); + String formattedName = CasedStringUtil.snakeToUpperSpacedCase(enchantIterator.next().getKey().getKey()); + enchantLore.add("\u00A77- \u00A75" + formattedName); + } + if(greaterThanMax){ + enchantLore.add("\u00A77And "+(enchants.size()-4)+" more..."); + } + + } + + // Prepare group lore + ArrayList groupLore = new ArrayList<>(); + groupLore.add("\u00A77Allow you to select a list of \u00A73Groups \u00A77that this conflict should include"); + Set grouos = getSelectedGroups(); + if(grouos.isEmpty()){ + groupLore.add("\u00A77There is no excluded groups for this conflict."); + }else{ + groupLore.add("\u00A77List of excluded groups for this conflict:"); + Iterator groupIterator = grouos.iterator(); + + boolean greaterThanMax = grouos.size() > 5; + int maxindex = (greaterThanMax ? 4 : grouos.size()); + for (int i = 0; i < maxindex; i++) { + // format string like "- Melee Weapons" + String formattedName = CasedStringUtil.snakeToUpperSpacedCase(groupIterator.next().getName()); + groupLore.add("\u00A77- \u00A73" + formattedName); + + } + if(greaterThanMax){ + groupLore.add("\u00A77And "+(grouos.size()-4)+" more..."); } } // Configure enchant setting item - ItemStack enchantItem = enchantSettingItem.getItem(); + ItemStack enchantItem = this.enchantSettingItem.getItem(); ItemMeta enchantMeta = enchantItem.getItemMeta(); - enchantMeta.setDisplayName("\u00A7aSelect \u00A75Enchantments \u00A7aSettings"); + enchantMeta.setDisplayName("\u00A7aSelect included \u00A75Enchantments \u00A7aSettings"); enchantMeta.setLore(enchantLore); enchantItem.setItemMeta(enchantMeta); - enchantSettingItem.setItem(enchantItem); // Just in case + this.enchantSettingItem.setItem(enchantItem); // Just in case + + // Configure group setting item + ItemStack groupItem = this.groupSettingItem.getItem(); + ItemMeta groupMeta = groupItem.getItemMeta(); + + groupMeta.setDisplayName("\u00A7aSelect excluded \u00A73Groups \u00A7aSettings"); + groupMeta.setLore(groupLore); + + groupItem.setItemMeta(groupMeta); + + this.groupSettingItem.setItem(groupItem); // Just in case - //todo: groupSettingItem - - - pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(minBeforeActiveSettingFactory, Material.COMMAND_BLOCK)); + this.pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(this.minBeforeActiveSettingFactory, Material.COMMAND_BLOCK)); update(); } @@ -183,25 +235,29 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S for (HumanEntity viewer : getViewers()) { this.parent.show(viewer); } - this.canOpen = false; + this.shouldWorld = false; // Just in case something is extremely wrong GuiItem background = GuiGlobalItems.backgroundItem(); - pane.bindItem('E', background); - pane.bindItem('G', background); - pane.bindItem('M', background); - pane.bindItem('D', background); + this.pane.bindItem('E', background); + this.pane.bindItem('G', background); + this.pane.bindItem('M', background); + this.pane.bindItem('D', background); } @Override public void show(@NotNull HumanEntity humanEntity) { - if(this.canOpen){ + if(this.shouldWorld){ super.show(humanEntity); }else{ this.parent.show(humanEntity); } } + public GuiItem getParentItemForThisGui() { + return parentItemForThisGui; + } + // Select enchantment container methods @Override @@ -211,12 +267,40 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S @Override public boolean setSelectedEnchantments(Set enchantments) { - return false; + if(!this.shouldWorld) { + CustomAnvil.instance.getLogger().info("Trying to save "+enchantConflict.getName()+" enchants but sub config is destroyed"); + return false; + } + + // Set live configuration + this.enchantConflict.setEnchants(enchantments); + + // Save on file configuration + String[] enchantKeys = new String[enchantments.size()]; + int index = 0; + for (Enchantment enchantment : enchantments) { + enchantKeys[index++] = enchantment.getKey().getKey(); + } + ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName()+".enchantments", enchantKeys); + + try { + updateGuiValues(); + }catch (Exception e){ + CustomAnvil.instance.getLogger().log(Level.WARNING, "An error occurred while updating enchants for "+this.enchantConflict.getName(), e); + } + + + // 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 true; } @Override public Set illegalEnchantments() { - return new HashSet<>(); + return Collections.emptySet(); } // Select group container methods @@ -228,19 +312,39 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S @Override public boolean setSelectedGroups(Set groups) { + if(!this.shouldWorld) { + CustomAnvil.instance.getLogger().info("Trying to save "+enchantConflict.getName()+" groups but sub config is destroyed"); + return false; + } + // Set live configuration this.enchantConflict.getCantConflictGroup().setGroups(groups); // Save on file configuration + String[] groupsNames = new String[groups.size()]; + int index = 0; + for (AbstractMaterialGroup group : groups) { + groupsNames[index++] = group.getName(); + } + ConfigHolder.CONFLICT_HOLDER.getConfig().set(this.enchantConflict.getName()+".notAffectedGroups", groupsNames); + + try { + updateGuiValues(); + }catch (Exception e){ + CustomAnvil.instance.getLogger().log(Level.WARNING, "An error occurred while updating group for "+this.enchantConflict.getName(), e); + } // 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 false; + return true; } @Override public Set illegalGroups() { - return new HashSet<>(); + return Collections.emptySet(); } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index f88cdca..ab72195 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -13,9 +13,7 @@ class EnchantConflictGroup( fun addEnchantment(enchant: Enchantment){ enchantments.add(enchant) } - fun removeEnchantment(enchant: Enchantment){ - enchantments.remove(enchant) - } + fun allowed(enchants: Set, mat: Material) : Boolean{ if(enchantments.size < minBeforeBlock){ return true @@ -45,4 +43,9 @@ class EnchantConflictGroup( return enchantments } + fun setEnchants(enchants: Set) { + enchantments.clear() + enchantments.addAll(enchants) + } + } \ No newline at end of file