diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java index b0f81c7..a0a077a 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java @@ -25,6 +25,7 @@ public class ConflictAPI { /** * Write and add a conflict. + * Will not write the conflict if it already exists. * * @param builder the conflict builder to base on * @return true if successful @@ -122,7 +123,7 @@ public class ConflictAPI { * You should use {@link #addConflict(ConflictBuilder)} or {@link #writeConflict(ConflictBuilder)} instead * * @param builder the builder - * @param updatePlanned if we should plan an update + * @param updatePlanned if we should plan a global update for conflicts * @return true if successful */ public boolean writeConflict(@NotNull ConflictBuilder builder, boolean updatePlanned){ @@ -135,7 +136,7 @@ public class ConflictAPI { return false; } - String basePath = name+"."; + String basePath = name + "."; Set enchantments = extractEnchantments(builder); Set excludedGroups = builder.getExcludedGroupNames(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java index cfa498c..08471a0 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/EnchantmentApi.java @@ -83,7 +83,7 @@ public class EnchantmentApi { } /** - * Get by key the enchantment. + * Get by key a enchantment. * * @param key the key used to fetch * @return the custom anvil enchantment @@ -94,7 +94,7 @@ public class EnchantmentApi { } /** - * Get by name the enchantment. + * Get by name a enchantment. * * @param name the name used to fetch * @return the custom anvil enchantment diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java new file mode 100644 index 0000000..3041675 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java @@ -0,0 +1,158 @@ +package xyz.alexcrea.cuanvil.api; + +import io.delilaheve.CustomAnvil; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import xyz.alexcrea.cuanvil.config.ConfigHolder; +import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; +import xyz.alexcrea.cuanvil.group.ExcludeGroup; +import xyz.alexcrea.cuanvil.group.IncludeGroup; +import xyz.alexcrea.cuanvil.group.ItemGroupManager; +import xyz.alexcrea.cuanvil.gui.config.global.GroupConfigGui; + +import java.util.EnumSet; +import java.util.Set; + +/** + * The type Material group api. + */ +@SuppressWarnings("unused") +public class MaterialGroupApi { + + private static int saveChangeTask = -1; + private static int reloadChangeTask = -1; + + /** + * Write and add a group. + * Will not write the group if it already exists. + * + * @param group the group to add + * @return true if successful + */ + public boolean addMaterialGroup(@NotNull AbstractMaterialGroup group){ + ItemGroupManager itemGroupManager = ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager(); + if(itemGroupManager.get(group.getName()) != null) return false; + + if(!writeMaterialGroup(group, false)) return false; + + if(group instanceof IncludeGroup includeGroup){ + GroupConfigGui.INSTANCE.updateValueForGeneric(includeGroup, true); + } + + return true; + } + + /** + * Write a material group to the config file and plan an update of groups. + *

+ * 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 + */ + public boolean writeMaterialGroup(@NotNull AbstractMaterialGroup group){ + return writeMaterialGroup(group, true); + } + + /** + * Write a material group to the config file. + *

+ * You should use {@link #addMaterialGroup(AbstractMaterialGroup)} or {@link #writeMaterialGroup(AbstractMaterialGroup)} instead + * + * @param group the group to write + * @param updatePlanned if we should plan a global update for material groups + * @return true if successful + */ + public 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."); + return false; + } + + if(group instanceof IncludeGroup includeGroup){ + writeKnownGroup("include", includeGroup); + }else if(group instanceof ExcludeGroup excludeGroup){ + writeKnownGroup("exclude", excludeGroup); + }else{ + writeUnknownGroup(group); + } + + prepareSaveTask(); + if(updatePlanned) prepareUpdateTask(); + + return true; + } + + private void 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, materialSet); + } + if(!groupSet.isEmpty()){ + config.set(basePath + ItemGroupManager.GROUP_LIST_PATH, groupSet); + } + + } + + private void 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, materials); + } + + } + + /** + * Prepare a task to reload every conflict. + */ + private static void prepareSaveTask() { + if(saveChangeTask != -1) return; + + saveChangeTask = Bukkit.getScheduler().scheduleSyncDelayedTask(CustomAnvil.instance, ()->{ + ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(true); + saveChangeTask = -1; + }, 0L); + } + + /** + * Prepare a task to save configuration. + */ + private static void prepareUpdateTask() { + if(reloadChangeTask != -1) return; + + reloadChangeTask = Bukkit.getScheduler().scheduleSyncDelayedTask(CustomAnvil.instance, ()->{ + ConfigHolder.ITEM_GROUP_HOLDER.reload(); + GroupConfigGui.INSTANCE.reloadValues(); + reloadChangeTask = -1; + }, 0L); + + } + + /** + * Get by name a group. + * + * @param groupName the group name used to fetch + * @return the abstract group of this name + */ + @Nullable + public static AbstractMaterialGroup getGroup(@NotNull String groupName){ + return ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().get(groupName); + } + + +} diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java index 4a9a766..826160d 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantment.java @@ -1,7 +1,6 @@ package xyz.alexcrea.cuanvil.enchant; import io.delilaheve.util.ItemUtil; -import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.ItemStack; @@ -11,13 +10,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import xyz.alexcrea.cuanvil.dependency.DependencyManager; import xyz.alexcrea.cuanvil.dependency.EnchantmentSquaredDependency; -import xyz.alexcrea.cuanvil.group.ConflictType; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.function.Supplier; /** * Represent an enchantment compatible with Custom Anvil. diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java index 7ea4666..cec89bd 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/CAEnchantmentBase.java @@ -1,20 +1,16 @@ package xyz.alexcrea.cuanvil.enchant; -import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.entity.HumanEntity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import xyz.alexcrea.cuanvil.group.ConflictType; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.function.Supplier; public abstract class CAEnchantmentBase implements CAEnchantment { diff --git a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java index 31991f4..f4ae757 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java +++ b/src/main/java/xyz/alexcrea/cuanvil/enchant/wrapped/CAEcoEnchant.java @@ -5,13 +5,11 @@ import com.willfp.ecoenchants.target.EnchantmentTarget; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment; import xyz.alexcrea.cuanvil.enchant.CAEnchantment; import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity; -import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment; -import xyz.alexcrea.cuanvil.group.ConflictType; import java.util.Map; -import java.util.function.Supplier; public class CAEcoEnchant extends CAVanillaEnchantment implements AdditionalTestEnchantment { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt index ea808aa..2ff60a7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/EnchantConflictManager.kt @@ -9,7 +9,6 @@ import xyz.alexcrea.cuanvil.enchant.AdditionalTestEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantment import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import java.util.* -import kotlin.collections.ArrayList class EnchantConflictManager {