From b1efb683048587369756199dec3180caa84deed1 Mon Sep 17 00:00:00 2001 From: alexcrea Date: Thu, 10 Jul 2025 03:49:00 +0200 Subject: [PATCH] progress on using item type # Conflicts: # src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CABukkitEnchantment.java --- .../cuanvil/api/MaterialGroupApi.java | 21 ++--- .../gui/config/SelectItemTypeContainer.java | 43 ++++++++++ .../gui/config/SelectMaterialContainer.java | 42 ---------- .../gui/config/global/EnchantConflictGui.java | 13 +-- .../gui/config/global/GroupConfigGui.java | 5 +- .../elements/GroupConfigSubSettingGui.java | 77 +++++++++-------- .../settings/GroupSelectSettingGui.java | 3 +- .../settings/MaterialSelectSettingGui.java | 83 ++++++++++--------- .../cuanvil/update/plugin/PUpdate_1_11_0.java | 40 +++++---- .../dependency/datapack/DataPackDependency.kt | 12 ++- .../plugins/EnchantmentSquaredDependency.kt | 43 +++++----- .../cuanvil/group/AbstractMaterialGroup.kt | 67 ++++++++------- .../cuanvil/group/EnchantConflictGroup.kt | 16 ++-- .../cuanvil/group/EnchantConflictManager.kt | 7 +- .../alexcrea/cuanvil/group/ExcludeGroup.kt | 35 +++++--- .../alexcrea/cuanvil/group/IncludeGroup.kt | 32 +++---- .../cuanvil/group/ItemGroupManager.kt | 36 ++++++-- .../cuanvil/api/MaterialGroupApiTests.java | 7 +- 18 files changed, 331 insertions(+), 251 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectItemTypeContainer.java delete mode 100644 src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java index 48dd500..27381de 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java @@ -2,8 +2,8 @@ package xyz.alexcrea.cuanvil.api; import io.delilaheve.CustomAnvil; import io.delilaheve.util.ConfigOptions; -import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.alexcrea.cuanvil.config.ConfigHolder; @@ -19,7 +19,7 @@ import java.util.*; /** * Custom Anvil api for material group registry. */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "UnstableApiUsage"}) public class MaterialGroupApi { private MaterialGroupApi() { @@ -105,6 +105,7 @@ public class MaterialGroupApi { if (group instanceof IncludeGroup includeGroup) { changed = writeKnownGroup("include", includeGroup); } else if (group instanceof ExcludeGroup excludeGroup) { + //TODO work on it when exclude group is reworked throw new UnsupportedOperationException("exclude group is temporarily disable for the time being. sorry"); // This code do not do what is intended ? idk why do it exist //changed = writeKnownGroup("exclude", excludeGroup); @@ -123,12 +124,12 @@ public class MaterialGroupApi { FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig(); String basePath = group.getName() + "."; - Set materialSet = group.getNonGroupInheritedMaterials(); + Set itemSets = group.getNonGroupInheritedMaterials(); Set groupSet = group.getGroups(); boolean empty = true; - if (!materialSet.isEmpty()) { - config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materialSet)); + if (!itemSets.isEmpty()) { + config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, itemTypesSetToStringList(itemSets)); empty = false; } else { config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, null); @@ -153,18 +154,18 @@ public class MaterialGroupApi { FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig(); String basePath = group.getName() + "."; - EnumSet materials = group.getMaterials(); + Set itemTypes = group.getItemTypes(); - if (materials.isEmpty()) return false; + if (itemTypes.isEmpty()) return false; config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include"); - config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials)); + config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, itemTypesSetToStringList(itemTypes)); return true; } - public static List materialSetToStringList(@NotNull Set materials) { - return materials.stream().map(material -> material.getKey().getKey().toLowerCase()).toList(); + public static List itemTypesSetToStringList(@NotNull Set types) { + return types.stream().map(item -> item.getKey().getKey().toLowerCase()).toList(); } public static List materialGroupSetToStringList(@NotNull Set groups) { diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectItemTypeContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectItemTypeContainer.java new file mode 100644 index 0000000..8b2b0ea --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectItemTypeContainer.java @@ -0,0 +1,43 @@ +package xyz.alexcrea.cuanvil.gui.config; + +import org.bukkit.inventory.ItemType; +import xyz.alexcrea.cuanvil.util.CasedStringUtil; + +import java.util.*; + +@SuppressWarnings("UnstableApiUsage") +public interface SelectItemTypeContainer { + + Set getSelectedMaterials(); + + boolean setSelectedItems(Set types); + + Set illegalMaterials(); + + static List getMaterialLore(SelectItemTypeContainer container, String containerType, String action) { + // Prepare material lore + ArrayList groupLore = new ArrayList<>(); + groupLore.add("§7Allow you to select a list of §ematerials §7that this " + containerType + " should " + action); + Set typeSet = container.getSelectedMaterials(); + if (typeSet.isEmpty()) { + groupLore.add("§7There is no " + action + "d material for this " + containerType + "."); + } else { + groupLore.add("§7List of " + action + "d materials for this " + containerType + ":"); + Iterator typeIterator = typeSet.iterator(); + + boolean greaterThanMax = typeSet.size() > 5; + int maxindex = (greaterThanMax ? 4 : typeSet.size()); + for (int i = 0; i < maxindex; i++) { + // format string like "- Stone Sword" + String formattedName = CasedStringUtil.snakeToUpperSpacedCase(typeIterator.next().key().value().toLowerCase()); + groupLore.add("§7- §e" + formattedName); + + } + if (greaterThanMax) { + groupLore.add("§7And " + (typeSet.size() - 4) + " more..."); + } + } + return groupLore; + } + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java deleted file mode 100644 index 2f76694..0000000 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java +++ /dev/null @@ -1,42 +0,0 @@ -package xyz.alexcrea.cuanvil.gui.config; - -import org.bukkit.Material; -import xyz.alexcrea.cuanvil.util.CasedStringUtil; - -import java.util.*; - -public interface SelectMaterialContainer { - - EnumSet getSelectedMaterials(); - - boolean setSelectedMaterials(EnumSet materials); - - EnumSet illegalMaterials(); - - static List getMaterialLore(SelectMaterialContainer container, String containerType, String action){ - // Prepare material lore - ArrayList groupLore = new ArrayList<>(); - groupLore.add("§7Allow you to select a list of §ematerials §7that this " + containerType + " should " + action); - Set materialSet = container.getSelectedMaterials(); - if (materialSet.isEmpty()) { - groupLore.add("§7There is no "+action+"d material for this "+containerType+"."); - } else { - groupLore.add("§7List of "+action+"d materials for this "+containerType+":"); - Iterator materialIterator = materialSet.iterator(); - - boolean greaterThanMax = materialSet.size() > 5; - int maxindex = (greaterThanMax ? 4 : materialSet.size()); - for (int i = 0; i < maxindex; i++) { - // format string like "- Stone Sword" - String formattedName = CasedStringUtil.snakeToUpperSpacedCase(materialIterator.next().name().toLowerCase()); - groupLore.add("§7- §e" + formattedName); - - } - if (greaterThanMax) { - groupLore.add("§7And " + (materialSet.size() - 4) + " more..."); - } - } - return groupLore; - } - -} 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 912e6cb..fb42212 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 @@ -18,32 +18,33 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil; import java.util.Arrays; import java.util.Collection; +@SuppressWarnings("UnstableApiUsage") public class EnchantConflictGui extends MappedGuiListConfigGui> { private static EnchantConflictGui INSTANCE; @Nullable - public static EnchantConflictGui getCurrentInstance(){ + public static EnchantConflictGui getCurrentInstance() { return INSTANCE; } @NotNull - public static EnchantConflictGui getInstance(){ - if(INSTANCE == null) INSTANCE = new EnchantConflictGui(); + public static EnchantConflictGui getInstance() { + if (INSTANCE == null) INSTANCE = new EnchantConflictGui(); return INSTANCE; } private EnchantConflictGui() { - super( "Conflict Config"); + super("Conflict Config"); init(); } @Override - protected EnchantConflictGroup createAndSaveNewEmptyGeneric(String name){ + protected EnchantConflictGroup createAndSaveNewEmptyGeneric(String name) { // Create new empty conflict and display it to the admin EnchantConflictGroup conflict = new EnchantConflictGroup( name, @@ -69,7 +70,7 @@ public class EnchantConflictGui extends MappedGuiListConfigGui> { private static GroupConfigGui INSTANCE; @@ -44,7 +45,7 @@ public class GroupConfigGui extends MappedGuiListConfigGui deleteSupplier = () -> { // test if group is used & cancel if so - if(!getUsedLocations(this.group).isEmpty()) return false; + if (!getUsedLocations(this.group).isEmpty()) return false; ItemGroupManager manager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager(); @@ -156,23 +159,23 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen ); } - public boolean testAndWarnIfUsed(HumanEntity player){ + public boolean testAndWarnIfUsed(HumanEntity player) { List usedLoc = getUsedLocations(this.group); - if(usedLoc.isEmpty()){ + if (usedLoc.isEmpty()) { return false; } - StringBuilder stb = new StringBuilder("§cCan't delete group " +this.group.getName()+ + StringBuilder stb = new StringBuilder("§cCan't delete group " + this.group.getName() + "\n§eUsed by:"); int maxIndex = usedLoc.size(); int nbMore = 0; - if(maxIndex > 10){ + if (maxIndex > 10) { nbMore = maxIndex - 9; maxIndex = 9; } for (int i = 0; i < maxIndex; i++) { stb.append("\n§r-§e ").append(usedLoc.get(i)); } - if(nbMore > 0){ + if (nbMore > 0) { stb.append("§cAnd ").append(nbMore).append(" More..."); } @@ -181,13 +184,13 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen } // return a string containing every instance of where this group is used - public static List getUsedLocations(AbstractMaterialGroup group){ + public static List getUsedLocations(AbstractMaterialGroup group) { ArrayList usageList = new ArrayList<>(); // Test used by another group ItemGroupManager groupManager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager(); for (AbstractMaterialGroup otherGroup : groupManager.getGroupMap().values()) { - if(otherGroup.getGroups().contains(group)) { + if (otherGroup.getGroups().contains(group)) { usageList.add("group " + otherGroup.getName()); } } @@ -195,7 +198,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen // Test if used for conflict EnchantConflictManager conflictManager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); for (EnchantConflictGroup conflict : conflictManager.getConflictList()) { - if(conflict.getCantConflictGroup().getGroups().contains(group)) { + if (conflict.getCantConflictGroup().getGroups().contains(group)) { usageList.add("conflict " + conflict); } } @@ -205,7 +208,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen @Override public void updateGuiValues() { - if(!this.usable) return; + if (!this.usable) return; // Parent should call updateLocal with this call this.parent.updateValueForGeneric(this.group, true); @@ -213,9 +216,9 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen @Override public void updateLocal() { - if(!this.usable) return; + if (!this.usable) return; // Prepare material lore - List matLore = SelectMaterialContainer.getMaterialLore(this, "group", "include"); + List matLore = SelectItemTypeContainer.getMaterialLore(this, "group", "include"); // Prepare group lore List groupLore = SelectGroupContainer.getGroupLore(this, "group", "include"); @@ -255,7 +258,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen @Override public void show(@NotNull HumanEntity player) { - if(!this.usable) { + if (!this.usable) { this.parent.show(player); return; } @@ -284,7 +287,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen return true; } - private void updateGroup(@NotNull AbstractMaterialGroup group, Set groups){ + private void updateGroup(@NotNull AbstractMaterialGroup group, Set groups) { // Set live configuration group.setGroups(groups); @@ -296,7 +299,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen groupNames[index++] = otherGroup.getName(); } - ConfigHolder.ITEM_GROUP_HOLDER.getConfig().set(group.getName()+"."+ItemGroupManager.GROUP_LIST_PATH, groupNames); + ConfigHolder.ITEM_GROUP_HOLDER.getConfig().set(group.getName() + "." + ItemGroupManager.GROUP_LIST_PATH, groupNames); // Try to update referencing group. kind of expensive operation in some case. updateDirectReferencingGroups(group); @@ -309,7 +312,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen Set illegal = new HashSet<>(); for (AbstractMaterialGroup otherGroup : ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().values()) { - if(otherGroup.isReferencing(this.group)){ + if (otherGroup.isReferencing(this.group)) { illegal.add(otherGroup); } } @@ -325,22 +328,22 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen // ---------------------------- @Override - public EnumSet getSelectedMaterials() { + public Set getSelectedMaterials() { return this.group.getNonGroupInheritedMaterials(); } @Override - public boolean setSelectedMaterials(EnumSet materials) { - this.group.setNonGroupInheritedMaterials(materials); + public boolean setSelectedItems(Set types) { + this.group.setNonGroupInheritedMaterials(types); // Write to file configuration - String[] groupNames = new String[materials.size()]; + String[] groupNames = new String[types.size()]; int index = 0; - for (Material otherGroup : materials) { - groupNames[index++] = otherGroup.name().toLowerCase(); + for (ItemType otherGroup : types) { + groupNames[index++] = otherGroup.key().value().toLowerCase(); } - ConfigHolder.ITEM_GROUP_HOLDER.getConfig().set(this.group.getName()+"."+ItemGroupManager.MATERIAL_LIST_PATH, groupNames); + ConfigHolder.ITEM_GROUP_HOLDER.getConfig().set(this.group.getName() + "." + ItemGroupManager.MATERIAL_LIST_PATH, groupNames); // update referencing groups updateDirectReferencingGroups(this.group); @@ -352,16 +355,24 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen return true; } + private static final Set ONLY_AIR_ITEM_SET; + + static { + Set onlyAir = new HashSet<>(); + onlyAir.add(ItemType.AIR); + ONLY_AIR_ITEM_SET = Collections.unmodifiableSet(onlyAir); + } + @Override - public EnumSet illegalMaterials() { - return EnumSet.of(Material.AIR); + public Set illegalMaterials() { + return ONLY_AIR_ITEM_SET; } // ---------------------------- // End of SelectMaterialContainer related methods // ---------------------------- - private void updateDirectReferencingGroups(AbstractMaterialGroup referenceTo){ + private void updateDirectReferencingGroups(AbstractMaterialGroup referenceTo) { Collection everyStoredGroups = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().values(); List everyConflicts = ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList(); @@ -370,7 +381,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen HashSet conflictGroupPlanned = new HashSet<>(); updateFuture.add(referenceTo); - while (!updateFuture.isEmpty()){ + while (!updateFuture.isEmpty()) { HashSet temp = updateFuture; updateFuture = toUpdate; updateFuture.clear(); @@ -379,7 +390,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen for (AbstractMaterialGroup testGroup : toUpdate) { // Update other stored group for (AbstractMaterialGroup otherGroup : everyStoredGroups) { - if(otherGroup.getGroups().contains(testGroup)){ + if (otherGroup.getGroups().contains(testGroup)) { otherGroup.updateMaterials(); updateFuture.add(otherGroup); } @@ -388,13 +399,13 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen // plan update for conflict groups for (EnchantConflictGroup everyConflict : everyConflicts) { AbstractMaterialGroup conflictGroup = everyConflict.getCantConflictGroup(); - if(conflictGroup.getGroups().contains(testGroup)){ + if (conflictGroup.getGroups().contains(testGroup)) { conflictGroupPlanned.add(conflictGroup); } } // Update parent & local by extension - if(testGroup instanceof IncludeGroup){ + if (testGroup instanceof IncludeGroup) { this.parent.updateValueForGeneric((IncludeGroup) testGroup, false); } } 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 28842e3..70332a2 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 @@ -86,8 +86,7 @@ public class GroupSelectSettingGui extends AbstractSettingGui { private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group) { boolean isIn = this.selectedGroups.contains(group); - Material usedMaterial = group.getRepresentativeMaterial(); - ItemStack item = new ItemStack(usedMaterial); + ItemStack item = group.getRepresentativeMaterial().createItemStack(); setGroupItemMeta(item, group.getName(), isIn); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java index a3963ce..0887196 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/MaterialSelectSettingGui.java @@ -9,9 +9,10 @@ import org.bukkit.entity.HumanEntity; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ItemType; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; -import xyz.alexcrea.cuanvil.gui.config.SelectMaterialContainer; +import xyz.alexcrea.cuanvil.gui.config.SelectItemTypeContainer; import xyz.alexcrea.cuanvil.gui.config.ask.ConfirmActionGui; import xyz.alexcrea.cuanvil.gui.config.list.MappedElementListConfigGui; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; @@ -22,19 +23,20 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil; import java.util.*; import java.util.function.Consumer; -public class MaterialSelectSettingGui extends MappedElementListConfigGui { +@SuppressWarnings("UnstableApiUsage") +public class MaterialSelectSettingGui extends MappedElementListConfigGui { - private final SelectMaterialContainer selector; + private final SelectItemTypeContainer selector; private final Gui backGui; private boolean instantRemove; - private final List defaultMaterials; - private final EnumSet illegalMaterials; + private final List defaultMaterials; + private final Set illegalMaterials; private final int defaultMaterialHash; private int nowMaterialHash; public MaterialSelectSettingGui( - @NotNull SelectMaterialContainer selector, + @NotNull SelectItemTypeContainer selector, @NotNull String title, @NotNull Gui backGui) { super(title); @@ -45,7 +47,7 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui(this.selector.getSelectedMaterials()); this.illegalMaterials = this.selector.illegalMaterials(); - this.defaultMaterialHash = hashFromMaterialList(this.defaultMaterials); + this.defaultMaterialHash = hashFromItemTypeList(this.defaultMaterials); this.nowMaterialHash = this.defaultMaterialHash; init(); @@ -55,7 +57,7 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui result = EnumSet.noneOf(Material.class); - result.addAll(this.elementGuiMap.keySet()); + Set result = new HashSet<>(this.elementGuiMap.keySet()); - if(!this.selector.setSelectedMaterials(result)){ + if (!this.selector.setSelectedItems(result)) { player.sendMessage("§cSomething went wrong while saving the change of value."); } // Return to parent this.backGui.show(player); - }, CustomAnvil.instance); + }, CustomAnvil.instance); } /** @@ -185,12 +186,12 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui getEveryDisplayableInstanceOfGeneric() { + protected Collection getEveryDisplayableInstanceOfGeneric() { return this.defaultMaterials; } @Override - protected void updateElement(Material material, GuiItem element) { + protected void updateElement(ItemType type, GuiItem element) { // Nothing happen here I think } @Override - protected GuiItem newElementRequested(Material material, GuiItem newItem) { + protected GuiItem newElementRequested(ItemType type, GuiItem newItem) { newItem.setAction(event -> { - if(this.instantRemove){ - removeMaterial(material); - }else { - String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()); + if (this.instantRemove) { + removeItemType(type); + } else { + String materialName = CasedStringUtil.snakeToUpperSpacedCase(type.key().value().toLowerCase()); // Create and show confirm remove gui. ConfirmActionGui confirmGui = new ConfirmActionGui( @@ -239,7 +240,7 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui { - removeMaterial(material); + removeItemType(type); return true; }, false ); @@ -250,37 +251,36 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui materialList){ + private static int hashFromItemTypeList(List itemTypeList) { int defaultMaterialHash = 0; - for (Material material : materialList) { - defaultMaterialHash ^= material.hashCode(); + for (ItemType type : itemTypeList) { + defaultMaterialHash ^= type.hashCode(); //TODO check would this be valid with item type } return defaultMaterialHash; } private void setSaveItem() { - if(testCantSave()){ + if (testCantSave()) { this.backgroundPane.bindItem('S', this.noChangeItem); - }else{ + } else { this.backgroundPane.bindItem('S', this.saveItem); } @@ -296,6 +296,7 @@ public class MaterialSelectSettingGui extends MappedElementListConfigGui prepareCreateItemConsumer(HumanEntity player) {// Not used return null; diff --git a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_11_0.java b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_11_0.java index 6d6baca..039e929 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_11_0.java +++ b/src/main/java/xyz/alexcrea/cuanvil/update/plugin/PUpdate_1_11_0.java @@ -1,8 +1,8 @@ package xyz.alexcrea.cuanvil.update.plugin; -import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.alexcrea.cuanvil.api.MaterialGroupApi; @@ -16,6 +16,7 @@ import java.util.Set; import static xyz.alexcrea.cuanvil.update.UpdateUtils.addAbsentToList; +@SuppressWarnings("UnstableApiUsage") public class PUpdate_1_11_0 { private static final List mace_expected = List.of( @@ -30,22 +31,22 @@ public class PUpdate_1_11_0 { "bane_of_arthropods" ); - private static final Material[] PICKAXES = new Material[]{ - Material.WOODEN_PICKAXE, Material.STONE_PICKAXE, - Material.IRON_PICKAXE, Material.DIAMOND_PICKAXE, - Material.GOLDEN_PICKAXE, Material.NETHERITE_PICKAXE + private static final ItemType[] PICKAXES = new ItemType[]{ + ItemType.WOODEN_PICKAXE, ItemType.STONE_PICKAXE, + ItemType.IRON_PICKAXE, ItemType.DIAMOND_PICKAXE, + ItemType.GOLDEN_PICKAXE, ItemType.NETHERITE_PICKAXE }; - private static final Material[] SHOVELS = new Material[]{ - Material.WOODEN_SHOVEL, Material.STONE_SHOVEL, - Material.IRON_SHOVEL, Material.DIAMOND_SHOVEL, - Material.GOLDEN_SHOVEL, Material.NETHERITE_SHOVEL + private static final ItemType[] SHOVELS = new ItemType[]{ + ItemType.WOODEN_SHOVEL, ItemType.STONE_SHOVEL, + ItemType.IRON_SHOVEL, ItemType.DIAMOND_SHOVEL, + ItemType.GOLDEN_SHOVEL, ItemType.NETHERITE_SHOVEL }; - private static final Material[] HOES = new Material[]{ - Material.WOODEN_HOE, Material.STONE_HOE, - Material.IRON_HOE, Material.DIAMOND_HOE, - Material.GOLDEN_HOE, Material.NETHERITE_HOE + private static final ItemType[] HOES = new ItemType[]{ + ItemType.WOODEN_HOE, ItemType.STONE_HOE, + ItemType.IRON_HOE, ItemType.DIAMOND_HOE, + ItemType.GOLDEN_HOE, ItemType.NETHERITE_HOE }; public static void handleUpdate(@Nonnull Set toSave) { @@ -65,7 +66,7 @@ public class PUpdate_1_11_0 { private static void migrateTools( @Nullable AbstractMaterialGroup tools, @NotNull String toolset, - @NotNull Material[] toolMats) { + @NotNull ItemType[] toolMats) { // Create new group IncludeGroup group = new IncludeGroup(toolset); @@ -77,11 +78,11 @@ public class PUpdate_1_11_0 { if (tools == null) return; if (!(tools instanceof IncludeGroup include)) return; - List mats = List.of(toolMats); - Set matSet = include.getNonGroupInheritedMaterials(); - if (!matSet.containsAll(mats)) return; + List types = List.of(toolMats); + Set typeSet = include.getNonGroupInheritedMaterials(); + if (!typeSet.containsAll(types)) return; - mats.forEach(matSet::remove); + types.forEach(typeSet::remove); tools.addToPolicy(group); MaterialGroupApi.writeMaterialGroup(tools); } @@ -108,6 +109,8 @@ public class PUpdate_1_11_0 { // Test sword_enchant_conflict is default ConfigurationSection sword_conflict = config.getConfigurationSection("sword_enchant_conflict"); + if (sword_conflict == null) return; + if (sword_conflict.getInt("maxEnchantmentBeforeConflict", 0) != 1) return; if (sword_conflict.isList("notAffectedGroups") && !sword_conflict.getList("notAffectedGroups").isEmpty()) @@ -124,6 +127,7 @@ public class PUpdate_1_11_0 { "minecraft:density", "minecraft:breach"); config.set("mace_enchant_conflict", null); + toSave.add(ConfigHolder.CONFLICT_HOLDER); } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt index 86e03b4..ae1155a 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/datapack/DataPackDependency.kt @@ -135,6 +135,9 @@ object DataPackDependency { // Order matter for this file // Could rewrite to not matter but not really important, so I keep it like that private fun handleItemGroups(yml: YamlConfiguration) { + //TODO see below todo + //val itemRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM) + for (groupName in yml.getKeys(false)) { val section = yml.getConfigurationSection(groupName) ?: continue @@ -144,12 +147,19 @@ object DataPackDependency { if (group == null) group = IncludeGroup(groupName) for (name in section.getStringList("items")) { + //TODO get item key from + /*val key = NamespacedKey.fromString(name.lowercase()) + if (key == null) throw IllegalStateException("Invalid item type: " + name) + val type = itemRegistry.get(key)*/ + val mat = Material.getMaterial(name.uppercase()) + + if (mat == null) { CustomAnvil.instance.logger.warning("Could not find material $name for item group $groupName") continue } - group.addToPolicy(mat) + group.addToPolicy(mat.asItemType()!!) } for (name in section.getStringList("groups")) { val otherGroup = MaterialGroupApi.getGroup(name) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/EnchantmentSquaredDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/EnchantmentSquaredDependency.kt index f4da612..1ac01ba 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/EnchantmentSquaredDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/EnchantmentSquaredDependency.kt @@ -4,11 +4,11 @@ import io.delilaheve.CustomAnvil import me.athlaeos.enchantssquared.enchantments.CustomEnchant import me.athlaeos.enchantssquared.listeners.AnvilListener import me.athlaeos.enchantssquared.managers.CustomEnchantManager -import org.bukkit.Material import org.bukkit.NamespacedKey import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.ItemType import org.bukkit.plugin.Plugin import xyz.alexcrea.cuanvil.api.ConflictBuilder import xyz.alexcrea.cuanvil.api.EnchantmentApi @@ -20,6 +20,7 @@ import xyz.alexcrea.cuanvil.enchant.wrapped.CAEnchantSquaredEnchantment import xyz.alexcrea.cuanvil.group.IncludeGroup import java.util.* +@Suppress("UnstableApiUsage") class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) { init { @@ -30,28 +31,29 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) "disable_anvil, " + "incompatible_vanilla_enchantments, " + "incompatible_custom_enchantments and max_level " + - "configuration values.") + "configuration values." + ) } - fun disableAnvilListener(){ + fun disableAnvilListener() { PrepareAnvilEvent.getHandlerList().unregister(this.enchantmentSquaredPlugin) // Find the anvil click event var toRemove: AnvilListener? = null for (registered in InventoryClickEvent.getHandlerList().registeredListeners) { val listener = registered.listener - if(listener is AnvilListener) { + if (listener is AnvilListener) { toRemove = listener break } } - if(toRemove != null) + if (toRemove != null) InventoryClickEvent.getHandlerList().unregister(toRemove) } - fun registerEnchantments(){ + fun registerEnchantments() { CustomAnvil.instance.logger.info("Preparing Enchantment Squared compatibility...") // Register enchantments @@ -69,20 +71,21 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) fun getEnchantmentsSquared(item: ItemStack, enchantments: MutableMap) { val customEnchants = CustomEnchantManager.getInstance().getItemsEnchantsFromPDC(item) - customEnchants.forEach{ - (enchantment, level ) -> enchantments[getWrappedEnchant(enchantment)] = level + customEnchants.forEach { (enchantment, level) -> + enchantments[getWrappedEnchant(enchantment)] = level } } - fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey{ + fun getKeyFromEnchant(enchant: CustomEnchant): NamespacedKey { return NamespacedKey.fromString(enchant.type.lowercase(Locale.getDefault()), this.enchantmentSquaredPlugin)!! } + private fun getWrappedEnchant(enchant: CustomEnchant): CAEnchantment { return CAEnchantment.getByKey(getKeyFromEnchant(enchant))!! } - fun registerPluginConfiguration(){ + fun registerPluginConfiguration() { CustomAnvil.instance.logger.info("Preparing Enchantment Squared config...") // Prepare enchantments @@ -99,22 +102,22 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) CustomAnvil.instance.logger.info("Enchantment Squared should now work as expected !") } - private fun writeMissingGroups(){ + private fun writeMissingGroups() { // Write group that do not exist on custom anvil. val shield = IncludeGroup("shield") - shield.addToPolicy(Material.SHIELD) + shield.addToPolicy(ItemType.SHIELD) MaterialGroupApi.addMaterialGroup(shield) val elytra = IncludeGroup("elytra") - elytra.addToPolicy(Material.ELYTRA) + elytra.addToPolicy(ItemType.ELYTRA) MaterialGroupApi.addMaterialGroup(elytra) val trinkets = IncludeGroup("trinkets") - trinkets.addToPolicy(Material.ROTTEN_FLESH) + trinkets.addToPolicy(ItemType.ROTTEN_FLESH) MaterialGroupApi.addMaterialGroup(trinkets) } - private fun writeMaterialRestriction(esEnchantments: List){ + private fun writeMaterialRestriction(esEnchantments: List) { for (enchantment in esEnchantments) { val conflict = ConflictBuilder("restriction_${enchantment.key.key}", CustomAnvil.instance) conflict.addEnchantment(enchantment) @@ -125,7 +128,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) // Get allowed groups for (esGroup in enchantment.enchant.compatibleItems) { val caGroup = esGroupToCAGroup(esGroup) - if(caGroup == null){ + if (caGroup == null) { CustomAnvil.instance.logger.info("Could not find equivalent custom anvil group for $esGroup") continue } @@ -136,7 +139,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) } } - private fun writeEnchantmentConflicts(esEnchantments: List){ + private fun writeEnchantmentConflicts(esEnchantments: List) { val otherEnchants = ArrayList() otherEnchants.addAll(CAEnchantmentRegistry.getInstance().values()) @@ -145,14 +148,14 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) // find conflicting enchantment. for (otherEnchant in otherEnchants) { - if(enchantment.enchant.conflictsWithEnchantment(otherEnchant.name)){ + if (enchantment.enchant.conflictsWithEnchantment(otherEnchant.name)) { writeConflict(enchantment, otherEnchant) } } } } - private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment){ + private fun writeConflict(enchantment1: CAEnchantment, enchantment2: CAEnchantment) { val conflict = ConflictBuilder("${enchantment1.name}_with_${enchantment2.name}_conflict", CustomAnvil.instance) conflict.addEnchantment(enchantment1).addEnchantment(enchantment2) @@ -165,7 +168,7 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) * Transform an Enchantment Squared group to a Custom Anvil group */ private fun esGroupToCAGroup(esGroup: String): String? { - return when(esGroup){ + return when (esGroup) { "SWORDS" -> "swords" "BOWS" -> "bow" "CROSSBOWS" -> "crossbow" diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index ec6e7bc..937dad7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -1,21 +1,22 @@ package xyz.alexcrea.cuanvil.group -import org.bukkit.Material -import java.util.* +import com.google.common.collect.ImmutableSet +import org.bukkit.inventory.ItemType +@Suppress("UnstableApiUsage") abstract class AbstractMaterialGroup(private val name: String) { - protected val includedMaterial by lazy { createDefaultSet() } + protected val includedItems by lazy { createDefaultSet() } /** * Get the group default set */ - protected abstract fun createDefaultSet(): EnumSet + protected abstract fun createDefaultSet(): MutableSet /** * Get if a material is allowed following the group policy */ - open fun contain(mat: Material): Boolean { - return mat in getMaterials() + open fun contain(mat: ItemType): Boolean { + return mat in getItemTypes() } /** @@ -24,30 +25,34 @@ abstract class AbstractMaterialGroup(private val name: String) { abstract fun isReferencing(other: AbstractMaterialGroup): Boolean /** - * Push a material to this group to follow this group policy + * Push an item to this group to follow this group policy + * * @return this instance. */ - abstract fun addToPolicy(mat: Material): AbstractMaterialGroup + abstract fun addToPolicy(type: ItemType): AbstractMaterialGroup /** - * Push a list of material to this group to follow this group policy + * Push a list of items to this group to follow this group policy + * * @return this instance. */ - fun addAll(vararg materials: Material): AbstractMaterialGroup { - for (material in materials) { - addToPolicy(material) + fun addAll(vararg types: ItemType): AbstractMaterialGroup { + for (type in types) { + addToPolicy(type) } return this } /** * Push a group to this group to follow this group policy + * * @return this instance. */ abstract fun addToPolicy(other: AbstractMaterialGroup): AbstractMaterialGroup /** * Push a list of group to this group to follow this group policy + * * @return this instance. */ fun addAll(vararg otherList: AbstractMaterialGroup): AbstractMaterialGroup { @@ -58,23 +63,23 @@ abstract class AbstractMaterialGroup(private val name: String) { } /** - * Get the group contained material as a set + * Get the group contained item as a set */ - abstract fun getMaterials(): EnumSet + abstract fun getItemTypes(): ImmutableSet /** - * Get the group non-inherited material as a set + * Get the group non-inherited items as a set */ - open fun getNonGroupInheritedMaterials(): EnumSet { - return includedMaterial + open fun getNonGroupInheritedMaterials(): MutableSet { + return includedItems } /** - * Get the group non-inherited material as a set + * Set the group non-inherited items */ - open fun setNonGroupInheritedMaterials(materials: EnumSet) { - this.includedMaterial.clear() - this.includedMaterial.addAll(materials) + open fun setNonGroupInheritedMaterials(types: Set) { + this.includedItems.clear() + this.includedItems.addAll(types) } /** @@ -98,22 +103,22 @@ abstract class AbstractMaterialGroup(private val name: String) { */ abstract fun getGroups(): MutableSet - open fun getRepresentativeMaterial(): Material { + open fun getRepresentativeMaterial(): ItemType { // Test inner material - val matIterator = includedMaterial.iterator() - while (matIterator.hasNext()) { - val material = matIterator.next() - if (material.isAir) continue - return material + val itemIterator = includedItems.iterator() + while (itemIterator.hasNext()) { + val type = itemIterator.next() + if (type == ItemType.AIR) continue + return type } // Test included group representative material val groupIterator = getGroups().iterator() while (groupIterator.hasNext()) { - val groupMat = groupIterator.next().getRepresentativeMaterial() - if (groupMat.isAir) continue - return groupMat + val groupType = groupIterator.next().getRepresentativeMaterial() + if (groupType == ItemType.AIR) continue + return groupType } - return Material.PAPER + return ItemType.PAPER } abstract fun updateMaterials() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt index 56e923f..a03a8a9 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictGroup.kt @@ -1,9 +1,10 @@ package xyz.alexcrea.cuanvil.group import io.delilaheve.CustomAnvil -import org.bukkit.Material +import org.bukkit.inventory.ItemType import xyz.alexcrea.cuanvil.enchant.CAEnchantment +@Suppress("UnstableApiUsage") class EnchantConflictGroup( val name: String, private val cantConflict: AbstractMaterialGroup, @@ -15,17 +16,18 @@ class EnchantConflictGroup( fun addEnchantment(enchant: CAEnchantment) { enchantments.add(enchant) } + fun addEnchantments(enchants: List) { enchantments.addAll(enchants) } - fun allowed(enchants: Set, mat: Material): Boolean { + fun allowed(enchants: Set, type: ItemType): Boolean { if (enchantments.size < minBeforeBlock) { CustomAnvil.verboseLog("Conflicting bc of to many enchantments") return true } - if (cantConflict.contain(mat)) { + if (cantConflict.contain(type)) { return true } @@ -56,15 +58,15 @@ class EnchantConflictGroup( enchantments.addAll(enchants) } - fun getRepresentativeMaterial(): Material { + fun getRepresentativeMaterial(): ItemType { val groups = getCantConflictGroup().getGroups() val groupIterator = groups.iterator() while (groupIterator.hasNext()) { - val mat = groupIterator.next().getRepresentativeMaterial() - if (mat != Material.ENCHANTED_BOOK) return mat + val itemType = groupIterator.next().getRepresentativeMaterial() + if (itemType != ItemType.ENCHANTED_BOOK) return itemType } - return Material.ENCHANTED_BOOK + return ItemType.ENCHANTED_BOOK } override fun toString(): String { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index 169d9e9..cb12a4c 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -10,6 +10,7 @@ import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import java.util.* +@Suppress("UnstableApiUsage") class EnchantConflictManager { companion object { @@ -176,7 +177,9 @@ class EnchantConflictManager { newEnchant: CAEnchantment ): ConflictType { val mat = item.type - CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${mat.key}") + val itemType = mat.asItemType()!! + + CustomAnvil.verboseLog("Testing conflict for ${newEnchant.key} on ${itemType.key}") val conflictList = newEnchant.conflicts var result = ConflictType.NO_CONFLICT @@ -187,7 +190,7 @@ class EnchantConflictManager { continue } - val allowed = conflict.allowed(appliedEnchants.keys, mat) + val allowed = conflict.allowed(appliedEnchants.keys, itemType) CustomAnvil.verboseLog("Was against $conflict and conflicting: ${!allowed} ") if (!allowed) { if (conflict.getEnchants().size <= 1) { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt index 7684c3f..1e2c1e4 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt @@ -1,11 +1,20 @@ package xyz.alexcrea.cuanvil.group -import org.bukkit.Material +import com.google.common.collect.ImmutableSet +import io.papermc.paper.registry.RegistryAccess +import io.papermc.paper.registry.RegistryKey +import org.bukkit.inventory.ItemType import java.util.* +@Deprecated("Need rework to reduce memory cost as not enum set") +@Suppress("UnstableApiUsage") class ExcludeGroup(name: String) : AbstractMaterialGroup(name) { - override fun createDefaultSet(): EnumSet { - return EnumSet.allOf(Material::class.java) + + override fun createDefaultSet(): MutableSet { + val types: MutableSet = HashSet() + + types.addAll(RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM)) + return types } private var includedGroup: MutableSet = HashSet() @@ -20,29 +29,29 @@ class ExcludeGroup(name: String) : AbstractMaterialGroup(name) { return false } - override fun addToPolicy(mat: Material): ExcludeGroup { - includedMaterial.remove(mat) - groupItems.remove(mat) + override fun addToPolicy(type: ItemType): ExcludeGroup { + includedItems.remove(type) + groupItems.remove(type) return this } override fun addToPolicy(other: AbstractMaterialGroup): ExcludeGroup { includedGroup.add(other) - groupItems.removeAll(other.getMaterials()) + groupItems.removeAll(other.getItemTypes()) return this } override fun setGroups(groups: MutableSet) { groupItems.clear() - groupItems.addAll(includedMaterial) + groupItems.addAll(includedItems) includedGroup.clear() groups.forEach { group -> if (!group.isReferencing(this)) { includedGroup.add(group) - groupItems.removeAll(group.getMaterials()) + groupItems.removeAll(group.getItemTypes()) } } } @@ -53,15 +62,15 @@ class ExcludeGroup(name: String) : AbstractMaterialGroup(name) { override fun updateMaterials() { groupItems.clear() - groupItems.addAll(includedMaterial) + groupItems.addAll(includedItems) includedGroup.forEach { group -> - groupItems.addAll(group.getMaterials()) + groupItems.addAll(group.getItemTypes()) } } - override fun getMaterials(): EnumSet { - return groupItems + override fun getItemTypes(): ImmutableSet { + return Collections.unmodifiableSet(groupItems) as ImmutableSet } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt index 848789f..5528ad1 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt @@ -1,11 +1,13 @@ package xyz.alexcrea.cuanvil.group -import org.bukkit.Material +import com.google.common.collect.ImmutableSet +import org.bukkit.inventory.ItemType import java.util.* +@Suppress("UnstableApiUsage") class IncludeGroup(name: String) : AbstractMaterialGroup(name) { - override fun createDefaultSet(): EnumSet { - return EnumSet.noneOf(Material::class.java) + override fun createDefaultSet(): MutableSet { + return HashSet() } private var includedGroup: MutableSet = HashSet() @@ -20,35 +22,35 @@ class IncludeGroup(name: String) : AbstractMaterialGroup(name) { return false } - override fun addToPolicy(mat: Material): IncludeGroup { - includedMaterial.add(mat) - groupItems.add(mat) + override fun addToPolicy(type: ItemType): IncludeGroup { + includedItems.add(type) + groupItems.add(type) return this } override fun addToPolicy(other: AbstractMaterialGroup): IncludeGroup { includedGroup.add(other) - groupItems.addAll(other.getMaterials()) + groupItems.addAll(other.getItemTypes()) return this } override fun setGroups(groups: MutableSet) { groupItems.clear() - groupItems.addAll(includedMaterial) + groupItems.addAll(includedItems) includedGroup.clear() groups.forEach { group -> if (!group.isReferencing(this)) { includedGroup.add(group) - groupItems.addAll(group.getMaterials()) + groupItems.addAll(group.getItemTypes()) } } } - override fun setNonGroupInheritedMaterials(materials: EnumSet) { - super.setNonGroupInheritedMaterials(materials) + override fun setNonGroupInheritedMaterials(types: Set) { + super.setNonGroupInheritedMaterials(types) updateMaterials() } @@ -59,15 +61,15 @@ class IncludeGroup(name: String) : AbstractMaterialGroup(name) { override fun updateMaterials() { groupItems.clear() - groupItems.addAll(includedMaterial) + groupItems.addAll(includedItems) includedGroup.forEach { group -> - groupItems.addAll(group.getMaterials()) + groupItems.addAll(group.getItemTypes()) } } - override fun getMaterials(): EnumSet { - return groupItems + override fun getItemTypes(): ImmutableSet { + return Collections.unmodifiableSet(groupItems) as ImmutableSet } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt index 65eef34..87aa2c6 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt @@ -39,7 +39,7 @@ class ItemGroupManager { fun createGroup( config: ConfigurationSection, name: String - ): AbstractMaterialGroup{ + ): AbstractMaterialGroup { return createGroup(config, groupMap.keys, name) } @@ -76,22 +76,48 @@ class ItemGroupManager { config: ConfigurationSection, keys: Set ) { + //TODO see below todo + //val itemRegistry = RegistryAccess.registryAccess().getRegistry(RegistryKey.ITEM) + // 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()) + for (typeName in materialList) { + //TODO get item key from + /*val key = NamespacedKey.fromString(typeName.lowercase()) + if (key == null) { + CustomAnvil.instance.logger.warning( + "Malformed item type $typeName on group ${group.getName()}" + ) + + continue + } + + val type = itemRegistry.get(key) + if(type == null){ + // Check if we should warn the user + if (typeName !in FUTURE_MATERIAL) { + CustomAnvil.instance.logger.warning( + "Unknown item type $typeName on group ${group.getName()}" + ) + + } + continue + }*/ + + + val materialName = typeName.uppercase(Locale.getDefault()) val material = Material.getMaterial(materialName) if (material == null) { // Check if we should warn the user if (materialName !in FUTURE_MATERIAL) { CustomAnvil.instance.logger.warning( - "Unknown material $materialTemp on group ${group.getName()}" + "Unknown material $materialName on group ${group.getName()}" ) } continue } - group.addToPolicy(material) + group.addToPolicy(material.asItemType()!!) } // Read group to include in this group policy. diff --git a/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java b/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java index 164bc58..5831f49 100644 --- a/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java +++ b/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java @@ -1,6 +1,6 @@ package xyz.alexcrea.cuanvil.api; -import org.bukkit.Material; +import org.bukkit.inventory.ItemType; import org.junit.jupiter.api.Test; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import xyz.alexcrea.cuanvil.group.IncludeGroup; @@ -9,13 +9,14 @@ import xyz.alexcrea.cuanvil.tests.ConfigResetCustomAnvilTest; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +@SuppressWarnings("UnstableApiUsage") public class MaterialGroupApiTests extends ConfigResetCustomAnvilTest { @Test void groupAddAndRemove() { String groupName = "group"; IncludeGroup group = new IncludeGroup(groupName); - group.addToPolicy(Material.DIAMOND_PICKAXE); // We do not want it to be empty + group.addToPolicy(ItemType.DIAMOND_PICKAXE); // We do not want it to be empty // Group not being set should not exist assertFalse(doGroupExist(groupName)); @@ -48,7 +49,7 @@ public class MaterialGroupApiTests extends ConfigResetCustomAnvilTest { void writeGroup_Reload() { String groupName = "group"; IncludeGroup group = new IncludeGroup(groupName); - group.addToPolicy(Material.DIAMOND_PICKAXE); // We do not want it to be empty + group.addToPolicy(ItemType.DIAMOND_PICKAXE); // We do not want it to be empty // Group not being set should not exist assertFalse(doGroupExist(groupName));