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 ac83977..77b5be2 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,14 @@ package xyz.alexcrea.cuanvil.update.plugin; +import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.alexcrea.cuanvil.api.MaterialGroupApi; import xyz.alexcrea.cuanvil.config.ConfigHolder; +import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; +import xyz.alexcrea.cuanvil.group.IncludeGroup; import javax.annotation.Nonnull; import java.util.List; @@ -24,10 +30,66 @@ public class PUpdate_1_11_0 { "bane_of_arthropods" ); - public static void handleUpdate(@Nonnull Set toSave) { - FileConfiguration config = ConfigHolder.DEFAULT_CONFIG.getConfig(); + 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 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 Material[] HOES = new Material[]{ + Material.WOODEN_HOE, Material.STONE_HOE, + Material.IRON_HOE, Material.DIAMOND_HOE, + Material.GOLDEN_HOE, Material.NETHERITE_HOE + }; + + public static void handleUpdate(@Nonnull Set toSave) { + handleToolsMigration(); + handleMaceMigration(toSave); + } + + private static void handleToolsMigration() { // We migrate the mace conflict if exist and unmodified + AbstractMaterialGroup tools = MaterialGroupApi.getGroup("tools"); + + migrateTools(tools, "pickaxes", PICKAXES); + migrateTools(tools, "shovels", SHOVELS); + migrateTools(tools, "hoes", HOES); + } + + private static void migrateTools( + @Nullable AbstractMaterialGroup tools, + @NotNull String toolset, + @NotNull Material[] toolMats) { + + // Create new group + IncludeGroup group = new IncludeGroup(toolset); + group.addAll(toolMats); + + MaterialGroupApi.addMaterialGroup(group, true); + + // Try to see if all the materials was in the tools group. and if so, replace it with the new group + if (tools == null) return; + if (!(tools instanceof IncludeGroup include)) return; + + List mats = List.of(toolMats); + Set matSet = include.getNonGroupInheritedMaterials(); + if (!matSet.containsAll(mats)) return; + + mats.forEach(matSet::remove); + tools.addToPolicy(group); + MaterialGroupApi.writeMaterialGroup(tools); + } + + private static void handleMaceMigration(@Nonnull Set toSave) { + // We migrate the mace conflict if exist and unmodified + FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); + if (!config.isConfigurationSection("sword_enchant_conflict")) return; if (!config.isConfigurationSection("mace_enchant_conflict")) return; @@ -41,19 +103,20 @@ public class PUpdate_1_11_0 { List enchantments = mace_conflict.getStringList("enchantments"); if (enchantments.size() != 4) return; for (String ench : mace_expected) { - if(!enchantments.contains(ench) && !enchantments.contains("minecraft:" + ench)) return; + if (!enchantments.contains(ench) && !enchantments.contains("minecraft:" + ench)) return; } // Test sword_enchant_conflict is default ConfigurationSection sword_conflict = config.getConfigurationSection("sword_enchant_conflict"); if (sword_conflict.getInt("maxEnchantmentBeforeConflict", 0) != 1) return; - if (sword_conflict.isList("notAffectedGroups") && !sword_conflict.getList("notAffectedGroups").isEmpty()) return; + if (sword_conflict.isList("notAffectedGroups") && !sword_conflict.getList("notAffectedGroups").isEmpty()) + return; enchantments = sword_conflict.getStringList("enchantments"); if (enchantments.size() != 3) return; for (String ench : sword_expected) { - if(!enchantments.contains(ench) && !enchantments.contains("minecraft:" + ench)) return; + if (!enchantments.contains(ench) && !enchantments.contains("minecraft:" + ench)) return; } // Finally we know both conflict are default. so we fix @@ -61,7 +124,7 @@ public class PUpdate_1_11_0 { "minecraft:density", "minecraft:breach"); config.set("mace_enchant_conflict", null); - + toSave.add(ConfigHolder.DEFAULT_CONFIG); } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt index e630e8c..96b485a 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt @@ -101,19 +101,6 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) private fun writeMissingGroups(){ // Write group that do not exist on custom anvil. - // (Tools group regroup most of the tool items. I did not create a seperated group for theses) - val pickaxes = IncludeGroup("pickaxes") - pickaxes.addAll(Material.WOODEN_PICKAXE, Material.STONE_PICKAXE, Material.IRON_PICKAXE, Material.DIAMOND_PICKAXE, Material.GOLDEN_PICKAXE, Material.NETHERITE_PICKAXE) - MaterialGroupApi.addMaterialGroup(pickaxes) - - val shovels = IncludeGroup("shovels") - shovels.addAll(Material.WOODEN_SHOVEL, Material.STONE_SHOVEL, Material.IRON_SHOVEL, Material.DIAMOND_SHOVEL, Material.GOLDEN_SHOVEL, Material.NETHERITE_SHOVEL) - MaterialGroupApi.addMaterialGroup(shovels) - - val hoes = IncludeGroup("hoes") - hoes.addAll(Material.WOODEN_HOE, Material.STONE_HOE, Material.IRON_HOE, Material.DIAMOND_HOE, Material.GOLDEN_HOE, Material.NETHERITE_HOE) - MaterialGroupApi.addMaterialGroup(hoes) - val shield = IncludeGroup("shield") shield.addToPolicy(Material.SHIELD) MaterialGroupApi.addMaterialGroup(shield) @@ -125,7 +112,6 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) val trinkets = IncludeGroup("trinkets") trinkets.addToPolicy(Material.ROTTEN_FLESH) MaterialGroupApi.addMaterialGroup(trinkets) - } private fun writeMaterialRestriction(esEnchantments: List){ diff --git a/src/main/resources/item_groups.yml b/src/main/resources/item_groups.yml index 3c1eb5d..8a62f3d 100644 --- a/src/main/resources/item_groups.yml +++ b/src/main/resources/item_groups.yml @@ -126,7 +126,7 @@ wearable: groups: - armors -tools: +pickaxes: type: include items: - wooden_pickaxe @@ -135,19 +135,33 @@ tools: - diamond_pickaxe - golden_pickaxe - netherite_pickaxe + +shovels: + type: include + items: - wooden_shovel - stone_shovel - iron_shovel - diamond_shovel - golden_shovel - netherite_shovel + +hoes: + type: include + items: - wooden_hoe - stone_hoe - iron_hoe - diamond_hoe - golden_hoe - netherite_hoe + +tools: + type: include groups: + - pickaxes + - shovels + - hoes - axes enchanted_book: