From e43f6276cdab8da23f32c780b4d599faea2751df Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Wed, 24 Jul 2024 02:16:40 +0200 Subject: [PATCH 1/2] Made unit repair test lower first. Default config and creation use lowercase by default. testing lowercase first is a small improvement of performances. --- .../kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt index 141d3a0..2047e79 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/UnitRepairUtil.kt @@ -22,10 +22,10 @@ object UnitRepairUtil { if (other == null) return null val config = ConfigHolder.UNIT_REPAIR_HOLDER.config // Get configuration section if exist - val otherName = other.type.name.uppercase() + val otherName = other.type.name.lowercase() var section = config.getConfigurationSection(otherName) if (section == null) { - section = config.getConfigurationSection(otherName.lowercase()) + section = config.getConfigurationSection(otherName.uppercase()) if (section == null) return null } @@ -44,11 +44,11 @@ object UnitRepairUtil { * If value is set to less than or equal to 0 then it will be set to default */ private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double? { - val itemName = item.type.name.uppercase() + val itemName = item.type.name.lowercase() val repairValue = if (section.isDouble(itemName)) { section.getDouble(itemName) - } else if (section.isDouble(itemName.lowercase())) { - section.getDouble(itemName.lowercase()) + } else if (section.isDouble(itemName.uppercase())) { + section.getDouble(itemName.uppercase()) } else { return null } From e89fff951ee51be96e514c67aceb296e4758a949 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:15:36 +0200 Subject: [PATCH 2/2] Add unit repair api. --- .../xyz/alexcrea/cuanvil/api/ConflictAPI.java | 2 +- .../cuanvil/api/CustomAnvilRecipeApi.java | 2 +- .../cuanvil/api/MaterialGroupApi.java | 2 +- .../alexcrea/cuanvil/api/UnitRepairApi.java | 215 ++++++++++++++++++ .../config/global/UnitRepairConfigGui.java | 12 + .../config/list/UnitRepairElementListGui.java | 2 +- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 11 +- 7 files changed, 240 insertions(+), 6 deletions(-) create mode 100644 src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java index 667fc6d..db577a1 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/ConflictAPI.java @@ -135,7 +135,7 @@ public class ConflictAPI { // Remove from registry ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict); - // Write as null and save to file + // Delete and save to file ConfigHolder.CONFLICT_HOLDER.delete(conflict.getName()); prepareSaveTask(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApi.java index ed628fe..250e854 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/CustomAnvilRecipeApi.java @@ -88,7 +88,7 @@ public class CustomAnvilRecipeApi { // Remove from registry ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanRemove(recipe); - // Write as null and save to file + // Delete and save to file ConfigHolder.CUSTOM_RECIPE_HOLDER.delete(recipe.getName()); prepareSaveTask(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java index ca061ba..6e50ff9 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java +++ b/src/main/java/xyz/alexcrea/cuanvil/api/MaterialGroupApi.java @@ -163,7 +163,7 @@ public class MaterialGroupApi { // Remove from registry ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().groupMap.remove(group.getName()); - // Write as null and save to file + // Delete and save to file ConfigHolder.ITEM_GROUP_HOLDER.delete(group.getName()); prepareSaveTask(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java b/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java new file mode 100644 index 0000000..fe3c164 --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java @@ -0,0 +1,215 @@ +package xyz.alexcrea.cuanvil.api; + +import io.delilaheve.CustomAnvil; +import kotlin.Triple; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; +import xyz.alexcrea.cuanvil.config.ConfigHolder; +import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; +import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Custom Anvil api for unit repair. + */ +@SuppressWarnings("unused") +public class UnitRepairApi { + + private UnitRepairApi(){} + + private static int saveChangeTask = -1; + + /** + * Write and add a custom anvil unit repair recipe. + * Will not write the recipe if it already exists or was deleted. + * Set the value to minecraft default value (0.25 = 25%) + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @return true if successful. + */ + public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable){ + return addUnitRepair(unit, repairable, 0.25, false); + } + + /** + * Write and add a custom anvil unit repair recipe. + * Will not write the recipe if it already exists or was deleted. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @param value The amount to be repaired by every unit. (1% = 0.01) + * @return true if successful. + */ + public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value){ + return addUnitRepair(unit, repairable, value, false); + } + + /** + * Write and add a custom anvil unit repair recipe. + * Will not write the recipe if it already exists. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @param value The amount to be repaired by every unit. (1% = 0.01) + * @param overrideDeleted If we should write even if the recipe was previously deleted. + * @return true if successful. + */ + public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted){ + FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); + String path = unit.name().toLowerCase() + "." + repairable.name().toLowerCase(); + + if(!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false; + if(config.contains(path)) return false; + + // Set unit repair + return setUnitRepair(unit, repairable, value); + } + + /** + * Write and add a custom anvil unit repair recipe. + * Do not check if it previously existed or exist. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item to be repaired. + * @param value The amount to be repaired by every unit. (1% = 0.01) + * @return true if successful. + */ + public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value){ + FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); + + String repairableName = repairable.name().toLowerCase(); + String path = unit.name().toLowerCase() + "." + repairableName; + + // Add to config then prepare save + config.set(path, value); + prepareSaveTask(); + + // Add to gui + UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance(); + if(repairConfigGui != null) { + UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit); + + elementGui.updateValueForGeneric(repairableName, true); + repairConfigGui.updateValueForGeneric(unit, true); + } + + return true; + } + + /** + * Remove a custom anvil unit repair recipe. + * + * @param unit The unit material used to repair the bellow item. + * @param repairable The item used to be repaired. + * @return true if successful. + */ + public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable){ + // Delete every possible variation and save to file + String unitName = unit.name(); + String repairableName = repairable.name(); + + FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); + config.set(unitName.toLowerCase() + repairableName.toUpperCase(), null); + config.set(unitName.toUpperCase() + repairableName.toLowerCase(), null); + config.set(unitName.toUpperCase() + repairableName.toUpperCase(), null); + + // Test if it was the last value of this section + boolean lastValue = false; + if(config.isConfigurationSection(unitName.toLowerCase())) { + ConfigurationSection section = config.getConfigurationSection(unitName.toLowerCase()); + if(section.getKeys(false).isEmpty()) { + lastValue = true; + config.set(unitName.toLowerCase(), null); + } + + } else if (config.isConfigurationSection(unitName.toUpperCase())) { + ConfigurationSection section = config.getConfigurationSection(unitName.toUpperCase()); + if(section.getKeys(false).isEmpty()) { + lastValue = true; + config.set(unitName.toUpperCase(), null); + } + + } else lastValue = true; + + + // We only need to "delete" as the lower case to be counted as deleted + ConfigHolder.UNIT_REPAIR_HOLDER.delete(unitName.toLowerCase() + repairableName.toLowerCase()); + prepareSaveTask(); + + // Remove from gui + UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance(); + if(repairConfigGui != null) { + UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit); + + elementGui.removeGeneric(repairableName); + if(lastValue){ + repairConfigGui.removeGeneric(unit); + } + } + + return true; + } + + /** + * Prepare a task to save custom unit repair recipe configuration. + */ + private static void prepareSaveTask() { + if(saveChangeTask != -1) return; + + saveChangeTask = Bukkit.getScheduler().scheduleSyncDelayedTask(CustomAnvil.instance, ()->{ + ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(true); + saveChangeTask = -1; + }, 0L); + } + + /** + * Get every unit repair recipes. + * @return An immutable collection of unit repair recipes. + *
+ * Each element of the provided triple represent a part of the recipe + *