From e99fd4d640f64b497aacd31a3b90f5e47dffce1f Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 21 Mar 2024 01:42:48 +0100 Subject: [PATCH] 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) {