Made conflict and material group api properly ignore empty

This commit is contained in:
alexcrea 2024-11-17 01:36:04 +01:00
parent 6ead1f03ac
commit 101047a25b
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
2 changed files with 25 additions and 17 deletions

View file

@ -27,6 +27,7 @@ public class ConflictAPI {
/** /**
* Write and add a conflict. * Write and add a conflict.
* Will not write the conflict if it already exists. * 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
* @return True if successful. * @return True if successful.
@ -38,6 +39,7 @@ public class ConflictAPI {
/** /**
* Write and add a conflict. * Write and add a conflict.
* Will not write the conflict if it already exists. * 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. * @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; if(!writeConflict(builder, false)) return false;
EnchantConflictGroup conflict = builder.build(); EnchantConflictGroup conflict = builder.build();
// Register conflict // Register conflict
ConfigHolder.CONFLICT_HOLDER.getConflictManager().addConflict(conflict); ConfigHolder.CONFLICT_HOLDER.getConflictManager().addConflict(conflict);
@ -69,8 +70,8 @@ public class ConflictAPI {
* <p> * <p>
* You may want to use {@link #addConflict(ConflictBuilder)} instead as it is more performance in most case as this function will reload every conflict. * 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 * @param builder the builder
* @return True if successful. * @return true if was written successfully.
*/ */
public static boolean writeConflict(@NotNull ConflictBuilder builder){ public static boolean writeConflict(@NotNull ConflictBuilder builder){
return writeConflict(builder, true); return writeConflict(builder, true);
@ -83,7 +84,7 @@ public class ConflictAPI {
* *
* @param builder The builder * @param builder The builder
* @param updatePlanned If we should plan a global update for conflicts * @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){ public static boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){
FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.CONFLICT_HOLDER.getConfig();
@ -103,6 +104,7 @@ public class ConflictAPI {
if(!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups); if(!excludedGroups.isEmpty()) config.set(basePath + "notAffectedGroups", excludedGroups);
if(builder.getMaxBeforeConflict() > 0) config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict()); if(builder.getMaxBeforeConflict() > 0) config.set(basePath + "maxEnchantmentBeforeConflict", builder.getMaxBeforeConflict());
if(!config.isConfigurationSection(name)) return false;
prepareSaveTask(); prepareSaveTask();
if(updatePlanned) prepareUpdateTask(); if(updatePlanned) prepareUpdateTask();

View file

@ -27,10 +27,10 @@ public class MaterialGroupApi {
private static Object saveChangeTask = null; private static Object saveChangeTask = null;
private static Object reloadChangeTask = null; private static Object reloadChangeTask = null;
/** /**
* Write and add a group. * Write and add a group.
* Will not write the group if it already exists. * 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
* @return true if successful. * @return true if successful.
@ -42,6 +42,7 @@ public class MaterialGroupApi {
/** /**
* Write and add a group. * Write and add a group.
* Will not write the group if it already exists. * 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. * @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. * 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 * @param group the group to write
* @return true if successful. * @return true if was written successfully.
*/ */
public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){ public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){
return writeMaterialGroup(group, true); return writeMaterialGroup(group, true);
@ -90,7 +91,7 @@ public class MaterialGroupApi {
* *
* @param group the group to write * @param group the group to write
* @param updatePlanned if we should plan a global update for material groups * @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){ public static boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group, boolean updatePlanned){
String name = group.getName(); String name = group.getName();
@ -99,13 +100,15 @@ public class MaterialGroupApi {
return false; return false;
} }
boolean changed;
if(group instanceof IncludeGroup includeGroup){ if(group instanceof IncludeGroup includeGroup){
writeKnownGroup("include", includeGroup); changed = writeKnownGroup("include", includeGroup);
}else if(group instanceof ExcludeGroup excludeGroup){ }else if(group instanceof ExcludeGroup excludeGroup){
writeKnownGroup("exclude", excludeGroup); changed = writeKnownGroup("exclude", excludeGroup);
}else{ }else{
writeUnknownGroup(group); changed = writeUnknownGroup(group);
} }
if(!changed) return false;
prepareSaveTask(); prepareSaveTask();
if(updatePlanned) prepareUpdateTask(); if(updatePlanned) prepareUpdateTask();
@ -113,34 +116,37 @@ public class MaterialGroupApi {
return true; 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(); FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig();
String basePath = group.getName() + "."; String basePath = group.getName() + ".";
Set<Material> materialSet = group.getNonGroupInheritedMaterials(); Set<Material> materialSet = group.getNonGroupInheritedMaterials();
Set<AbstractMaterialGroup> groupSet = group.getGroups(); Set<AbstractMaterialGroup> groupSet = group.getGroups();
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, groupType);
if(!materialSet.isEmpty()){ if(!materialSet.isEmpty()){
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materialSet)); config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materialSet));
} }
if(!groupSet.isEmpty()){ if(!groupSet.isEmpty()){
config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, materialGroupSetToStringList(groupSet)); 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(); FileConfiguration config = ConfigHolder.ITEM_GROUP_HOLDER.getConfig();
String basePath = group.getName() + "."; String basePath = group.getName() + ".";
EnumSet<Material> materials = group.getMaterials(); EnumSet<Material> materials = group.getMaterials();
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include"); if(materials.isEmpty()) return false;
if(!materials.isEmpty()){
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials));
}
config.set(basePath + ItemGroupManager.GROUP_TYPE_PATH, "include");
config.set(basePath + ItemGroupManager.MATERIAL_LIST_PATH, materialSetToStringList(materials));
return true;
} }
public static List<String> materialSetToStringList(@NotNull Set<Material> materials){ public static List<String> materialSetToStringList(@NotNull Set<Material> materials){