Add display item for group and order them in create order

This commit is contained in:
alexcrea 2024-03-21 01:42:48 +01:00
parent 41235d3024
commit e99fd4d640
5 changed files with 76 additions and 28 deletions

View file

@ -19,10 +19,7 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer; import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer;
import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Collections; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
public class GroupSelectSettingGui extends AbstractSettingGui{ public class GroupSelectSettingGui extends AbstractSettingGui{
@ -30,7 +27,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
SelectGroupContainer groupContainer; SelectGroupContainer groupContainer;
int page; int page;
HashSet<AbstractMaterialGroup> selectedGroups; Set<AbstractMaterialGroup> selectedGroups;
public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) { public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) {
super(6, title, parent); super(6, title, parent);
@ -63,13 +60,13 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL); filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
Set<AbstractMaterialGroup> illegalGroup = this.groupContainer.illegalGroups(); Set<AbstractMaterialGroup> 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)) { if(illegalGroup.contains(group)) {
return; return;
} }
filledEnchant.addItem(getGuiItemFromGroup(group)); filledEnchant.addItem(getGuiItemFromGroup(group));
}
});
addPane(filledEnchant); addPane(filledEnchant);
} }
@ -77,7 +74,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){ private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){
boolean isIn = this.selectedGroups.contains(group); boolean isIn = this.selectedGroups.contains(group);
Material usedMaterial = Material.PAPER; Material usedMaterial = group.getRepresentativeMaterial();
ItemStack item = new ItemStack(usedMaterial); ItemStack item = new ItemStack(usedMaterial);
setGroupItemMeta(item, group.getName(), isIn); setGroupItemMeta(item, group.getName(), isIn);
@ -93,6 +90,14 @@ public class GroupSelectSettingGui extends AbstractSettingGui{
public void setGroupItemMeta(ItemStack item, String name, boolean isIn){ public void setGroupItemMeta(ItemStack item, String name, boolean isIn){
ItemMeta meta = item.getItemMeta(); 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)); meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name));
if(isIn){ if(isIn){
meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true); meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true);

View file

@ -5,7 +5,6 @@ import java.util.*
abstract class AbstractMaterialGroup(private val name: String) { abstract class AbstractMaterialGroup(private val name: String) {
protected val includedMaterial by lazy {createDefaultSet()} protected val includedMaterial by lazy {createDefaultSet()}
protected var groupChangeNotified = false
/** /**
* Get the group default set * 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 * 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 return mat in includedMaterial
} }
@ -35,14 +34,28 @@ abstract class AbstractMaterialGroup(private val name: String) {
abstract fun addToPolicy(other : AbstractMaterialGroup) abstract fun addToPolicy(other : AbstractMaterialGroup)
/** /**
* Get the group as a set * Get the group contained material as a set
*/ */
abstract fun getMaterials(): MutableSet<Material> abstract fun getMaterials(): EnumSet<Material>
/**
* Get the group non-inherited material as a set
*/
open fun getNonGroupInheritedMaterials(): EnumSet<Material> {
return includedMaterial
}
/**
* Get the group non-inherited material as a set
*/
open fun setNonGroupInheritedMaterials(materials: EnumSet<Material>) {
this.includedMaterial.clear()
this.includedMaterial.addAll(materials)
}
/** /**
* Get the group name in case something is wrong * Get the group name in case something is wrong
*/ */
fun getName(): String { open fun getName(): String {
return name return name
} }
@ -56,4 +69,22 @@ abstract class AbstractMaterialGroup(private val name: String) {
*/ */
abstract fun getGroups(): MutableSet<AbstractMaterialGroup> abstract fun getGroups(): MutableSet<AbstractMaterialGroup>
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;
}
} }

View file

@ -9,7 +9,7 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
} }
private var includedGroup: MutableSet<AbstractMaterialGroup> = HashSet() private var includedGroup: MutableSet<AbstractMaterialGroup> = HashSet()
private val groupItems: MutableSet<Material> by lazy {createDefaultSet()} private val groupItems by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean { override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) { for (materialGroup in includedGroup.iterator()) {
@ -27,28 +27,28 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(other: AbstractMaterialGroup) { override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other) includedGroup.add(other)
groupItems.removeAll(other.getMaterials()); groupItems.removeAll(other.getMaterials())
} }
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) { override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {
groupItems.clear() groupItems.clear()
groupItems.addAll(includedMaterial) groupItems.addAll(includedMaterial)
includedGroup.clear(); includedGroup.clear()
groups.forEach { group -> groups.forEach { group ->
if(!group.isReferencing(this)) { if(!group.isReferencing(this)) {
includedGroup.add(group); includedGroup.add(group)
groupItems.removeAll(group.getMaterials()) groupItems.removeAll(group.getMaterials())
} }
} }
} }
override fun getGroups(): MutableSet<AbstractMaterialGroup> { override fun getGroups(): MutableSet<AbstractMaterialGroup> {
return includedGroup; return includedGroup
} }
override fun getMaterials(): MutableSet<Material> { override fun getMaterials(): EnumSet<Material> {
return groupItems; return groupItems
} }

View file

@ -9,7 +9,7 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
} }
private var includedGroup: MutableSet<AbstractMaterialGroup> = HashSet() private var includedGroup: MutableSet<AbstractMaterialGroup> = HashSet()
private val groupItems: MutableSet<Material> by lazy {createDefaultSet()} private val groupItems by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean { override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) { for (materialGroup in includedGroup.iterator()) {
@ -27,27 +27,38 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(other: AbstractMaterialGroup) { override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other) includedGroup.add(other)
groupItems.addAll(other.getMaterials()); groupItems.addAll(other.getMaterials())
} }
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) { override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {
groupItems.clear(); groupItems.clear()
groupItems.addAll(includedMaterial) groupItems.addAll(includedMaterial)
includedGroup.clear(); includedGroup.clear()
groups.forEach { group -> groups.forEach { group ->
if(!group.isReferencing(this)){ if(!group.isReferencing(this)){
includedGroup.add(group); includedGroup.add(group)
groupItems.addAll(group.getMaterials()) groupItems.addAll(group.getMaterials())
} }
} }
} }
override fun setNonGroupInheritedMaterials(materials: EnumSet<Material>) {
super.setNonGroupInheritedMaterials(materials)
// Update group items
groupItems.clear()
groupItems.addAll(includedMaterial)
includedGroup.forEach { group ->
groupItems.addAll(group.getMaterials())
}
}
override fun getGroups(): MutableSet<AbstractMaterialGroup> { override fun getGroups(): MutableSet<AbstractMaterialGroup> {
return includedGroup return includedGroup
} }
override fun getMaterials(): MutableSet<Material> { override fun getMaterials(): EnumSet<Material> {
return groupItems return groupItems
} }

View file

@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection import org.bukkit.configuration.ConfigurationSection
import java.util.* import java.util.*
import kotlin.collections.LinkedHashMap
class ItemGroupManager { class ItemGroupManager {
@ -18,11 +19,11 @@ class ItemGroupManager {
private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH") private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH")
} }
lateinit var groupMap : HashMap<String, AbstractMaterialGroup> lateinit var groupMap : LinkedHashMap<String, AbstractMaterialGroup>
// Read and create material groups // Read and create material groups
fun prepareGroups(config: ConfigurationSection){ fun prepareGroups(config: ConfigurationSection){
groupMap = HashMap() groupMap = LinkedHashMap()
val keys = config.getKeys(false) val keys = config.getKeys(false)
for (key in keys) { for (key in keys) {