diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java index c256b7b..2c95766 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java @@ -19,7 +19,8 @@ import java.util.List; @SuppressWarnings("unused") public class ConflictAPI { - private ConflictAPI() {} + private ConflictAPI() { + } private static Object saveChangeTask = null; private static Object reloadChangeTask = null; @@ -32,7 +33,7 @@ public class ConflictAPI { * @param builder The conflict builder to be based on * @return True if successful. */ - public static boolean addConflict(@NotNull ConflictBuilder builder){ + public static boolean addConflict(@NotNull ConflictBuilder builder) { return addConflict(builder, false); } @@ -41,18 +42,18 @@ public class ConflictAPI { * Will not write the conflict if it already exists. * Will not be successful if the conflict is empty. * - * @param builder The conflict builder to be based on + * @param builder The conflict builder to be based on * @param overrideDeleted If we should write even if the conflict was previously deleted. * @return True if successful. */ - public static boolean addConflict(@NotNull ConflictBuilder builder, boolean overrideDeleted){ + public static boolean addConflict(@NotNull ConflictBuilder builder, boolean overrideDeleted) { FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); // Test if conflict can be added - if(!overrideDeleted && ConfigHolder.CONFLICT_HOLDER.isDeleted(builder.getName())) return false; - if(config.contains(builder.getName())) return false; + if (!overrideDeleted && ConfigHolder.CONFLICT_HOLDER.isDeleted(builder.getName())) return false; + if (config.contains(builder.getName())) return false; - if(!writeConflict(builder, false)) return false; + if (!writeConflict(builder, false)) return false; EnchantConflictGroup conflict = builder.build(); // Register conflict @@ -60,7 +61,7 @@ public class ConflictAPI { // Add conflict to gui EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance(); - if(conflictGui != null) conflictGui.updateValueForGeneric(conflict, true); + if (conflictGui != null) conflictGui.updateValueForGeneric(conflict, true); return true; } @@ -73,7 +74,7 @@ public class ConflictAPI { * @param builder the builder * @return true if was written successfully. */ - public static boolean writeConflict(@NotNull ConflictBuilder builder){ + public static boolean writeConflict(@NotNull ConflictBuilder builder) { return writeConflict(builder, true); } @@ -86,12 +87,12 @@ public class ConflictAPI { * @param updatePlanned If we should plan a global update for conflicts * @return true if was written successfully. */ - public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){ + public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned) { FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); String name = builder.getName(); - if(name.contains(".")) { - CustomAnvil.instance.getLogger().warning("Conflict " + name +" contain \".\" in its name but should not. this conflict is ignored."); + if (name.contains(".")) { + CustomAnvil.instance.getLogger().warning("Conflict " + name + " contain \".\" in its name but should not. this conflict is ignored."); logConflictOrigin(builder); return false; } @@ -100,25 +101,27 @@ public class ConflictAPI { List enchantments = extractEnchantments(builder); List excludedGroups = new ArrayList<>(builder.getExcludedGroupNames()); - if(!enchantments.isEmpty()) config.set(basePath + "enchantments", enchantments); - if(!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups); - if(builder.getMaxBeforeConflict() > 0) config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict()); + if (!enchantments.isEmpty()) config.set(basePath + "enchantments", enchantments); + if (!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups); + if (builder.getMaxBeforeConflict() > 0) + config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict()); - if(!config.isConfigurationSection(name)) return false; + if (!config.isConfigurationSection(name)) return false; prepareSaveTask(); - if(updatePlanned) prepareUpdateTask(); + if (updatePlanned) prepareUpdateTask(); return true; } /** * Extract every enchantment names from a builder. + * * @param builder The builder storing the enchantments * @return Builder's stored enchantment. */ @NotNull - private static List extractEnchantments(@NotNull ConflictBuilder builder){ + private static List extractEnchantments(@NotNull ConflictBuilder builder) { List result = new ArrayList<>(builder.getEnchantmentNames()); for (NamespacedKey enchantmentKey : builder.getEnchantmentKeys()) { result.add(enchantmentKey.toString()); @@ -133,7 +136,7 @@ public class ConflictAPI { * @param conflict The conflict to remove * @return True if successful. */ - public static boolean removeConflict(@NotNull EnchantConflictGroup conflict){ + public static boolean removeConflict(@NotNull EnchantConflictGroup conflict) { // Remove from registry ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict); @@ -143,7 +146,7 @@ public class ConflictAPI { // Remove from gui EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance(); - if(conflictGui != null) conflictGui.removeGeneric(conflict); + if (conflictGui != null) conflictGui.removeGeneric(conflict); return true; @@ -153,9 +156,9 @@ public class ConflictAPI { * Prepare a task to save conflict configuration. */ private static void prepareSaveTask() { - if(saveChangeTask != null) return; + if (saveChangeTask != null) return; - saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{ + saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> { ConfigHolder.CONFLICT_HOLDER.saveToDisk(true); saveChangeTask = null; }); @@ -165,28 +168,29 @@ public class ConflictAPI { * Prepare a task to reload every conflict. */ private static void prepareUpdateTask() { - if(reloadChangeTask != null) return; + if (reloadChangeTask != null) return; - reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{ + reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> { ConfigHolder.CONFLICT_HOLDER.reload(); EnchantConflictGui conflictGui = EnchantConflictGui.getCurrentInstance(); - if(conflictGui != null) conflictGui.reloadValues(); + if (conflictGui != null) conflictGui.reloadValues(); reloadChangeTask = null; }); } - static void logConflictOrigin(@NotNull ConflictBuilder builder){ + static void logConflictOrigin(@NotNull ConflictBuilder builder) { CustomAnvil.instance.getLogger().warning("Conflict " + builder.getName() + " came from " + builder.getSourceName() + "."); } /** * Get every registered conflict. + * * @return An immutable collection of conflict. */ @NotNull - public static List getRegisteredConflict(){ + public static List getRegisteredConflict() { List mutableList = ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList(); return Collections.unmodifiableList(mutableList); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java index 5aed0e4..250f797 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java @@ -22,7 +22,8 @@ import java.util.*; @SuppressWarnings("unused") public class MaterialGroupApi { - private MaterialGroupApi(){} + private MaterialGroupApi() { + } private static Object saveChangeTask = null; private static Object reloadChangeTask = null; @@ -35,7 +36,7 @@ public class MaterialGroupApi { * @param group The group to add * @return true if successful. */ - public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group){ + public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group) { return addMaterialGroup(group, false); } @@ -44,28 +45,28 @@ public class MaterialGroupApi { * Will not write the group if it already exists. * Will not be successful if the group is empty. * - * @param group The group to add + * @param group The group to add * @param overrideDeleted If we should write even if the group was previously deleted. * @return true if successful. */ - public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group, boolean overrideDeleted){ + public static boolean addMaterialGroup(@NotNull AbstractMaterialGroup group, boolean overrideDeleted) { ItemGroupManager itemGroupManager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager(); // Test if it exists/existed - if(!overrideDeleted && ConfigHolder.ITEM_GROUP_HOLDER.isDeleted(group.getName())) return false; - if(itemGroupManager.get(group.getName()) != null) return false; + if (!overrideDeleted && ConfigHolder.ITEM_GROUP_HOLDER.isDeleted(group.getName())) return false; + if (itemGroupManager.get(group.getName()) != null) return false; // Add group itemGroupManager.getGroupMap().put(group.getName(), group); - if(!writeMaterialGroup(group, false)) return false; + if (!writeMaterialGroup(group, false)) return false; - if(group instanceof IncludeGroup includeGroup){ + if (group instanceof IncludeGroup includeGroup) { GroupConfigGui configGui = GroupConfigGui.getCurrentInstance(); - if(configGui != null) configGui.updateValueForGeneric(includeGroup, true); + if (configGui != null) configGui.updateValueForGeneric(includeGroup, true); } - if(ConfigOptions.INSTANCE.getVerboseDebugLog()){ + if (ConfigOptions.INSTANCE.getVerboseDebugLog()) { CustomAnvil.instance.getLogger().info("Registered group " + group.getName()); } @@ -80,7 +81,7 @@ public class MaterialGroupApi { * @param group the group to write * @return true if was written successfully. */ - public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){ + public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group) { return writeMaterialGroup(group, true); } @@ -93,43 +94,43 @@ public class MaterialGroupApi { * @param updatePlanned if we should plan a global update for material groups * @return true if was written successfully. */ - public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group, boolean updatePlanned){ + public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group, boolean updatePlanned) { String name = group.getName(); - if(name.contains(".")) { - CustomAnvil.instance.getLogger().warning("Group " + name +" contain . in its name but should not. this material group is ignored."); + if (name.contains(".")) { + CustomAnvil.instance.getLogger().warning("Group " + name + " contain . in its name but should not. this material group is ignored."); return false; } boolean changed; - if(group instanceof IncludeGroup includeGroup){ + if (group instanceof IncludeGroup includeGroup) { changed = writeKnownGroup("include", includeGroup); - }else if(group instanceof ExcludeGroup excludeGroup){ + } else if (group instanceof ExcludeGroup excludeGroup) { changed = writeKnownGroup("exclude", excludeGroup); - }else{ + } else { changed = writeUnknownGroup(group); } - if(!changed) return false; + if (!changed) return false; prepareSaveTask(); - if(updatePlanned) prepareUpdateTask(); + if (updatePlanned) prepareUpdateTask(); return true; } - private static boolean writeKnownGroup(@NotNull String groupType, @NotNull AbstractMaterialGroup group){ + private static boolean writeKnownGroup(@NotNull String groupType, @NotNull AbstractMaterialGroup group) { FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig(); String basePath = group.getName() + "."; Set materialSet = group.getNonGroupInheritedMaterials(); Set groupSet = group.getGroups(); - if(!materialSet.isEmpty()){ + if (!materialSet.isEmpty()) { config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materialSet)); } - if(!groupSet.isEmpty()){ + if (!groupSet.isEmpty()) { config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, materialGroupSetToStringList(groupSet)); } - if(!config.isConfigurationSection(group.getName())) return false; + if (!config.isConfigurationSection(group.getName())) return false; config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, groupType); return true; @@ -141,7 +142,7 @@ public class MaterialGroupApi { String basePath = group.getName() + "."; EnumSet materials = group.getMaterials(); - if(materials.isEmpty()) return false; + if (materials.isEmpty()) return false; config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include"); config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials)); @@ -149,11 +150,11 @@ public class MaterialGroupApi { return true; } - public static List materialSetToStringList(@NotNull Set materials){ + public static List materialSetToStringList(@NotNull Set materials) { return materials.stream().map(material -> material.getKey().getKey().toLowerCase()).toList(); } - public static List materialGroupSetToStringList(@NotNull Set groups){ + public static List materialGroupSetToStringList(@NotNull Set groups) { return groups.stream().map(AbstractMaterialGroup::getName).toList(); } @@ -165,19 +166,19 @@ public class MaterialGroupApi { * @param group The recipe to remove * @return True if the group was present. */ - public static boolean removeGroup(@NotNull AbstractMaterialGroup group){ + public static boolean removeGroup(@NotNull AbstractMaterialGroup group) { // Remove from registry AbstractMaterialGroup removed = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().groupMap.remove(group.getName()); - if(removed == null) return false; + if (removed == null) return false; // Delete and save to file ConfigHolder.ITEM_GROUP_HOLDER.delete(group.getName()); prepareSaveTask(); // Remove from gui - if(group instanceof IncludeGroup includeGroup){ + if (group instanceof IncludeGroup includeGroup) { GroupConfigGui configGui = GroupConfigGui.getCurrentInstance(); - if(configGui != null) configGui.removeGeneric(includeGroup); + if (configGui != null) configGui.removeGeneric(includeGroup); } return true; @@ -187,9 +188,9 @@ public class MaterialGroupApi { * Prepare a task to reload every conflict. */ private static void prepareSaveTask() { - if(saveChangeTask != null) return; + if (saveChangeTask != null) return; - saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{ + saveChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> { ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true); saveChangeTask = null; }); @@ -199,13 +200,13 @@ public class MaterialGroupApi { * Prepare a task to save configuration. */ private static void prepareUpdateTask() { - if(reloadChangeTask != null) return; + if (reloadChangeTask != null) return; - reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, ()->{ + reloadChangeTask = DependencyManager.scheduler.scheduleGlobally(CustomAnvil.instance, () -> { ConfigHolder.ITEM_GROUP_HOLDER.reload(); GroupConfigGui configGui = GroupConfigGui.getCurrentInstance(); - if(configGui != null) configGui.reloadValues(); + if (configGui != null) configGui.reloadValues(); reloadChangeTask = null; }); @@ -219,16 +220,17 @@ public class MaterialGroupApi { * @return the abstract group of this name. null if not found. */ @Nullable - public static AbstractMaterialGroup getGroup(@NotNull String groupName){ + public static AbstractMaterialGroup getGroup(@NotNull String groupName) { return ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().get(groupName); } /** * Get every registered material groups. + * * @return An immutable map of group name as its key and group as mapped value. */ @NotNull - public static Map getRegisteredGroups(){ + public static Map getRegisteredGroups() { return Collections.unmodifiableMap(ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap()); } diff --git a/src/test/java/xyz/alexcrea/cuanvil/api/ConflictApiTests.java b/src/test/java/xyz/alexcrea/cuanvil/api/ConflictApiTests.java index c9ee095..4b43624 100644 --- a/src/test/java/xyz/alexcrea/cuanvil/api/ConflictApiTests.java +++ b/src/test/java/xyz/alexcrea/cuanvil/api/ConflictApiTests.java @@ -100,6 +100,7 @@ public class ConflictApiTests extends ConfigResetCustomAnvilTest { void writeGroup_Reload() { String conflictName = "conflict"; ConflictBuilder builder = new ConflictBuilder(conflictName); + builder.addEnchantment("bane_of_arthropods"); // Group not being set should not exist assertFalse(doGroupExist(conflictName)); @@ -114,6 +115,24 @@ public class ConflictApiTests extends ConfigResetCustomAnvilTest { assertTrue(doGroupExist(conflictName)); } + @Test + void writeGroup_Empty() { + String conflictName = "conflict"; + ConflictBuilder builder = new ConflictBuilder(conflictName); + + // Group not being set should not exist + assertFalse(doGroupExist(conflictName)); + + // Add group and reload + assertFalse(ConflictAPI.writeConflict(builder)); + assertFalse(doGroupExist(conflictName)); + + // Tick so write get reloaded + server.getScheduler().performOneTick(); + + assertFalse(doGroupExist(conflictName)); + } + @Test void writeGroup_InvalidDot() { String conflictName = "conflict.conflict"; @@ -124,13 +143,13 @@ public class ConflictApiTests extends ConfigResetCustomAnvilTest { } // Maybe move to ConflictApi class ? - private static boolean doGroupExist(@NotNull String groupName) { + private static boolean doGroupExist(@NotNull String groupName) { return findGroup(groupName) != null; } // Maybe move to ConflictApi class ? @Nullable - private static EnchantConflictGroup findGroup(@NotNull String groupName){ + private static EnchantConflictGroup findGroup(@NotNull String groupName) { for (EnchantConflictGroup enchantConflictGroup : ConflictAPI.getRegisteredConflict()) { if (groupName.equalsIgnoreCase(enchantConflictGroup.getName())) { return enchantConflictGroup; diff --git a/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java b/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java index e9c9485..164bc58 100644 --- a/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java +++ b/src/test/java/xyz/alexcrea/cuanvil/api/MaterialGroupApiTests.java @@ -1,5 +1,6 @@ package xyz.alexcrea.cuanvil.api; +import org.bukkit.Material; import org.junit.jupiter.api.Test; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import xyz.alexcrea.cuanvil.group.IncludeGroup; @@ -14,6 +15,7 @@ public class MaterialGroupApiTests extends ConfigResetCustomAnvilTest { void groupAddAndRemove() { String groupName = "group"; IncludeGroup group = new IncludeGroup(groupName); + group.addToPolicy(Material.DIAMOND_PICKAXE); // We do not want it to be empty // Group not being set should not exist assertFalse(doGroupExist(groupName)); @@ -46,6 +48,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 not being set should not exist assertFalse(doGroupExist(groupName)); @@ -63,6 +66,22 @@ public class MaterialGroupApiTests extends ConfigResetCustomAnvilTest { assertTrue(doGroupCanBeFound(groupName)); } + @Test + void writeGroup_Empty() { + String groupName = "group"; + IncludeGroup group = new IncludeGroup(groupName); + + // Add group and reload + assertFalse(MaterialGroupApi.writeMaterialGroup(group)); + assertFalse(doGroupExist(groupName)); + assertFalse(doGroupCanBeFound(groupName)); + + // Tick so write get reloaded + server.getScheduler().performOneTick(); + + assertFalse(doGroupExist(groupName)); + assertFalse(doGroupCanBeFound(groupName)); + } @Test void writeGroup_InvalidDot() {