From b4cb6c2848d3373d72b6c21cb7b8116cd276e3af Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 20 Mar 2024 18:22:27 +0100 Subject: [PATCH 01/17] move openable groups and add interface for future gui. --- .../xyz/alexcrea/cuanvil/gui/MainConfigGui.java | 6 +++--- .../gui/config/SelectEnchantmentContainer.java | 16 ++++++++++++++++ .../cuanvil/gui/config/SelectGroupContainer.java | 15 +++++++++++++++ .../gui/config/SelectMaterialContainer.java | 16 ++++++++++++++++ .../config/{ => openable}/BasicConfigGui.java | 2 +- .../{ => openable}/EnchantCostConfigGui.java | 3 ++- .../{ => openable}/EnchantLimitConfigGui.java | 3 ++- .../alexcrea/cuanvil/command/ReloadExecutor.kt | 6 +++--- 8 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{ => openable}/BasicConfigGui.java (99%) rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{ => openable}/EnchantCostConfigGui.java (96%) rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{ => openable}/EnchantLimitConfigGui.java (93%) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java index 3e17b56..1b8cc54 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java @@ -8,9 +8,9 @@ import io.delilaheve.CustomAnvil; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui; -import xyz.alexcrea.cuanvil.gui.config.EnchantCostConfigGui; -import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui; +import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui; +import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui; +import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java new file mode 100644 index 0000000..9a468de --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java @@ -0,0 +1,16 @@ +package xyz.alexcrea.cuanvil.gui.config; + +import org.bukkit.enchantments.Enchantment; +import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; + +import java.util.List; +import java.util.Set; + +public interface SelectEnchantmentContainer { + + List getSelectedEnchantments(); + void setSelectedEnchantments(List enchantments); + + Set illegalEnchantments(); + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java new file mode 100644 index 0000000..7574f85 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java @@ -0,0 +1,15 @@ +package xyz.alexcrea.cuanvil.gui.config; + +import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; + +import java.util.List; +import java.util.Set; + +public interface SelectGroupContainer { + + List getSelectedGroups(); + void setSelectedGroups(List groups); + + Set illegalGroups(); + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java new file mode 100644 index 0000000..a510371 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java @@ -0,0 +1,16 @@ +package xyz.alexcrea.cuanvil.gui.config; + +import javafx.scene.paint.Material; +import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; + +import java.util.List; +import java.util.Set; + +public interface SelectMaterialContainer { + + List getSelectedMaterials(); + void setSelectedMaterials(List materials); + + Set illegalMaterials(); + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java similarity index 99% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java index 7b39d8f..3e86948 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/BasicConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui.config; +package xyz.alexcrea.cuanvil.gui.config.openable; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.pane.PatternPane; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java similarity index 96% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java index b5a2a50..7ecd771 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantCostConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui.config; +package xyz.alexcrea.cuanvil.gui.config.openable; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import org.bukkit.Material; @@ -8,6 +8,7 @@ import org.bukkit.inventory.meta.ItemMeta; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.enchant.EnchantmentProperties; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; +import xyz.alexcrea.cuanvil.gui.config.AbstractEnchantConfigGui; import xyz.alexcrea.cuanvil.gui.config.settings.EnchantCostSettingsGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.util.CasedStringUtil; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java similarity index 93% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java index fa63937..92b7833 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/EnchantLimitConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java @@ -1,9 +1,10 @@ -package xyz.alexcrea.cuanvil.gui.config; +package xyz.alexcrea.cuanvil.gui.config.openable; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import xyz.alexcrea.cuanvil.config.ConfigHolder; +import xyz.alexcrea.cuanvil.gui.config.AbstractEnchantConfigGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.util.CasedStringUtil; diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index fdcae7d..e8d201d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -5,9 +5,9 @@ import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import xyz.alexcrea.cuanvil.config.ConfigHolder -import xyz.alexcrea.cuanvil.gui.config.BasicConfigGui -import xyz.alexcrea.cuanvil.gui.config.EnchantCostConfigGui -import xyz.alexcrea.cuanvil.gui.config.EnchantLimitConfigGui +import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui +import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui +import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui import xyz.alexcrea.cuanvil.util.MetricsUtil class ReloadExecutor : CommandExecutor { From 41235d30245436751a6349a14ebacd555d21d7b3 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Wed, 20 Mar 2024 22:32:44 +0100 Subject: [PATCH 02/17] Add group select gui and edit group logic to be able to handle group set. --- .../config/SelectEnchantmentContainer.java | 3 +- .../gui/config/SelectGroupContainer.java | 5 +- .../gui/config/SelectMaterialContainer.java | 13 +- .../settings/GroupSelectSettingGui.java | 140 ++++++++++++++++++ .../cuanvil/group/AbstractMaterialGroup.kt | 17 ++- .../alexcrea/cuanvil/group/ExcludeGroup.kt | 27 +++- .../alexcrea/cuanvil/group/IncludeGroup.kt | 28 +++- .../cuanvil/group/ItemGroupManager.kt | 2 +- 8 files changed, 216 insertions(+), 19 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java index 9a468de..668aace 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java @@ -9,7 +9,8 @@ import java.util.Set; public interface SelectEnchantmentContainer { List getSelectedEnchantments(); - void setSelectedEnchantments(List enchantments); + + boolean setSelectedEnchantments(List enchantments); Set illegalEnchantments(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java index 7574f85..33d76f9 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java @@ -7,8 +7,9 @@ import java.util.Set; public interface SelectGroupContainer { - List getSelectedGroups(); - void setSelectedGroups(List groups); + Set getSelectedGroups(); + + boolean setSelectedGroups(Set groups); Set illegalGroups(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java index a510371..bd78db4 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java @@ -1,16 +1,15 @@ package xyz.alexcrea.cuanvil.gui.config; -import javafx.scene.paint.Material; -import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; +import org.bukkit.Material; -import java.util.List; -import java.util.Set; +import java.util.EnumSet; public interface SelectMaterialContainer { - List getSelectedMaterials(); - void setSelectedMaterials(List materials); + EnumSet getSelectedMaterials(); - Set illegalMaterials(); + boolean setSelectedMaterials(EnumSet materials); + + EnumSet illegalMaterials(); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java new file mode 100644 index 0000000..5ed1a6d --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java @@ -0,0 +1,140 @@ +package xyz.alexcrea.cuanvil.gui.config.settings; + +import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.pane.Orientable; +import com.github.stefvanschie.inventoryframework.pane.OutlinePane; +import com.github.stefvanschie.inventoryframework.pane.Pane; +import com.github.stefvanschie.inventoryframework.pane.util.Pattern; +import io.delilaheve.CustomAnvil; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +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.config.ConfigHolder; +import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; +import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; +import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; +import xyz.alexcrea.cuanvil.util.CasedStringUtil; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Consumer; + +public class GroupSelectSettingGui extends AbstractSettingGui{ + + SelectGroupContainer groupContainer; + int page; + + HashSet selectedGroups; + + public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) { + super(6, title, parent); + this.groupContainer = groupContainer; + //Not used but planned + this.page = page; + + this.selectedGroups = new HashSet<>(groupContainer.getSelectedGroups()); + + initGroups(); + } + + @Override + protected Pattern getGuiPattern() { + return new Pattern( + "000000000", + "000000000", + "000000000", + "000000000", + "000000000", + "B1111111S" + ); + } + + protected void initGroups(){ + // Add enchantment gui item + OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5); + filledEnchant.setPriority(Pane.Priority.HIGH); + filledEnchant.align(OutlinePane.Alignment.BEGIN); + filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); + + Set illegalGroup = this.groupContainer.illegalGroups(); + ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().forEach((name, group)->{ + if(illegalGroup.contains(group)) { + return; + } + filledEnchant.addItem(getGuiItemFromGroup(group)); + + }); + addPane(filledEnchant); + + } + + private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){ + boolean isIn = this.selectedGroups.contains(group); + + Material usedMaterial = Material.PAPER; + ItemStack item = new ItemStack(usedMaterial); + + setGroupItemMeta(item, group.getName(), isIn); + + GuiItem guiItem = new GuiItem(item, CustomAnvil.instance); + guiItem.setAction(getGrpupItemConsumer(group, guiItem)); + return guiItem; + } + + private static final List TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected"); + private static final List FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected"); + + public void setGroupItemMeta(ItemStack item, String name, boolean isIn){ + ItemMeta meta = item.getItemMeta(); + + meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name)); + if(isIn){ + meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); + meta.setLore(TRUE_LORE); + }else{ + meta.removeEnchant(Enchantment.DAMAGE_UNDEAD); + meta.setLore(FALSE_LORE); + } + meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS); + + item.setItemMeta(meta); + } + + private Consumer getGrpupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){ + return event -> { + event.setCancelled(true); + + boolean isIn = this.selectedGroups.contains(group); + if(isIn){ + this.selectedGroups.remove(group); + }else{ + this.selectedGroups.add(group); + } + + ItemStack item = guiItem.getItem(); + setGroupItemMeta(item, group.getName(), !isIn); + guiItem.setItem(item);// Just in case + + update(); + }; + } + + @Override + public boolean onSave() { + return this.groupContainer.setSelectedGroups(this.selectedGroups); + } + + @Override + public boolean hadChange() { + Set baseGroup = this.groupContainer.getSelectedGroups(); + return baseGroup.size() != this.selectedGroups.size() || + !baseGroup.containsAll(this.selectedGroups); + } + +} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index ffcaab4..2030741 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -5,6 +5,7 @@ import java.util.* abstract class AbstractMaterialGroup(private val name: String) { protected val includedMaterial by lazy {createDefaultSet()} + protected var groupChangeNotified = false /** * Get the group default set @@ -33,6 +34,11 @@ abstract class AbstractMaterialGroup(private val name: String) { */ abstract fun addToPolicy(other : AbstractMaterialGroup) + /** + * Get the group as a set + */ + abstract fun getMaterials(): MutableSet + /** * Get the group name in case something is wrong */ @@ -41,10 +47,13 @@ abstract class AbstractMaterialGroup(private val name: String) { } /** - * Get the group as a set + * Update the contained groups of this group */ - fun getSet(): Set { - return includedMaterial - } + abstract fun setGroups(groups: MutableSet) + + /** + * Get the contained group of this material group + */ + abstract fun getGroups(): MutableSet } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt index d8ee38b..24eec47 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt @@ -8,7 +8,8 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { return EnumSet.allOf(Material::class.java) } - private val includedGroup = HashSet() + private var includedGroup: MutableSet = HashSet() + private val groupItems: MutableSet by lazy {createDefaultSet()} override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { @@ -21,11 +22,33 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { override fun addToPolicy(mat: Material) { includedMaterial.remove(mat) + groupItems.remove(mat) } override fun addToPolicy(other: AbstractMaterialGroup) { includedGroup.add(other) - includedMaterial.removeAll(other.getSet()) + groupItems.removeAll(other.getMaterials()); + } + + override fun setGroups(groups: MutableSet) { + groupItems.clear() + groupItems.addAll(includedMaterial) + + includedGroup.clear(); + groups.forEach { group -> + if(!group.isReferencing(this)) { + includedGroup.add(group); + groupItems.removeAll(group.getMaterials()) + } + } + } + + override fun getGroups(): MutableSet { + return includedGroup; + } + + override fun getMaterials(): MutableSet { + return groupItems; } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt index bd962c2..20e9ef3 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt @@ -8,7 +8,8 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { return EnumSet.noneOf(Material::class.java) } - private val includedGroup = HashSet() + private var includedGroup: MutableSet = HashSet() + private val groupItems: MutableSet by lazy {createDefaultSet()} override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { @@ -21,11 +22,34 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { override fun addToPolicy(mat: Material) { includedMaterial.add(mat) + groupItems.add(mat) } override fun addToPolicy(other: AbstractMaterialGroup) { includedGroup.add(other) - includedMaterial.addAll(other.getSet()) + groupItems.addAll(other.getMaterials()); } + override fun setGroups(groups: MutableSet) { + groupItems.clear(); + groupItems.addAll(includedMaterial) + + includedGroup.clear(); + groups.forEach { group -> + if(!group.isReferencing(this)){ + includedGroup.add(group); + groupItems.addAll(group.getMaterials()) + } + } + } + + override fun getGroups(): MutableSet { + return includedGroup + } + + override fun getMaterials(): MutableSet { + return groupItems + } + + } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt index a7c927a..5d57dfa 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt @@ -18,7 +18,7 @@ class ItemGroupManager { private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH") } - private lateinit var groupMap : HashMap + lateinit var groupMap : HashMap // Read and create material groups fun prepareGroups(config: ConfigurationSection){ From e99fd4d640f64b497aacd31a3b90f5e47dffce1f Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 21 Mar 2024 01:42:48 +0100 Subject: [PATCH 03/17] Add display item for group and order them in create order --- .../settings/GroupSelectSettingGui.java | 21 ++++++---- .../cuanvil/group/AbstractMaterialGroup.kt | 41 ++++++++++++++++--- .../alexcrea/cuanvil/group/ExcludeGroup.kt | 14 +++---- .../alexcrea/cuanvil/group/IncludeGroup.kt | 23 ++++++++--- .../cuanvil/group/ItemGroupManager.kt | 5 ++- 5 files changed, 76 insertions(+), 28 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java index 5ed1a6d..13c183b 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java @@ -19,10 +19,7 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; import xyz.alexcrea.cuanvil.util.CasedStringUtil; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.function.Consumer; public class GroupSelectSettingGui extends AbstractSettingGui{ @@ -30,7 +27,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ SelectGroupContainer groupContainer; int page; - HashSet selectedGroups; + Set selectedGroups; public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) { super(6, title, parent); @@ -63,13 +60,13 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); Set illegalGroup = this.groupContainer.illegalGroups(); - ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().forEach((name, group)->{ + for (AbstractMaterialGroup group : ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().values()) { if(illegalGroup.contains(group)) { return; } filledEnchant.addItem(getGuiItemFromGroup(group)); + } - }); addPane(filledEnchant); } @@ -77,7 +74,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){ boolean isIn = this.selectedGroups.contains(group); - Material usedMaterial = Material.PAPER; + Material usedMaterial = group.getRepresentativeMaterial(); ItemStack item = new ItemStack(usedMaterial); setGroupItemMeta(item, group.getName(), isIn); @@ -93,6 +90,14 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ public void setGroupItemMeta(ItemStack item, String name, boolean isIn){ ItemMeta meta = item.getItemMeta(); + if(meta == null){ + CustomAnvil.instance.getLogger().warning("Could not create item for group: "+name+":\n" + + "Item do not gave item meta: "+item+". Using placeholder instead"); + item.setType(Material.PAPER); + meta = item.getItemMeta(); + assert meta != null; + } + meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name)); if(isIn){ meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index 2030741..233d990 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -5,7 +5,6 @@ import java.util.* abstract class AbstractMaterialGroup(private val name: String) { protected val includedMaterial by lazy {createDefaultSet()} - protected var groupChangeNotified = false /** * Get the group default set @@ -15,7 +14,7 @@ abstract class AbstractMaterialGroup(private val name: String) { /** * Get if a material is allowed following the group policy */ - fun contain(mat : Material): Boolean { + open fun contain(mat : Material): Boolean { return mat in includedMaterial } @@ -35,14 +34,28 @@ abstract class AbstractMaterialGroup(private val name: String) { abstract fun addToPolicy(other : AbstractMaterialGroup) /** - * Get the group as a set + * Get the group contained material as a set */ - abstract fun getMaterials(): MutableSet + abstract fun getMaterials(): EnumSet + + /** + * Get the group non-inherited material as a set + */ + open fun getNonGroupInheritedMaterials(): EnumSet { + return includedMaterial + } + /** + * Get the group non-inherited material as a set + */ + open fun setNonGroupInheritedMaterials(materials: EnumSet) { + this.includedMaterial.clear() + this.includedMaterial.addAll(materials) + } /** * Get the group name in case something is wrong */ - fun getName(): String { + open fun getName(): String { return name } @@ -56,4 +69,22 @@ abstract class AbstractMaterialGroup(private val name: String) { */ abstract fun getGroups(): MutableSet + open fun getRepresentativeMaterial() : Material { + // Test inner material + val matIterator = includedMaterial.iterator() + while(matIterator.hasNext()){ + val material = matIterator.next(); + if(material.isAir) continue + return material; + } + // Test included group representative material + val groupIterator = getGroups().iterator() + while (groupIterator.hasNext()){ + val groupMat = groupIterator.next().getRepresentativeMaterial() + if(groupMat.isAir) continue + return groupMat; + } + return Material.PAPER; + } + } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt index 24eec47..82a691b 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt @@ -9,7 +9,7 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { } private var includedGroup: MutableSet = HashSet() - private val groupItems: MutableSet by lazy {createDefaultSet()} + private val groupItems by lazy {createDefaultSet()} override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { @@ -27,28 +27,28 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { override fun addToPolicy(other: AbstractMaterialGroup) { includedGroup.add(other) - groupItems.removeAll(other.getMaterials()); + groupItems.removeAll(other.getMaterials()) } override fun setGroups(groups: MutableSet) { groupItems.clear() groupItems.addAll(includedMaterial) - includedGroup.clear(); + includedGroup.clear() groups.forEach { group -> if(!group.isReferencing(this)) { - includedGroup.add(group); + includedGroup.add(group) groupItems.removeAll(group.getMaterials()) } } } override fun getGroups(): MutableSet { - return includedGroup; + return includedGroup } - override fun getMaterials(): MutableSet { - return groupItems; + override fun getMaterials(): EnumSet { + return groupItems } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt index 20e9ef3..05a85c2 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt @@ -9,7 +9,7 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { } private var includedGroup: MutableSet = HashSet() - private val groupItems: MutableSet by lazy {createDefaultSet()} + private val groupItems by lazy {createDefaultSet()} override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { @@ -27,27 +27,38 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { override fun addToPolicy(other: AbstractMaterialGroup) { includedGroup.add(other) - groupItems.addAll(other.getMaterials()); + groupItems.addAll(other.getMaterials()) } override fun setGroups(groups: MutableSet) { - groupItems.clear(); + groupItems.clear() groupItems.addAll(includedMaterial) - includedGroup.clear(); + includedGroup.clear() groups.forEach { group -> if(!group.isReferencing(this)){ - includedGroup.add(group); + includedGroup.add(group) groupItems.addAll(group.getMaterials()) } } } + override fun setNonGroupInheritedMaterials(materials: EnumSet) { + super.setNonGroupInheritedMaterials(materials) + // Update group items + groupItems.clear() + groupItems.addAll(includedMaterial) + + includedGroup.forEach { group -> + groupItems.addAll(group.getMaterials()) + } + } + override fun getGroups(): MutableSet { return includedGroup } - override fun getMaterials(): MutableSet { + override fun getMaterials(): EnumSet { return groupItems } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt index 5d57dfa..6403e73 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt @@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil import org.bukkit.Material import org.bukkit.configuration.ConfigurationSection import java.util.* +import kotlin.collections.LinkedHashMap class ItemGroupManager { @@ -18,11 +19,11 @@ class ItemGroupManager { private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH") } - lateinit var groupMap : HashMap + lateinit var groupMap : LinkedHashMap // Read and create material groups fun prepareGroups(config: ConfigurationSection){ - groupMap = HashMap() + groupMap = LinkedHashMap() val keys = config.getKeys(false) for (key in keys) { From 7129aed585c27516fcd3e03a5334325b58b35a8a Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 21 Mar 2024 02:22:07 +0100 Subject: [PATCH 04/17] Add select enchant setting gui & fix a typo. --- .../gui/config/AbstractEnchantConfigGui.java | 11 +- .../config/SelectEnchantmentContainer.java | 4 +- .../settings/EnchantSelectSettingGui.java | 157 ++++++++++++++++++ .../settings/GroupSelectSettingGui.java | 4 +- 4 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index ae04fb2..4998f8e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -26,6 +26,12 @@ import java.util.List; */ public abstract class AbstractEnchantConfigGui extends ValueUpdatableGui { + public static final List SORTED_ENCHANTMENT_LIST; + static { + SORTED_ENCHANTMENT_LIST = Arrays.asList(Enchantment.values()); + SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey())); + } + private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE; private final Gui backGui; @@ -90,10 +96,7 @@ public abstract class AbstractEnchantConfigGui(); - List enchantments = Arrays.asList(Enchantment.values()); - enchantments.sort(Comparator.comparing(ench -> ench.getKey().getKey())); - - for (Enchantment enchant : enchantments) { + for (Enchantment enchant : SORTED_ENCHANTMENT_LIST) { T factory = getFactoryFromEnchant(enchant); bookItemFactoryList.add(factory); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java index 668aace..dcbbb47 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java @@ -8,9 +8,9 @@ import java.util.Set; public interface SelectEnchantmentContainer { - List getSelectedEnchantments(); + Set getSelectedEnchantments(); - boolean setSelectedEnchantments(List enchantments); + boolean setSelectedEnchantments(Set enchantments); Set illegalEnchantments(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java new file mode 100644 index 0000000..55a129d --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java @@ -0,0 +1,157 @@ +package xyz.alexcrea.cuanvil.gui.config.settings; + +import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.pane.Orientable; +import com.github.stefvanschie.inventoryframework.pane.OutlinePane; +import com.github.stefvanschie.inventoryframework.pane.Pane; +import com.github.stefvanschie.inventoryframework.pane.util.Pattern; +import io.delilaheve.CustomAnvil; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +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.group.AbstractMaterialGroup; +import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; +import xyz.alexcrea.cuanvil.gui.config.AbstractEnchantConfigGui; +import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer; +import xyz.alexcrea.cuanvil.util.CasedStringUtil; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Consumer; + +public class EnchantSelectSettingGui extends AbstractSettingGui{ + + SelectEnchantmentContainer enchantContainer; + int page; + + Set selectedEnchant; + + public EnchantSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectEnchantmentContainer enchantContainer, int page) { + super(6, title, parent); + this.enchantContainer = enchantContainer; + // Not used and not planned rn + this.page = page; + + this.selectedEnchant = new HashSet<>(enchantContainer.getSelectedEnchantments()); + + initGroups(); + } + + @Override + protected Pattern getGuiPattern() { + return new Pattern( + "000000000", + "000000000", + "000000000", + "000000000", + "000000000", + "B1111111S" + ); + } + + protected void initGroups(){ + // Add enchantment gui item + OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5); + filledEnchant.setPriority(Pane.Priority.HIGH); + filledEnchant.align(OutlinePane.Alignment.BEGIN); + filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); + + Set illegalEnchant = this.enchantContainer.illegalEnchantments(); + for (Enchantment enchant : AbstractEnchantConfigGui.SORTED_ENCHANTMENT_LIST) { + if(illegalEnchant.contains(enchant)) { + return; + } + filledEnchant.addItem(getGuiItemFromEnchant(enchant)); + } + + addPane(filledEnchant); + + } + + private GuiItem getGuiItemFromEnchant(Enchantment enchantment){ + boolean isIn = this.selectedEnchant.contains(enchantment); + + Material usedMaterial; + if(isIn){ + usedMaterial = Material.ENCHANTED_BOOK; + }else{ + usedMaterial = Material.BOOK; + } + ItemStack item = new ItemStack(usedMaterial); + + setEnchantItemMeta(item, enchantment.getKey().getKey(), isIn); + + GuiItem guiItem = new GuiItem(item, CustomAnvil.instance); + guiItem.setAction(getEnchantItemConsumer(enchantment, guiItem)); + return guiItem; + } + + + private static final List TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected"); + private static final List FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected"); + + public void setEnchantItemMeta(ItemStack item, String name, boolean isIn){ + ItemMeta meta = item.getItemMeta(); + + if(meta == null){ + CustomAnvil.instance.getLogger().warning("Could not create item for enchantment: "+name+":\n" + + "Item do not gave item meta: "+item+". Using placeholder instead"); + item.setType(Material.PAPER); + meta = item.getItemMeta(); + assert meta != null; + } + + meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name)); + if(isIn){ + meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); + meta.setLore(TRUE_LORE); + }else{ + meta.removeEnchant(Enchantment.DAMAGE_UNDEAD); + meta.setLore(FALSE_LORE); + } + meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS); + + item.setItemMeta(meta); + } + + private Consumer getEnchantItemConsumer(Enchantment enchant, GuiItem guiItem){ + return event -> { + event.setCancelled(true); + + ItemStack item = guiItem.getItem(); + + boolean isIn = this.selectedEnchant.contains(enchant); + if(isIn){ + this.selectedEnchant.remove(enchant); + item.setType(Material.BOOK); + }else{ + this.selectedEnchant.add(enchant); + item.setType(Material.ENCHANTED_BOOK); + } + + setEnchantItemMeta(item, enchant.getKey().getKey(), !isIn); + guiItem.setItem(item);// Just in case + + update(); + }; + } + + @Override + public boolean onSave() { + return this.enchantContainer.setSelectedEnchantments(this.selectedEnchant); + } + + @Override + public boolean hadChange() { + Set baseGroup = this.enchantContainer.getSelectedEnchantments(); + return baseGroup.size() != this.selectedEnchant.size() || + !baseGroup.containsAll(this.selectedEnchant); + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java index 13c183b..6a2c7be 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java @@ -80,7 +80,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ setGroupItemMeta(item, group.getName(), isIn); GuiItem guiItem = new GuiItem(item, CustomAnvil.instance); - guiItem.setAction(getGrpupItemConsumer(group, guiItem)); + guiItem.setAction(getGroupItemConsumer(group, guiItem)); return guiItem; } @@ -111,7 +111,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ item.setItemMeta(meta); } - private Consumer getGrpupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){ + private Consumer getGroupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){ return event -> { event.setCancelled(true); From ce96fefc79162908c9797f752e825f16897e7c99 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 21 Mar 2024 02:48:28 +0100 Subject: [PATCH 05/17] Add secondary background item & put some constant on a util class. --- .../gui/config/AbstractEnchantConfigGui.java | 13 +++------ .../config/settings/AbstractSettingGui.java | 4 --- .../gui/config/settings/BoolSettingsGui.java | 5 ++-- .../settings/EnchantCostSettingsGui.java | 5 ++-- .../settings/EnchantSelectSettingGui.java | 7 ++++- .../settings/GroupSelectSettingGui.java | 4 +++ .../gui/config/settings/IntSettingsGui.java | 5 ++-- .../cuanvil/gui/util/GuiSharedConstant.java | 27 +++++++++++++++++++ 8 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index 4998f8e..4398af5 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -14,6 +14,7 @@ import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import java.util.ArrayList; import java.util.Arrays; @@ -26,14 +27,6 @@ import java.util.List; */ public abstract class AbstractEnchantConfigGui extends ValueUpdatableGui { - public static final List SORTED_ENCHANTMENT_LIST; - static { - SORTED_ENCHANTMENT_LIST = Arrays.asList(Enchantment.values()); - SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey())); - } - - private final static Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE; - private final Gui backGui; /** @@ -76,7 +69,7 @@ public abstract class AbstractEnchantConfigGui(); - for (Enchantment enchant : SORTED_ENCHANTMENT_LIST) { + for (Enchantment enchant : GuiSharedConstant.SORTED_ENCHANTMENT_LIST) { T factory = getFactoryFromEnchant(enchant); bookItemFactoryList.add(factory); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java index 7639746..65fa446 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java @@ -20,10 +20,6 @@ import java.util.List; */ public abstract class AbstractSettingGui extends ChestGui { - // Temporary values, until I get something better. - public static final boolean TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE = true; - public static final boolean TEMPORARY_DO_BACKUP_EVERY_SAVE = true; - protected final static List CLICK_LORE = Collections.singletonList("\u00A77Click Here to change the value"); private PatternPane pane; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java index d3c56be..ac074c7 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java @@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.util.MetricsUtil; import java.util.Collections; @@ -125,8 +126,8 @@ public class BoolSettingsGui extends AbstractSettingGui{ holder.config.getConfig().set(holder.configPath, now); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); - if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ - return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE); + if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + return holder.config.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java index 200323d..10a3bed 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java @@ -14,6 +14,7 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; 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.MetricsUtil; import java.util.Arrays; @@ -207,8 +208,8 @@ public class EnchantCostSettingsGui extends IntSettingsGui { holder.config.getConfig().set(holder.configPath+BOOK_PATH, nowBook); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); - if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ - return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE); + if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + return holder.config.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java index 55a129d..e749c3f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java @@ -17,6 +17,8 @@ import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.AbstractEnchantConfigGui; import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.util.CasedStringUtil; import java.util.Collections; @@ -40,6 +42,9 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ this.selectedEnchant = new HashSet<>(enchantContainer.getSelectedEnchantments()); + // Add secondary background item + this.getPane().bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + initGroups(); } @@ -63,7 +68,7 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); Set illegalEnchant = this.enchantContainer.illegalEnchantments(); - for (Enchantment enchant : AbstractEnchantConfigGui.SORTED_ENCHANTMENT_LIST) { + for (Enchantment enchant : GuiSharedConstant.SORTED_ENCHANTMENT_LIST) { if(illegalEnchant.contains(enchant)) { return; } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java index 6a2c7be..b05a1f4 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java @@ -17,6 +17,7 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.util.CasedStringUtil; import java.util.*; @@ -37,6 +38,9 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ this.selectedGroups = new HashSet<>(groupContainer.getSelectedGroups()); + // Add secondary background item + this.getPane().bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + initGroups(); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java index f8ef20a..7f6d32a 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java @@ -13,6 +13,7 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; 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.MetricsUtil; import java.util.Collections; @@ -247,8 +248,8 @@ public class IntSettingsGui extends AbstractSettingGui{ holder.config.getConfig().set(holder.configPath, now); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); - if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ - return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE); + if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + return holder.config.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java new file mode 100644 index 0000000..532b02d --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -0,0 +1,27 @@ +package xyz.alexcrea.cuanvil.gui.util; + +import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class GuiSharedConstant { + + public static final List SORTED_ENCHANTMENT_LIST; + static { + SORTED_ENCHANTMENT_LIST = Arrays.asList(Enchantment.values()); + SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey())); + } + + public static final Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE; + public static final GuiItem SECONDARY_BACKGROUND_ITEM = GuiGlobalItems.backgroundItem(GuiSharedConstant.SECONDARY_BACKGROUND_MATERIAL); + + + // Temporary values, until I get something better. + public static final boolean TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE = true; + public static final boolean TEMPORARY_DO_BACKUP_EVERY_SAVE = true; +} From dc35d590771e1f83c27973b8a9c77e2a7fafe153 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 21 Mar 2024 19:14:09 +0100 Subject: [PATCH 06/17] Add confirm action gui. --- .../gui/config/AbstractEnchantConfigGui.java | 3 - .../cuanvil/gui/config/ConfirmActionGui.java | 92 +++++++++++++++++++ .../config/SelectEnchantmentContainer.java | 2 - .../gui/config/SelectGroupContainer.java | 1 - .../settings/EnchantSelectSettingGui.java | 3 - .../cuanvil/gui/util/GuiSharedConstant.java | 1 - .../cuanvil/group/AbstractMaterialGroup.kt | 8 +- 7 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index 4398af5..4094244 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -8,7 +8,6 @@ import com.github.stefvanschie.inventoryframework.pane.Pane; import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import io.delilaheve.CustomAnvil; -import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; @@ -17,8 +16,6 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; import java.util.List; /** diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java new file mode 100644 index 0000000..8a85cd2 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java @@ -0,0 +1,92 @@ +package xyz.alexcrea.cuanvil.gui.config; + +import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder; +import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; +import com.github.stefvanschie.inventoryframework.pane.PatternPane; +import com.github.stefvanschie.inventoryframework.pane.util.Pattern; +import io.delilaheve.CustomAnvil; +import org.bukkit.Material; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; + +import java.util.Arrays; +import java.util.Collections; +import java.util.function.Supplier; +import java.util.logging.Level; + +public class ConfirmActionGui extends ChestGui { + + private static final ItemStack CANCEL_ITEM; + private static final ItemStack CONFIRM_ITEM; + static { + CANCEL_ITEM = new ItemStack(Material.RED_TERRACOTTA); + ItemMeta meta = CANCEL_ITEM.getItemMeta(); + meta.setDisplayName("\u00A7cCancel"); + meta.setLore(Collections.singletonList("\u00A77Cancel current action and return to previous menu.")); + CANCEL_ITEM.setItemMeta(meta); + + CONFIRM_ITEM = new ItemStack(Material.GREEN_TERRACOTTA); + meta = CONFIRM_ITEM.getItemMeta(); + meta.setDisplayName("\u00A7aConfirm"); + meta.setLore(Arrays.asList("\u00A77Confirm current action.", + "\u00A74Cation: This action can't be canceled.")); + CONFIRM_ITEM.setItemMeta(meta); + } + + public ConfirmActionGui(@NotNull TextHolder title, String actionDescription, Gui backOnCancel, Gui backOnConfirm, Supplier onConfirm) { + super(3, title, CustomAnvil.instance); + + Pattern pattern = new Pattern( + "000000000", + "00B0I0S00", + "000000000" + ); + PatternPane pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern); + addPane(pane); + + pane.bindItem('0', GuiGlobalItems.backgroundItem()); + + pane.bindItem('B', new GuiItem(CANCEL_ITEM, GuiGlobalActions.openGuiAction(backOnCancel), CustomAnvil.instance)); + pane.bindItem('S', new GuiItem(CONFIRM_ITEM, event -> { + event.setCancelled(true); + HumanEntity player = event.getWhoClicked(); + + if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + player.closeInventory(); + player.sendMessage(GuiGlobalActions.NO_EDIT_PERM); + return; + } + + boolean success; + try{ + success = onConfirm.get(); + }catch (Exception e){ + CustomAnvil.instance.getLogger().log(Level.WARNING, "Could not process confirmation supplier.", e); + success = false; + } + + if(!success){ + event.getWhoClicked().sendMessage("\u00A7cAction could not be completed. "); + } + backOnConfirm.show(player); + + }, CustomAnvil.instance)); + + ItemStack infoItem = new ItemStack(Material.PAPER); + ItemMeta infoMeta = infoItem.getItemMeta(); + + infoMeta.setDisplayName("\u00A7eAre you sure ?"); + infoMeta.setLore(Arrays.asList(actionDescription.split("\n"))); + + infoItem.setItemMeta(infoMeta); + + pane.bindItem('I', new GuiItem(infoItem, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java index dcbbb47..ee6ce77 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java @@ -1,9 +1,7 @@ package xyz.alexcrea.cuanvil.gui.config; import org.bukkit.enchantments.Enchantment; -import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; -import java.util.List; import java.util.Set; public interface SelectEnchantmentContainer { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java index 33d76f9..a8b2dba 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java @@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.gui.config; import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; -import java.util.List; import java.util.Set; public interface SelectGroupContainer { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java index e749c3f..3cfa8c2 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java @@ -13,11 +13,8 @@ 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.group.AbstractMaterialGroup; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; -import xyz.alexcrea.cuanvil.gui.config.AbstractEnchantConfigGui; import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer; -import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.util.CasedStringUtil; 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 532b02d..94b5da1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -3,7 +3,6 @@ package xyz.alexcrea.cuanvil.gui.util; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; import java.util.Arrays; import java.util.Comparator; diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index 233d990..f7a62b7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -73,18 +73,18 @@ abstract class AbstractMaterialGroup(private val name: String) { // Test inner material val matIterator = includedMaterial.iterator() while(matIterator.hasNext()){ - val material = matIterator.next(); + val material = matIterator.next() if(material.isAir) continue - return material; + return material } // Test included group representative material val groupIterator = getGroups().iterator() while (groupIterator.hasNext()){ val groupMat = groupIterator.next().getRepresentativeMaterial() if(groupMat.isAir) continue - return groupMat; + return groupMat } - return Material.PAPER; + return Material.PAPER } } \ No newline at end of file From 814a8a2c891f3548791d2e1c9088826e39217bb9 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Fri, 22 Mar 2024 00:07:56 +0100 Subject: [PATCH 07/17] Start to create Enchantment sub setting gui & enchantment conflict gui --- .../alexcrea/cuanvil/gui/MainConfigGui.java | 14 +- .../gui/config/AbstractEnchantConfigGui.java | 37 +---- .../cuanvil/gui/config/ConfirmActionGui.java | 9 +- .../gui/config/openable/BasicConfigGui.java | 3 +- .../config/openable/EnchantConflictGui.java | 75 ++++++++++ .../config/openable/EnchantCostConfigGui.java | 4 +- .../gui/config/settings/BoolSettingsGui.java | 2 +- .../settings/EnchantSelectSettingGui.java | 10 +- .../settings/GroupSelectSettingGui.java | 10 +- .../EnchantConflictSubSettingGui.java | 133 ++++++++++++++++++ .../cuanvil/gui/util/GuiSharedConstant.java | 26 ++++ .../cuanvil/group/EnchantConflictGroup.kt | 12 +- .../cuanvil/group/EnchantConflictManager.kt | 27 ++-- 13 files changed, 297 insertions(+), 65 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/subsetting/EnchantConflictSubSettingGui.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java index 1b8cc54..c1ee55e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java @@ -9,6 +9,7 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui; +import xyz.alexcrea.cuanvil.gui.config.openable.EnchantConflictGui; import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui; import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; @@ -72,17 +73,26 @@ public class MainConfigGui extends ChestGui { GuiItem enchantCostItem = GuiGlobalItems.goToGuiItem(enchantCostItemstack, EnchantCostConfigGui.INSTANCE); pane.bindItem('3', enchantCostItem); + // Enchantment Conflicts + ItemStack EnchantConflictItemstack = new ItemStack(Material.EXPERIENCE_BOTTLE); + ItemMeta enchantConflictMeta = EnchantConflictItemstack.getItemMeta(); + + enchantConflictMeta.setDisplayName("\u00A7aEnchantment Conflict"); + enchantConflictMeta.setLore(Collections.singletonList("\u00A77Click here to open enchantment conflict menu")); + EnchantConflictItemstack.setItemMeta(enchantConflictMeta); + + GuiItem enchantConflictItem = GuiGlobalItems.goToGuiItem(EnchantConflictItemstack, EnchantConflictGui.INSTANCE); + pane.bindItem('4', enchantConflictItem); + // WIP configuration items ItemStack wipItemstack = new ItemStack(Material.BARRIER); ItemMeta wipMeta = wipItemstack.getItemMeta(); wipMeta.setDisplayName("\u00A7cWIP"); wipItemstack.setItemMeta(wipMeta); - GuiItem wip4 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); GuiItem wip5 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); GuiItem wip6 = new GuiItem(wipItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance); - pane.bindItem('4', wip4); pane.bindItem('5', wip5); pane.bindItem('6', wip6); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index 4094244..1a1c176 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -4,15 +4,11 @@ import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import com.github.stefvanschie.inventoryframework.pane.Orientable; import com.github.stefvanschie.inventoryframework.pane.OutlinePane; -import com.github.stefvanschie.inventoryframework.pane.Pane; -import com.github.stefvanschie.inventoryframework.pane.PatternPane; -import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import io.delilaheve.CustomAnvil; import org.bukkit.enchantments.Enchantment; import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; -import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import java.util.ArrayList; @@ -24,49 +20,22 @@ import java.util.List; */ public abstract class AbstractEnchantConfigGui extends ValueUpdatableGui { - private final Gui backGui; - - /** - * Constructor for a gui displaying available enchantment to edit a enchantment setting. - * @param title Title of the gui. - * @param backGui Gui to go back on click on the "back" button. - */ - protected AbstractEnchantConfigGui(String title, Gui backGui){ - super(6, title, CustomAnvil.instance); - this.backGui = backGui; - } /** * Constructor for a gui displaying available enchantment to edit a enchantment setting. * @param title Title of the gui. */ protected AbstractEnchantConfigGui(String title){ - this(title, MainConfigGui.INSTANCE); + super(6, title, CustomAnvil.instance); } - PatternPane backgroundItems; - OutlinePane filledEnchant; + private OutlinePane filledEnchant; - // Why is called like it is rn /** * Initialise value updatable gui pattern */ protected void init(){ // Back item panel - Pattern pattern = new Pattern( - "000000000", - "000000000", - "000000000", - "000000000", - "000000000", - "B11111111" - ); - this.backgroundItems = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern); - addPane(this.backgroundItems); - - GuiGlobalItems.addBackItem(this.backgroundItems, this.backGui); - - GuiGlobalItems.addBackgroundItem(this.backgroundItems); - this.backgroundItems.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + addPane(GuiSharedConstant.BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE); // enchant item panel this.filledEnchant = new OutlinePane(0, 0, 9, 5); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java index 8a85cd2..7ae180b 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java @@ -1,6 +1,5 @@ package xyz.alexcrea.cuanvil.gui.config; -import com.github.stefvanschie.inventoryframework.adventuresupport.TextHolder; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; @@ -14,6 +13,7 @@ import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import java.util.Arrays; import java.util.Collections; @@ -39,13 +39,14 @@ public class ConfirmActionGui extends ChestGui { CONFIRM_ITEM.setItemMeta(meta); } - public ConfirmActionGui(@NotNull TextHolder title, String actionDescription, Gui backOnCancel, Gui backOnConfirm, Supplier onConfirm) { + public ConfirmActionGui(@NotNull String title, String actionDescription, + Gui backOnCancel, Gui backOnConfirm, Supplier onConfirm) { super(3, title, CustomAnvil.instance); Pattern pattern = new Pattern( - "000000000", + GuiSharedConstant.EMPTY_GUI_FULL_LINE, "00B0I0S00", - "000000000" + GuiSharedConstant.EMPTY_GUI_FULL_LINE ); PatternPane pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern); addPane(pane); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java index 3e86948..ee9b3ba 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java @@ -16,6 +16,7 @@ import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import java.util.Collections; @@ -44,7 +45,7 @@ public class BasicConfigGui extends ValueUpdatableGui { */ private void init(){ Pattern pattern = new Pattern( - "000000000", + GuiSharedConstant.EMPTY_GUI_FULL_LINE, "012345670", "B00000000" ); 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 new file mode 100644 index 0000000..5c1a138 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java @@ -0,0 +1,75 @@ +package xyz.alexcrea.cuanvil.gui.config.openable; + +import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; +import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; +import com.github.stefvanschie.inventoryframework.pane.Orientable; +import com.github.stefvanschie.inventoryframework.pane.OutlinePane; +import io.delilaheve.CustomAnvil; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import xyz.alexcrea.cuanvil.config.ConfigHolder; +import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; +import xyz.alexcrea.cuanvil.gui.MainConfigGui; +import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.EnchantConflictSubSettingGui; +import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; +import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; + +import java.util.HashMap; + +public class EnchantConflictGui extends ChestGui { + + public final static EnchantConflictGui INSTANCE = new EnchantConflictGui(); + + + private final HashMap conflictGuiMap; + static { + INSTANCE.init(); + } + + private EnchantConflictGui() { + super(6, "§eConflict Config", CustomAnvil.instance); + this.conflictGuiMap = new HashMap<>(); + } + + private OutlinePane filledEnchant; + + private void init(){ + // Back item panel + addPane(GuiSharedConstant.BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE); + + // enchant item panel + this.filledEnchant = new OutlinePane(0, 0, 9, 5); + this.filledEnchant.align(OutlinePane.Alignment.BEGIN); + this.filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); + addPane(this.filledEnchant); + + reloadValues(); + } + + public void reloadValues(){ + this.conflictGuiMap.forEach((conflict, gui) -> gui.cleanUnused()); + 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); + + // Temporaire, il faut faire un item avec le conflict et donc un generateur arbitraire + + ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); + filledEnchant.addItem(new GuiItem(item, GuiGlobalActions.openGuiAction(conflictGui), CustomAnvil.instance)); + } + + } + + public void updateValueForConflict(EnchantConflictGroup conflict){ + + } + + public void removeConflict(EnchantConflictGroup conflict){ + + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java index 7ecd771..d87c4b1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java @@ -59,13 +59,13 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui deleteSupplier = () ->{ + EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); + + // Remove from manager + for (Enchantment enchantment : enchantConflict.getEnchants()) { + manager.removeConflictFromMap(enchantment, enchantConflict); + } + manager.conflictList.remove(enchantConflict); + + // Update config file storage + ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName(), null); + + // Save + boolean success = true; + if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + success = ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); + } + + return success; + }; + + return new ConfirmActionGui("\u00A7cDelete \u00A7e"+CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName())+"\u00A7c ?", + "\u00A77Confirm that you want to delete this conflict.", + this, this.parent, deleteSupplier + ); + } + + @Override + public void updateGuiValues() { + this.parent.updateValueForConflict(this.enchantConflict); + // Parent should call updateLocal + } + + public void updateLocal(){ + if(!this.canOpen) return; + + + } + + public void cleanUnused(){ + for (HumanEntity viewer : getViewers()) { + this.parent.show(viewer); + } + this.canOpen = 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); + } + + @Override + public void show(@NotNull HumanEntity humanEntity) { + if(this.canOpen){ + super.show(humanEntity); + }else{ + parent.show(humanEntity); + } + } +} 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 94b5da1..bc0ff6d 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -1,8 +1,12 @@ package xyz.alexcrea.cuanvil.gui.util; import com.github.stefvanschie.inventoryframework.gui.GuiItem; +import com.github.stefvanschie.inventoryframework.pane.Pane; +import com.github.stefvanschie.inventoryframework.pane.PatternPane; +import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import xyz.alexcrea.cuanvil.gui.MainConfigGui; import java.util.Arrays; import java.util.Comparator; @@ -11,6 +15,7 @@ import java.util.List; public class GuiSharedConstant { public static final List SORTED_ENCHANTMENT_LIST; + static { SORTED_ENCHANTMENT_LIST = Arrays.asList(Enchantment.values()); SORTED_ENCHANTMENT_LIST.sort(Comparator.comparing(ench -> ench.getKey().getKey())); @@ -19,8 +24,29 @@ public class GuiSharedConstant { public static final Material SECONDARY_BACKGROUND_MATERIAL = Material.BLACK_STAINED_GLASS_PANE; public static final GuiItem SECONDARY_BACKGROUND_ITEM = GuiGlobalItems.backgroundItem(GuiSharedConstant.SECONDARY_BACKGROUND_MATERIAL); + public static final String EMPTY_GUI_FULL_LINE = "000000000"; // Temporary values, until I get something better. public static final boolean TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE = true; public static final boolean TEMPORARY_DO_BACKUP_EVERY_SAVE = true; + + public static final PatternPane BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE; + static { + Pattern pattern = 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, + "B11111111" + ); + BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern); + + GuiGlobalItems.addBackItem(BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE, MainConfigGui.INSTANCE); + + GuiGlobalItems.addBackgroundItem(BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE); + BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index 08b1e02..e4d7cfe 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -3,12 +3,18 @@ package xyz.alexcrea.cuanvil.group import org.bukkit.Material import org.bukkit.enchantments.Enchantment -class EnchantConflictGroup(private val cantConflict: AbstractMaterialGroup, private val minBeforeBlock: Int){ +class EnchantConflictGroup( + val name: String, + private val cantConflict: AbstractMaterialGroup, + private val minBeforeBlock: Int){ private val enchantments = HashSet() - fun addEnchantment(ench: Enchantment){ - enchantments.add(ench) + 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){ diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index 4ebc510..aac5ef4 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -10,11 +10,11 @@ class EnchantConflictManager { companion object { // Path for the enchantments list - private const val ENCH_LIST_PATH = "enchantments" + const val ENCH_LIST_PATH = "enchantments" // Path for group list related to the conflict - private const val CONFLICT_GROUP_PATH = "notAffectedGroups" + const val CONFLICT_GROUP_PATH = "notAffectedGroups" // Path for the maximum number of enchantment before validating the conflict - private const val ENCH_MAX_PATH = "maxEnchantmentBeforeConflict" + const val ENCH_MAX_PATH = "maxEnchantmentBeforeConflict" // Path for a flag: if the enchantment will be used in the last supported version // TODO maybe replace this system by a list of "future" enchantment. private const val FUTURE_USE_PATH = "useInFuture" @@ -25,10 +25,12 @@ class EnchantConflictManager { } private lateinit var conflictMap: HashMap> + lateinit var conflictList: ArrayList // Read and prepare all conflict fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager){ conflictMap = HashMap() + conflictList = ArrayList() val keys = config.getKeys(false) for (key in keys) { @@ -36,6 +38,7 @@ class EnchantConflictManager { val conflict = createConflict(section,itemManager,key) if(conflict != null){ addToMap(conflict) + conflictList.add(conflict) } } @@ -45,13 +48,21 @@ class EnchantConflictManager { // Add the conflict to the map private fun addToMap(conflict: EnchantConflictGroup){ conflict.getEnchants().forEach{ enchant -> - if(!conflictMap.containsKey(enchant)){ - conflictMap[enchant] = ArrayList() - } - conflictMap[enchant]!!.add(conflict) + addConflictToConflictMap(enchant, conflict); } } + fun addConflictToConflictMap(enchant: Enchantment, conflict: EnchantConflictGroup){ + if(!conflictMap.containsKey(enchant)){ + conflictMap[enchant] = ArrayList() + } + conflictMap[enchant]!!.add(conflict) + } + + fun removeConflictFromMap(enchant: Enchantment, conflict: EnchantConflictGroup): Boolean{ + return conflictMap[enchant]!!.remove(conflict) + } + // create and read a conflict from a yaml section private fun createConflict(section: ConfigurationSection, itemManager: ItemGroupManager, @@ -107,7 +118,7 @@ class EnchantConflictManager { } } // Return conflict - return EnchantConflictGroup(finalGroup, minBeforeBlock) + return EnchantConflictGroup(conflictName, finalGroup, minBeforeBlock) } private fun findGroup(groupName: String, itemManager: ItemGroupManager, conflictName: String): AbstractMaterialGroup { From 277cf5bedbd1eee7dc68c277c65655b2c34a2ba9 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sat, 23 Mar 2024 00:25:35 +0100 Subject: [PATCH 08/17] progress on sub setting gui --- .../gui/config/AbstractEnchantConfigGui.java | 2 - .../config/openable/EnchantConflictGui.java | 2 - .../EnchantConflictSubSettingGui.java | 123 +++++++++++++++++- .../cuanvil/group/EnchantConflictGroup.kt | 4 + 4 files changed, 122 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index 1a1c176..b933b87 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -1,12 +1,10 @@ package xyz.alexcrea.cuanvil.gui.config; import com.github.stefvanschie.inventoryframework.gui.GuiItem; -import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import com.github.stefvanschie.inventoryframework.pane.Orientable; import com.github.stefvanschie.inventoryframework.pane.OutlinePane; import io.delilaheve.CustomAnvil; import org.bukkit.enchantments.Enchantment; -import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; 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 5c1a138..9112a83 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 @@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.gui.config.openable; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; -import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import com.github.stefvanschie.inventoryframework.pane.Orientable; import com.github.stefvanschie.inventoryframework.pane.OutlinePane; import io.delilaheve.CustomAnvil; @@ -10,7 +9,6 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; -import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.EnchantConflictSubSettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; 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 a16e3de..730423d 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 @@ -11,21 +11,27 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; 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; +import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; import xyz.alexcrea.cuanvil.gui.config.openable.EnchantConflictGui; +import xyz.alexcrea.cuanvil.gui.config.settings.EnchantSelectSettingGui; +import xyz.alexcrea.cuanvil.gui.config.settings.GroupSelectSettingGui; +import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; 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.Collections; +import java.util.*; import java.util.function.Supplier; -public class EnchantConflictSubSettingGui extends ValueUpdatableGui { +public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements SelectEnchantmentContainer, SelectGroupContainer { private final EnchantConflictGui parent; private final EnchantConflictGroup enchantConflict; @@ -35,7 +41,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui { public EnchantConflictSubSettingGui( @NotNull EnchantConflictGui parent, @NotNull EnchantConflictGroup enchantConflict) { - super(3, "\u00A7aConfig for \u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), CustomAnvil.instance); + super(3, "\u00A72Config for \u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), CustomAnvil.instance); this.parent = parent; this.enchantConflict = enchantConflict; @@ -48,9 +54,12 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui { addPane(this.pane); prepareStaticValues(); - //updateGuiValues(); } + private GuiItem enchantSettingItem; + private GuiItem groupSettingItem; + private IntSettingsGui.IntSettingFactory minBeforeActiveSettingFactory; + private void prepareStaticValues() { GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE); @@ -66,6 +75,35 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui { deleteItem.setItemMeta(deleteMeta); 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)->{ + event.setCancelled(true); + EnchantSelectSettingGui enchantGui = new EnchantSelectSettingGui( + "\u00A7eEnchantments for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), + this, this, 0); + enchantGui.show(event.getWhoClicked()); + }, CustomAnvil.instance); + + groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{ + event.setCancelled(true); + GroupSelectSettingGui enchantGui = new GroupSelectSettingGui( + "\u00A7eGroups for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), + 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, + 0, 255, 0, 1 + ); + + pane.bindItem('E', enchantSettingItem); + pane.bindItem('G', groupSettingItem); + + // Now we update the items + updateLocal(); + } private ConfirmActionGui createDeleteGui() { @@ -105,7 +143,40 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui { public void updateLocal(){ if(!this.canOpen) 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."); + }else{ + enchantLore.add("\u00A77List of included enchantment for this conflict:"); + Iterator enchantIterator = enchants.iterator(); + int maxindex = (enchants.size() > 5 ? 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())); + } + } + + // Configure enchant setting item + ItemStack enchantItem = enchantSettingItem.getItem(); + ItemMeta enchantMeta = enchantItem.getItemMeta(); + + enchantMeta.setDisplayName("\u00A7aSelect \u00A75Enchantments \u00A7aSettings"); + enchantMeta.setLore(enchantLore); + + enchantItem.setItemMeta(enchantMeta); + + enchantSettingItem.setItem(enchantItem); // Just in case + + + //todo: groupSettingItem + + + pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(minBeforeActiveSettingFactory, Material.COMMAND_BLOCK)); + update(); } public void cleanUnused(){ @@ -127,7 +198,49 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui { if(this.canOpen){ super.show(humanEntity); }else{ - parent.show(humanEntity); + this.parent.show(humanEntity); } } + + // Select enchantment container methods + + @Override + public Set getSelectedEnchantments() { + return this.enchantConflict.getEnchants(); + } + + @Override + public boolean setSelectedEnchantments(Set enchantments) { + return false; + } + + @Override + public Set illegalEnchantments() { + return new HashSet<>(); + } + + // Select group container methods + + @Override + public Set getSelectedGroups() { + return this.enchantConflict.getCantConflictGroup().getGroups(); + } + + @Override + public boolean setSelectedGroups(Set groups) { + // Set live configuration + this.enchantConflict.getCantConflictGroup().setGroups(groups); + + // Save on file configuration + + // Save file configuration to disk + + return false; + } + + @Override + public Set illegalGroups() { + + return new HashSet<>(); + } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index e4d7cfe..f88cdca 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -37,6 +37,10 @@ class EnchantConflictGroup( return true } + fun getCantConflictGroup(): AbstractMaterialGroup{ + return this.cantConflict; + } + fun getEnchants(): HashSet { return enchantments } From a239a063aee9436e2aa7d07945de43675d637bf2 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sat, 23 Mar 2024 14:34:13 +0100 Subject: [PATCH 09/17] probably finished sub setting gui --- .../config/openable/EnchantConflictGui.java | 53 +++-- .../EnchantConflictSubSettingGui.java | 190 ++++++++++++++---- .../cuanvil/group/EnchantConflictGroup.kt | 9 +- 3 files changed, 194 insertions(+), 58 deletions(-) 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 From 27b949d1e5babc368fb8f025afc27127e271c407 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sun, 24 Mar 2024 22:55:17 +0100 Subject: [PATCH 10/17] Start of page system --- .../config/openable/EnchantConflictGui.java | 129 +++++++++++++++++- 1 file changed, 124 insertions(+), 5 deletions(-) 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 614f052..6a70fa6 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 @@ -4,16 +4,27 @@ import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import com.github.stefvanschie.inventoryframework.pane.Orientable; import com.github.stefvanschie.inventoryframework.pane.OutlinePane; +import com.github.stefvanschie.inventoryframework.pane.Pane; +import com.github.stefvanschie.inventoryframework.pane.PatternPane; +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.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; +import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.EnchantConflictSubSettingGui; 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.HashMap; +import java.util.UUID; public class EnchantConflictGui extends ChestGui { @@ -30,20 +41,72 @@ public class EnchantConflictGui extends ChestGui { } private OutlinePane filledEnchant; + private ArrayList pages; + private HashMap pageMap; + private PatternPane backgroundPane; private void init(){ // Back item panel - addPane(GuiSharedConstant.BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE); + Pattern pattern = 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, + "B11L1R11C" + ); + backgroundPane = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern); + + GuiGlobalItems.addBackItem(backgroundPane, MainConfigGui.INSTANCE); + + GuiGlobalItems.addBackgroundItem(backgroundPane); + backgroundPane.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + backgroundPane.bindItem('C', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + addPane(backgroundPane); + + // Page init + this.pages = new ArrayList<>(); + this.pageMap = new HashMap<>(); // enchant item panel - this.filledEnchant = new OutlinePane(0, 0, 9, 5); - this.filledEnchant.align(OutlinePane.Alignment.BEGIN); - this.filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); - addPane(this.filledEnchant); + this.filledEnchant = creatEmptyPage(); + this.pages.add(this.filledEnchant); + prepareOtherValues(); reloadValues(); } + + private GuiItem goLeftItem; + private GuiItem goRightItem; + private void prepareOtherValues() { + //TODO item x2 + this.goLeftItem = new GuiItem(new ItemStack(Material.PAPER), event -> { + UUID playerUUID = event.getWhoClicked().getUniqueId(); + int page =this.pageMap.getOrDefault(event.getWhoClicked().getUniqueId(), 0); + this.pageMap.put(playerUUID, page-1); + + show(event.getWhoClicked()); + }, CustomAnvil.instance); + + this.goRightItem = new GuiItem(new ItemStack(Material.PAPER), event -> { + UUID playerUUID = event.getWhoClicked().getUniqueId(); + int page = pageMap.getOrDefault(event.getWhoClicked().getUniqueId(), 0); + this.pageMap.put(playerUUID, page+1); + + show(event.getWhoClicked()); + }, CustomAnvil.instance); + + } + + private OutlinePane creatEmptyPage(){ + OutlinePane page = new OutlinePane(0, 0, 9, 5); + page.align(OutlinePane.Alignment.BEGIN); + page.setOrientation(Orientable.Orientation.HORIZONTAL); + + return page; + } + public void reloadValues(){ this.conflictGuiMap.forEach((conflict, gui) -> gui.cleanUnused()); this.conflictGuiMap.clear(); @@ -59,7 +122,11 @@ public class EnchantConflictGui extends ChestGui { public ItemStack createItemForConflict(EnchantConflictGroup conflict){ ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); //TODO item + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(conflict.getName()); + + item.setItemMeta(meta); return item; } @@ -99,4 +166,56 @@ public class EnchantConflictGui extends ChestGui { update(); } + public int getPlayerPageID(UUID uuid){ + int pageId = this.pageMap.getOrDefault(uuid, 0); + if(pageId >= this.pages.size()){ + pageId = this.pages.size()-1; + } + return pageId; + } + + public void placeArrow(int page){ + // Place left arrow + if(page > 0){ + this.backgroundPane.bindItem('R', this.goLeftItem); + }else{ + this.backgroundPane.bindItem('L', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + } + + // Place right arrow + if(page < pages.size()){ + this.backgroundPane.bindItem('R', this.goRightItem); + }else{ + this.backgroundPane.bindItem('R', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + } + } + + @Override // assume will not be called in multiple thread + public void show(@NotNull HumanEntity humanEntity) { + int pageID = getPlayerPageID(humanEntity.getUniqueId()); + OutlinePane page = this.pages.get(pageID); + + // display the page arrow pane + placeArrow(pageID); + + addPane(page); + super.show(humanEntity); + getPanes().remove(page); + + } + + @Override // assume will not be called in multiple thread + public void click(@NotNull InventoryClickEvent event) { + int pageID = getPlayerPageID(event.getWhoClicked().getUniqueId()); + OutlinePane page = this.pages.get(pageID); + + // set the page arrow pane + placeArrow(pageID); + + // use the clicked page + addPane(page); + super.click(event); + getPanes().remove(page); + } + } From 4b5ecf301a3eca7463109a88b87fb98251a0ff8b Mon Sep 17 00:00:00 2001 From: alexcrea Date: Mon, 25 Mar 2024 02:09:09 +0100 Subject: [PATCH 11/17] add verbose log --- .../kotlin/io/delilaheve/AnvilEventListener.kt | 4 ++++ src/main/kotlin/io/delilaheve/CustomAnvil.kt | 9 +++++++++ .../kotlin/io/delilaheve/util/ConfigOptions.kt | 14 ++++++++++++++ .../kotlin/io/delilaheve/util/EnchantmentUtil.kt | 11 ++++++++--- .../alexcrea/cuanvil/group/EnchantConflictGroup.kt | 2 ++ .../cuanvil/group/EnchantConflictManager.kt | 8 +++++++- src/main/resources/config.yml | 3 +++ 7 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt index 26a27bd..6a72777 100644 --- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt +++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt @@ -64,6 +64,7 @@ class AnvilEventListener : Listener { // Test/stop if nothing changed. if(first == resultItem){ + CustomAnvil.log("no right item, But input is same as output") event.result = null return } @@ -95,6 +96,7 @@ class AnvilEventListener : Listener { // Test/stop if nothing changed. if(first == resultItem){ + CustomAnvil.log("Mergable with second, But input is same as output") event.result = null return } @@ -125,6 +127,7 @@ class AnvilEventListener : Listener { // Test/stop if nothing changed. if(first == resultItem){ + CustomAnvil.log("unit repair, But input is same as output") event.result = null return } @@ -132,6 +135,7 @@ class AnvilEventListener : Listener { handleAnvilXp(inventory, event, anvilCost) }else{ + CustomAnvil.log("no anvil fuse type found") event.result = null } } diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 2f4fe43..28c8dd0 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -49,6 +49,15 @@ class CustomAnvil : JavaPlugin() { instance.logger.info(message) } } + + /** + * Vebose Logging handler + */ + fun verboseLog(message: String) { + if (ConfigOptions.verboseDebugLog) { + instance.logger.info(message) + } + } } /** diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index 7708568..fcbf42a 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -35,6 +35,8 @@ object ConfigOptions { private const val KEY_ITEM = "item" // Debug logging toggle path private const val DEBUG_LOGGING = "debug_log" + // Debug verbose logging toggle path + private const val VERBOSE_DEBUG_LOGGING = "debug_log_verbose" // Default value for enchantment limits private const val DEFAULT_ENCHANT_LIMIT = 5 @@ -71,6 +73,8 @@ object ConfigOptions { private const val DEFAULT_ENCHANT_VALUE = 0 // Default value for debug logging private const val DEFAULT_DEBUG_LOG = false + // Default value for debug logging + private const val DEFAULT_VERBOSE_DEBUG_LOG = false /** * Default enchantment limit @@ -171,6 +175,16 @@ object ConfigOptions { .getBoolean(DEBUG_LOGGING, DEFAULT_DEBUG_LOG) } + /** + * Whether to show verbose debug logging + */ + val verboseDebugLog: Boolean + get() { + return ConfigHolder.DEFAULT_CONFIG + .config + .getBoolean(VERBOSE_DEBUG_LOGGING, DEFAULT_VERBOSE_DEBUG_LOG) + } + /** * Get the given [enchantment]'s limit */ diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt index f2f90d6..24a0f97 100644 --- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt @@ -32,10 +32,12 @@ object EnchantmentUtil { other.forEach { (enchantment, level) -> // Enchantment not yet in result list if (!containsKey(enchantment)) { - // Add the enchantment if it doesn't have conflicts, or, if player is allowed to bypass enchantment restrictions + // Add the enchantment if it doesn't have conflicts, or if player is allowed to bypass enchantment restrictions this[enchantment] = level + val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment); if(!player.hasPermission(CustomAnvil.bypassFusePermission) && - (ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT)){ + (conflictType != ConflictType.NO_CONFLICT)){ + CustomAnvil.verboseLog("Enchantment not yet in result list, but there is conflict (${enchantment.key}, conflict: $conflictType)") this.remove(enchantment) } @@ -43,8 +45,10 @@ object EnchantmentUtil { // Enchantment already in result list else{ // ... and they are conflicting - if((ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) != ConflictType.NO_CONFLICT) + val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) + if((conflictType != ConflictType.NO_CONFLICT) && !player.hasPermission(CustomAnvil.bypassFusePermission)){ + CustomAnvil.verboseLog("Enchantment already in result list, and they are conflicting (${enchantment.key}, conflict: $conflictType)") return@forEach } @@ -52,6 +56,7 @@ object EnchantmentUtil { if(this[enchantment] != other[enchantment]){ val newLevel = max(this[enchantment] ?: 0, other[enchantment] ?: 0) // apply the greater of the two if non-zero + if (newLevel > 0) { this[enchantment] = newLevel } } // ... and they're the same level diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index ab72195..075b0c6 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -1,5 +1,6 @@ package xyz.alexcrea.cuanvil.group +import io.delilaheve.CustomAnvil import org.bukkit.Material import org.bukkit.enchantments.Enchantment @@ -27,6 +28,7 @@ class EnchantConflictGroup( var enchantAmount = 0 for (enchantment in enchants) { if(enchantment !in enchantments) continue + CustomAnvil.verboseLog("Enchant ${enchantment.key} is in: ${enchantAmount + 1}/$minBeforeBlock ") if(++enchantAmount > minBeforeBlock){ return false } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index aac5ef4..3d6eb29 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -132,13 +132,19 @@ class EnchantConflictManager { } fun isConflicting(base: Set,mat: Material, newEnchant: Enchantment): ConflictType { + CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}") val conflictList = conflictMap[newEnchant] ?: return ConflictType.NO_CONFLICT + CustomAnvil.verboseLog("Did not get skipped") var result = ConflictType.NO_CONFLICT for (conflict in conflictList) { - if(!conflict.allowed(base,mat)) { + CustomAnvil.verboseLog("Is against ${conflict.name}") + val conflicting = conflict.allowed(base,mat) + CustomAnvil.verboseLog("Was against ${conflict.name} and conflicting: $conflicting ") + if(!conflicting) { if(conflict.getEnchants().size <= 1){ result = ConflictType.SMALL_CONFLICT + CustomAnvil.verboseLog("Small conflict, continuing") }else{ return ConflictType.BIG_CONFLICT } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 2de074b..61f169b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -220,3 +220,6 @@ enchant_values: # Whether to show debug logging debug_log: false + +# Whether to show verbose debug logging +debug_log_verbose: false From 21c39421b917fe0bd9fc90e79062ae706c35d679 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Mon, 25 Mar 2024 02:21:13 +0100 Subject: [PATCH 12/17] fix the plugin not working --- .../kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index f7a62b7..1ff4242 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -15,7 +15,7 @@ abstract class AbstractMaterialGroup(private val name: String) { * Get if a material is allowed following the group policy */ open fun contain(mat : Material): Boolean { - return mat in includedMaterial + return mat in getMaterials() } /** From a30f471c6ab621ca6af827e13054634cc35ffd2f Mon Sep 17 00:00:00 2001 From: alexcrea Date: Tue, 26 Mar 2024 18:32:47 +0100 Subject: [PATCH 13/17] finished page system. --- .../{openable => global}/BasicConfigGui.java | 3 +- .../EnchantConflictGui.java | 107 ++++++++++++++---- .../EnchantCostConfigGui.java | 2 +- .../EnchantLimitConfigGui.java | 2 +- .../{ => config/global}/MainConfigGui.java | 6 +- .../EnchantConflictSubSettingGui.java | 2 +- .../cuanvil/gui/util/GuiSharedConstant.java | 2 +- .../cuanvil/command/EditConfigExecutor.kt | 2 +- .../cuanvil/command/ReloadExecutor.kt | 6 +- 9 files changed, 95 insertions(+), 37 deletions(-) rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{openable => global}/BasicConfigGui.java (98%) rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{openable => global}/EnchantConflictGui.java (69%) rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{openable => global}/EnchantCostConfigGui.java (98%) rename src/main/java/xyz/alexcrea/cuanvil/gui/config/{openable => global}/EnchantLimitConfigGui.java (97%) rename src/main/java/xyz/alexcrea/cuanvil/gui/{ => config/global}/MainConfigGui.java (94%) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java similarity index 98% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java index ee9b3ba..75e898a 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/BasicConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui.config.openable; +package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.pane.PatternPane; @@ -10,7 +10,6 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import xyz.alexcrea.cuanvil.config.ConfigHolder; -import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java similarity index 69% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java index 6a70fa6..fd392d3 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui.config.openable; +package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; @@ -16,7 +16,6 @@ import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; -import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.EnchantConflictSubSettingGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; @@ -36,11 +35,11 @@ public class EnchantConflictGui extends ChestGui { private final HashMap conflictGuiMap; private EnchantConflictGui() { - super(6, "§eConflict Config", CustomAnvil.instance); + super(6, "Conflict Config", CustomAnvil.instance); this.conflictGuiMap = new HashMap<>(); } - private OutlinePane filledEnchant; + private OutlinePane firstPage; private ArrayList pages; private HashMap pageMap; private PatternPane backgroundPane; @@ -69,8 +68,8 @@ public class EnchantConflictGui extends ChestGui { this.pageMap = new HashMap<>(); // enchant item panel - this.filledEnchant = creatEmptyPage(); - this.pages.add(this.filledEnchant); + this.firstPage = creatEmptyPage(); + this.pages.add(this.firstPage); prepareOtherValues(); reloadValues(); @@ -82,19 +81,33 @@ public class EnchantConflictGui extends ChestGui { private void prepareOtherValues() { //TODO item x2 this.goLeftItem = new GuiItem(new ItemStack(Material.PAPER), event -> { - UUID playerUUID = event.getWhoClicked().getUniqueId(); - int page =this.pageMap.getOrDefault(event.getWhoClicked().getUniqueId(), 0); + HumanEntity viewer = event.getWhoClicked(); + UUID playerUUID = viewer.getUniqueId(); + int page =this.pageMap.getOrDefault(playerUUID, 0); this.pageMap.put(playerUUID, page-1); - show(event.getWhoClicked()); + ItemStack cursor = viewer.getItemOnCursor(); + viewer.setItemOnCursor(new ItemStack(Material.AIR)); + + viewer.closeInventory();// Hacky trick to make the inventory update properly + show(viewer); + + viewer.setItemOnCursor(cursor); }, CustomAnvil.instance); this.goRightItem = new GuiItem(new ItemStack(Material.PAPER), event -> { - UUID playerUUID = event.getWhoClicked().getUniqueId(); - int page = pageMap.getOrDefault(event.getWhoClicked().getUniqueId(), 0); + HumanEntity viewer = event.getWhoClicked(); + UUID playerUUID = viewer.getUniqueId(); + int page = pageMap.getOrDefault(playerUUID, 0); this.pageMap.put(playerUUID, page+1); - show(event.getWhoClicked()); + ItemStack cursor = viewer.getItemOnCursor(); + viewer.setItemOnCursor(new ItemStack(Material.AIR)); + + viewer.closeInventory();// Hacky trick to make the inventory update properly + show(viewer); + + viewer.setItemOnCursor(cursor); }, CustomAnvil.instance); } @@ -110,7 +123,9 @@ public class EnchantConflictGui extends ChestGui { public void reloadValues(){ this.conflictGuiMap.forEach((conflict, gui) -> gui.cleanUnused()); this.conflictGuiMap.clear(); - this.filledEnchant.clear(); + this.firstPage.clear(); + this.pages.clear(); + this.pages.add(this.firstPage); for (EnchantConflictGroup conflict : ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList()) { updateValueForConflict(conflict, false); @@ -143,7 +158,7 @@ public class EnchantConflictGui extends ChestGui { guiItem.setAction(GuiGlobalActions.openGuiAction(gui)); this.conflictGuiMap.put(conflict, gui); - this.filledEnchant.addItem(guiItem); + addToPage(guiItem); }else{ // replace item with the updated one guiItem = gui.getParentItemForThisGui(); @@ -161,11 +176,55 @@ public class EnchantConflictGui extends ChestGui { EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict); if(gui == null) return; - this.filledEnchant.removeItem(gui.getParentItemForThisGui()); this.conflictGuiMap.remove(conflict); + removeFromPage(gui.getParentItemForThisGui()); + update(); } + private void addToPage(GuiItem guiItem) { + // Get first available page or create one + OutlinePane page = this.pages.get(this.pages.size()-1); + if(page.getItems().size() >= 5*9){ + page = creatEmptyPage(); + this.pages.add(page); + } + + page.addItem(guiItem); + } + + private void removeFromPage(GuiItem guiItem) { + // get item page + OutlinePane page = null; + int pageID = 0; + while(pageID < this.pages.size()){ + OutlinePane tempPage = this.pages.get(pageID++); + if(tempPage.getItems().contains(guiItem)){ + page = tempPage; + break; + } + } + + if(page == null){// Why... + return; + } + removeFromPage(page, pageID, guiItem); + } + + private void removeFromPage(OutlinePane page, int pageID, GuiItem guiItem) { + page.removeItem(guiItem); + + // There is now a slot available, let fill it if possible + if(pageID < this.pages.size() - 1){ + OutlinePane newPage = this.pages.get(pageID+1); + GuiItem newItem = newPage.getItems().get(0); + removeFromPage(newPage, pageID+1, newItem); + }else if(pageID > 0 && page.getItems().isEmpty()){ + this.pages.remove(pageID); + } + } + + public int getPlayerPageID(UUID uuid){ int pageId = this.pageMap.getOrDefault(uuid, 0); if(pageId >= this.pages.size()){ @@ -176,14 +235,15 @@ public class EnchantConflictGui extends ChestGui { public void placeArrow(int page){ // Place left arrow + addPane(this.backgroundPane); if(page > 0){ - this.backgroundPane.bindItem('R', this.goLeftItem); + this.backgroundPane.bindItem('L', this.goLeftItem); }else{ this.backgroundPane.bindItem('L', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); } // Place right arrow - if(page < pages.size()){ + if(page < pages.size()-1){ this.backgroundPane.bindItem('R', this.goRightItem); }else{ this.backgroundPane.bindItem('R', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); @@ -195,12 +255,14 @@ public class EnchantConflictGui extends ChestGui { int pageID = getPlayerPageID(humanEntity.getUniqueId()); OutlinePane page = this.pages.get(pageID); + getPanes().clear(); + // display the page arrow pane placeArrow(pageID); - + // and add actual page addPane(page); + super.show(humanEntity); - getPanes().remove(page); } @@ -209,13 +271,14 @@ public class EnchantConflictGui extends ChestGui { int pageID = getPlayerPageID(event.getWhoClicked().getUniqueId()); OutlinePane page = this.pages.get(pageID); + getPanes().clear(); + // set the page arrow pane placeArrow(pageID); - - // use the clicked page + // and add actual page addPane(page); + super.click(event); - getPanes().remove(page); } } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java similarity index 98% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java index d87c4b1..e757d5f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantCostConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui.config.openable; +package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import org.bukkit.Material; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantLimitConfigGui.java similarity index 97% rename from src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantLimitConfigGui.java index 92b7833..658bb7e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/openable/EnchantLimitConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantLimitConfigGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui.config.openable; +package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import org.bukkit.Material; diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java similarity index 94% rename from src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java rename to src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java index c1ee55e..620359a 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/MainConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java @@ -1,4 +1,4 @@ -package xyz.alexcrea.cuanvil.gui; +package xyz.alexcrea.cuanvil.gui.config.global; import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; @@ -8,10 +8,6 @@ import io.delilaheve.CustomAnvil; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui; -import xyz.alexcrea.cuanvil.gui.config.openable.EnchantConflictGui; -import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui; -import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; 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 1122d80..6e42cb5 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 @@ -18,7 +18,7 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.config.ConfirmActionGui; import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer; import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; -import xyz.alexcrea.cuanvil.gui.config.openable.EnchantConflictGui; +import xyz.alexcrea.cuanvil.gui.config.global.EnchantConflictGui; import xyz.alexcrea.cuanvil.gui.config.settings.EnchantSelectSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.GroupSelectSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.IntSettingsGui; 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 bc0ff6d..e3604b3 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -6,7 +6,7 @@ import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; -import xyz.alexcrea.cuanvil.gui.MainConfigGui; +import xyz.alexcrea.cuanvil.gui.config.global.MainConfigGui; import java.util.Arrays; import java.util.Comparator; diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index 0d4ebd0..a978abc 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -5,7 +5,7 @@ import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.entity.HumanEntity -import xyz.alexcrea.cuanvil.gui.MainConfigGui +import xyz.alexcrea.cuanvil.gui.config.global.MainConfigGui import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions class EditConfigExecutor : CommandExecutor { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index e8d201d..936127e 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -5,9 +5,9 @@ import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import xyz.alexcrea.cuanvil.config.ConfigHolder -import xyz.alexcrea.cuanvil.gui.config.openable.BasicConfigGui -import xyz.alexcrea.cuanvil.gui.config.openable.EnchantCostConfigGui -import xyz.alexcrea.cuanvil.gui.config.openable.EnchantLimitConfigGui +import xyz.alexcrea.cuanvil.gui.config.global.BasicConfigGui +import xyz.alexcrea.cuanvil.gui.config.global.EnchantCostConfigGui +import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui import xyz.alexcrea.cuanvil.util.MetricsUtil class ReloadExecutor : CommandExecutor { From bf320e1ffc72b5700eeab4b9915b40cbca6ee43e Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Fri, 29 Mar 2024 00:07:12 +0100 Subject: [PATCH 14/17] Work on the displayed items --- .../gui/config/global/EnchantConflictGui.java | 33 +++++++++++-------- .../gui/config/global/MainConfigGui.java | 2 +- .../cuanvil/gui/util/GuiGlobalItems.java | 2 +- .../cuanvil/util/CasedStringUtil.java | 26 +++++++++++++++ .../cuanvil/group/EnchantConflictGroup.kt | 13 +++++++- 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java index fd392d3..802301e 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java @@ -20,8 +20,10 @@ import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.EnchantConflictSubSet 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.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.UUID; @@ -54,21 +56,21 @@ public class EnchantConflictGui extends ChestGui { GuiSharedConstant.EMPTY_GUI_FULL_LINE, "B11L1R11C" ); - backgroundPane = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern); + this.backgroundPane = new PatternPane(0, 0, 9, 6, Pane.Priority.LOW, pattern); - GuiGlobalItems.addBackItem(backgroundPane, MainConfigGui.INSTANCE); + GuiGlobalItems.addBackItem(this.backgroundPane, MainConfigGui.INSTANCE); - GuiGlobalItems.addBackgroundItem(backgroundPane); - backgroundPane.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); - backgroundPane.bindItem('C', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); - addPane(backgroundPane); + GuiGlobalItems.addBackgroundItem(this.backgroundPane); + this.backgroundPane.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + this.backgroundPane.bindItem('C', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); + addPane(this.backgroundPane); // Page init this.pages = new ArrayList<>(); this.pageMap = new HashMap<>(); // enchant item panel - this.firstPage = creatEmptyPage(); + this.firstPage = createEmptyPage(); this.pages.add(this.firstPage); prepareOtherValues(); @@ -83,7 +85,7 @@ public class EnchantConflictGui extends ChestGui { this.goLeftItem = new GuiItem(new ItemStack(Material.PAPER), event -> { HumanEntity viewer = event.getWhoClicked(); UUID playerUUID = viewer.getUniqueId(); - int page =this.pageMap.getOrDefault(playerUUID, 0); + int page = this.pageMap.getOrDefault(playerUUID, 0); this.pageMap.put(playerUUID, page-1); ItemStack cursor = viewer.getItemOnCursor(); @@ -112,7 +114,7 @@ public class EnchantConflictGui extends ChestGui { } - private OutlinePane creatEmptyPage(){ + private OutlinePane createEmptyPage(){ OutlinePane page = new OutlinePane(0, 0, 9, 5); page.align(OutlinePane.Alignment.BEGIN); page.setOrientation(Orientable.Orientation.HORIZONTAL); @@ -135,11 +137,16 @@ public class EnchantConflictGui extends ChestGui { } public ItemStack createItemForConflict(EnchantConflictGroup conflict){ - ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); - //TODO item + ItemStack item = new ItemStack(conflict.getRepresentativeMaterial()); + ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(conflict.getName()); + meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(conflict.getName()) + " \u00A7rConflict"); + meta.setLore(Arrays.asList( + "\u00A77Enchantment count: \u00A7e"+conflict.getEnchants().size(), + "\u00A77Group count: \u00A7e"+conflict.getCantConflictGroup().getGroups().size(), + "\u00A77Min enchantments count: \u00A7e"+conflict.getMinBeforeBlock() + )); item.setItemMeta(meta); return item; @@ -186,7 +193,7 @@ public class EnchantConflictGui extends ChestGui { // Get first available page or create one OutlinePane page = this.pages.get(this.pages.size()-1); if(page.getItems().size() >= 5*9){ - page = creatEmptyPage(); + page = createEmptyPage(); this.pages.add(page); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java index 620359a..c599deb 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/MainConfigGui.java @@ -70,7 +70,7 @@ public class MainConfigGui extends ChestGui { pane.bindItem('3', enchantCostItem); // Enchantment Conflicts - ItemStack EnchantConflictItemstack = new ItemStack(Material.EXPERIENCE_BOTTLE); + ItemStack EnchantConflictItemstack = new ItemStack(Material.OAK_FENCE); ItemMeta enchantConflictMeta = EnchantConflictItemstack.getItemMeta(); enchantConflictMeta.setDisplayName("\u00A7aEnchantment Conflict"); 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 bc959fb..125edf1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java @@ -244,7 +244,7 @@ public class GuiGlobalItems { @NotNull Material itemMat ){ String configPath = getConfigNameFromPath(factory.getConfigPath()); - return intSettingGuiItem(factory, itemMat, CasedStringUtil.snakeToUpperSpacedCase(configPath)); + return intSettingGuiItem(factory, itemMat, CasedStringUtil.detectToUpperSpacedCase(configPath)); } /** diff --git a/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java b/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java index 28525f8..2b6ce8c 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java +++ b/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java @@ -28,4 +28,30 @@ public class CasedStringUtil { return result.substring(1); } + public static String camelCaseToUpperSpaceCase(String camelCasedString){ + if(camelCasedString.isEmpty()) return camelCasedString; + StringBuilder stb = new StringBuilder(); + + char[] chars = camelCasedString.toCharArray(); + stb.append(chars[0]); + for (int i = 1; i < chars.length; i++) { + char chr = chars[i]; + if(Character.isUpperCase(chr)){ + stb.append(" "); + } + stb.append(chr); + } + + return stb.toString(); + } + + public static String detectToUpperSpacedCase(String toDetect){ + //not advanced detection + if(toDetect.contains("_")){ + return snakeToUpperSpacedCase(toDetect); + }else{ + return camelCaseToUpperSpaceCase(toDetect); + } + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index 075b0c6..e3c9af3 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -7,7 +7,7 @@ import org.bukkit.enchantments.Enchantment class EnchantConflictGroup( val name: String, private val cantConflict: AbstractMaterialGroup, - private val minBeforeBlock: Int){ + val minBeforeBlock: Int){ private val enchantments = HashSet() @@ -50,4 +50,15 @@ class EnchantConflictGroup( enchantments.addAll(enchants) } + fun getRepresentativeMaterial(): Material { + val groups = getCantConflictGroup().getGroups() + val groupIterator = groups.iterator() + while (groupIterator.hasNext()){ + val mat = groupIterator.next().getRepresentativeMaterial() + if(mat != Material.ENCHANTED_BOOK) return mat + + } + return Material.ENCHANTED_BOOK + } + } \ No newline at end of file From 081266accf95a49c3af4290dfdbc2b899b25a735 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sat, 30 Mar 2024 21:20:12 +0100 Subject: [PATCH 15/17] Add create conflict item. --- .../gui/config/global/EnchantConflictGui.java | 91 +++++++++++++++++-- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 12 ++- .../cuanvil/command/ReloadExecutor.kt | 1 + .../cuanvil/group/EnchantConflictManager.kt | 18 +--- .../cuanvil/listener/ChatEventListener.kt | 47 ++++++++++ 5 files changed, 147 insertions(+), 22 deletions(-) create mode 100644 src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java index 802301e..2f9ff19 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java @@ -9,6 +9,7 @@ import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import io.delilaheve.CustomAnvil; import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; @@ -16,16 +17,16 @@ import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; +import xyz.alexcrea.cuanvil.group.IncludeGroup; import xyz.alexcrea.cuanvil.gui.config.settings.subsetting.EnchantConflictSubSettingGui; 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.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.UUID; +import java.util.*; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; public class EnchantConflictGui extends ChestGui { @@ -62,7 +63,6 @@ public class EnchantConflictGui extends ChestGui { GuiGlobalItems.addBackgroundItem(this.backgroundPane); this.backgroundPane.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); - this.backgroundPane.bindItem('C', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); addPane(this.backgroundPane); // Page init @@ -112,6 +112,85 @@ public class EnchantConflictGui extends ChestGui { viewer.setItemOnCursor(cursor); }, CustomAnvil.instance); + // Create new conflict item + ItemStack createItem = new ItemStack(Material.GREEN_TERRACOTTA); + ItemMeta createMeta = createItem.getItemMeta(); + + createItem.setItemMeta(createMeta); + + this.backgroundPane.bindItem('C', new GuiItem(createItem, (clickEvent)->{ + clickEvent.setCancelled(true); + HumanEntity player = clickEvent.getWhoClicked(); + + // check permission + if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + player.closeInventory(); + player.sendMessage(GuiGlobalActions.NO_EDIT_PERM); + return; + } + player.closeInventory(); + + player.sendMessage("\u00A7eWrite the conflict you want to create in the chat.\n" + + "\u00A7eOr write \u00A7ccancel \u00A7eto go back to conflict config menu"); + + CustomAnvil.Companion.getChatListener().setListenedCallback(player, prepareCreateItemConsumer(player)); + + }, CustomAnvil.instance)); + } + + private Consumer prepareCreateItemConsumer(HumanEntity player) { + AtomicReference> selfRef = new AtomicReference<>(); + Consumer selfCallback = (message) -> { + if(message == null) return; + + message = message.toLowerCase(Locale.ROOT); + if("cancel".equalsIgnoreCase(message)) { + player.sendMessage("cancelled..."); + show(player); + return; + } + + message = message.replace('_', ' '); + + // Try to find if it already exists in a for loop + // Not the most efficient on large number of conflict, but it should not run often. + for (EnchantConflictGroup conflict : ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList()) { + if(conflict.getName().equalsIgnoreCase(message)){ + player.sendMessage("\u00A7cPlease enter a conflict name that do not already exist..."); + // wait next message. + CustomAnvil.Companion.getChatListener().setListenedCallback(player, selfRef.get()); + return; + } + } + + // Create new empty conflict and display it to the admin + EnchantConflictGroup conflict = new EnchantConflictGroup( + message, + new IncludeGroup("new_group"), + 0); + + ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList().add(conflict); + updateValueForConflict(conflict, true); + + // save empty conflict in config + String[] emptyStringArray = new String[0]; + + FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); + config.set(message+".enchantments", emptyStringArray); + config.set(message+".notAffectedGroups", emptyStringArray); + config.set(message+".maxEnchantmentBeforeConflict", 0); + + if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); + } + + // show the new conflict config to the player + this.conflictGuiMap.get(conflict).show(player); + + }; + + selfRef.set(selfCallback); + return selfCallback; } private OutlinePane createEmptyPage(){ @@ -136,7 +215,7 @@ public class EnchantConflictGui extends ChestGui { update(); } - public ItemStack createItemForConflict(EnchantConflictGroup conflict){ + public static ItemStack createItemForConflict(EnchantConflictGroup conflict){ ItemStack item = new ItemStack(conflict.getRepresentativeMaterial()); ItemMeta meta = item.getItemMeta(); diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 28c8dd0..532b4a7 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -7,14 +7,14 @@ import org.bukkit.plugin.java.JavaPlugin import xyz.alexcrea.cuanvil.command.EditConfigExecutor import xyz.alexcrea.cuanvil.command.ReloadExecutor import xyz.alexcrea.cuanvil.config.ConfigHolder +import xyz.alexcrea.cuanvil.listener.ChatEventListener import xyz.alexcrea.cuanvil.util.Metrics import xyz.alexcrea.cuanvil.util.MetricsUtil import java.io.File import java.io.FileReader /** - * Bukkit/Spigot/Paper plugin to alter enchantment max - * levels and allow unsafe enchantment combinations + * Bukkit/Spigot/Paper plugin to alter anvil feature */ class CustomAnvil : JavaPlugin() { @@ -41,6 +41,9 @@ class CustomAnvil : JavaPlugin() { // Current plugin instance lateinit var instance: CustomAnvil + // Chat message listener + lateinit var chatListener: ChatEventListener + /** * Logging handler */ @@ -74,8 +77,11 @@ class CustomAnvil : JavaPlugin() { logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus") } - // Load config + // Load chat listener + chatListener = ChatEventListener() + Bukkit.getPluginManager().registerEvents(chatListener, this) + // Load config val success = ConfigHolder.loadConfig() if(!success) return diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index 936127e..ce864ab 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -41,6 +41,7 @@ class ReloadExecutor : CommandExecutor { BasicConfigGui.INSTANCE.updateGuiValues() EnchantCostConfigGui.INSTANCE.updateGuiValues() EnchantLimitConfigGui.INSTANCE.updateGuiValues() + EnchantCostConfigGui.INSTANCE.updateGuiValues() // & update metric MetricsUtil.testIfConfigIsDefault() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index 3d6eb29..633e49c 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -18,8 +18,6 @@ class EnchantConflictManager { // Path for a flag: if the enchantment will be used in the last supported version // TODO maybe replace this system by a list of "future" enchantment. private const val FUTURE_USE_PATH = "useInFuture" - // Default name for an empty Material group - private val DEFAULT_EMPTY_GROUP = IncludeGroup("empty") // Default name for a joining group private const val DEFAULT_GROUP_NAME = "joinedGroup" } @@ -106,17 +104,11 @@ class EnchantConflictManager { } // Find or create the selected group for the conflict val groupList = section.getStringList(CONFLICT_GROUP_PATH) - val finalGroup: AbstractMaterialGroup - if(groupList.size < 1){ - finalGroup = DEFAULT_EMPTY_GROUP - }else if(groupList.size == 1){ - finalGroup = findGroup(groupList[0], itemManager, conflictName) - }else{ - finalGroup = IncludeGroup(DEFAULT_GROUP_NAME) - for (groupName in groupList) { - finalGroup.addToPolicy(findGroup(groupName, itemManager, conflictName)) - } + val finalGroup = IncludeGroup(DEFAULT_GROUP_NAME) + for (groupName in groupList) { + finalGroup.addToPolicy(findGroup(groupName, itemManager, conflictName)) } + // Return conflict return EnchantConflictGroup(conflictName, finalGroup, minBeforeBlock) } @@ -125,7 +117,7 @@ class EnchantConflictManager { val group = itemManager.get(groupName) if(group == null){ CustomAnvil.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName") - return DEFAULT_EMPTY_GROUP + return IncludeGroup("error_placeholder") } return group diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt new file mode 100644 index 0000000..a7ea8da --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt @@ -0,0 +1,47 @@ +package xyz.alexcrea.cuanvil.listener + +import io.delilaheve.CustomAnvil +import org.bukkit.Bukkit +import org.bukkit.entity.HumanEntity +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.player.AsyncPlayerChatEvent +import org.bukkit.event.player.PlayerQuitEvent +import java.util.* +import java.util.concurrent.ConcurrentHashMap +import java.util.function.Consumer + +class ChatEventListener : Listener{ + + private val playerListenMap: ConcurrentHashMap> = ConcurrentHashMap() + + fun setListenedCallback(playeruuid: UUID, callback: Consumer) { + playerListenMap[playeruuid] = callback + } + + fun setListenedCallback(player: HumanEntity, callback: Consumer) { + setListenedCallback(player.uniqueId, callback) + } + + @EventHandler + fun onQuit(event: PlayerQuitEvent) { + val eventCallback = playerListenMap.remove(event.player.uniqueId) ?: return + eventCallback.accept(null) + + } + + @EventHandler + fun onChat(event: AsyncPlayerChatEvent) { + if (event.isCancelled) return + val player = event.player + val eventCallback = playerListenMap.remove(player.uniqueId) ?: return + + event.isCancelled = true + + // sync callback with default server thread + Bukkit.getScheduler().runTask(CustomAnvil.instance, Runnable { + eventCallback.accept(event.message) + }) + } + +} \ No newline at end of file From 1b83c8db81f45527e2e0503a664514f7d4b544d9 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sun, 31 Mar 2024 00:51:58 +0100 Subject: [PATCH 16/17] Finished custom item for the conflcit config gui. Fixed multiple issue. --- .../gui/config/AbstractEnchantConfigGui.java | 1 + .../gui/config/global/EnchantConflictGui.java | 69 ++++++++++++++----- .../cuanvil/command/ReloadExecutor.kt | 3 +- .../cuanvil/group/EnchantConflictManager.kt | 3 +- src/main/resources/enchant_conflict.yml | 4 ++ 5 files changed, 60 insertions(+), 20 deletions(-) diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index b933b87..c4fa341 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -62,6 +62,7 @@ public abstract class AbstractEnchantConfigGui { + // Left item creation for consumer & bind + this.goLeftItem = new GuiItem(new ItemStack(Material.RED_TERRACOTTA), event -> { HumanEntity viewer = event.getWhoClicked(); UUID playerUUID = viewer.getUniqueId(); int page = this.pageMap.getOrDefault(playerUUID, 0); @@ -91,13 +91,13 @@ public class EnchantConflictGui extends ChestGui { ItemStack cursor = viewer.getItemOnCursor(); viewer.setItemOnCursor(new ItemStack(Material.AIR)); - viewer.closeInventory();// Hacky trick to make the inventory update properly show(viewer); viewer.setItemOnCursor(cursor); }, CustomAnvil.instance); - this.goRightItem = new GuiItem(new ItemStack(Material.PAPER), event -> { + // Right item creation for consumer & bind + this.goRightItem = new GuiItem(new ItemStack(Material.GREEN_TERRACOTTA), event -> { HumanEntity viewer = event.getWhoClicked(); UUID playerUUID = viewer.getUniqueId(); int page = pageMap.getOrDefault(playerUUID, 0); @@ -106,16 +106,22 @@ public class EnchantConflictGui extends ChestGui { ItemStack cursor = viewer.getItemOnCursor(); viewer.setItemOnCursor(new ItemStack(Material.AIR)); - viewer.closeInventory();// Hacky trick to make the inventory update properly show(viewer); viewer.setItemOnCursor(cursor); }, CustomAnvil.instance); // Create new conflict item - ItemStack createItem = new ItemStack(Material.GREEN_TERRACOTTA); + ItemStack createItem = new ItemStack(Material.PAPER); ItemMeta createMeta = createItem.getItemMeta(); + createMeta.setDisplayName("\u00A7aCreate new conflict"); + createMeta.setLore(Arrays.asList( + "\u00A77Create a new anvil restriction.", + "\u00A77You will be asked to name the conflict in chat.", + "\u00A77Then, you should edit the conflict config as you need" + )); + createItem.setItemMeta(createMeta); this.backgroundPane.bindItem('C', new GuiItem(createItem, (clickEvent)->{ @@ -145,12 +151,12 @@ public class EnchantConflictGui extends ChestGui { message = message.toLowerCase(Locale.ROOT); if("cancel".equalsIgnoreCase(message)) { - player.sendMessage("cancelled..."); + player.sendMessage("conflict creation cancelled..."); show(player); return; } - message = message.replace('_', ' '); + message = message.replace(' ', '_'); // Try to find if it already exists in a for loop // Not the most efficient on large number of conflict, but it should not run often. @@ -220,7 +226,7 @@ public class EnchantConflictGui extends ChestGui { ItemMeta meta = item.getItemMeta(); - meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(conflict.getName()) + " \u00A7rConflict"); + meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(conflict.getName()) + " \u00A7fConflict"); meta.setLore(Arrays.asList( "\u00A77Enchantment count: \u00A7e"+conflict.getEnchants().size(), "\u00A77Group count: \u00A7e"+conflict.getCantConflictGroup().getGroups().size(), @@ -246,7 +252,7 @@ public class EnchantConflictGui extends ChestGui { this.conflictGuiMap.put(conflict, gui); addToPage(guiItem); }else{ - // replace item with the updated one + // Replace item with the updated one guiItem = gui.getParentItemForThisGui(); guiItem.setItem(item); } @@ -284,11 +290,12 @@ public class EnchantConflictGui extends ChestGui { OutlinePane page = null; int pageID = 0; while(pageID < this.pages.size()){ - OutlinePane tempPage = this.pages.get(pageID++); + OutlinePane tempPage = this.pages.get(pageID); if(tempPage.getItems().contains(guiItem)){ page = tempPage; break; } + pageID++; } if(page == null){// Why... @@ -301,10 +308,14 @@ public class EnchantConflictGui extends ChestGui { page.removeItem(guiItem); // There is now a slot available, let fill it if possible - if(pageID < this.pages.size() - 1){ + if(pageID < (this.pages.size() - 1)){ OutlinePane newPage = this.pages.get(pageID+1); - GuiItem newItem = newPage.getItems().get(0); - removeFromPage(newPage, pageID+1, newItem); + GuiItem nextPageItem = newPage.getItems().get(0); + + removeFromPage(newPage, pageID+1, nextPageItem); + + OutlinePane thisPage = this.pages.get(pageID); + thisPage.addItem(nextPageItem); }else if(pageID > 0 && page.getItems().isEmpty()){ this.pages.remove(pageID); } @@ -319,10 +330,21 @@ public class EnchantConflictGui extends ChestGui { return pageId; } - public void placeArrow(int page){ + public void placeArrow(int page, boolean customise){ + // Place left arrow addPane(this.backgroundPane); if(page > 0){ + if(customise){ + ItemStack leftItem = this.goLeftItem.getItem(); + ItemMeta leftMeta = leftItem.getItemMeta(); + + leftMeta.setDisplayName("\u00A7eReturn to page " +(page)); + + leftItem.setItemMeta(leftMeta); + this.goLeftItem.setItem(leftItem); + } + this.backgroundPane.bindItem('L', this.goLeftItem); }else{ this.backgroundPane.bindItem('L', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); @@ -330,6 +352,16 @@ public class EnchantConflictGui extends ChestGui { // Place right arrow if(page < pages.size()-1){ + if(customise){ + ItemStack rightItem = this.goRightItem.getItem(); + ItemMeta rightMeta = rightItem.getItemMeta(); + + rightMeta.setDisplayName("\u00A7eGo to page " +(page+2)); + + rightItem.setItemMeta(rightMeta); + this.goRightItem.setItem(rightItem); + } + this.backgroundPane.bindItem('R', this.goRightItem); }else{ this.backgroundPane.bindItem('R', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); @@ -344,10 +376,13 @@ public class EnchantConflictGui extends ChestGui { getPanes().clear(); // display the page arrow pane - placeArrow(pageID); + placeArrow(pageID, true); // and add actual page addPane(page); + // set title + setTitle("Conflict Config ("+(pageID+1)+"/"+(pages.size())+")"); + super.show(humanEntity); } @@ -360,7 +395,7 @@ public class EnchantConflictGui extends ChestGui { getPanes().clear(); // set the page arrow pane - placeArrow(pageID); + placeArrow(pageID, false); // and add actual page addPane(page); diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index ce864ab..4268cbf 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -6,6 +6,7 @@ import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.gui.config.global.BasicConfigGui +import xyz.alexcrea.cuanvil.gui.config.global.EnchantConflictGui import xyz.alexcrea.cuanvil.gui.config.global.EnchantCostConfigGui import xyz.alexcrea.cuanvil.gui.config.global.EnchantLimitConfigGui import xyz.alexcrea.cuanvil.util.MetricsUtil @@ -41,7 +42,7 @@ class ReloadExecutor : CommandExecutor { BasicConfigGui.INSTANCE.updateGuiValues() EnchantCostConfigGui.INSTANCE.updateGuiValues() EnchantLimitConfigGui.INSTANCE.updateGuiValues() - EnchantCostConfigGui.INSTANCE.updateGuiValues() + EnchantConflictGui.INSTANCE.reloadValues() // & update metric MetricsUtil.testIfConfigIsDefault() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index 633e49c..c5d8c8c 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -84,9 +84,8 @@ class EnchantConflictManager { } if(conflict.getEnchants().size == 0){ if(!futureUse){ - CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not work") + CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not do anything") } - return null } return conflict diff --git a/src/main/resources/enchant_conflict.yml b/src/main/resources/enchant_conflict.yml index c5fea81..86492f4 100644 --- a/src/main/resources/enchant_conflict.yml +++ b/src/main/resources/enchant_conflict.yml @@ -232,3 +232,7 @@ crossbow_conflict: notAffectedGroups: [] maxEnchantmentBeforeConflict: 1 +# ---------------------------------------------------- +# Bellow is for custom conflicts. +# This is also where conflict create from the gui will be placed. +# ---------------------------------------------------- From 00fba2f4b0ff177bd721276f2d78fd4cf279095a Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sun, 31 Mar 2024 00:59:50 +0100 Subject: [PATCH 17/17] alpha version up & cleanup code. --- build.gradle.kts | 2 +- .../alexcrea/cuanvil/config/ConfigHolder.java | 80 ++++--- .../enchant/EnchantmentProperties.java | 7 +- .../cuanvil/enchant/EnchantmentRarity.java | 11 +- .../gui/config/AbstractEnchantConfigGui.java | 10 +- .../cuanvil/gui/config/ConfirmActionGui.java | 9 +- .../gui/config/global/BasicConfigGui.java | 24 +-- .../gui/config/global/EnchantConflictGui.java | 94 +++++---- .../config/global/EnchantCostConfigGui.java | 7 +- .../config/global/EnchantLimitConfigGui.java | 4 +- .../gui/config/global/MainConfigGui.java | 5 +- .../config/settings/AbstractSettingGui.java | 25 ++- .../gui/config/settings/BoolSettingsGui.java | 53 ++--- .../settings/EnchantCostSettingsGui.java | 100 ++++----- .../settings/EnchantSelectSettingGui.java | 32 +-- .../settings/GroupSelectSettingGui.java | 33 +-- .../gui/config/settings/IntSettingsGui.java | 123 ++++++----- .../EnchantConflictSubSettingGui.java | 66 +++--- .../cuanvil/gui/util/GuiGlobalActions.java | 27 ++- .../cuanvil/gui/util/GuiGlobalItems.java | 81 ++++--- .../cuanvil/gui/util/GuiSharedConstant.java | 1 + .../cuanvil/util/CasedStringUtil.java | 21 +- .../io/delilaheve/AnvilEventListener.kt | 198 ++++++++++-------- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 27 ++- .../io/delilaheve/util/ConfigOptions.kt | 33 ++- .../io/delilaheve/util/EnchantmentUtil.kt | 34 +-- .../kotlin/io/delilaheve/util/ItemUtil.kt | 8 +- .../cuanvil/command/EditConfigExecutor.kt | 4 +- .../cuanvil/command/ReloadExecutor.kt | 16 +- .../cuanvil/group/AbstractMaterialGroup.kt | 21 +- .../cuanvil/group/EnchantConflictGroup.kt | 23 +- .../cuanvil/group/EnchantConflictManager.kt | 78 ++++--- .../alexcrea/cuanvil/group/ExcludeGroup.kt | 8 +- .../alexcrea/cuanvil/group/IncludeGroup.kt | 8 +- .../cuanvil/group/ItemGroupManager.kt | 70 ++++--- .../cuanvil/listener/ChatEventListener.kt | 2 +- .../xyz/alexcrea/cuanvil/util/MetricsUtil.kt | 73 ++++--- .../alexcrea/cuanvil/util/UnitRepairUtil.kt | 21 +- src/main/resources/enchant_conflict.yml | 168 +++++++-------- src/main/resources/plugin.yml | 4 +- 40 files changed, 893 insertions(+), 718 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8d9b438..0a71e7f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "xyz.alexcrea" -version = "1.3.2-A1" +version = "1.3.2-A2" repositories { mavenCentral() diff --git a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java index 6c25230..b7f351b 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java +++ b/src/main/java/xyz/alexcrea/cuanvil/config/ConfigHolder.java @@ -19,27 +19,27 @@ public abstract class ConfigHolder { public static ConflictConfigHolder CONFLICT_HOLDER; public static UnitRepairHolder UNIT_REPAIR_HOLDER; - public static boolean loadConfig(){ + public static boolean loadConfig() { DEFAULT_CONFIG = new DefaultConfigHolder(); ITEM_GROUP_HOLDER = new ItemGroupConfigHolder(); CONFLICT_HOLDER = new ConflictConfigHolder(); UNIT_REPAIR_HOLDER = new UnitRepairHolder(); boolean result = reloadAllFromDisk(true); - if(result){ + if (result) { MetricsUtil.INSTANCE.testIfConfigIsDefault(); } return result; } - public static boolean reloadAllFromDisk(boolean hardfail){ + public static boolean reloadAllFromDisk(boolean hardfail) { boolean sucess = DEFAULT_CONFIG.reloadFromDisk(hardfail); - if(!sucess) return false; + if (!sucess) return false; sucess = ITEM_GROUP_HOLDER.reloadFromDisk(hardfail); - if(!sucess) return false; + if (!sucess) return false; sucess = CONFLICT_HOLDER.reloadFromDisk(hardfail); - if(!sucess) return false; + if (!sucess) return false; sucess = UNIT_REPAIR_HOLDER.reloadFromDisk(hardfail); return sucess; } @@ -49,43 +49,49 @@ public abstract class ConfigHolder { private static final File BACKUP_FOLDER = new File(CustomAnvil.instance.getDataFolder(), "backup"); protected FileConfiguration configuration; - protected ConfigHolder(){ + + protected ConfigHolder() { } public abstract boolean reloadFromDisk(boolean hardFail); + public abstract void reload(); - public FileConfiguration getConfig(){ + + public FileConfiguration getConfig() { return configuration; } // Config name and files protected abstract String getConfigFileName(); - protected String getConfigFileExtension(){ + protected String getConfigFileExtension() { return ".yml"; } - protected File getConfigFile(){ - return new File(CustomAnvil.instance.getDataFolder(), getConfigFileName()+getConfigFileExtension()); + + protected File getConfigFile() { + return new File(CustomAnvil.instance.getDataFolder(), getConfigFileName() + getConfigFileExtension()); } - protected File getFirstBackup(){ - return new File(BACKUP_FOLDER, getConfigFileName()+"-first"+getConfigFileExtension()); + + protected File getFirstBackup() { + return new File(BACKUP_FOLDER, getConfigFileName() + "-first" + getConfigFileExtension()); } - protected File getLastBackup(){ - return new File(BACKUP_FOLDER, getConfigFileName()+"-latest"+getConfigFileExtension()); + + protected File getLastBackup() { + return new File(BACKUP_FOLDER, getConfigFileName() + "-latest" + getConfigFileExtension()); } // Save logic - public boolean saveToDisk(boolean doBackup){ - if(doBackup){ - if(!saveBackup()){ + public boolean saveToDisk(boolean doBackup) { + if (doBackup) { + if (!saveBackup()) { CustomAnvil.instance.getLogger().severe("Could not save backup. see above."); return false; } } File base = getConfigFile(); // if file exist and can't be deleted the file, then we gave up. - if(base.exists() && !base.delete()) { + if (base.exists() && !base.delete()) { CustomAnvil.instance.getLogger().severe("Could not save config: can't delete existing file."); return false; } @@ -101,15 +107,15 @@ public abstract class ConfigHolder { return true; } - protected boolean saveBackup(){ + protected boolean saveBackup() { File base = getConfigFile(); - if(!base.exists()) return true; // We did back up everything we had to (nothing in this case) + if (!base.exists()) return true; // We did back up everything we had to (nothing in this case) boolean sufficientSuccess = false; BACKUP_FOLDER.mkdirs(); // save first backup if do not exist File firstBackup = getFirstBackup(); - if(!firstBackup.exists()){ + if (!firstBackup.exists()) { try { Files.copy(base, firstBackup); sufficientSuccess = true; @@ -120,7 +126,7 @@ public abstract class ConfigHolder { // save last backup File lastBackup = getLastBackup(); // if file exist and can't be deleted the file, then we gave up. - if(lastBackup.exists() && !lastBackup.delete()){ + if (lastBackup.exists() && !lastBackup.delete()) { return sufficientSuccess; } @@ -134,7 +140,7 @@ public abstract class ConfigHolder { return sufficientSuccess; } - public static class DefaultConfigHolder extends ConfigHolder{ + public static class DefaultConfigHolder extends ConfigHolder { @Override protected String getConfigFileName() { @@ -150,15 +156,17 @@ public abstract class ConfigHolder { } @Override - public void reload() {}// Nothing to do + public void reload() { + }// Nothing to do } // Abstract class for non default config - public abstract static class ResourceConfigHolder extends ConfigHolder{ + public abstract static class ResourceConfigHolder extends ConfigHolder { String resourceName; - private ResourceConfigHolder(String resourceName){ + + private ResourceConfigHolder(String resourceName) { this.resourceName = resourceName; } @@ -170,8 +178,8 @@ public abstract class ConfigHolder { @Override public boolean reloadFromDisk(boolean hardFail) { YamlConfiguration configuration = CustomAnvil.instance.reloadResource( - getConfigFileName()+getConfigFileExtension(), hardFail); - if(configuration == null) return false; + getConfigFileName() + getConfigFileExtension(), hardFail); + if (configuration == null) return false; this.configuration = configuration; reload(); return true; @@ -180,10 +188,11 @@ public abstract class ConfigHolder { } // Class for itemGroupsManager config - public static class ItemGroupConfigHolder extends ResourceConfigHolder{ + public static class ItemGroupConfigHolder extends ResourceConfigHolder { private final static String FILE_NAME = "item_groups"; ItemGroupManager itemGroupsManager; + private ItemGroupConfigHolder() { super(FILE_NAME); } @@ -198,7 +207,7 @@ public abstract class ConfigHolder { this.itemGroupsManager = new ItemGroupManager(); this.itemGroupsManager.prepareGroups(this.configuration); - if(CONFLICT_HOLDER.getConfig() != null){ + if (CONFLICT_HOLDER.getConfig() != null) { CONFLICT_HOLDER.reload(); } } @@ -206,10 +215,11 @@ public abstract class ConfigHolder { } // Class for enchant conflict config - public static class ConflictConfigHolder extends ResourceConfigHolder{ + public static class ConflictConfigHolder extends ResourceConfigHolder { private final static String FILE_NAME = "enchant_conflict"; EnchantConflictManager conflictManager; + private ConflictConfigHolder() { super(FILE_NAME); } @@ -229,14 +239,16 @@ public abstract class ConfigHolder { } // Class for unit repair config - public static class UnitRepairHolder extends ResourceConfigHolder{ + public static class UnitRepairHolder extends ResourceConfigHolder { private final static String ITEM_GROUP_FILE_NAME = "unit_repair_item"; private UnitRepairHolder() { super(ITEM_GROUP_FILE_NAME); } + @Override - public void reload() {} // Do nothing + public void reload() { + } // Do nothing } diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java index cb481df..eb217b9 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentProperties.java @@ -41,12 +41,11 @@ public enum EnchantmentProperties { SWEEPING(EnchantmentRarity.RARE), THORNS(EnchantmentRarity.VERY_RARE), UNBREAKING(EnchantmentRarity.UNCOMMON), - VANISHING_CURSE(EnchantmentRarity.VERY_RARE) - - ; + VANISHING_CURSE(EnchantmentRarity.VERY_RARE); private final EnchantmentRarity rarity; - EnchantmentProperties(EnchantmentRarity rarity){ + + EnchantmentProperties(EnchantmentRarity rarity) { this.rarity = rarity; } diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java index e1ff834..50fdfdf 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/EnchantmentRarity.java @@ -7,19 +7,18 @@ public enum EnchantmentRarity { COMMON(1), UNCOMMON(2), RARE(4), - VERY_RARE(8) - - ; + VERY_RARE(8); private final int itemValue; private final int bookValue; - EnchantmentRarity(int itemValue, int bookValue){ + EnchantmentRarity(int itemValue, int bookValue) { this.itemValue = itemValue; this.bookValue = bookValue; } - EnchantmentRarity(int itemValue){ - this(itemValue, Math.max(1,itemValue/2)); + + EnchantmentRarity(int itemValue) { + this(itemValue, Math.max(1, itemValue / 2)); } public int getBookValue() { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java index c4fa341..da8e34d 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/AbstractEnchantConfigGui.java @@ -14,15 +14,17 @@ import java.util.List; /** * Abstract Global Config gui for enchantment setting configuration. + * * @param Type of the factory of the type of setting the gui should edit. */ public abstract class AbstractEnchantConfigGui extends ValueUpdatableGui { /** * Constructor for a gui displaying available enchantment to edit a enchantment setting. + * * @param title Title of the gui. */ - protected AbstractEnchantConfigGui(String title){ + protected AbstractEnchantConfigGui(String title) { super(6, title, CustomAnvil.instance); } @@ -30,8 +32,8 @@ public abstract class AbstractEnchantConfigGui(); for (Enchantment enchant : GuiSharedConstant.SORTED_ENCHANTMENT_LIST) { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java index 7ae180b..96f08c9 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/ConfirmActionGui.java @@ -24,6 +24,7 @@ public class ConfirmActionGui extends ChestGui { private static final ItemStack CANCEL_ITEM; private static final ItemStack CONFIRM_ITEM; + static { CANCEL_ITEM = new ItemStack(Material.RED_TERRACOTTA); ItemMeta meta = CANCEL_ITEM.getItemMeta(); @@ -58,21 +59,21 @@ public class ConfirmActionGui extends ChestGui { event.setCancelled(true); HumanEntity player = event.getWhoClicked(); - if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + if (!player.hasPermission(CustomAnvil.editConfigPermission)) { player.closeInventory(); player.sendMessage(GuiGlobalActions.NO_EDIT_PERM); return; } boolean success; - try{ + try { success = onConfirm.get(); - }catch (Exception e){ + } catch (Exception e) { CustomAnvil.instance.getLogger().log(Level.WARNING, "Could not process confirmation supplier.", e); success = false; } - if(!success){ + if (!success) { event.getWhoClicked().sendMessage("\u00A7cAction could not be completed. "); } backOnConfirm.show(player); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java index 75e898a..89c4c34 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/BasicConfigGui.java @@ -33,7 +33,7 @@ public class BasicConfigGui extends ValueUpdatableGui { /** * Constructor of this Global gui for basic settings. */ - private BasicConfigGui(){ + private BasicConfigGui() { super(3, "\u00A78Basic Config", CustomAnvil.instance); } @@ -42,7 +42,7 @@ public class BasicConfigGui extends ValueUpdatableGui { /** * Initialise Basic gui */ - private void init(){ + private void init() { Pattern pattern = new Pattern( GuiSharedConstant.EMPTY_GUI_FULL_LINE, "012345670", @@ -70,15 +70,15 @@ public class BasicConfigGui extends ValueUpdatableGui { /** * Prepare basic gui displayed items factory and static items.. */ - protected void prepareValues(){ + protected void prepareValues() { // limit repair item - this.limitRepairFactory = BoolSettingsGui.boolFactory("\u00A78Limit Repair Cost ?",this, + this.limitRepairFactory = BoolSettingsGui.boolFactory("\u00A78Limit Repair Cost ?", this, ConfigOptions.LIMIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, ConfigOptions.DEFAULT_LIMIT_REPAIR); // rename cost item IntRange range = ConfigOptions.REPAIR_LIMIT_RANGE; this.repairCostFactory = IntSettingsGui.intFactory("\u00A78Repair Cost Limit", this, - ConfigOptions.LIMIT_REPAIR_VALUE, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), + ConfigOptions.LIMIT_REPAIR_VALUE, ConfigHolder.DEFAULT_CONFIG, range.getFirst(), range.getLast(), ConfigOptions.DEFAULT_LIMIT_REPAIR_VALUE, 1, 5, 10); @@ -92,33 +92,33 @@ public class BasicConfigGui extends ValueUpdatableGui { this.notNeededLimitValueItem = new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance); // remove repair limit item - this.removeRepairLimit = BoolSettingsGui.boolFactory("\u00A78Remove Repair Limit ?",this, + this.removeRepairLimit = BoolSettingsGui.boolFactory("\u00A78Remove Repair Limit ?", this, ConfigOptions.REMOVE_REPAIR_LIMIT, ConfigHolder.DEFAULT_CONFIG, ConfigOptions.DEFAULT_REMOVE_LIMIT); // item repair cost range = ConfigOptions.REPAIR_COST_RANGE; this.itemRepairCost = IntSettingsGui.intFactory("\u00A78Item Repair Cost", this, - ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), + ConfigOptions.ITEM_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(), range.getLast(), ConfigOptions.DEFAULT_ITEM_REPAIR_COST, 1, 5, 10, 50, 100); // unit repair cost this.unitRepairCost = IntSettingsGui.intFactory("\u00A78Unit Repair Cost", this, - ConfigOptions.UNIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), + ConfigOptions.UNIT_REPAIR_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(), range.getLast(), ConfigOptions.DEFAULT_UNIT_REPAIR_COST, 1, 5, 10, 50, 100); // item rename cost range = ConfigOptions.ITEM_RENAME_COST_RANGE; this.itemRenameCost = IntSettingsGui.intFactory("\u00A78Rename Cost", this, - ConfigOptions.ITEM_RENAME_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), + ConfigOptions.ITEM_RENAME_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(), range.getLast(), ConfigOptions.DEFAULT_ITEM_RENAME_COST, 1, 5, 10, 50, 100); // sacrifice illegal enchant cost range = ConfigOptions.SACRIFICE_ILLEGAL_COST_RANGE; this.sacrificeIllegalEnchantCost = IntSettingsGui.intFactory("\u00A78Sacrifice Illegal Enchant Cost", this, - ConfigOptions.SACRIFICE_ILLEGAL_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(),range.getLast(), + ConfigOptions.SACRIFICE_ILLEGAL_COST, ConfigHolder.DEFAULT_CONFIG, range.getFirst(), range.getLast(), ConfigOptions.DEFAULT_SACRIFICE_ILLEGAL_COST, 1, 5, 10, 50, 100); @@ -132,9 +132,9 @@ public class BasicConfigGui extends ValueUpdatableGui { // rename cost item GuiItem limitRepairValueItem; - if(this.limitRepairFactory.getConfiguredValue()){ + if (this.limitRepairFactory.getConfiguredValue()) { limitRepairValueItem = GuiGlobalItems.intSettingGuiItem(this.repairCostFactory, Material.EXPERIENCE_BOTTLE); - }else{ + } else { limitRepairValueItem = this.notNeededLimitValueItem; } pane.bindItem('2', limitRepairValueItem); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java index a186c27..f5cadf6 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantConflictGui.java @@ -31,6 +31,7 @@ import java.util.function.Consumer; public class EnchantConflictGui extends ChestGui { public final static EnchantConflictGui INSTANCE = new EnchantConflictGui(); + static { INSTANCE.init(); } @@ -47,7 +48,7 @@ public class EnchantConflictGui extends ChestGui { private HashMap pageMap; private PatternPane backgroundPane; - private void init(){ + private void init() { // Back item panel Pattern pattern = new Pattern( GuiSharedConstant.EMPTY_GUI_FULL_LINE, @@ -80,13 +81,14 @@ public class EnchantConflictGui extends ChestGui { private GuiItem goLeftItem; private GuiItem goRightItem; + private void prepareOtherValues() { // Left item creation for consumer & bind this.goLeftItem = new GuiItem(new ItemStack(Material.RED_TERRACOTTA), event -> { HumanEntity viewer = event.getWhoClicked(); UUID playerUUID = viewer.getUniqueId(); int page = this.pageMap.getOrDefault(playerUUID, 0); - this.pageMap.put(playerUUID, page-1); + this.pageMap.put(playerUUID, page - 1); ItemStack cursor = viewer.getItemOnCursor(); viewer.setItemOnCursor(new ItemStack(Material.AIR)); @@ -101,7 +103,7 @@ public class EnchantConflictGui extends ChestGui { HumanEntity viewer = event.getWhoClicked(); UUID playerUUID = viewer.getUniqueId(); int page = pageMap.getOrDefault(playerUUID, 0); - this.pageMap.put(playerUUID, page+1); + this.pageMap.put(playerUUID, page + 1); ItemStack cursor = viewer.getItemOnCursor(); viewer.setItemOnCursor(new ItemStack(Material.AIR)); @@ -124,12 +126,12 @@ public class EnchantConflictGui extends ChestGui { createItem.setItemMeta(createMeta); - this.backgroundPane.bindItem('C', new GuiItem(createItem, (clickEvent)->{ + this.backgroundPane.bindItem('C', new GuiItem(createItem, (clickEvent) -> { clickEvent.setCancelled(true); HumanEntity player = clickEvent.getWhoClicked(); // check permission - if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + if (!player.hasPermission(CustomAnvil.editConfigPermission)) { player.closeInventory(); player.sendMessage(GuiGlobalActions.NO_EDIT_PERM); return; @@ -147,10 +149,10 @@ public class EnchantConflictGui extends ChestGui { private Consumer prepareCreateItemConsumer(HumanEntity player) { AtomicReference> selfRef = new AtomicReference<>(); Consumer selfCallback = (message) -> { - if(message == null) return; + if (message == null) return; message = message.toLowerCase(Locale.ROOT); - if("cancel".equalsIgnoreCase(message)) { + if ("cancel".equalsIgnoreCase(message)) { player.sendMessage("conflict creation cancelled..."); show(player); return; @@ -161,7 +163,7 @@ public class EnchantConflictGui extends ChestGui { // Try to find if it already exists in a for loop // Not the most efficient on large number of conflict, but it should not run often. for (EnchantConflictGroup conflict : ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList()) { - if(conflict.getName().equalsIgnoreCase(message)){ + if (conflict.getName().equalsIgnoreCase(message)) { player.sendMessage("\u00A7cPlease enter a conflict name that do not already exist..."); // wait next message. CustomAnvil.Companion.getChatListener().setListenedCallback(player, selfRef.get()); @@ -182,11 +184,11 @@ public class EnchantConflictGui extends ChestGui { String[] emptyStringArray = new String[0]; FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); - config.set(message+".enchantments", emptyStringArray); - config.set(message+".notAffectedGroups", emptyStringArray); - config.set(message+".maxEnchantmentBeforeConflict", 0); + config.set(message + ".enchantments", emptyStringArray); + config.set(message + ".notAffectedGroups", emptyStringArray); + config.set(message + ".maxEnchantmentBeforeConflict", 0); - if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } @@ -199,7 +201,7 @@ public class EnchantConflictGui extends ChestGui { return selfCallback; } - private OutlinePane createEmptyPage(){ + private OutlinePane createEmptyPage() { OutlinePane page = new OutlinePane(0, 0, 9, 5); page.align(OutlinePane.Alignment.BEGIN); page.setOrientation(Orientable.Orientation.HORIZONTAL); @@ -207,7 +209,7 @@ public class EnchantConflictGui extends ChestGui { return page; } - public void reloadValues(){ + public void reloadValues() { this.conflictGuiMap.forEach((conflict, gui) -> gui.cleanUnused()); this.conflictGuiMap.clear(); this.firstPage.clear(); @@ -221,28 +223,28 @@ public class EnchantConflictGui extends ChestGui { update(); } - public static ItemStack createItemForConflict(EnchantConflictGroup conflict){ + public static ItemStack createItemForConflict(EnchantConflictGroup conflict) { ItemStack item = new ItemStack(conflict.getRepresentativeMaterial()); ItemMeta meta = item.getItemMeta(); meta.setDisplayName("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(conflict.getName()) + " \u00A7fConflict"); meta.setLore(Arrays.asList( - "\u00A77Enchantment count: \u00A7e"+conflict.getEnchants().size(), - "\u00A77Group count: \u00A7e"+conflict.getCantConflictGroup().getGroups().size(), - "\u00A77Min enchantments count: \u00A7e"+conflict.getMinBeforeBlock() + "\u00A77Enchantment count: \u00A7e" + conflict.getEnchants().size(), + "\u00A77Group count: \u00A7e" + conflict.getCantConflictGroup().getGroups().size(), + "\u00A77Min enchantments count: \u00A7e" + conflict.getMinBeforeBlock() )); item.setItemMeta(meta); return item; } - public void updateValueForConflict(EnchantConflictGroup conflict, boolean shouldUpdate){ + public void updateValueForConflict(EnchantConflictGroup conflict, boolean shouldUpdate) { EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict); ItemStack item = createItemForConflict(conflict); GuiItem guiItem; - if(gui == null){ + if (gui == null) { // Create new sub setting gui guiItem = new GuiItem(item, CustomAnvil.instance); gui = new EnchantConflictSubSettingGui(this, conflict, guiItem); @@ -251,22 +253,22 @@ public class EnchantConflictGui extends ChestGui { this.conflictGuiMap.put(conflict, gui); addToPage(guiItem); - }else{ + } else { // Replace item with the updated one guiItem = gui.getParentItemForThisGui(); guiItem.setItem(item); } gui.updateLocal(); - if(shouldUpdate){ + if (shouldUpdate) { update(); } } - public void removeConflict(EnchantConflictGroup conflict){ + public void removeConflict(EnchantConflictGroup conflict) { EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict); - if(gui == null) return; + if (gui == null) return; this.conflictGuiMap.remove(conflict); removeFromPage(gui.getParentItemForThisGui()); @@ -276,8 +278,8 @@ public class EnchantConflictGui extends ChestGui { private void addToPage(GuiItem guiItem) { // Get first available page or create one - OutlinePane page = this.pages.get(this.pages.size()-1); - if(page.getItems().size() >= 5*9){ + OutlinePane page = this.pages.get(this.pages.size() - 1); + if (page.getItems().size() >= 5 * 9) { page = createEmptyPage(); this.pages.add(page); } @@ -289,16 +291,16 @@ public class EnchantConflictGui extends ChestGui { // get item page OutlinePane page = null; int pageID = 0; - while(pageID < this.pages.size()){ + while (pageID < this.pages.size()) { OutlinePane tempPage = this.pages.get(pageID); - if(tempPage.getItems().contains(guiItem)){ + if (tempPage.getItems().contains(guiItem)) { page = tempPage; break; } pageID++; } - if(page == null){// Why... + if (page == null) {// Why... return; } removeFromPage(page, pageID, guiItem); @@ -308,62 +310,62 @@ public class EnchantConflictGui extends ChestGui { page.removeItem(guiItem); // There is now a slot available, let fill it if possible - if(pageID < (this.pages.size() - 1)){ - OutlinePane newPage = this.pages.get(pageID+1); + if (pageID < (this.pages.size() - 1)) { + OutlinePane newPage = this.pages.get(pageID + 1); GuiItem nextPageItem = newPage.getItems().get(0); - removeFromPage(newPage, pageID+1, nextPageItem); + removeFromPage(newPage, pageID + 1, nextPageItem); OutlinePane thisPage = this.pages.get(pageID); thisPage.addItem(nextPageItem); - }else if(pageID > 0 && page.getItems().isEmpty()){ + } else if (pageID > 0 && page.getItems().isEmpty()) { this.pages.remove(pageID); } } - public int getPlayerPageID(UUID uuid){ + public int getPlayerPageID(UUID uuid) { int pageId = this.pageMap.getOrDefault(uuid, 0); - if(pageId >= this.pages.size()){ - pageId = this.pages.size()-1; + if (pageId >= this.pages.size()) { + pageId = this.pages.size() - 1; } return pageId; } - public void placeArrow(int page, boolean customise){ + public void placeArrow(int page, boolean customise) { // Place left arrow addPane(this.backgroundPane); - if(page > 0){ - if(customise){ + if (page > 0) { + if (customise) { ItemStack leftItem = this.goLeftItem.getItem(); ItemMeta leftMeta = leftItem.getItemMeta(); - leftMeta.setDisplayName("\u00A7eReturn to page " +(page)); + leftMeta.setDisplayName("\u00A7eReturn to page " + (page)); leftItem.setItemMeta(leftMeta); this.goLeftItem.setItem(leftItem); } this.backgroundPane.bindItem('L', this.goLeftItem); - }else{ + } else { this.backgroundPane.bindItem('L', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); } // Place right arrow - if(page < pages.size()-1){ - if(customise){ + if (page < pages.size() - 1) { + if (customise) { ItemStack rightItem = this.goRightItem.getItem(); ItemMeta rightMeta = rightItem.getItemMeta(); - rightMeta.setDisplayName("\u00A7eGo to page " +(page+2)); + rightMeta.setDisplayName("\u00A7eGo to page " + (page + 2)); rightItem.setItemMeta(rightMeta); this.goRightItem.setItem(rightItem); } this.backgroundPane.bindItem('R', this.goRightItem); - }else{ + } else { this.backgroundPane.bindItem('R', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM); } } @@ -381,7 +383,7 @@ public class EnchantConflictGui extends ChestGui { addPane(page); // set title - setTitle("Conflict Config ("+(pageID+1)+"/"+(pages.size())+")"); + setTitle("Conflict Config (" + (pageID + 1) + "/" + (pages.size()) + ")"); super.show(humanEntity); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java index e757d5f..2c2427b 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/global/EnchantCostConfigGui.java @@ -46,10 +46,11 @@ public class EnchantCostConfigGui extends AbstractEnchantConfigGui { event.setCancelled(true); event.getWhoClicked().closeInventory(); - },CustomAnvil.instance); + }, CustomAnvil.instance); pane.bindItem('Q', quitItem); // create & bind "info" item diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java index 65fa446..21a291f 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/AbstractSettingGui.java @@ -26,8 +26,9 @@ public abstract class AbstractSettingGui extends ChestGui { /** * Prepare necessary object for a setting gui. - * @param rows Number of row for this gui. - * @param title Title of this gui. + * + * @param rows Number of row for this gui. + * @param title Title of this gui. * @param parent Parent gui to go back when completed. */ public AbstractSettingGui(int rows, @NotNull TextHolder title, ValueUpdatableGui parent) { @@ -37,8 +38,9 @@ public abstract class AbstractSettingGui extends ChestGui { /** * Prepare necessary object for a setting gui. - * @param rows Number of row for this gui. - * @param title Title of this gui. + * + * @param rows Number of row for this gui. + * @param title Title of this gui. * @param parent Parent gui to go back when completed. */ public AbstractSettingGui(int rows, @NotNull String title, ValueUpdatableGui parent) { @@ -50,9 +52,10 @@ public abstract class AbstractSettingGui extends ChestGui { /** * Initialise and prepare value for this gui. + * * @param parent Parent gui to go back when completed. */ - private void initBase(ValueUpdatableGui parent){ + private void initBase(ValueUpdatableGui parent) { Pattern pattern = getGuiPattern(); pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern); addPane(pane); @@ -75,6 +78,7 @@ public abstract class AbstractSettingGui extends ChestGui { /** * Get main pane for this setting gui. + * * @return Main pattern pain of this gui. */ protected PatternPane getPane() { @@ -89,12 +93,14 @@ public abstract class AbstractSettingGui extends ChestGui { *
  • B: "back to previous gui" button.
  • *
  • 0: default background item.
  • * + * * @return The gui's pattern. */ protected abstract Pattern getGuiPattern(); /** * Called when the associated setting need to be saved. + * * @return true if the save was successful. false otherwise */ public abstract boolean onSave(); @@ -102,6 +108,7 @@ public abstract class AbstractSettingGui extends ChestGui { /** * If this function return true * the gui assume the associated setting can be saved. + * * @return true if there is a change to the setting. false otherwise */ public abstract boolean hadChange(); @@ -111,16 +118,17 @@ public abstract class AbstractSettingGui extends ChestGui { *

    * It is better to keep a factory that hold setting data than find what parameters to use every time. */ - public abstract static class SettingGuiFactory{ + public abstract static class SettingGuiFactory { protected String configPath; protected ConfigHolder config; /** * Constructor for settings gui factory + * * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. + * @param config Configuration holder of this setting. */ - protected SettingGuiFactory(String configPath, ConfigHolder config){ + protected SettingGuiFactory(String configPath, ConfigHolder config) { this.configPath = configPath; this.config = config; } @@ -141,6 +149,7 @@ public abstract class AbstractSettingGui extends ChestGui { /** * Create a gui using setting parameters and current setting value. + * * @return A new instance of the implemented setting gui. */ public abstract AbstractSettingGui create(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java index 909a179..f65d5cf 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/BoolSettingsGui.java @@ -21,7 +21,7 @@ import java.util.function.Consumer; /** * An instance of a gui used to edit a boolean setting. */ -public class BoolSettingsGui extends AbstractSettingGui{ +public class BoolSettingsGui extends AbstractSettingGui { private final BoolSettingFactory holder; private final boolean before; @@ -29,8 +29,9 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * Create a boolean setting config gui. + * * @param holder Configuration factory of this setting. - * @param now The defined value of this setting. + * @param now The defined value of this setting. */ protected BoolSettingsGui(BoolSettingFactory holder, boolean now) { super(3, holder.getTitle(), holder.parent); @@ -57,12 +58,12 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * Prepare "return to default value" gui item. */ - protected void prepareReturnToDefault(){ + protected void prepareReturnToDefault() { ItemStack item = new ItemStack(Material.COMMAND_BLOCK); ItemMeta meta = item.getItemMeta(); meta.setDisplayName("\u00A7eReset to default value"); - meta.setLore(Collections.singletonList("\u00A77Default value is: "+holder.defaultVal)); + meta.setLore(Collections.singletonList("\u00A77Default value is: " + holder.defaultVal)); item.setItemMeta(meta); returnToDefault = new GuiItem(item, event -> { event.setCancelled(true); @@ -75,16 +76,16 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * Update item using the setting value to match the new value */ - protected void updateValueDisplay(){ + protected void updateValueDisplay() { PatternPane pane = getPane(); // Get displayed value for this config. String displayedName; Material displayedMat; - if(now){ + if (now) { displayedName = "\u00A7aTrue"; displayedMat = Material.GREEN_TERRACOTTA; - }else{ + } else { displayedName = "\u00A7cFalse"; displayedMat = Material.RED_TERRACOTTA; } @@ -100,9 +101,9 @@ public class BoolSettingsGui extends AbstractSettingGui{ // reset to default GuiItem returnToDefault; - if(now != holder.defaultVal){ + if (now != holder.defaultVal) { returnToDefault = this.returnToDefault; - }else{ + } else { returnToDefault = GuiGlobalItems.backgroundItem(); } pane.bindItem('D', returnToDefault); @@ -112,8 +113,8 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * @return A consumer to update the current setting's value. */ - protected Consumer inverseNowConsumer(){ - return event->{ + protected Consumer inverseNowConsumer() { + return event -> { event.setCancelled(true); now = !now; updateValueDisplay(); @@ -126,7 +127,7 @@ public class BoolSettingsGui extends AbstractSettingGui{ holder.config.getConfig().set(holder.configPath, now); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); - if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { return holder.config.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; @@ -139,18 +140,19 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * Create a bool setting factory from setting's parameters. - * @param title The title of the gui. - * @param parent Parent gui to go back when completed. + * + * @param title The title of the gui. + * @param parent Parent gui to go back when completed. * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. + * @param config Configuration holder of this setting. * @param defaultVal Default value if not found on the config. * @return A factory for a boolean setting gui. */ public static BoolSettingFactory boolFactory(@NotNull String title, ValueUpdatableGui parent, String configPath, ConfigHolder config, - boolean defaultVal){ + boolean defaultVal) { return new BoolSettingFactory( - title,parent, + title, parent, configPath, config, defaultVal); } @@ -158,22 +160,25 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * A factory for a boolean setting gui that hold setting's information. */ - public static class BoolSettingFactory extends SettingGuiFactory{ - @NotNull String title; ValueUpdatableGui parent; + public static class BoolSettingFactory extends SettingGuiFactory { + @NotNull + String title; + ValueUpdatableGui parent; boolean defaultVal; /** * Constructor for a boolean setting gui factory. - * @param title The title of the gui. - * @param parent Parent gui to go back when completed. + * + * @param title The title of the gui. + * @param parent Parent gui to go back when completed. * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. + * @param config Configuration holder of this setting. * @param defaultVal Default value if not found on the config. */ protected BoolSettingFactory( @NotNull String title, ValueUpdatableGui parent, String configPath, ConfigHolder config, - boolean defaultVal){ + boolean defaultVal) { super(configPath, config); this.title = title; this.parent = parent; @@ -192,7 +197,7 @@ public class BoolSettingsGui extends AbstractSettingGui{ /** * @return The configured value for the associated setting. */ - public boolean getConfiguredValue(){ + public boolean getConfiguredValue() { return this.config.getConfig().getBoolean(this.configPath, this.defaultVal); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java index 10a3bed..c38b047 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantCostSettingsGui.java @@ -34,7 +34,8 @@ public class EnchantCostSettingsGui extends IntSettingsGui { /** * Create an enchantment cost setting config gui. - * @param holder Configuration factory of this setting. + * + * @param holder Configuration factory of this setting. * @param nowItem The defined value of this setting item's value. */ protected EnchantCostSettingsGui(EnchantCostSettingFactory holder, int nowItem) { @@ -54,7 +55,7 @@ public class EnchantCostSettingsGui extends IntSettingsGui { protected void initStepsValue() { super.initStepsValue(); - int nowBook = ((EnchantCostSettingFactory)this.holder).getConfiguredBookValue(); + int nowBook = ((EnchantCostSettingFactory) this.holder).getConfiguredBookValue(); this.beforeBook = nowBook; this.nowBook = nowBook; } @@ -81,7 +82,8 @@ public class EnchantCostSettingsGui extends IntSettingsGui { bookMeta.setDisplayName("\u00A7aCost of an Enchantment by Book"); bookMeta.setLore(Arrays.asList( "\u00A77Cost per result item level of an sacrifice enchantment", - "\u00A77Only apply if sacrificed item \u00A7cis \u00A77a book"));bookItemstack.setItemMeta(bookMeta); + "\u00A77Only apply if sacrificed item \u00A7cis \u00A77a book")); + bookItemstack.setItemMeta(bookMeta); // sword display ItemStack swordItemstack = new ItemStack(Material.WOODEN_SWORD); @@ -95,12 +97,12 @@ public class EnchantCostSettingsGui extends IntSettingsGui { swordItemstack.setItemMeta(swordMeta); pane.bindItem('1', GuiGlobalItems.backgroundItem(Material.BLACK_STAINED_GLASS_PANE)); - pane.bindItem('2', new GuiItem(bookItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); + pane.bindItem('2', new GuiItem(bookItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); pane.bindItem('3', new GuiItem(swordItemstack, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); } @Override - protected void prepareReturnToDefault(){ + protected void prepareReturnToDefault() { ItemStack item = new ItemStack(Material.COMMAND_BLOCK); ItemMeta meta = item.getItemMeta(); @@ -118,11 +120,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui { now = holder.defaultVal; updateValueDisplay(); update(); - }, CustomAnvil.instance); + }, CustomAnvil.instance); } @Override - protected void updateValueDisplay(){ + protected void updateValueDisplay() { super.updateValueDisplay(); PatternPane pane = getPane(); @@ -133,32 +135,32 @@ public class EnchantCostSettingsGui extends IntSettingsGui { // minus item GuiItem minusItem; - if(nowBook > holder.min){ + if (nowBook > holder.min) { int planned = Math.max(holder.min, nowBook - step); ItemStack item = new ItemStack(Material.RED_TERRACOTTA); ItemMeta meta = item.getItemMeta(); - meta.setDisplayName("\u00A7e"+nowBook+" -> "+planned + " \u00A7r(\u00A7c-"+(nowBook-planned)+"\u00A7r)"); + meta.setDisplayName("\u00A7e" + nowBook + " -> " + planned + " \u00A7r(\u00A7c-" + (nowBook - planned) + "\u00A7r)"); meta.setLore(AbstractSettingGui.CLICK_LORE); item.setItemMeta(meta); minusItem = new GuiItem(item, updateNowBookConsumer(planned), CustomAnvil.instance); - }else{ + } else { minusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); } pane.bindItem('M', minusItem); //plus item GuiItem plusItem; - if(nowBook < holder.max){ + if (nowBook < holder.max) { int planned = Math.min(holder.max, nowBook + step); ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA); ItemMeta meta = item.getItemMeta(); - meta.setDisplayName("\u00A7e"+nowBook+" -> "+planned + " \u00A7r(\u00A7a+"+(planned-nowBook)+"\u00A7r)"); + meta.setDisplayName("\u00A7e" + nowBook + " -> " + planned + " \u00A7r(\u00A7a+" + (planned - nowBook) + "\u00A7r)"); meta.setLore(AbstractSettingGui.CLICK_LORE); item.setItemMeta(meta); plusItem = new GuiItem(item, updateNowBookConsumer(planned), CustomAnvil.instance); - }else{ + } else { plusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); } pane.bindItem('P', plusItem); @@ -166,7 +168,7 @@ public class EnchantCostSettingsGui extends IntSettingsGui { // "result" display ItemStack resultPaper = new ItemStack(Material.PAPER); ItemMeta resultMeta = resultPaper.getItemMeta(); - resultMeta.setDisplayName("\u00A7eValue: "+nowBook); + resultMeta.setDisplayName("\u00A7eValue: " + nowBook); resultPaper.setItemMeta(resultMeta); GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance); @@ -174,9 +176,9 @@ public class EnchantCostSettingsGui extends IntSettingsGui { // reset to default GuiItem returnToDefault; - if(now != holder.defaultVal || nowBook != holder.defaultBookVal){ + if (now != holder.defaultVal || nowBook != holder.defaultBookVal) { returnToDefault = this.returnToDefault; - }else{ + } else { returnToDefault = GuiGlobalItems.backgroundItem(); } pane.bindItem('D', returnToDefault); @@ -188,8 +190,8 @@ public class EnchantCostSettingsGui extends IntSettingsGui { * @param planned Value to change current book cost setting to. * @return A consumer to update the current book cost setting's value. */ - protected Consumer updateNowBookConsumer(int planned){ - return event->{ + protected Consumer updateNowBookConsumer(int planned) { + return event -> { event.setCancelled(true); nowBook = planned; updateValueDisplay(); @@ -204,11 +206,11 @@ public class EnchantCostSettingsGui extends IntSettingsGui { @Override public boolean onSave() { - holder.config.getConfig().set(holder.configPath+ITEM_PATH, now); - holder.config.getConfig().set(holder.configPath+BOOK_PATH, nowBook); + holder.config.getConfig().set(holder.configPath + ITEM_PATH, now); + holder.config.getConfig().set(holder.configPath + BOOK_PATH, nowBook); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); - if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { return holder.config.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; @@ -221,27 +223,28 @@ public class EnchantCostSettingsGui extends IntSettingsGui { /** * Create an int setting factory from setting's parameters. - * @param title The title of the gui. - * @param parent Parent gui to go back when completed. - * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. - * @param min Minimum value of this setting. - * @param max Maximum value of this setting. + * + * @param title The title of the gui. + * @param parent Parent gui to go back when completed. + * @param configPath Configuration path of this setting. + * @param config Configuration holder of this setting. + * @param min Minimum value of this setting. + * @param max Maximum value of this setting. * @param defaultItemVal Default item value if not found on the config. * @param defaultBookVal Default book value if not found on the config. - * @param steps List of step the value can increment/decrement. - * List's size should be between 1 (included) and 3 (included). - * it is visually preferable to have an odd number of step. - * If step only contain 1 value, no step item should be displayed. + * @param steps List of step the value can increment/decrement. + * List's size should be between 1 (included) and 3 (included). + * it is visually preferable to have an odd number of step. + * If step only contain 1 value, no step item should be displayed. * @return A factory for an enchant cost setting gui. */ public static EnchantCostSettingFactory enchantCostFactory( @NotNull String title, ValueUpdatableGui parent, String configPath, ConfigHolder config, int min, int max, int defaultItemVal, int defaultBookVal, - int... steps){ + int... steps) { return new EnchantCostSettingFactory( - title,parent, + title, parent, configPath, config, min, max, defaultItemVal, defaultBookVal, steps); } @@ -255,26 +258,27 @@ public class EnchantCostSettingsGui extends IntSettingsGui { /** * Constructor for an enchantment cost setting gui factory. - * @param title The title of the gui. - * @param parent Parent gui to go back when completed. - * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. - * @param min Minimum value of this setting. - * @param max Maximum value of this setting. + * + * @param title The title of the gui. + * @param parent Parent gui to go back when completed. + * @param configPath Configuration path of this setting. + * @param config Configuration holder of this setting. + * @param min Minimum value of this setting. + * @param max Maximum value of this setting. * @param defaultItemVal Default item value if not found on the config. * @param defaultBookVal Default book value if not found on the config. - * @param steps List of step the value can increment/decrement. - * List's size should be between 1 (included) and 3 (included). - * it is visually preferable to have an odd number of step. - * If step only contain 1 value, no step item should be displayed. + * @param steps List of step the value can increment/decrement. + * List's size should be between 1 (included) and 3 (included). + * it is visually preferable to have an odd number of step. + * If step only contain 1 value, no step item should be displayed. */ protected EnchantCostSettingFactory( @NotNull String title, ValueUpdatableGui parent, String configPath, ConfigHolder config, int min, int max, int defaultItemVal, int defaultBookVal, - int... steps){ + int... steps) { - super(title,parent, + super(title, parent, configPath, config, min, max, defaultItemVal, steps); this.defaultBookVal = defaultBookVal; @@ -285,14 +289,14 @@ public class EnchantCostSettingsGui extends IntSettingsGui { */ @Override public int getConfiguredValue() { - return this.config.getConfig().getInt(this.configPath+ITEM_PATH, this.defaultVal); + return this.config.getConfig().getInt(this.configPath + ITEM_PATH, this.defaultVal); } /** * @return The configured value for the enchant setting book value. */ - public int getConfiguredBookValue(){ - return this.config.getConfig().getInt(this.configPath+BOOK_PATH, this.defaultBookVal); + public int getConfiguredBookValue() { + return this.config.getConfig().getInt(this.configPath + BOOK_PATH, this.defaultBookVal); } @Override diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java index e9fe0c4..f4d3337 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/EnchantSelectSettingGui.java @@ -24,7 +24,7 @@ import java.util.List; import java.util.Set; import java.util.function.Consumer; -public class EnchantSelectSettingGui extends AbstractSettingGui{ +public class EnchantSelectSettingGui extends AbstractSettingGui { SelectEnchantmentContainer enchantContainer; int page; @@ -57,7 +57,7 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ ); } - protected void initGroups(){ + protected void initGroups() { // Add enchantment gui item OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5); filledEnchant.setPriority(Pane.Priority.HIGH); @@ -66,7 +66,7 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ Set illegalEnchant = this.enchantContainer.illegalEnchantments(); for (Enchantment enchant : GuiSharedConstant.SORTED_ENCHANTMENT_LIST) { - if(illegalEnchant.contains(enchant)) { + if (illegalEnchant.contains(enchant)) { return; } filledEnchant.addItem(getGuiItemFromEnchant(enchant)); @@ -76,13 +76,13 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ } - private GuiItem getGuiItemFromEnchant(Enchantment enchantment){ + private GuiItem getGuiItemFromEnchant(Enchantment enchantment) { boolean isIn = this.selectedEnchant.contains(enchantment); Material usedMaterial; - if(isIn){ + if (isIn) { usedMaterial = Material.ENCHANTED_BOOK; - }else{ + } else { usedMaterial = Material.BOOK; } ItemStack item = new ItemStack(usedMaterial); @@ -98,22 +98,22 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ private static final List TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected"); private static final List FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected"); - public void setEnchantItemMeta(ItemStack item, String name, boolean isIn){ + public void setEnchantItemMeta(ItemStack item, String name, boolean isIn) { ItemMeta meta = item.getItemMeta(); - if(meta == null){ - CustomAnvil.instance.getLogger().warning("Could not create item for enchantment: "+name+":\n" + - "Item do not gave item meta: "+item+". Using placeholder instead"); + if (meta == null) { + CustomAnvil.instance.getLogger().warning("Could not create item for enchantment: " + name + ":\n" + + "Item do not gave item meta: " + item + ". Using placeholder instead"); item.setType(Material.PAPER); meta = item.getItemMeta(); assert meta != null; } - meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name)); - if(isIn){ + meta.setDisplayName("\u00A7" + (isIn ? 'a' : 'c') + CasedStringUtil.snakeToUpperSpacedCase(name)); + if (isIn) { meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); meta.setLore(TRUE_LORE); - }else{ + } else { meta.removeEnchant(Enchantment.DAMAGE_UNDEAD); meta.setLore(FALSE_LORE); } @@ -122,17 +122,17 @@ public class EnchantSelectSettingGui extends AbstractSettingGui{ item.setItemMeta(meta); } - private Consumer getEnchantItemConsumer(Enchantment enchant, GuiItem guiItem){ + private Consumer getEnchantItemConsumer(Enchantment enchant, GuiItem guiItem) { return event -> { event.setCancelled(true); ItemStack item = guiItem.getItem(); boolean isIn = this.selectedEnchant.contains(enchant); - if(isIn){ + if (isIn) { this.selectedEnchant.remove(enchant); item.setType(Material.BOOK); - }else{ + } else { this.selectedEnchant.add(enchant); item.setType(Material.ENCHANTED_BOOK); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java index 9c13c86..7623ae5 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java @@ -20,10 +20,13 @@ import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.util.CasedStringUtil; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.function.Consumer; -public class GroupSelectSettingGui extends AbstractSettingGui{ +public class GroupSelectSettingGui extends AbstractSettingGui { SelectGroupContainer groupContainer; int page; @@ -56,7 +59,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ ); } - protected void initGroups(){ + protected void initGroups() { // Add enchantment gui item OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5); filledEnchant.setPriority(Pane.Priority.HIGH); @@ -65,7 +68,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ Set illegalGroup = this.groupContainer.illegalGroups(); for (AbstractMaterialGroup group : ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().values()) { - if(illegalGroup.contains(group)) { + if (illegalGroup.contains(group)) { return; } filledEnchant.addItem(getGuiItemFromGroup(group)); @@ -75,7 +78,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ } - private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){ + private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group) { boolean isIn = this.selectedGroups.contains(group); Material usedMaterial = group.getRepresentativeMaterial(); @@ -91,22 +94,22 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ private static final List TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected"); private static final List FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected"); - public void setGroupItemMeta(ItemStack item, String name, boolean isIn){ + public void setGroupItemMeta(ItemStack item, String name, boolean isIn) { ItemMeta meta = item.getItemMeta(); - if(meta == null){ - CustomAnvil.instance.getLogger().warning("Could not create item for group: "+name+":\n" + - "Item do not gave item meta: "+item+". Using placeholder instead"); + if (meta == null) { + CustomAnvil.instance.getLogger().warning("Could not create item for group: " + name + ":\n" + + "Item do not gave item meta: " + item + ". Using placeholder instead"); item.setType(Material.PAPER); meta = item.getItemMeta(); assert meta != null; } - meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name)); - if(isIn){ + meta.setDisplayName("\u00A7" + (isIn ? 'a' : 'c') + CasedStringUtil.snakeToUpperSpacedCase(name)); + if (isIn) { meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); meta.setLore(TRUE_LORE); - }else{ + } else { meta.removeEnchant(Enchantment.DAMAGE_UNDEAD); meta.setLore(FALSE_LORE); } @@ -115,14 +118,14 @@ public class GroupSelectSettingGui extends AbstractSettingGui{ item.setItemMeta(meta); } - private Consumer getGroupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){ + private Consumer getGroupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem) { return event -> { event.setCancelled(true); boolean isIn = this.selectedGroups.contains(group); - if(isIn){ + if (isIn) { this.selectedGroups.remove(group); - }else{ + } else { this.selectedGroups.add(group); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java index 7f6d32a..10a97e1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/IntSettingsGui.java @@ -23,7 +23,7 @@ import java.util.function.Consumer; /** * An instance of a gui used to edit an int setting. */ -public class IntSettingsGui extends AbstractSettingGui{ +public class IntSettingsGui extends AbstractSettingGui { protected final IntSettingFactory holder; protected final int before; @@ -32,8 +32,9 @@ public class IntSettingsGui extends AbstractSettingGui{ /** * Create an int setting config gui. + * * @param holder Configuration factory of this setting. - * @param now The defined value of this setting. + * @param now The defined value of this setting. */ protected IntSettingsGui(IntSettingFactory holder, int now) { super(3, holder.getTitle(), holder.parent); @@ -59,43 +60,44 @@ public class IntSettingsGui extends AbstractSettingGui{ } protected GuiItem returnToDefault; + /** * Prepare "return to default value" gui item. */ - protected void prepareReturnToDefault(){ + protected void prepareReturnToDefault() { ItemStack item = new ItemStack(Material.COMMAND_BLOCK); ItemMeta meta = item.getItemMeta(); meta.setDisplayName("\u00A7eReset to default value"); - meta.setLore(Collections.singletonList("\u00A77Default value is: "+holder.defaultVal)); + meta.setLore(Collections.singletonList("\u00A77Default value is: " + holder.defaultVal)); item.setItemMeta(meta); returnToDefault = new GuiItem(item, event -> { event.setCancelled(true); now = holder.defaultVal; updateValueDisplay(); update(); - }, CustomAnvil.instance); + }, CustomAnvil.instance); } /** * Update item using the setting value to match the new value. */ - protected void updateValueDisplay(){ + protected void updateValueDisplay() { PatternPane pane = getPane(); // minus item GuiItem minusItem; - if(now > holder.min){ + if (now > holder.min) { int planned = Math.max(holder.min, now - step); ItemStack item = new ItemStack(Material.RED_TERRACOTTA); ItemMeta meta = item.getItemMeta(); - meta.setDisplayName("\u00A7e"+now+" -> "+planned + " \u00A7r(\u00A7c-"+(now-planned)+"\u00A7r)"); + meta.setDisplayName("\u00A7e" + now + " -> " + planned + " \u00A7r(\u00A7c-" + (now - planned) + "\u00A7r)"); meta.setLore(AbstractSettingGui.CLICK_LORE); item.setItemMeta(meta); minusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance); - }else{ + } else { minusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); } pane.bindItem('-', minusItem); @@ -103,16 +105,16 @@ public class IntSettingsGui extends AbstractSettingGui{ //plus item // may do a function to generalise ? GuiItem plusItem; - if(now < holder.max){ + if (now < holder.max) { int planned = Math.min(holder.max, now + step); ItemStack item = new ItemStack(Material.GREEN_TERRACOTTA); ItemMeta meta = item.getItemMeta(); - meta.setDisplayName("\u00A7e"+now+" -> "+planned + " \u00A7r(\u00A7a+"+(planned-now)+"\u00A7r)"); + meta.setDisplayName("\u00A7e" + now + " -> " + planned + " \u00A7r(\u00A7a+" + (planned - now) + "\u00A7r)"); meta.setLore(AbstractSettingGui.CLICK_LORE); item.setItemMeta(meta); plusItem = new GuiItem(item, updateNowConsumer(planned), CustomAnvil.instance); - }else{ + } else { plusItem = GuiGlobalItems.backgroundItem(Material.BARRIER); } pane.bindItem('+', plusItem); @@ -120,7 +122,7 @@ public class IntSettingsGui extends AbstractSettingGui{ // "result" display ItemStack resultPaper = new ItemStack(Material.PAPER); ItemMeta resultMeta = resultPaper.getItemMeta(); - resultMeta.setDisplayName("\u00A7eValue: "+now); + resultMeta.setDisplayName("\u00A7eValue: " + now); resultPaper.setItemMeta(resultMeta); GuiItem resultItem = new GuiItem(resultPaper, GuiGlobalActions.stayInPlace, CustomAnvil.instance); @@ -128,9 +130,9 @@ public class IntSettingsGui extends AbstractSettingGui{ // reset to default GuiItem returnToDefault; - if(now != holder.defaultVal){ + if (now != holder.defaultVal) { returnToDefault = this.returnToDefault; - }else{ + } else { returnToDefault = GuiGlobalItems.backgroundItem(); } pane.bindItem('D', returnToDefault); @@ -142,8 +144,8 @@ public class IntSettingsGui extends AbstractSettingGui{ * @param planned Value to change current setting to. * @return A consumer to update the current setting's value. */ - protected Consumer updateNowConsumer(int planned){ - return event->{ + protected Consumer updateNowConsumer(int planned) { + return event -> { event.setCancelled(true); now = planned; updateValueDisplay(); @@ -154,12 +156,12 @@ public class IntSettingsGui extends AbstractSettingGui{ /** * Initialise step items. */ - protected void initStepsValue(){ + protected void initStepsValue() { // Put background glass on the background of 'a' to 'b' GuiItem background = GuiGlobalItems.backgroundItem(); PatternPane pane = getPane(); - for (char i = 'a'; i < (getMidStepChar()-'a')*2+1; i++) { + for (char i = 'a'; i < (getMidStepChar() - 'a') * 2 + 1; i++) { pane.bindItem(i, background); } // Then update legit step values @@ -169,35 +171,37 @@ public class IntSettingsGui extends AbstractSettingGui{ /** * Update steps items value. */ - protected void updateStepValue(){ - if(holder.steps.length <= 1) return; + protected void updateStepValue() { + if (holder.steps.length <= 1) return; // We assume steps have a length of 2k+1 cause its more pretty char val = getMidStepChar(); // Offset to start (not the best way to do it) - val -= (char) ((holder.steps.length-1)/2); + val -= (char) ((holder.steps.length - 1) / 2); // Then place items PatternPane pane = getPane(); for (int i = 0; i < holder.steps.length; i++) { - pane.bindItem(val+i, stepGuiItem(i)); + pane.bindItem(val + i, stepGuiItem(i)); } } /** * Step use lower case character from 'a' to a certain char. + * * @return The middle value of the character set for steps. */ - protected char getMidStepChar(){ + protected char getMidStepChar() { return 'e'; } /** * Create a step item from a step value. + * * @param stepIndex the index of the step item. * @return A step item corresponding to its index value. */ - protected GuiItem stepGuiItem(int stepIndex){ + protected GuiItem stepGuiItem(int stepIndex) { int stepValue = holder.steps[stepIndex]; // Get material properties @@ -205,15 +209,15 @@ public class IntSettingsGui extends AbstractSettingGui{ StringBuilder stepName = new StringBuilder("\u00A7"); List stepLore; Consumer clickEvent; - if(stepValue == step){ + if (stepValue == step) { stepMat = Material.GREEN_STAINED_GLASS_PANE; stepName.append('a'); - stepLore = Collections.singletonList("\u00A77Value is changing by "+stepValue); + stepLore = Collections.singletonList("\u00A77Value is changing by " + stepValue); clickEvent = GuiGlobalActions.stayInPlace; - }else{ + } else { stepMat = Material.RED_STAINED_GLASS_PANE; stepName.append('c'); - stepLore = Collections.singletonList("\u00A77Click here to change the value by "+stepValue); + stepLore = Collections.singletonList("\u00A77Click here to change the value by " + stepValue); clickEvent = updateStepValue(stepValue); } stepName.append("Step of: ").append(stepValue); @@ -233,7 +237,7 @@ public class IntSettingsGui extends AbstractSettingGui{ * @param stepValue Value to change current step to. * @return A consumer to update the current step of this setting. */ - protected Consumer updateStepValue(int stepValue){ + protected Consumer updateStepValue(int stepValue) { return event -> { event.setCancelled(true); this.step = stepValue; @@ -248,7 +252,7 @@ public class IntSettingsGui extends AbstractSettingGui{ holder.config.getConfig().set(holder.configPath, now); MetricsUtil.INSTANCE.notifyChange(this.holder.config, this.holder.configPath); - if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { return holder.config.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } return true; @@ -261,24 +265,25 @@ public class IntSettingsGui extends AbstractSettingGui{ /** * Create an int setting factory from setting's parameters. - * @param title The title of the gui. - * @param parent Parent gui to go back when completed. + * + * @param title The title of the gui. + * @param parent Parent gui to go back when completed. * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. - * @param min Minimum value of this setting. - * @param max Maximum value of this setting. + * @param config Configuration holder of this setting. + * @param min Minimum value of this setting. + * @param max Maximum value of this setting. * @param defaultVal Default value if not found on the config. - * @param steps List of step the value can increment/decrement. - * List's size should be between 1 (included) and 5 (included). - * it is visually preferable to have an odd number of step. - * If step only contain 1 value, no step item should be displayed. + * @param steps List of step the value can increment/decrement. + * List's size should be between 1 (included) and 5 (included). + * it is visually preferable to have an odd number of step. + * If step only contain 1 value, no step item should be displayed. * @return A factory for an int setting gui. */ public static IntSettingFactory intFactory(@NotNull String title, ValueUpdatableGui parent, String configPath, ConfigHolder config, - int min, int max, int defaultVal, int... steps){ + int min, int max, int defaultVal, int... steps) { return new IntSettingFactory( - title,parent, + title, parent, configPath, config, min, max, defaultVal, steps); } @@ -286,28 +291,34 @@ public class IntSettingsGui extends AbstractSettingGui{ /** * A factory for an int setting gui that hold setting's information. */ - public static class IntSettingFactory extends SettingGuiFactory{ - @NotNull String title; ValueUpdatableGui parent; - int min; int max; int defaultVal; int[] steps; + public static class IntSettingFactory extends SettingGuiFactory { + @NotNull + String title; + ValueUpdatableGui parent; + int min; + int max; + int defaultVal; + int[] steps; /** * Constructor for an int setting gui factory. - * @param title The title of the gui. - * @param parent Parent gui to go back when completed. + * + * @param title The title of the gui. + * @param parent Parent gui to go back when completed. * @param configPath Configuration path of this setting. - * @param config Configuration holder of this setting. - * @param min Minimum value of this setting. - * @param max Maximum value of this setting. + * @param config Configuration holder of this setting. + * @param min Minimum value of this setting. + * @param max Maximum value of this setting. * @param defaultVal Default value if not found on the config. - * @param steps List of step the value can increment/decrement. - * List's size should be between 1 (included) and 5 (included). - * it is visually preferable to have an odd number of step. - * If step only contain 1 value, no step item should be displayed. + * @param steps List of step the value can increment/decrement. + * List's size should be between 1 (included) and 5 (included). + * it is visually preferable to have an odd number of step. + * If step only contain 1 value, no step item should be displayed. */ protected IntSettingFactory( @NotNull String title, ValueUpdatableGui parent, String configPath, ConfigHolder config, - int min, int max, int defaultVal, int... steps){ + int min, int max, int defaultVal, int... steps) { super(configPath, config); this.title = title; this.parent = parent; @@ -328,7 +339,7 @@ public class IntSettingsGui extends AbstractSettingGui{ /** * @return The configured value for the associated setting. */ - public int getConfiguredValue(){ + public int getConfiguredValue() { return this.config.getConfig().getInt(this.configPath, this.defaultVal); } 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 6e42cb5..6671d12 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 @@ -85,7 +85,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S // Displayed item will be updated later - this.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( "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A75Enchantments", @@ -93,7 +93,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S enchantGui.show(event.getWhoClicked()); }, CustomAnvil.instance); - this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{ + this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event) -> { event.setCancelled(true); GroupSelectSettingGui enchantGui = new GroupSelectSettingGui( "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(this.enchantConflict.getName()) + " \u00A73Groups", @@ -103,9 +103,9 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S this.minBeforeActiveSettingFactory = IntSettingsGui.intFactory( "\u00A78Minimum enchantment count", - this, this.enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER, + this, this.enchantConflict.getName() + ".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER, 0, 255, 0, 1 - ); + ); this.pane.bindItem('E', this.enchantSettingItem); this.pane.bindItem('G', this.groupSettingItem); @@ -116,7 +116,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S } private ConfirmActionGui createDeleteGui() { - Supplier deleteSupplier = () ->{ + Supplier deleteSupplier = () -> { EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); // Remove from manager @@ -136,17 +136,17 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S // Save boolean success = true; - if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { success = ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } 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 - ); + ); } @Override @@ -155,16 +155,16 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S // Parent should call updateLocal } - public void updateLocal(){ - if(!this.shouldWorld) return; + public void updateLocal() { + 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()){ + if (enchants.isEmpty()) { enchantLore.add("\u00A77There is no included enchantment for this conflict."); - }else{ + } else { enchantLore.add("\u00A77List of included enchantment for this conflict:"); Iterator enchantIterator = enchants.iterator(); @@ -175,8 +175,8 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S String formattedName = CasedStringUtil.snakeToUpperSpacedCase(enchantIterator.next().getKey().getKey()); enchantLore.add("\u00A77- \u00A75" + formattedName); } - if(greaterThanMax){ - enchantLore.add("\u00A77And "+(enchants.size()-4)+" more..."); + if (greaterThanMax) { + enchantLore.add("\u00A77And " + (enchants.size() - 4) + " more..."); } } @@ -185,9 +185,9 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S 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()){ + if (grouos.isEmpty()) { groupLore.add("\u00A77There is no excluded groups for this conflict."); - }else{ + } else { groupLore.add("\u00A77List of excluded groups for this conflict:"); Iterator groupIterator = grouos.iterator(); @@ -199,8 +199,8 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S groupLore.add("\u00A77- \u00A73" + formattedName); } - if(greaterThanMax){ - groupLore.add("\u00A77And "+(grouos.size()-4)+" more..."); + if (greaterThanMax) { + groupLore.add("\u00A77And " + (grouos.size() - 4) + " more..."); } } @@ -231,7 +231,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S update(); } - public void cleanUnused(){ + public void cleanUnused() { for (HumanEntity viewer : getViewers()) { this.parent.show(viewer); } @@ -247,9 +247,9 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S @Override public void show(@NotNull HumanEntity humanEntity) { - if(this.shouldWorld){ + if (this.shouldWorld) { super.show(humanEntity); - }else{ + } else { this.parent.show(humanEntity); } } @@ -267,8 +267,8 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S @Override public boolean setSelectedEnchantments(Set enchantments) { - if(!this.shouldWorld) { - CustomAnvil.instance.getLogger().info("Trying to save "+enchantConflict.getName()+" enchants but sub config is destroyed"); + if (!this.shouldWorld) { + CustomAnvil.instance.getLogger().info("Trying to save " + enchantConflict.getName() + " enchants but sub config is destroyed"); return false; } @@ -281,17 +281,17 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S for (Enchantment enchantment : enchantments) { enchantKeys[index++] = enchantment.getKey().getKey(); } - ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName()+".enchantments", enchantKeys); + 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); + } 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){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { return ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } @@ -312,8 +312,8 @@ 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"); + if (!this.shouldWorld) { + CustomAnvil.instance.getLogger().info("Trying to save " + enchantConflict.getName() + " groups but sub config is destroyed"); return false; } @@ -326,16 +326,16 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S for (AbstractMaterialGroup group : groups) { groupsNames[index++] = group.getName(); } - ConfigHolder.CONFLICT_HOLDER.getConfig().set(this.enchantConflict.getName()+".notAffectedGroups", groupsNames); + 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); + } 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){ + if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { return ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java index 0322fc5..e8c8334 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalActions.java @@ -27,21 +27,22 @@ public class GuiGlobalActions { /** * Create a consumer to create and open a new GUI. * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem. - * @param clazz The class of the gui to open. - * It is assumed this class contain a constructor requiring arguments of argClass in the same order as argClass array. + * + * @param clazz The class of the gui to open. + * It is assumed this class contain a constructor requiring arguments of argClass in the same order as argClass array. * @param argClass Classes of the argument that will be passed to the constructor of the GUI class. - * @param args Arguments for the constructor the GUI class. + * @param args Arguments for the constructor the GUI class. * @return A consumer to create a new gui and open it. */ public static @NotNull Consumer openGuiAction( @NotNull Class clazz, @NotNull Class[] argClass, - @NotNull Object... args){ + @NotNull Object... args) { return event -> { event.setCancelled(true); HumanEntity player = event.getWhoClicked(); // Do not allow to open inventory if player do not have edit configuration permission - if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + if (!player.hasPermission(CustomAnvil.editConfigPermission)) { player.closeInventory(); player.sendMessage(NO_EDIT_PERM); return; @@ -62,22 +63,24 @@ public class GuiGlobalActions { /** * Create a consumer to create and open a new GUI. * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem. + * * @param clazz The class of the gui to open. * It is assumed this class contain a constructor with no argument. * @return A consumer to create a new gui and open it. */ public static @NotNull Consumer openGuiAction( - @NotNull Class clazz){ + @NotNull Class clazz) { return openGuiAction(clazz, new Class[0]); } /** * Create a consumer to open a setting gui from a setting GUI factory. * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem. + * * @param factory The setting gui factory. * @return A consumer to create and open a new setting GUI. */ - public static @NotNull Consumer openSettingGuiAction(AbstractSettingGui.SettingGuiFactory factory){ + public static @NotNull Consumer openSettingGuiAction(AbstractSettingGui.SettingGuiFactory factory) { return event -> { event.setCancelled(true); Gui gui = factory.create(); @@ -88,6 +91,7 @@ public class GuiGlobalActions { /** * Create a consumer to open a global GUI. * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem. + * * @param goal The gui to open when consumer is run. * @return A consumer to open a global GUI. */ @@ -95,7 +99,7 @@ public class GuiGlobalActions { return event -> { HumanEntity player = event.getWhoClicked(); // Do not allow to open inventory if player do not have edit configuration permission - if(!player.hasPermission(CustomAnvil.editConfigPermission)) { + if (!player.hasPermission(CustomAnvil.editConfigPermission)) { player.closeInventory(); player.sendMessage(NO_EDIT_PERM); return; @@ -109,8 +113,9 @@ public class GuiGlobalActions { * Create a consumer to update and open an updatable GUI. * Used with InventoryClickEvent as the consumer argument as it is planned to be used on click on an GuiItem. * This consumer check if the player who interacted with the item have the permission to save before saving. + * * @param setting The gui that contain the modified setting. - * @param goal The gui to update and open when consumer is run. + * @param goal The gui to update and open when consumer is run. * @return A consumer to open a global GUI. */ public static @NotNull Consumer saveSettingAction( @@ -120,14 +125,14 @@ public class GuiGlobalActions { 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)) { + if (!player.hasPermission(CustomAnvil.editConfigPermission)) { player.closeInventory(); player.sendMessage(NO_EDIT_PERM); return; } // Save setting - if(!setting.onSave()){ + if (!setting.onSave()) { player.sendMessage("\u00A7cSomething went wrong while saving the change of value."); } // Update gui for those who have it open. 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 125edf1..fb10605 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiGlobalItems.java @@ -23,6 +23,7 @@ public class GuiGlobalItems { // statically create default back itemstack private static final ItemStack BACK_ITEM; + static { BACK_ITEM = new ItemStack(Material.BARRIER); ItemMeta meta = BACK_ITEM.getItemMeta(); @@ -32,32 +33,35 @@ public class GuiGlobalItems { /** * Create a GuiItem that open the given GUi. + * * @param item The item to display in the GUI. * @param goal The GUI to open on click. * @return An GuiItem that open goal on click. */ - public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal){ + public static GuiItem goToGuiItem(@NotNull ItemStack item, @NotNull Gui goal) { return new GuiItem(item, GuiGlobalActions.openGuiAction(goal), CustomAnvil.instance); } /** * Create back button item from default back GuiItem. * The back item will open the goal inventory when clicked. + * * @param goal The GUI to go back to. * @return An GuiItem that go back to goal on click. */ - public static GuiItem backItem(@NotNull Gui goal){ + public static GuiItem backItem(@NotNull Gui goal) { return goToGuiItem(BACK_ITEM, goal); } /** * Add default back item to a GUI pattern with the reserved character key B. * The back item will open the target inventory when clicked. + * * @param target The pattern to add the back item. - * @param goal The GUI to go back to. + * @param goal The GUI to go back to. */ public static void addBackItem(@NotNull PatternPane target, - @NotNull Gui goal){ + @NotNull Gui goal) { target.bindItem('B', backItem(goal)); } @@ -66,42 +70,47 @@ public class GuiGlobalItems { /** * Get a background item with backgroundMat as the displayed material. * A background item is a GuiItem that do nothing when interacted with and have an empty name. + * * @param backgroundMat The material to which the background item should be made of. * @return A background item with backgroundMat as material. */ - public static GuiItem backgroundItem(Material backgroundMat){ + public static GuiItem backgroundItem(Material backgroundMat) { ItemStack item = new ItemStack(backgroundMat); ItemMeta meta = item.getItemMeta(); meta.setDisplayName("\u00A7c"); item.setItemMeta(meta); return new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance); } + /** * Get default background GuiItem. * A background item is a GuiItem that do nothing when interacted with and have an empty name. + * * @return A new instance of the default background item. */ - public static GuiItem backgroundItem(){ + public static GuiItem backgroundItem() { return backgroundItem(DEFAULT_BACKGROUND_MAT); } /** * Add default background item to a GUI pattern with the reserved character key 0. * A background item is a GuiItem that do nothing when interacted with and have an empty name. - * @param target The pattern to add the background item. + * + * @param target The pattern to add the background item. * @param backgroundMat The material of the background item. */ public static void addBackgroundItem(@NotNull PatternPane target, - @NotNull Material backgroundMat){ + @NotNull Material backgroundMat) { target.bindItem('0', backgroundItem(backgroundMat)); } /** * Add default background item to a GUI pattern with the reserved character key 0. * A background item is a GuiItem that do nothing when interacted with and have an empty name. + * * @param target The pattern to add the background item. */ - public static void addBackgroundItem(@NotNull PatternPane target){ + public static void addBackgroundItem(@NotNull PatternPane target) { addBackgroundItem(target, DEFAULT_BACKGROUND_MAT); } @@ -112,13 +121,14 @@ public class GuiGlobalItems { * Create a new save setting GuiItem. * A save setting item is a GuiItem that save a changed setting when clicked. * This item also check if the player who interacted with the item have the permission to save before saving. + * * @param setting The setting to change. - * @param goal Parent GUI of this setting GUI. as setting will be change the display of goal GUI will be updated. + * @param goal Parent GUI of this setting GUI. as setting will be change the display of goal GUI will be updated. * @return A save setting item. */ public static GuiItem saveItem( @NotNull AbstractSettingGui setting, - @NotNull ValueUpdatableGui goal){ + @NotNull ValueUpdatableGui goal) { ItemStack item = new ItemStack(DEFAULT_SAVE_ITEM); ItemMeta meta = item.getItemMeta(); @@ -131,34 +141,37 @@ public class GuiGlobalItems { // Create static non change item private static final GuiItem NO_CHANGE_ITEM; + static { ItemStack item = new ItemStack(DEFAULT_NO_CHANGE_ITEM); ItemMeta meta = item.getItemMeta(); meta.setDisplayName("\u00A77No change. can't save."); item.setItemMeta(meta); - NO_CHANGE_ITEM = new GuiItem(item,GuiGlobalActions.stayInPlace, CustomAnvil.instance); + NO_CHANGE_ITEM = new GuiItem(item, GuiGlobalActions.stayInPlace, CustomAnvil.instance); } /** * Get the global "no change" GuiItem. * The no change item do nothing when interacted, only the title is change to show there is no change. + * * @return The global "no change" item. */ - public static GuiItem noChangeItem(){ + public static GuiItem noChangeItem() { return NO_CHANGE_ITEM; } /** * Create a new "create and go to the setting GUI" GuiItem. * This item will create and open a setting GUI from the factory. - * @param item The item that will be displayed. + * + * @param item The item that will be displayed. * @param factory The setting's GUI factory. * @return A formatted GuiItem that will create and open a GUI for the setting. */ public static GuiItem openSettingGuiItem( @NotNull ItemStack item, @NotNull AbstractSettingGui.SettingGuiFactory factory - ){ + ) { return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance); } @@ -169,23 +182,24 @@ public class GuiGlobalItems { * Create a new Boolean setting GuiItem. * This item will create and open a boolean setting GUI from the factory. * The item will have its value written in the lore part of the item. + * * @param factory The setting's GUI factory. - * @param name Name of the item. + * @param name Name of the item. * @return A formatted GuiItem that will create and open a GUI for the boolean setting. */ public static GuiItem boolSettingGuiItem( @NotNull BoolSettingsGui.BoolSettingFactory factory, @NotNull String name - ){ + ) { // Get item properties boolean value = factory.getConfiguredValue(); Material itemMat; StringBuilder itemName = new StringBuilder("\u00A7"); - if(value){ + if (value) { itemMat = Material.GREEN_TERRACOTTA; itemName.append("a"); - }else{ + } else { itemMat = Material.RED_TERRACOTTA; itemName.append("c"); } @@ -199,12 +213,13 @@ public class GuiGlobalItems { * This item will create and open a boolean setting GUI from the factory. * The item will have its value written in the lore part of the item. * Item's name will be the factory set title. + * * @param factory The setting's GUI factory. * @return A formatted GuiItem that will create and open a GUI for the boolean setting. */ public static GuiItem boolSettingGuiItem( @NotNull BoolSettingsGui.BoolSettingFactory factory - ){ + ) { String configPath = getConfigNameFromPath(factory.getConfigPath()); return boolSettingGuiItem(factory, CasedStringUtil.snakeToUpperSpacedCase(configPath)); } @@ -213,16 +228,17 @@ public class GuiGlobalItems { * Create a new int setting GuiItem. * This item will create and open an int setting GUI from the factory. * The item will have its value written in the lore part of the item. + * * @param factory The setting's GUI factory. * @param itemMat Displayed material of the item. - * @param name Name of the item. + * @param name Name of the item. * @return A formatted GuiItem that will create and open a GUI for the int setting. */ public static GuiItem intSettingGuiItem( @NotNull IntSettingsGui.IntSettingFactory factory, @NotNull Material itemMat, @NotNull String name - ){ + ) { // Get item properties int value = factory.getConfiguredValue(); StringBuilder itemName = new StringBuilder("\u00A7a").append(name); @@ -235,6 +251,7 @@ public class GuiGlobalItems { * This item will create and open an int setting GUI from the factory. * The item will have its value written in the lore part of the item. * Item's name will be the factory set title. + * * @param factory The setting's GUI factory. * @param itemMat Displayed material of the item. * @return A formatted GuiItem that will create and open a GUI for the int setting. @@ -242,18 +259,19 @@ public class GuiGlobalItems { public static GuiItem intSettingGuiItem( @NotNull IntSettingsGui.IntSettingFactory factory, @NotNull Material itemMat - ){ + ) { String configPath = getConfigNameFromPath(factory.getConfigPath()); return intSettingGuiItem(factory, itemMat, CasedStringUtil.detectToUpperSpacedCase(configPath)); } /** * Create an arbitrary GuiItem from a unique setting and item's property. - * @param factory The setting's GUI factory. - * @param itemMat Displayed material of the item. + * + * @param factory The setting's GUI factory. + * @param itemMat Displayed material of the item. * @param itemName Name of the item. - * @param value Value of the setting when the item is created. - * Will not update automatically, if the setting's value change, the item need to be created again. + * @param value Value of the setting when the item is created. + * Will not update automatically, if the setting's value change, the item need to be created again. * @return A formatted GuiItem that will create and open a GUI for the setting. */ private static GuiItem createGuiItemFromProperties( @@ -261,13 +279,13 @@ public class GuiGlobalItems { @NotNull Material itemMat, @NotNull StringBuilder itemName, @NotNull Object value - ){ + ) { // Create & initialise item ItemStack item = new ItemStack(itemMat); ItemMeta itemMeta = item.getItemMeta(); itemMeta.setDisplayName(itemName.toString()); - itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value)); + itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX + value)); item.setItemMeta(itemMeta); // Create GuiItem @@ -277,14 +295,15 @@ public class GuiGlobalItems { /** * Get the setting name from the setting path. * For example: "gui.command.name" will return "name". + * * @param path The setting's path. * @return The setting's name. */ - public static String getConfigNameFromPath(String path){ + public static String getConfigNameFromPath(String path) { // Get index of first dot int indexOfDot = path.indexOf("."); // when indexOfDot == -1 (not fond), it is implied that indexOfDot+1 = 0. substring will keep the full path as expected - return path.substring(indexOfDot+1); + return path.substring(indexOfDot + 1); } } 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 e3604b3..b52401b 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/util/GuiSharedConstant.java @@ -31,6 +31,7 @@ public class GuiSharedConstant { public static final boolean TEMPORARY_DO_BACKUP_EVERY_SAVE = true; public static final PatternPane BACK_TO_MAIN_MENU_BIG_LIST_DISPLAY_BACKGROUND_PANE; + static { Pattern pattern = new Pattern( GuiSharedConstant.EMPTY_GUI_FULL_LINE, diff --git a/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java b/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java index 2b6ce8c..cb45d1c 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java +++ b/src/main/java/xyz/alexcrea/cuanvil/util/CasedStringUtil.java @@ -9,17 +9,18 @@ public class CasedStringUtil { * Transform a snake cased string to an upper-cased spaced string. *

    * for example: if we use "hello_world" as an input this function will return "Hello World". + * * @param snake_cased_string The input string. - * This argument NEED to be a snake cased string, or it will not work + * This argument NEED to be a snake cased string, or it will not work * @return The input as an upper-cased string with space separator. */ - public static String snakeToUpperSpacedCase(String snake_cased_string){ - if(snake_cased_string.contentEquals("")) return ""; + public static String snakeToUpperSpacedCase(String snake_cased_string) { + if (snake_cased_string.contentEquals("")) return ""; StringBuilder result = new StringBuilder(); for (String word : snake_cased_string.split("_")) { result.append(" "); - if(word.isEmpty()) continue; + if (word.isEmpty()) continue; char firstChar = word.charAt(0); result.append(Character.toUpperCase(firstChar)); @@ -28,15 +29,15 @@ public class CasedStringUtil { return result.substring(1); } - public static String camelCaseToUpperSpaceCase(String camelCasedString){ - if(camelCasedString.isEmpty()) return camelCasedString; + public static String camelCaseToUpperSpaceCase(String camelCasedString) { + if (camelCasedString.isEmpty()) return camelCasedString; StringBuilder stb = new StringBuilder(); char[] chars = camelCasedString.toCharArray(); stb.append(chars[0]); for (int i = 1; i < chars.length; i++) { char chr = chars[i]; - if(Character.isUpperCase(chr)){ + if (Character.isUpperCase(chr)) { stb.append(" "); } stb.append(chr); @@ -45,11 +46,11 @@ public class CasedStringUtil { return stb.toString(); } - public static String detectToUpperSpacedCase(String toDetect){ + public static String detectToUpperSpacedCase(String toDetect) { //not advanced detection - if(toDetect.contains("_")){ + if (toDetect.contains("_")) { return snakeToUpperSpacedCase(toDetect); - }else{ + } else { return camelCaseToUpperSpaceCase(toDetect); } } diff --git a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt index 6a72777..eab2c6b 100644 --- a/src/main/kotlin/io/delilaheve/AnvilEventListener.kt +++ b/src/main/kotlin/io/delilaheve/AnvilEventListener.kt @@ -39,9 +39,10 @@ class AnvilEventListener : Listener { private const val ANVIL_INPUT_LEFT = 0 private const val ANVIL_INPUT_RIGHT = 1 private const val ANVIL_OUTPUT_SLOT = 2 + // static slot container - private val NO_SLOT = SlotContainer(SlotType.NO_SLOT,0) - private val CURSOR_SLOT = SlotContainer(SlotType.CURSOR,0) + private val NO_SLOT = SlotContainer(SlotType.NO_SLOT, 0) + private val CURSOR_SLOT = SlotContainer(SlotType.CURSOR, 0) } /** @@ -55,15 +56,15 @@ class AnvilEventListener : Listener { // Should find player val player = event.view.player - if(!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return + if (!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return // Test rename lonely item - if(second == null){ + if (second == null) { val resultItem = first.clone() var anvilCost = handleRename(resultItem, inventory) // Test/stop if nothing changed. - if(first == resultItem){ + if (first == resultItem) { CustomAnvil.log("no right item, But input is same as output") event.result = null return @@ -71,7 +72,7 @@ class AnvilEventListener : Listener { // We don't manually set item here as vanilla do it (renaming) //event.result = null - anvilCost+= calculatePenalty(first,null,resultItem) + anvilCost += calculatePenalty(first, null, resultItem) handleAnvilXp(inventory, event, anvilCost) return @@ -91,19 +92,19 @@ class AnvilEventListener : Listener { if (!first.isEnchantedBook() && !second.isEnchantedBook()) { // we only need to be concerned with repair when neither item is a book val repaired = resultItem.repairFrom(first, second) - anvilCost += if(repaired) ConfigOptions.itemRepairCost else 0 + anvilCost += if (repaired) ConfigOptions.itemRepairCost else 0 } // Test/stop if nothing changed. - if(first == resultItem){ + if (first == resultItem) { CustomAnvil.log("Mergable with second, But input is same as output") event.result = null return } // As calculatePenalty edit result, we need to calculate penalty after checking equality - anvilCost+= calculatePenalty(first, second, resultItem) + anvilCost += calculatePenalty(first, second, resultItem) // Calculate rename cost - anvilCost+= handleRename(resultItem, inventory) + anvilCost += handleRename(resultItem, inventory) // Finally, we set result event.result = resultItem @@ -114,19 +115,19 @@ class AnvilEventListener : Listener { // Test for unit repair val unitRepairAmount = first.getRepair(second) - if(unitRepairAmount != null){ + if (unitRepairAmount != null) { val resultItem = first.clone() var anvilCost = handleRename(resultItem, inventory) val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount) - if(repairAmount > 0){ - anvilCost += repairAmount*ConfigOptions.unitRepairCost + if (repairAmount > 0) { + anvilCost += repairAmount * ConfigOptions.unitRepairCost } // We do not care about right item penalty for unit repair - anvilCost+= calculatePenalty(first,null,resultItem) + anvilCost += calculatePenalty(first, null, resultItem) // Test/stop if nothing changed. - if(first == resultItem){ + if (first == resultItem) { CustomAnvil.log("unit repair, But input is same as output") event.result = null return @@ -134,16 +135,16 @@ class AnvilEventListener : Listener { event.result = resultItem handleAnvilXp(inventory, event, anvilCost) - }else{ + } else { CustomAnvil.log("no anvil fuse type found") event.result = null } } - private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory): Int{ + private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory): Int { // Rename item and add renaming cost resultItem.itemMeta?.let { - if(!it.displayName.contentEquals(inventory.renameText)){ + if (!it.displayName.contentEquals(inventory.renameText)) { it.setDisplayName(inventory.renameText) resultItem.itemMeta = it return ConfigOptions.itemRenameCost @@ -158,9 +159,11 @@ class AnvilEventListener : Listener { @EventHandler(ignoreCancelled = true) fun anvilExtractionCheck(event: InventoryClickEvent) { val player = event.whoClicked as? Player ?: return - if(!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return + if (!player.hasPermission(CustomAnvil.affectedByPluginPermission)) return val inventory = event.inventory as? AnvilInventory ?: return - if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return } + if (event.rawSlot != ANVIL_OUTPUT_SLOT) { + return + } val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return val leftItem = inventory.getItem(ANVIL_INPUT_LEFT) ?: return val rightItem = inventory.getItem(ANVIL_INPUT_RIGHT) @@ -172,110 +175,117 @@ class AnvilEventListener : Listener { || (unitRepairResult != null) // True if there was no change or not allowed - if((output == inventory.getItem(ANVIL_INPUT_LEFT)) - || !allowed){ + if ((output == inventory.getItem(ANVIL_INPUT_LEFT)) + || !allowed + ) { event.result = Event.Result.DENY return } - if(rightItem == null){ + if (rightItem == null) { event.result = Event.Result.ALLOW return } - if(canMerge){ + if (canMerge) { event.result = Event.Result.ALLOW - }else if(unitRepairResult != null){ - onUnitRepairExtract(leftItem, rightItem, output, - unitRepairResult, event, player, inventory) + } else if (unitRepairResult != null) { + onUnitRepairExtract( + leftItem, rightItem, output, + unitRepairResult, event, player, inventory + ) return } } - private fun onUnitRepairExtract(leftItem: ItemStack, - rightItem: ItemStack, - output: ItemStack, - unitRepairResult: Double, - event: InventoryClickEvent, - player: Player, - inventory: AnvilInventory){ + private fun onUnitRepairExtract( + leftItem: ItemStack, + rightItem: ItemStack, + output: ItemStack, + unitRepairResult: Double, + event: InventoryClickEvent, + player: Player, + inventory: AnvilInventory + ) { val resultCopy = leftItem.clone() val resultAmount = resultCopy.unitRepair( - rightItem.amount, unitRepairResult) + rightItem.amount, unitRepairResult + ) // To avoid vanilla, we cancel the event for unit repair event.result = Event.Result.DENY event.isCancelled = true // And we give the item manually // But first we check if we should give the item - val slotDestination = getActionSlot(event,player) - if(slotDestination.type == SlotType.NO_SLOT) return + val slotDestination = getActionSlot(event, player) + if (slotDestination.type == SlotType.NO_SLOT) return // Test repair cost var repairCost = 0 - if(player.gameMode != GameMode.CREATIVE){ + if (player.gameMode != GameMode.CREATIVE) { // Get repairCost leftItem.itemMeta?.let { leftMeta -> val leftName = leftMeta.displayName output.itemMeta?.let { - if(!leftName.contentEquals(it.displayName)){ - repairCost+= ConfigOptions.itemRenameCost + if (!leftName.contentEquals(it.displayName)) { + repairCost += ConfigOptions.itemRenameCost } } } - repairCost+= calculatePenalty(leftItem,null,resultCopy) - repairCost+= resultAmount*ConfigOptions.unitRepairCost + repairCost += calculatePenalty(leftItem, null, resultCopy) + repairCost += resultAmount * ConfigOptions.unitRepairCost - if((inventory.maximumRepairCost < repairCost) - || (player.level < repairCost)) return + if ((inventory.maximumRepairCost < repairCost) + || (player.level < repairCost) + ) return } // If not creative middle click... - if(event.click != ClickType.MIDDLE){ + if (event.click != ClickType.MIDDLE) { // We remove what should be removed - inventory.setItem(ANVIL_INPUT_LEFT,null) - rightItem.amount-= resultAmount - inventory.setItem(ANVIL_INPUT_RIGHT,rightItem) + inventory.setItem(ANVIL_INPUT_LEFT, null) + rightItem.amount -= resultAmount + inventory.setItem(ANVIL_INPUT_RIGHT, rightItem) inventory.setItem(ANVIL_OUTPUT_SLOT, null) - player.level-= repairCost + player.level -= repairCost } // Finally, we add the item to the player - if(slotDestination.type == SlotType.CURSOR){ + if (slotDestination.type == SlotType.CURSOR) { player.setItemOnCursor(output) - }else{// We assume SlotType == SlotType.INVENTORY - player.inventory.setItem(slotDestination.slot,output) + } else {// We assume SlotType == SlotType.INVENTORY + player.inventory.setItem(slotDestination.slot, output) } } /** * Get the destination slot or "NO_SLOT" slot container if there is no slot available */ - private fun getActionSlot(event: InventoryClickEvent, player: Player): SlotContainer{ - if(event.isShiftClick){ + private fun getActionSlot(event: InventoryClickEvent, player: Player): SlotContainer { + if (event.isShiftClick) { val inventory = player.inventory - val firstEmpty = inventory.firstEmpty() - if(firstEmpty == -1){ + val firstEmpty = inventory.firstEmpty() + if (firstEmpty == -1) { return NO_SLOT } //check hotbare full var slotIndex = 8 - while(slotIndex >= 0 && ((inventory.getItem(slotIndex)?.type ?: Material.AIR) != Material.AIR)){ + while (slotIndex >= 0 && ((inventory.getItem(slotIndex)?.type ?: Material.AIR) != Material.AIR)) { slotIndex-- } - if(slotIndex >= 0){ - return SlotContainer(SlotType.INVENTORY,slotIndex) + if (slotIndex >= 0) { + return SlotContainer(SlotType.INVENTORY, slotIndex) } slotIndex = 35 //4*9 - 1 (max of player inventory) - while(slotIndex >= 9 && ((inventory.getItem(slotIndex)?.type ?: Material.AIR) != Material.AIR)){ + while (slotIndex >= 9 && ((inventory.getItem(slotIndex)?.type ?: Material.AIR) != Material.AIR)) { slotIndex-- } - if(slotIndex < 9){ + if (slotIndex < 9) { return NO_SLOT } - return SlotContainer(SlotType.INVENTORY,slotIndex) - }else{ - if(player.itemOnCursor.type != Material.AIR){ + return SlotContainer(SlotType.INVENTORY, slotIndex) + } else { + if (player.itemOnCursor.type != Material.AIR) { return NO_SLOT } return CURSOR_SLOT @@ -286,24 +296,29 @@ class AnvilEventListener : Listener { * Function to calculate work penalty of anvil work * Also change result work penalty */ - private fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack): Int{ + private fun calculatePenalty(left: ItemStack, right: ItemStack?, result: ItemStack): Int { // Extracted From https://minecraft.fandom.com/wiki/Anvil_mechanics#Enchantment_equation // Calculate work penalty val leftPenalty = (left.itemMeta as? Repairable)?.repairCost ?: 0 val rightPenalty = - if(right == null){ 0 } - else{ (right.itemMeta as? Repairable)?.repairCost ?: 0 } + if (right == null) { + 0 + } else { + (right.itemMeta as? Repairable)?.repairCost ?: 0 + } // Try to set work penalty for the result item result.itemMeta?.let { - (it as? Repairable)?.repairCost = leftPenalty*2+1 + (it as? Repairable)?.repairCost = leftPenalty * 2 + 1 result.itemMeta = it } - CustomAnvil.log("Calculated penalty: " + - "leftPenalty: $leftPenalty, " + - "rightPenalty: $rightPenalty, " + - "result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}") + CustomAnvil.log( + "Calculated penalty: " + + "leftPenalty: $leftPenalty, " + + "rightPenalty: $rightPenalty, " + + "result penalty: ${(result.itemMeta as? Repairable)?.repairCost ?: "none"}" + ) return leftPenalty + rightPenalty } @@ -312,7 +327,7 @@ class AnvilEventListener : Listener { * Function to calculate right enchantment values * it include enchantment placed on final item and conflicting enchantment */ - private fun getRightValues(right: ItemStack, result:ItemStack) : Int { + private fun getRightValues(right: ItemStack, result: ItemStack): Int { // Calculate right value and illegal enchant penalty var illegalPenalty = 0 var rightValue = 0 @@ -323,12 +338,16 @@ class AnvilEventListener : Listener { for (enchantment in right.findEnchantments()) { // count enchant as illegal enchant if it conflicts with another enchant or not in result - if((enchantment.key !in resultEnchsKeys)){ + if ((enchantment.key !in resultEnchsKeys)) { resultEnchsKeys.add(enchantment.key) - val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(resultEnchsKeys,result.type,enchantment.key) + val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting( + resultEnchsKeys, + result.type, + enchantment.key + ) resultEnchsKeys.remove(enchantment.key) - if(ConflictType.BIG_CONFLICT == conflictType){ + if (ConflictType.BIG_CONFLICT == conflictType) { illegalPenalty += ConfigOptions.sacrificeIllegalCost } continue @@ -342,9 +361,11 @@ class AnvilEventListener : Listener { rightValue += value } - CustomAnvil.log("Calculated right values: " + - "rightValue: $rightValue, " + - "illegalPenalty: $illegalPenalty") + CustomAnvil.log( + "Calculated right values: " + + "rightValue: $rightValue, " + + "illegalPenalty: $illegalPenalty" + ) return rightValue + illegalPenalty } @@ -352,15 +373,16 @@ class AnvilEventListener : Listener { /** * Display xp needed for the work on the anvil inventory */ - private fun handleAnvilXp(inventory: AnvilInventory, - event: PrepareAnvilEvent, - anvilCost: Int){ + private fun handleAnvilXp( + inventory: AnvilInventory, + event: PrepareAnvilEvent, + anvilCost: Int + ) { // Test repair cost limit - val finalAnvilCost: Int - if (ConfigOptions.limitRepairCost) { - finalAnvilCost = min(anvilCost, ConfigOptions.limitRepairValue) - }else{ - finalAnvilCost = anvilCost + val finalAnvilCost = if (ConfigOptions.limitRepairCost) { + min(anvilCost, ConfigOptions.limitRepairValue) + } else { + anvilCost } /* Because Minecraft likes to have the final say in the repair cost displayed @@ -383,7 +405,7 @@ class AnvilEventListener : Listener { private class SlotContainer(val type: SlotType, val slot: Int) -private enum class SlotType{ +private enum class SlotType { CURSOR, INVENTORY, NO_SLOT diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 532b4a7..52ee56e 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -24,17 +24,22 @@ class CustomAnvil : JavaPlugin() { // Permission string required to use the plugin's features const val affectedByPluginPermission = "ca.affected" + // Permission string required to bypass enchantment conflicts test const val bypassFusePermission = "ca.bypass.fuse" + // Permission string required to bypass enchantment conflicts test const val bypassLevelPermission = "ca.bypass.level" + // Permission string required to reload the config const val commandReloadPermission = "ca.command.reload" + // Permission string required to edit the plugin's config const val editConfigPermission = "ca.config.edit" // Command Name to reload the config const val commandReloadName = "anvilconfigreload" + // Test command name const val commandTestName = "customanvilconfig" @@ -71,7 +76,7 @@ class CustomAnvil : JavaPlugin() { // Disable old plugin name if exist val potentialPlugin = Bukkit.getPluginManager().getPlugin("UnsafeEnchantsPlus") - if(potentialPlugin != null){ + if (potentialPlugin != null) { Bukkit.getPluginManager().disablePlugin(potentialPlugin) logger.warning("An old version of this plugin was detected") logger.warning("Please note CustomAnvil is a more recent version of UnsafeEnchantsPlus") @@ -83,7 +88,7 @@ class CustomAnvil : JavaPlugin() { // Load config val success = ConfigHolder.loadConfig() - if(!success) return + if (!success) return // Load metrics val metric = Metrics(this, bstatsPluginId) @@ -98,26 +103,28 @@ class CustomAnvil : JavaPlugin() { ) } - fun reloadResource(resourceName: String, - hardFailSafe:Boolean = true): YamlConfiguration?{ + fun reloadResource( + resourceName: String, + hardFailSafe: Boolean = true + ): YamlConfiguration? { // Save default resource - val file = File(dataFolder,resourceName) - if(!file.exists()){ - saveResource(resourceName,false) + val file = File(dataFolder, resourceName) + if (!file.exists()) { + saveResource(resourceName, false) } // Load resource val yamlConfig = YamlConfiguration() try { val configReader = FileReader(file) yamlConfig.load(configReader) - } catch (test: Exception){ - if(hardFailSafe){ + } catch (test: Exception) { + if (hardFailSafe) { // This is important and may impact gameplay if it does not load. // Failsafe is to stop the plugin logger.severe("Resource $resourceName Could not be load or reload.") logger.severe("Disabling plugin.") Bukkit.getPluginManager().disablePlugin(this) - }else{ + } else { logger.warning("Resource $resourceName Could not be load or reload.") } return null diff --git a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt index fcbf42a..7d3b6d7 100644 --- a/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt +++ b/src/main/kotlin/io/delilaheve/util/ConfigOptions.kt @@ -12,67 +12,94 @@ object ConfigOptions { // Path for default enchantment limits private const val DEFAULT_LIMIT_PATH = "default_limit" + // Path for limiting repair cost const val LIMIT_REPAIR_COST = "limit_repair_cost" + // Path for repair value limit const val LIMIT_REPAIR_VALUE = "limit_repair_value" + // Path for level cost on item repair const val ITEM_REPAIR_COST = "item_repair_cost" + // Path for level cost on unit repair const val UNIT_REPAIR_COST = "unit_repair_cost" + // Path for level cost on item renaming const val ITEM_RENAME_COST = "item_rename_cost" + // Path for level cost on illegal enchantment on sacrifice const val SACRIFICE_ILLEGAL_COST = "sacrifice_illegal_enchant_cost" + // Path for removing repair cost limits const val REMOVE_REPAIR_LIMIT = "remove_repair_limit" + // Root path for enchantment limits const val ENCHANT_LIMIT_ROOT = "enchant_limits" + // Root path for enchantment values const val ENCHANT_VALUES_ROOT = "enchant_values" + // Keys for specific enchantment values private const val KEY_BOOK = "book" private const val KEY_ITEM = "item" + // Debug logging toggle path private const val DEBUG_LOGGING = "debug_log" + // Debug verbose logging toggle path private const val VERBOSE_DEBUG_LOGGING = "debug_log_verbose" // Default value for enchantment limits private const val DEFAULT_ENCHANT_LIMIT = 5 + // Default value for limiting repair cost const val DEFAULT_LIMIT_REPAIR = false + // Default value for repair cost limit const val DEFAULT_LIMIT_REPAIR_VALUE = 39 + // Default value for level cost on item repair const val DEFAULT_ITEM_REPAIR_COST = 2 + // Default value for level cost per unit repair const val DEFAULT_UNIT_REPAIR_COST = 1 + // Default value for level cost on item renaming const val DEFAULT_ITEM_RENAME_COST = 1 + // Default value for level cost on illegal enchantment on sacrifice const val DEFAULT_SACRIFICE_ILLEGAL_COST = 1 + // Valid range for repair cost limit @JvmField val REPAIR_LIMIT_RANGE = 1..39 + // Valid range for repair cost @JvmField val REPAIR_COST_RANGE = 0..255 + // Valid range for rename cost @JvmField val ITEM_RENAME_COST_RANGE = 0..255 + // Valid range for illegal enchantment conflict cost @JvmField val SACRIFICE_ILLEGAL_COST_RANGE = 0..255 + // Default for removing repair cost limits const val DEFAULT_REMOVE_LIMIT = false + // Valid range for an enchantment limit @JvmField val ENCHANT_LIMIT_RANGE = 1..255 + // Default value for an enchantment multiplier private const val DEFAULT_ENCHANT_VALUE = 0 + // Default value for debug logging private const val DEFAULT_DEBUG_LOG = false + // Default value for debug logging private const val DEFAULT_VERBOSE_DEBUG_LOG = false @@ -155,6 +182,7 @@ object ConfigOptions { .takeIf { it in SACRIFICE_ILLEGAL_COST_RANGE } ?: DEFAULT_SACRIFICE_ILLEGAL_COST } + /** * Whether to remove repair cost limit */ @@ -217,8 +245,9 @@ object ConfigOptions { /** * Get an array of key of basic config options */ - fun getBasicConfigKeys(): Array{ - return arrayOf(DEFAULT_LIMIT_PATH, + fun getBasicConfigKeys(): Array { + return arrayOf( + DEFAULT_LIMIT_PATH, LIMIT_REPAIR_COST, LIMIT_REPAIR_VALUE, ITEM_REPAIR_COST, diff --git a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt index 24a0f97..069a0ed 100644 --- a/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/EnchantmentUtil.kt @@ -34,43 +34,51 @@ object EnchantmentUtil { if (!containsKey(enchantment)) { // Add the enchantment if it doesn't have conflicts, or if player is allowed to bypass enchantment restrictions this[enchantment] = level - val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment); - if(!player.hasPermission(CustomAnvil.bypassFusePermission) && - (conflictType != ConflictType.NO_CONFLICT)){ + val conflictType = + ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys, mat, enchantment) + if (!player.hasPermission(CustomAnvil.bypassFusePermission) && + (conflictType != ConflictType.NO_CONFLICT) + ) { CustomAnvil.verboseLog("Enchantment not yet in result list, but there is conflict (${enchantment.key}, conflict: $conflictType)") this.remove(enchantment) } } // Enchantment already in result list - else{ + else { // ... and they are conflicting - val conflictType = ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys,mat,enchantment) - if((conflictType != ConflictType.NO_CONFLICT) - && !player.hasPermission(CustomAnvil.bypassFusePermission)){ + val conflictType = + ConfigHolder.CONFLICT_HOLDER.conflictManager.isConflicting(this.keys, mat, enchantment) + if ((conflictType != ConflictType.NO_CONFLICT) + && !player.hasPermission(CustomAnvil.bypassFusePermission) + ) { CustomAnvil.verboseLog("Enchantment already in result list, and they are conflicting (${enchantment.key}, conflict: $conflictType)") return@forEach } // ... and they're not the same level - if(this[enchantment] != other[enchantment]){ + if (this[enchantment] != other[enchantment]) { val newLevel = max(this[enchantment] ?: 0, other[enchantment] ?: 0) // apply the greater of the two if non-zero - if (newLevel > 0) { this[enchantment] = newLevel } + if (newLevel > 0) { + this[enchantment] = newLevel + } } // ... and they're the same level else { // try to increase the enchantment level by 1 - var newLevel = this[enchantment]!! +1 + var newLevel = this[enchantment]!! + 1 // Get max level or 255 if player can bypass - val maxLevel = if(player.hasPermission(CustomAnvil.bypassLevelPermission)){ + val maxLevel = if (player.hasPermission(CustomAnvil.bypassLevelPermission)) { 255 - }else{ + } else { ConfigOptions.enchantLimit(enchantment) } newLevel = min(newLevel, maxLevel) - if (newLevel > 0) { this[enchantment] = newLevel } + if (newLevel > 0) { + this[enchantment] = newLevel + } } } } diff --git a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt index f438fae..7eed593 100644 --- a/src/main/kotlin/io/delilaheve/util/ItemUtil.kt +++ b/src/main/kotlin/io/delilaheve/util/ItemUtil.kt @@ -75,7 +75,7 @@ object ItemUtil { (itemMeta as? Damageable)?.let { val durability = type.maxDurability.toInt() val firstDamage = (first.itemMeta as? Damageable)?.damage ?: 0 - if( firstDamage == 0) return false + if (firstDamage == 0) return false val firstDurability = durability - firstDamage val secondDamage = (second.itemMeta as? Damageable)?.damage ?: 0 @@ -96,12 +96,12 @@ object ItemUtil { (itemMeta as? Damageable)?.let { val durability = type.maxDurability.toInt() val firstDamage = it.damage - if( firstDamage == 0) return 0 + if (firstDamage == 0) return 0 var unitCount = 0 var damage = firstDamage - while((unitCount < unitAmount) && (damage > 0)){ + while ((unitCount < unitAmount) && (damage > 0)) { unitCount++ - damage = ceil(firstDamage - durability*percentPerUnit*unitCount).toInt() + damage = ceil(firstDamage - durability * percentPerUnit * unitCount).toInt() } it.damage = max(damage, 0) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index a978abc..c45da8c 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -11,11 +11,11 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions class EditConfigExecutor : CommandExecutor { override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { - if(!sender.hasPermission(CustomAnvil.editConfigPermission)) { + if (!sender.hasPermission(CustomAnvil.editConfigPermission)) { sender.sendMessage(GuiGlobalActions.NO_EDIT_PERM) return false } - if(sender !is HumanEntity) return false + if (sender !is HumanEntity) return false MainConfigGui.INSTANCE.show(sender) return true diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index 4268cbf..459db0f 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -13,18 +13,18 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil class ReloadExecutor : CommandExecutor { override fun onCommand(sender: CommandSender, cmd: Command, cmdstr: String, args: Array): Boolean { - if(!sender.hasPermission(CustomAnvil.commandReloadPermission)) { + if (!sender.hasPermission(CustomAnvil.commandReloadPermission)) { sender.sendMessage("§cYou do not have permission to reload the config") return false } sender.sendMessage("§eReloading config...") - val hardfail = args.isNotEmpty() && ("hard".equals(args[0],true)) + val hardfail = args.isNotEmpty() && ("hard".equals(args[0], true)) val commandSuccess = commandBody(hardfail) - if(commandSuccess){ + if (commandSuccess) { sender.sendMessage("§aConfig reloaded !") - }else{ + } else { sender.sendMessage("§cConfig was not able to be reloaded...") - if(hardfail){ + if (hardfail) { sender.sendMessage("§4Hard fail, plugin disabled") } } @@ -34,9 +34,9 @@ class ReloadExecutor : CommandExecutor { /** * Execute the command, return true if success or false otherwise */ - private fun commandBody(hardfail: Boolean): Boolean{ + private fun commandBody(hardfail: Boolean): Boolean { try { - if(!ConfigHolder.reloadAllFromDisk(hardfail)) return false + if (!ConfigHolder.reloadAllFromDisk(hardfail)) return false // Then update all global gui containing value from config BasicConfigGui.INSTANCE.updateGuiValues() @@ -48,7 +48,7 @@ class ReloadExecutor : CommandExecutor { MetricsUtil.testIfConfigIsDefault() return true - }catch (e: Exception){ + } catch (e: Exception) { e.printStackTrace() return false } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index 1ff4242..f13b039 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -4,7 +4,7 @@ import org.bukkit.Material import java.util.* abstract class AbstractMaterialGroup(private val name: String) { - protected val includedMaterial by lazy {createDefaultSet()} + protected val includedMaterial by lazy { createDefaultSet() } /** * Get the group default set @@ -14,24 +14,24 @@ abstract class AbstractMaterialGroup(private val name: String) { /** * Get if a material is allowed following the group policy */ - open fun contain(mat : Material): Boolean { + open fun contain(mat: Material): Boolean { return mat in getMaterials() } /** * Get if a group is referenced by this: */ - abstract fun isReferencing(other : AbstractMaterialGroup): Boolean + abstract fun isReferencing(other: AbstractMaterialGroup): Boolean /** * Push a material to this group to follow this group policy */ - abstract fun addToPolicy(mat : Material) + abstract fun addToPolicy(mat: Material) /** * Push a group to this group to follow this group policy */ - abstract fun addToPolicy(other : AbstractMaterialGroup) + abstract fun addToPolicy(other: AbstractMaterialGroup) /** * Get the group contained material as a set @@ -44,6 +44,7 @@ abstract class AbstractMaterialGroup(private val name: String) { open fun getNonGroupInheritedMaterials(): EnumSet { return includedMaterial } + /** * Get the group non-inherited material as a set */ @@ -69,19 +70,19 @@ abstract class AbstractMaterialGroup(private val name: String) { */ abstract fun getGroups(): MutableSet - open fun getRepresentativeMaterial() : Material { + open fun getRepresentativeMaterial(): Material { // Test inner material val matIterator = includedMaterial.iterator() - while(matIterator.hasNext()){ + while (matIterator.hasNext()) { val material = matIterator.next() - if(material.isAir) continue + if (material.isAir) continue return material } // Test included group representative material val groupIterator = getGroups().iterator() - while (groupIterator.hasNext()){ + while (groupIterator.hasNext()) { val groupMat = groupIterator.next().getRepresentativeMaterial() - if(groupMat.isAir) continue + if (groupMat.isAir) continue return groupMat } return Material.PAPER diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index e3c9af3..3e5c5d0 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -7,29 +7,30 @@ import org.bukkit.enchantments.Enchantment class EnchantConflictGroup( val name: String, private val cantConflict: AbstractMaterialGroup, - val minBeforeBlock: Int){ + val minBeforeBlock: Int +) { private val enchantments = HashSet() - fun addEnchantment(enchant: Enchantment){ + fun addEnchantment(enchant: Enchantment) { enchantments.add(enchant) } - fun allowed(enchants: Set, mat: Material) : Boolean{ - if(enchantments.size < minBeforeBlock){ + fun allowed(enchants: Set, mat: Material): Boolean { + if (enchantments.size < minBeforeBlock) { return true } - if(cantConflict.contain(mat)){ + if (cantConflict.contain(mat)) { return true } // Count the amount of enchantment that are in the list var enchantAmount = 0 for (enchantment in enchants) { - if(enchantment !in enchantments) continue + if (enchantment !in enchantments) continue CustomAnvil.verboseLog("Enchant ${enchantment.key} is in: ${enchantAmount + 1}/$minBeforeBlock ") - if(++enchantAmount > minBeforeBlock){ + if (++enchantAmount > minBeforeBlock) { return false } @@ -37,8 +38,8 @@ class EnchantConflictGroup( return true } - fun getCantConflictGroup(): AbstractMaterialGroup{ - return this.cantConflict; + fun getCantConflictGroup(): AbstractMaterialGroup { + return this.cantConflict } fun getEnchants(): HashSet { @@ -53,9 +54,9 @@ class EnchantConflictGroup( fun getRepresentativeMaterial(): Material { val groups = getCantConflictGroup().getGroups() val groupIterator = groups.iterator() - while (groupIterator.hasNext()){ + while (groupIterator.hasNext()) { val mat = groupIterator.next().getRepresentativeMaterial() - if(mat != Material.ENCHANTED_BOOK) return mat + if (mat != Material.ENCHANTED_BOOK) return mat } return Material.ENCHANTED_BOOK diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index c5d8c8c..80aebf7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -11,13 +11,17 @@ class EnchantConflictManager { companion object { // Path for the enchantments list const val ENCH_LIST_PATH = "enchantments" + // Path for group list related to the conflict const val CONFLICT_GROUP_PATH = "notAffectedGroups" + // Path for the maximum number of enchantment before validating the conflict const val ENCH_MAX_PATH = "maxEnchantmentBeforeConflict" + // Path for a flag: if the enchantment will be used in the last supported version // TODO maybe replace this system by a list of "future" enchantment. private const val FUTURE_USE_PATH = "useInFuture" + // Default name for a joining group private const val DEFAULT_GROUP_NAME = "joinedGroup" } @@ -26,64 +30,64 @@ class EnchantConflictManager { lateinit var conflictList: ArrayList // Read and prepare all conflict - fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager){ + fun prepareConflicts(config: ConfigurationSection, itemManager: ItemGroupManager) { conflictMap = HashMap() conflictList = ArrayList() val keys = config.getKeys(false) for (key in keys) { val section = config.getConfigurationSection(key)!! - val conflict = createConflict(section,itemManager,key) - if(conflict != null){ - addToMap(conflict) - conflictList.add(conflict) - } + val conflict = createConflict(section, itemManager, key) + addToMap(conflict) + conflictList.add(conflict) } } // Add the conflict to the map - private fun addToMap(conflict: EnchantConflictGroup){ - conflict.getEnchants().forEach{ enchant -> - addConflictToConflictMap(enchant, conflict); + private fun addToMap(conflict: EnchantConflictGroup) { + conflict.getEnchants().forEach { enchant -> + addConflictToConflictMap(enchant, conflict) } } - fun addConflictToConflictMap(enchant: Enchantment, conflict: EnchantConflictGroup){ - if(!conflictMap.containsKey(enchant)){ + fun addConflictToConflictMap(enchant: Enchantment, conflict: EnchantConflictGroup) { + if (!conflictMap.containsKey(enchant)) { conflictMap[enchant] = ArrayList() } conflictMap[enchant]!!.add(conflict) } - fun removeConflictFromMap(enchant: Enchantment, conflict: EnchantConflictGroup): Boolean{ + fun removeConflictFromMap(enchant: Enchantment, conflict: EnchantConflictGroup): Boolean { return conflictMap[enchant]!!.remove(conflict) } // create and read a conflict from a yaml section - private fun createConflict(section: ConfigurationSection, - itemManager: ItemGroupManager, - conflictName: String): EnchantConflictGroup? { + private fun createConflict( + section: ConfigurationSection, + itemManager: ItemGroupManager, + conflictName: String + ): EnchantConflictGroup { // Is it planed for the future - val futureUse = section.getBoolean(FUTURE_USE_PATH,false) + val futureUse = section.getBoolean(FUTURE_USE_PATH, false) // Create conflict - val conflict = createConflictObject(section,itemManager,conflictName) + val conflict = createConflictObject(section, itemManager, conflictName) // Read and add enchantment to conflict val enchantList = section.getStringList(ENCH_LIST_PATH) for (enchantName in enchantList) { val enchantKey = NamespacedKey.minecraft(enchantName) val enchant = Enchantment.getByKey(enchantKey) - if(enchant == null){ - if(!futureUse){ + if (enchant == null) { + if (!futureUse) { CustomAnvil.instance.logger.warning("Enchantment $enchantName do not exist but was asked for conflict $conflictName") } continue } conflict.addEnchantment(enchant) } - if(conflict.getEnchants().size == 0){ - if(!futureUse){ + if (conflict.getEnchants().size == 0) { + if (!futureUse) { CustomAnvil.instance.logger.warning("Conflict $conflictName do not have valid enchantment, it will not do anything") } } @@ -91,12 +95,14 @@ class EnchantConflictManager { return conflict } - private fun createConflictObject(section: ConfigurationSection, - itemManager: ItemGroupManager, - conflictName: String): EnchantConflictGroup { + private fun createConflictObject( + section: ConfigurationSection, + itemManager: ItemGroupManager, + conflictName: String + ): EnchantConflictGroup { // Get the maximum number of enchantment before validating the conflict - var minBeforeBlock = section.getInt(ENCH_MAX_PATH,0) - if(minBeforeBlock < 0){ + var minBeforeBlock = section.getInt(ENCH_MAX_PATH, 0) + if (minBeforeBlock < 0) { minBeforeBlock = 0 CustomAnvil.instance.logger.warning("Conflict $conflictName have an invalid value of $ENCH_MAX_PATH") CustomAnvil.instance.logger.warning("It should be more or equal to 0. default to 0") @@ -112,9 +118,13 @@ class EnchantConflictManager { return EnchantConflictGroup(conflictName, finalGroup, minBeforeBlock) } - private fun findGroup(groupName: String, itemManager: ItemGroupManager, conflictName: String): AbstractMaterialGroup { + private fun findGroup( + groupName: String, + itemManager: ItemGroupManager, + conflictName: String + ): AbstractMaterialGroup { val group = itemManager.get(groupName) - if(group == null){ + if (group == null) { CustomAnvil.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName") return IncludeGroup("error_placeholder") } @@ -122,7 +132,7 @@ class EnchantConflictManager { return group } - fun isConflicting(base: Set,mat: Material, newEnchant: Enchantment): ConflictType { + fun isConflicting(base: Set, mat: Material, newEnchant: Enchantment): ConflictType { CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}") val conflictList = conflictMap[newEnchant] ?: return ConflictType.NO_CONFLICT CustomAnvil.verboseLog("Did not get skipped") @@ -130,13 +140,13 @@ class EnchantConflictManager { var result = ConflictType.NO_CONFLICT for (conflict in conflictList) { CustomAnvil.verboseLog("Is against ${conflict.name}") - val conflicting = conflict.allowed(base,mat) + val conflicting = conflict.allowed(base, mat) CustomAnvil.verboseLog("Was against ${conflict.name} and conflicting: $conflicting ") - if(!conflicting) { - if(conflict.getEnchants().size <= 1){ + if (!conflicting) { + if (conflict.getEnchants().size <= 1) { result = ConflictType.SMALL_CONFLICT CustomAnvil.verboseLog("Small conflict, continuing") - }else{ + } else { return ConflictType.BIG_CONFLICT } } @@ -146,7 +156,7 @@ class EnchantConflictManager { } -enum class ConflictType{ +enum class ConflictType { NO_CONFLICT, SMALL_CONFLICT, BIG_CONFLICT diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt index 82a691b..609f0f5 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt @@ -3,17 +3,17 @@ package xyz.alexcrea.cuanvil.group import org.bukkit.Material import java.util.* -class ExcludeGroup(name: String): AbstractMaterialGroup(name) { +class ExcludeGroup(name: String) : AbstractMaterialGroup(name) { override fun createDefaultSet(): EnumSet { return EnumSet.allOf(Material::class.java) } private var includedGroup: MutableSet = HashSet() - private val groupItems by lazy {createDefaultSet()} + private val groupItems by lazy { createDefaultSet() } override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { - if((materialGroup == other) || (materialGroup.isReferencing(other))){ + if ((materialGroup == other) || (materialGroup.isReferencing(other))) { return true } } @@ -36,7 +36,7 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { includedGroup.clear() groups.forEach { group -> - if(!group.isReferencing(this)) { + if (!group.isReferencing(this)) { includedGroup.add(group) groupItems.removeAll(group.getMaterials()) } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt index 05a85c2..33354f1 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt @@ -3,17 +3,17 @@ package xyz.alexcrea.cuanvil.group import org.bukkit.Material import java.util.* -class IncludeGroup(name: String): AbstractMaterialGroup(name) { +class IncludeGroup(name: String) : AbstractMaterialGroup(name) { override fun createDefaultSet(): EnumSet { return EnumSet.noneOf(Material::class.java) } private var includedGroup: MutableSet = HashSet() - private val groupItems by lazy {createDefaultSet()} + private val groupItems by lazy { createDefaultSet() } override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { - if((materialGroup == other) || (materialGroup.isReferencing(other))){ + if ((materialGroup == other) || (materialGroup.isReferencing(other))) { return true } } @@ -36,7 +36,7 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { includedGroup.clear() groups.forEach { group -> - if(!group.isReferencing(this)){ + if (!group.isReferencing(this)) { includedGroup.add(group) groupItems.addAll(group.getMaterials()) } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt index 6403e73..e1fb59b 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt @@ -4,49 +4,53 @@ import io.delilaheve.CustomAnvil import org.bukkit.Material import org.bukkit.configuration.ConfigurationSection import java.util.* -import kotlin.collections.LinkedHashMap class ItemGroupManager { companion object { // Path for group type private const val GROUP_TYPE_PATH = "type" + // Path for included items list private const val MATERIAL_LIST_PATH = "items" + // Path for included groups list private const val GROUP_LIST_PATH = "groups" + // Temporary list of elements in default config that are use in future - private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH") + private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD", "BRUSH") } - lateinit var groupMap : LinkedHashMap + lateinit var groupMap: LinkedHashMap // Read and create material groups - fun prepareGroups(config: ConfigurationSection){ + fun prepareGroups(config: ConfigurationSection) { groupMap = LinkedHashMap() val keys = config.getKeys(false) for (key in keys) { - if(groupMap.containsKey(key)) + if (groupMap.containsKey(key)) continue createGroup(config, keys, key) } } // Create group by key - private fun createGroup(config: ConfigurationSection, - keys: Set, - key: String): AbstractMaterialGroup { + private fun createGroup( + config: ConfigurationSection, + keys: Set, + key: String + ): AbstractMaterialGroup { val groupSection = config.getConfigurationSection(key)!! - val groupType = groupSection.getString(GROUP_TYPE_PATH,null) + val groupType = groupSection.getString(GROUP_TYPE_PATH, null) // Create Material group according to the group type val group: AbstractMaterialGroup - if(GroupType.EXCLUDE.equal(groupType)){ + if (GroupType.EXCLUDE.equal(groupType)) { group = ExcludeGroup(key) - }else { + } else { group = IncludeGroup(key) - if(!GroupType.INCLUDE.equal(groupType)){ + if (!GroupType.INCLUDE.equal(groupType)) { CustomAnvil.instance.logger.warning("Group $key have an invalid group type. default to Include.") } } @@ -57,20 +61,23 @@ class ItemGroupManager { } // Read Group elements - private fun readGroup(group: AbstractMaterialGroup, - groupSection: ConfigurationSection, - config: ConfigurationSection, - keys: Set){ + private fun readGroup( + group: AbstractMaterialGroup, + groupSection: ConfigurationSection, + config: ConfigurationSection, + keys: Set + ) { // Read material to include in this group policy val materialList = groupSection.getStringList(MATERIAL_LIST_PATH) for (materialTemp in materialList) { val materialName = materialTemp.uppercase(Locale.getDefault()) val material = Material.getMaterial(materialName) - if(material == null){ + if (material == null) { // Check if we should warn the user - if(materialName !in FUTURE_MATERIAL){ + if (materialName !in FUTURE_MATERIAL) { CustomAnvil.instance.logger.warning( - "Unknown material $materialTemp on group ${group.getName()}") + "Unknown material $materialTemp on group ${group.getName()}" + ) } continue @@ -82,23 +89,26 @@ class ItemGroupManager { // please note the group name is case-sensitive. val groupList = groupSection.getStringList(GROUP_LIST_PATH) for (groupName in groupList) { - if(groupName !in keys){ + if (groupName !in keys) { CustomAnvil.instance.logger.warning( - "Group $groupName do not exist but is included in group ${group.getName()}") + "Group $groupName do not exist but is included in group ${group.getName()}" + ) continue } // Get other group or create it if not yet created - val otherGroup = if(!groupMap.containsKey(groupName)){ - createGroup(config,keys,groupName) - }else{ + val otherGroup = if (!groupMap.containsKey(groupName)) { + createGroup(config, keys, groupName) + } else { groupMap[groupName]!! } // Avoid self reference or it will create an infinite loop - if(otherGroup.isReferencing(group)){ + if (otherGroup.isReferencing(group)) { CustomAnvil.instance.logger.warning( - "Group $groupName is on a reference loop with group ${group.getName()} !") + "Group $groupName is on a reference loop with group ${group.getName()} !" + ) CustomAnvil.instance.logger.warning( - "Please fix it in your item_groups config or the plugin will probably not work as expected.") + "Please fix it in your item_groups config or the plugin will probably not work as expected." + ) continue } @@ -121,10 +131,10 @@ enum class GroupType(private val groupID: String) { ; // Test if string is equal to the groupID of this enum - fun equal(toTest: String?): Boolean{ - if(toTest == null) + fun equal(toTest: String?): Boolean { + if (toTest == null) return false - return groupID.contentEquals(toTest.lowercase(Locale.getDefault())) + return groupID.contentEquals(toTest.lowercase(Locale.getDefault())) } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt index a7ea8da..4ea85fd 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/ChatEventListener.kt @@ -11,7 +11,7 @@ import java.util.* import java.util.concurrent.ConcurrentHashMap import java.util.function.Consumer -class ChatEventListener : Listener{ +class ChatEventListener : Listener { private val playerListenMap: ConcurrentHashMap> = ConcurrentHashMap() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt index 8d6b246..0f60e48 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/MetricsUtil.kt @@ -31,12 +31,11 @@ object MetricsUtil { */ private fun getHashFromKey(section: ConfigurationSection, key: String): Int { // Key is assumend to exist - val resultHash: Int - if(section.isConfigurationSection(key)){ + val resultHash = if (section.isConfigurationSection(key)) { val sectionResult = getConfigurationHash(section.getConfigurationSection(key)!!) - resultHash = key.hashCode() xor sectionResult - }else{ - resultHash = key.hashCode() xor section.getString(key).hashCode() + key.hashCode() xor sectionResult + } else { + key.hashCode() xor section.getString(key).hashCode() } return resultHash.hashCode() } @@ -47,7 +46,7 @@ object MetricsUtil { private fun getConfigurationHash(section: ConfigurationSection): Int { var resultHash = 0 for (key in section.getKeys(false)) { - resultHash = resultHash xor getHashFromKey(section,key) + resultHash = resultHash xor getHashFromKey(section, key) } return resultHash } @@ -55,10 +54,10 @@ object MetricsUtil { /** * Get hash value of the default config */ - private fun testBaseConfig(defaultConfig: ConfigurationSection): Int{ + private fun testBaseConfig(defaultConfig: ConfigurationSection): Int { var result = 0 for (key in ConfigOptions.getBasicConfigKeys()) { - result = result xor getHashFromKey(defaultConfig,key) + result = result xor getHashFromKey(defaultConfig, key) } return result } @@ -66,7 +65,7 @@ object MetricsUtil { /** * Test if the used configuration is the default config */ - fun testIfConfigIsDefault(){ + fun testIfConfigIsDefault() { // Calculate hash of config val baseConfig = testBaseConfig(ConfigHolder.DEFAULT_CONFIG.config) val limitEnchantConfig = getHashFromKey(ConfigHolder.DEFAULT_CONFIG.config, ConfigOptions.ENCHANT_LIMIT_ROOT) @@ -77,37 +76,49 @@ object MetricsUtil { // Test if default isDefaultBaseConfig = baseConfigHash == baseConfig isDefaultEnchantLimitsConfig = enchantLimitsConfigHash == limitEnchantConfig - isDefaultEnchantValuesConfig = enchantValuesConfigHash == enchantValueConfig + isDefaultEnchantValuesConfig = enchantValuesConfigHash == enchantValueConfig isDefaultEnchantConflictConfig = enchantConflictConfigHash == enchantConflictConfig isDefaultItemGroupsConfig = itemGroupsConfigHash == itemGroupConfig isDefaultUnitRepairItemConfig = unitRepairItemConfigHash == unitRepairConfig // If not default and debug flag active, print the hash. - if(ConfigOptions.debugLog){ - if(!isDefaultBaseConfig){CustomAnvil.log("baseConfig: $baseConfig")} - if(!isDefaultEnchantLimitsConfig){CustomAnvil.log("limitEnchantConfig: $limitEnchantConfig")} - if(!isDefaultEnchantValuesConfig){CustomAnvil.log("enchantValueConfig: $enchantValueConfig")} - if(!isDefaultEnchantConflictConfig){CustomAnvil.log("enchantConflictConfig: $enchantConflictConfig")} - if(!isDefaultItemGroupsConfig){CustomAnvil.log("itemGroupConfig: $itemGroupConfig")} - if(!isDefaultUnitRepairItemConfig){CustomAnvil.log("unitRepairConfig: $unitRepairConfig")} + if (ConfigOptions.debugLog) { + if (!isDefaultBaseConfig) { + CustomAnvil.log("baseConfig: $baseConfig") + } + if (!isDefaultEnchantLimitsConfig) { + CustomAnvil.log("limitEnchantConfig: $limitEnchantConfig") + } + if (!isDefaultEnchantValuesConfig) { + CustomAnvil.log("enchantValueConfig: $enchantValueConfig") + } + if (!isDefaultEnchantConflictConfig) { + CustomAnvil.log("enchantConflictConfig: $enchantConflictConfig") + } + if (!isDefaultItemGroupsConfig) { + CustomAnvil.log("itemGroupConfig: $itemGroupConfig") + } + if (!isDefaultUnitRepairItemConfig) { + CustomAnvil.log("unitRepairConfig: $unitRepairConfig") + } } } - fun notifyChange(holder: ConfigHolder, path: String){ - if(ConfigHolder.DEFAULT_CONFIG.equals(holder)){ - if(path.startsWith(ConfigOptions.ENCHANT_LIMIT_ROOT+".")){ - isDefaultEnchantLimitsConfig = false; - }else if(path.startsWith(ConfigOptions.ENCHANT_VALUES_ROOT+".")){ - isDefaultEnchantValuesConfig = false; - }else{ - isDefaultBaseConfig = false; + fun notifyChange(holder: ConfigHolder, path: String) { + if (ConfigHolder.DEFAULT_CONFIG.equals(holder)) { + if (path.startsWith(ConfigOptions.ENCHANT_LIMIT_ROOT + ".")) { + isDefaultEnchantLimitsConfig = false + } else if (path.startsWith(ConfigOptions.ENCHANT_VALUES_ROOT + ".")) { + isDefaultEnchantValuesConfig = false + } else { + isDefaultBaseConfig = false } - }else if(ConfigHolder.CONFLICT_HOLDER.equals(holder)){ - isDefaultEnchantConflictConfig = false; - }else if(ConfigHolder.ITEM_GROUP_HOLDER.equals(holder)){ - isDefaultItemGroupsConfig = false; - }else if(ConfigHolder.UNIT_REPAIR_HOLDER.equals(holder)){ - isDefaultUnitRepairItemConfig = false; + } else if (ConfigHolder.CONFLICT_HOLDER.equals(holder)) { + isDefaultEnchantConflictConfig = false + } else if (ConfigHolder.ITEM_GROUP_HOLDER.equals(holder)) { + isDefaultItemGroupsConfig = false + } else if (ConfigHolder.UNIT_REPAIR_HOLDER.equals(holder)) { + isDefaultUnitRepairItemConfig = false } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt index a71d912..141d3a0 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt @@ -8,6 +8,7 @@ object UnitRepairUtil { // Default value for user set default unit repair % private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25 + // Path to user default unit repair value private const val UNIT_REPAIR_DEFAULT_PATH = "default_repair_amount" @@ -18,23 +19,23 @@ object UnitRepairUtil { fun ItemStack.getRepair( other: ItemStack? ): Double? { - if(other == null) return null + if (other == null) return null val config = ConfigHolder.UNIT_REPAIR_HOLDER.config // Get configuration section if exist val otherName = other.type.name.uppercase() var section = config.getConfigurationSection(otherName) - if(section == null){ + if (section == null) { section = config.getConfigurationSection(otherName.lowercase()) - if(section == null) return null + if (section == null) return null } // Get repair amount var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR) - if(userDefault <= 0){ + if (userDefault <= 0) { userDefault = DEFAULT_DEFAULT_UNIT_REPAIR } - return getRepairAmount(this,section,userDefault) + return getRepairAmount(this, section, userDefault) } /** @@ -42,16 +43,16 @@ object UnitRepairUtil { * null if not found. * If value is set to less than or equal to 0 then it will be set to default */ - private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double?{ + private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double? { val itemName = item.type.name.uppercase() - val repairValue = if(section.isDouble(itemName)){ + val repairValue = if (section.isDouble(itemName)) { section.getDouble(itemName) - }else if(section.isDouble(itemName.lowercase())){ + } else if (section.isDouble(itemName.lowercase())) { section.getDouble(itemName.lowercase()) - }else{ + } else { return null } - if(repairValue <= 0) + if (repairValue <= 0) return default return repairValue } diff --git a/src/main/resources/enchant_conflict.yml b/src/main/resources/enchant_conflict.yml index 86492f4..dfc5261 100644 --- a/src/main/resources/enchant_conflict.yml +++ b/src/main/resources/enchant_conflict.yml @@ -12,159 +12,159 @@ # ---------------------------------------------------- restriction_aqua_affinity: - enchantments: [aqua_affinity] - notAffectedGroups: [enchanted_book, helmets] + enchantments: [ aqua_affinity ] + notAffectedGroups: [ enchanted_book, helmets ] restriction_bane_of_arthropods: - enchantments: [bane_of_arthropods] - notAffectedGroups: [enchanted_book, melee_weapons] + enchantments: [ bane_of_arthropods ] + notAffectedGroups: [ enchanted_book, melee_weapons ] restriction_blast_protection: - enchantments: [blast_protection] - notAffectedGroups: [enchanted_book, armors] + enchantments: [ blast_protection ] + notAffectedGroups: [ enchanted_book, armors ] restriction_channeling: - enchantments: [channeling] - notAffectedGroups: [enchanted_book, trident] + enchantments: [ channeling ] + notAffectedGroups: [ enchanted_book, trident ] restriction_binding_curse: - enchantments: [binding_curse] - notAffectedGroups: [enchanted_book, wearable] + enchantments: [ binding_curse ] + notAffectedGroups: [ enchanted_book, wearable ] restriction_vanishing_curse: - enchantments: [vanishing_curse] - notAffectedGroups: [enchanted_book, can_vanish] + enchantments: [ vanishing_curse ] + notAffectedGroups: [ enchanted_book, can_vanish ] restriction_depth_strider: - enchantments: [depth_strider] - notAffectedGroups: [enchanted_book, boots] + enchantments: [ depth_strider ] + notAffectedGroups: [ enchanted_book, boots ] restriction_efficiency: - enchantments: [efficiency] - notAffectedGroups: [enchanted_book, tools, shears] + enchantments: [ efficiency ] + notAffectedGroups: [ enchanted_book, tools, shears ] restriction_feather_falling: - enchantments: [feather_falling] - notAffectedGroups: [enchanted_book, boots] + enchantments: [ feather_falling ] + notAffectedGroups: [ enchanted_book, boots ] restriction_fire_aspect: - enchantments: [fire_aspect] - notAffectedGroups: [enchanted_book, swords] + enchantments: [ fire_aspect ] + notAffectedGroups: [ enchanted_book, swords ] restriction_fire_protection: - enchantments: [fire_protection] - notAffectedGroups: [enchanted_book, armors] + enchantments: [ fire_protection ] + notAffectedGroups: [ enchanted_book, armors ] restriction_flame: - enchantments: [flame] - notAffectedGroups: [enchanted_book, bow] + enchantments: [ flame ] + notAffectedGroups: [ enchanted_book, bow ] restriction_fortune: - enchantments: [fortune] - notAffectedGroups: [enchanted_book, tools] + enchantments: [ fortune ] + notAffectedGroups: [ enchanted_book, tools ] restriction_frost_walker: - enchantments: [frost_walker] - notAffectedGroups: [enchanted_book, boots] + enchantments: [ frost_walker ] + notAffectedGroups: [ enchanted_book, boots ] restriction_impaling: - enchantments: [impaling] - notAffectedGroups: [enchanted_book, trident] + enchantments: [ impaling ] + notAffectedGroups: [ enchanted_book, trident ] restriction_infinity: - enchantments: [infinity] - notAffectedGroups: [enchanted_book, bow] + enchantments: [ infinity ] + notAffectedGroups: [ enchanted_book, bow ] restriction_knockback: - enchantments: [knockback] - notAffectedGroups: [enchanted_book, swords] + enchantments: [ knockback ] + notAffectedGroups: [ enchanted_book, swords ] restriction_looting: - enchantments: [looting] - notAffectedGroups: [enchanted_book, swords] + enchantments: [ looting ] + notAffectedGroups: [ enchanted_book, swords ] restriction_loyalty: - enchantments: [loyalty] - notAffectedGroups: [enchanted_book, trident] + enchantments: [ loyalty ] + notAffectedGroups: [ enchanted_book, trident ] restriction_lure: - enchantments: [lure] - notAffectedGroups: [enchanted_book, fishing_rod] + enchantments: [ lure ] + notAffectedGroups: [ enchanted_book, fishing_rod ] restriction_mending: - enchantments: [mending] - notAffectedGroups: [enchanted_book, can_unbreak] + enchantments: [ mending ] + notAffectedGroups: [ enchanted_book, can_unbreak ] restriction_multishot: - enchantments: [multishot] - notAffectedGroups: [enchanted_book, crossbow] + enchantments: [ multishot ] + notAffectedGroups: [ enchanted_book, crossbow ] restriction_piercing: - enchantments: [piercing] - notAffectedGroups: [enchanted_book, crossbow] + enchantments: [ piercing ] + notAffectedGroups: [ enchanted_book, crossbow ] restriction_power: - enchantments: [power] - notAffectedGroups: [enchanted_book, bow] + enchantments: [ power ] + notAffectedGroups: [ enchanted_book, bow ] restriction_projectile_protection: - enchantments: [projectile_protection] - notAffectedGroups: [enchanted_book, armors] + enchantments: [ projectile_protection ] + notAffectedGroups: [ enchanted_book, armors ] restriction_protection: - enchantments: [protection] - notAffectedGroups: [enchanted_book, armors] + enchantments: [ protection ] + notAffectedGroups: [ enchanted_book, armors ] restriction_punch: - enchantments: [punch] - notAffectedGroups: [enchanted_book, bow] + enchantments: [ punch ] + notAffectedGroups: [ enchanted_book, bow ] restriction_quick_charge: - enchantments: [quick_charge] - notAffectedGroups: [enchanted_book, crossbow] + enchantments: [ quick_charge ] + notAffectedGroups: [ enchanted_book, crossbow ] restriction_respiration: - enchantments: [respiration] - notAffectedGroups: [enchanted_book, helmets] + enchantments: [ respiration ] + notAffectedGroups: [ enchanted_book, helmets ] restriction_riptide: - enchantments: [riptide] - notAffectedGroups: [enchanted_book, trident] + enchantments: [ riptide ] + notAffectedGroups: [ enchanted_book, trident ] restriction_sharpness: - enchantments: [sharpness] - notAffectedGroups: [enchanted_book, melee_weapons] + enchantments: [ sharpness ] + notAffectedGroups: [ enchanted_book, melee_weapons ] restriction_silk_touch: - enchantments: [silk_touch] - notAffectedGroups: [enchanted_book, tools] + enchantments: [ silk_touch ] + notAffectedGroups: [ enchanted_book, tools ] restriction_smite: - enchantments: [smite] - notAffectedGroups: [enchanted_book, melee_weapons] + enchantments: [ smite ] + notAffectedGroups: [ enchanted_book, melee_weapons ] restriction_soul_speed: - enchantments: [soul_speed] - notAffectedGroups: [enchanted_book, boots] + enchantments: [ soul_speed ] + notAffectedGroups: [ enchanted_book, boots ] restriction_sweeping: - enchantments: [sweeping] - notAffectedGroups: [enchanted_book, swords] + enchantments: [ sweeping ] + notAffectedGroups: [ enchanted_book, swords ] # Do not exist in 1.18, that mean useInFuture will be set to true # useInFuture set to true also mean it will not warn if there is an issue restriction_swift_sneak: useInFuture: true - enchantments: [swift_sneak] - notAffectedGroups: [enchanted_book, boots] + enchantments: [ swift_sneak ] + notAffectedGroups: [ enchanted_book, boots ] restriction_thorns: - enchantments: [thorns] - notAffectedGroups: [enchanted_book, armors] + enchantments: [ thorns ] + notAffectedGroups: [ enchanted_book, armors ] restriction_unbreaking: - enchantments: [unbreaking] - notAffectedGroups: [enchanted_book, can_unbreak] + enchantments: [ unbreaking ] + notAffectedGroups: [ enchanted_book, can_unbreak ] # ---------------------------------------------------- # Now we have conflicts about enchantment Incompatibility @@ -178,7 +178,7 @@ sword_enchant_conflict: - bane_of_arthropods - smite - sharpness - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 protection_enchant_conflict: @@ -187,49 +187,49 @@ protection_enchant_conflict: - fire_protection - projectile_protection - protection - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 trident_conflict1: enchantments: - channeling - riptide - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 trident_conflict2: enchantments: - loyalty - riptide - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 boot_conflict: enchantments: - depth_strider - frost_walker - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 tool_conflict: enchantments: - fortune - silk_touch - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 bow_conflict: enchantments: - mending - infinity - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 crossbow_conflict: enchantments: - multishot - piercing - notAffectedGroups: [] + notAffectedGroups: [ ] maxEnchantmentBeforeConflict: 1 # ---------------------------------------------------- diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3b2a340..52fe71d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,11 +1,11 @@ main: io.delilaheve.CustomAnvil name: CustomAnvil prefix: "Custom Anvil" -version: 1.3.2-A1 +version: 1.3.2-A2 description: Allow to customise anvil mechanics api-version: 1.18 load: POSTWORLD -authors: [DelilahEve, alexcrea] +authors: [ DelilahEve, alexcrea ] libraries: - org.jetbrains.kotlin:kotlin-stdlib:1.6.21 - com.github.stefvanschie.inventoryframework:IF:0.10.13