diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java index 8047382..c256b7b 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java @@ -27,6 +27,7 @@ public class ConflictAPI { /** * Write and add a conflict. * 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 * @return True if successful. @@ -38,6 +39,7 @@ public class ConflictAPI { /** * Write and add a conflict. * 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 overrideDeleted If we should write even if the conflict was previously deleted. @@ -52,7 +54,6 @@ public class ConflictAPI { if(!writeConflict(builder, false)) return false; - EnchantConflictGroup conflict = builder.build(); // Register conflict ConfigHolder.CONFLICT_HOLDER.getConflictManager().addConflict(conflict); @@ -69,8 +70,8 @@ public class ConflictAPI { *

* You may want to use {@link #addConflict(ConflictBuilder)} instead as it is more performance in most case as this function will reload every conflict. * - * @param builder The builder - * @return True if successful. + * @param builder the builder + * @return true if was written successfully. */ public static boolean writeConflict(@NotNull ConflictBuilder builder){ return writeConflict(builder, true); @@ -83,7 +84,7 @@ public class ConflictAPI { * * @param builder The builder * @param updatePlanned If we should plan a global update for conflicts - * @return True if successful. + * @return true if was written successfully. */ public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){ FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); @@ -103,6 +104,7 @@ public class ConflictAPI { if(!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups); if(builder.getMaxBeforeConflict() > 0) config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict()); + if(!config.isConfigurationSection(name)) return false; prepareSaveTask(); if(updatePlanned) prepareUpdateTask(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java index 61fc4f1..5aed0e4 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java @@ -27,10 +27,10 @@ public class MaterialGroupApi { private static Object saveChangeTask = null; private static Object reloadChangeTask = null; - /** * Write and add a group. * Will not write the group if it already exists. + * Will not be successful if the group is empty. * * @param group The group to add * @return true if successful. @@ -42,6 +42,7 @@ public class MaterialGroupApi { /** * Write and add a group. * 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 overrideDeleted If we should write even if the group was previously deleted. @@ -77,7 +78,7 @@ public class MaterialGroupApi { * You may want to use {@link #addMaterialGroup(AbstractMaterialGroup)} instead as it is more performance in most case as this function will reload every conflict. * * @param group the group to write - * @return true if successful. + * @return true if was written successfully. */ public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){ return writeMaterialGroup(group, true); @@ -90,7 +91,7 @@ public class MaterialGroupApi { * * @param group the group to write * @param updatePlanned if we should plan a global update for material groups - * @return true if successful. + * @return true if was written successfully. */ public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group, boolean updatePlanned){ String name = group.getName(); @@ -99,13 +100,15 @@ public class MaterialGroupApi { return false; } + boolean changed; if(group instanceof IncludeGroup includeGroup){ - writeKnownGroup("include", includeGroup); + changed = writeKnownGroup("include", includeGroup); }else if(group instanceof ExcludeGroup excludeGroup){ - writeKnownGroup("exclude", excludeGroup); + changed = writeKnownGroup("exclude", excludeGroup); }else{ - writeUnknownGroup(group); + changed = writeUnknownGroup(group); } + if(!changed) return false; prepareSaveTask(); if(updatePlanned) prepareUpdateTask(); @@ -113,34 +116,37 @@ public class MaterialGroupApi { return true; } - private static void 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(); - config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, groupType); if(!materialSet.isEmpty()){ config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materialSet)); } if(!groupSet.isEmpty()){ config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, materialGroupSetToStringList(groupSet)); } + if(!config.isConfigurationSection(group.getName())) return false; + config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, groupType); + return true; } - private static void writeUnknownGroup(@NotNull AbstractMaterialGroup group) { + private static boolean writeUnknownGroup(@NotNull AbstractMaterialGroup group) { FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig(); String basePath = group.getName() + "."; EnumSet materials = group.getMaterials(); - config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include"); - if(!materials.isEmpty()){ - config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials)); - } + if(materials.isEmpty()) return false; + config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include"); + config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials)); + + return true; } public static List materialSetToStringList(@NotNull Set materials){