reworked unit repair

This commit is contained in:
alexcrea 2026-07-01 02:06:14 +02:00
parent d174988dc5
commit af9050eb15
Signed by: alexcrea
GPG key ID: E346CD16413450E3
2 changed files with 172 additions and 74 deletions

View file

@ -3,18 +3,16 @@ package xyz.alexcrea.cuanvil.api;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import kotlin.Triple; import kotlin.Triple;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.dependency.DependencyManager; import xyz.alexcrea.cuanvil.dependency.DependencyManager;
import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui;
import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui;
import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui; import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
/** /**
* Custom Anvil api for unit repair. * Custom Anvil api for unit repair.
@ -22,7 +20,8 @@ import java.util.List;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class UnitRepairApi { public class UnitRepairApi {
private UnitRepairApi(){} private UnitRepairApi() {
}
private static Object saveChangeTask = null; private static Object saveChangeTask = null;
@ -35,7 +34,20 @@ public class UnitRepairApi {
* @param repairable The item to be repaired. * @param repairable The item to be repaired.
* @return true if successful. * @return true if successful.
*/ */
public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable){ 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.
* 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 NamespacedKey unit, @NotNull NamespacedKey repairable) {
return addUnitRepair(unit, repairable, 0.25, false); return addUnitRepair(unit, repairable, 0.25, false);
} }
@ -48,7 +60,7 @@ public class UnitRepairApi {
* @param value The amount to be repaired by every unit. (1% = 0.01) * @param value The amount to be repaired by every unit. (1% = 0.01)
* @return true if successful. * @return true if successful.
*/ */
public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value){ public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) {
return addUnitRepair(unit, repairable, value, false); return addUnitRepair(unit, repairable, value, false);
} }
@ -62,12 +74,26 @@ public class UnitRepairApi {
* @param overrideDeleted If we should write even if the recipe was previously deleted. * @param overrideDeleted If we should write even if the recipe was previously deleted.
* @return true if successful. * @return true if successful.
*/ */
public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted){ public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted) {
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); return addUnitRepair(unit.getKey(), repairable.getKey(), value, overrideDeleted);
String path = unit.name().toLowerCase() + "." + repairable.name().toLowerCase(); }
if(!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false; /**
if(config.contains(path)) return 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 NamespacedKey unit, @NotNull NamespacedKey repairable, double value, boolean overrideDeleted) {
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String path = unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase();
if (!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false;
if (config.contains(path)) return false;
// Set unit repair // Set unit repair
return setUnitRepair(unit, repairable, value); return setUnitRepair(unit, repairable, value);
@ -82,11 +108,24 @@ public class UnitRepairApi {
* @param value The amount to be repaired by every unit. (1% = 0.01) * @param value The amount to be repaired by every unit. (1% = 0.01)
* @return true if successful. * @return true if successful.
*/ */
public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value){ public static boolean setUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) {
return setUnitRepair(unit.getKey(), repairable.getKey(), 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 NamespacedKey unit, @NotNull NamespacedKey repairable, double value) {
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String repairableName = repairable.name().toLowerCase(); String repairableName = repairable.toString().toLowerCase();
String path = unit.name().toLowerCase() + "." + repairableName; String path = unit.toString().toLowerCase() + "." + repairableName;
// Add to config then prepare save // Add to config then prepare save
config.set(path, value); config.set(path, value);
@ -94,10 +133,10 @@ public class UnitRepairApi {
// Add to gui // Add to gui
UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance(); UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance();
if(repairConfigGui != null) { if (repairConfigGui != null) {
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored(); UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
if(elementGui != null) elementGui.updateValueForGeneric(repairableName, true); if (elementGui != null) elementGui.updateValueForGeneric(repairable, true);
repairConfigGui.updateValueForGeneric(unit, true); repairConfigGui.updateValueForGeneric(unit, true);
} }
@ -111,48 +150,56 @@ public class UnitRepairApi {
* @param repairable The item used to be repaired. * @param repairable The item used to be repaired.
* @return true if successful. * @return true if successful.
*/ */
public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable){ public static boolean removeUnitRepair(@NotNull Material unit, @NotNull Material repairable) {
return removeUnitRepair(unit.getKey(), repairable.getKey());
}
/**
* 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 NamespacedKey unit, @NotNull NamespacedKey repairable) {
// Delete every possible variation and save to file // Delete every possible variation and save to file
String unitName = unit.name();
String repairableName = repairable.name();
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
config.set(unitName.toLowerCase() + "." + repairableName.toUpperCase(), null); config.set(unit.getKey() + "." + repairable.getKey(), null);
config.set(unitName.toUpperCase() + "." + repairableName.toLowerCase(), null); config.set(unit.getKey() + "." + repairable, null);
config.set(unitName.toUpperCase() + "." + repairableName.toUpperCase(), null); config.set(unit + "." + repairable.getKey(), null);
config.set(unitName.toLowerCase() + "." + repairableName.toLowerCase(), null); config.set(unit + "." + repairable, null);
// Test if it was the last value of this section // Test if it was the last value of this section
boolean lastValue = false; boolean lastValue = false;
if(config.isConfigurationSection(unitName.toLowerCase())) { if (config.isConfigurationSection(unit.toString())) {
ConfigurationSection section = config.getConfigurationSection(unitName.toLowerCase()); ConfigurationSection section = config.getConfigurationSection(unit.toString());
if(section != null && section.getKeys(false).isEmpty()) { if (section != null && section.getKeys(false).isEmpty()) {
lastValue = true; lastValue = true;
config.set(unitName.toLowerCase(), null); config.set(unit.toString(), null);
} }
} else if (config.isConfigurationSection(unitName.toUpperCase())) { } else if (config.isConfigurationSection(unit.getKey())) {
ConfigurationSection section = config.getConfigurationSection(unitName.toUpperCase()); ConfigurationSection section = config.getConfigurationSection(unit.getKey());
if(section != null && section.getKeys(false).isEmpty()) { if (section != null && section.getKeys(false).isEmpty()) {
lastValue = true; lastValue = true;
config.set(unitName.toUpperCase(), null); config.set(unit.getKey(), null);
} }
} else lastValue = true; } else lastValue = true;
// We only need to "delete" as the lower case to be counted as deleted ConfigHolder.UNIT_REPAIR_HOLDER.delete(unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase());
ConfigHolder.UNIT_REPAIR_HOLDER.delete(unitName.toLowerCase() + "." + repairableName.toLowerCase());
prepareSaveTask(); prepareSaveTask();
// Remove from gui // Remove from gui
UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance(); UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance();
if(repairConfigGui != null) { if (repairConfigGui != null) {
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored(); UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
if(elementGui != null) elementGui.removeGeneric(repairableName); if (elementGui != null) elementGui.removeGeneric(repairable);
if(lastValue){ if (lastValue) {
repairConfigGui.removeGeneric(unit); repairConfigGui.removeGeneric(unit);
} }
} }
@ -164,9 +211,9 @@ public class UnitRepairApi {
* Prepare a task to save custom unit repair recipe configuration. * Prepare a task to save custom unit repair recipe configuration.
*/ */
private static void prepareSaveTask() { 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.UNIT_REPAIR_HOLDER.saveToDisk(true); ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(true);
saveChangeTask = null; saveChangeTask = null;
}); });
@ -174,6 +221,7 @@ public class UnitRepairApi {
/** /**
* Get every unit repair recipes. * Get every unit repair recipes.
*
* @return An immutable collection of unit repair recipes. * @return An immutable collection of unit repair recipes.
* <p> * <p>
* Each element of the provided triple represent a part of the recipe * Each element of the provided triple represent a part of the recipe
@ -182,29 +230,32 @@ public class UnitRepairApi {
* <li>Second object is the item to be repaired. * <li>Second object is the item to be repaired.
* <li>Last object is the amount to be repaired by every unit. (1% = 0.01) * <li>Last object is the amount to be repaired by every unit. (1% = 0.01)
* </ul> * </ul>
* @deprecated some may be missing. use {@link #getModernUnitRepairs()}
*
*/ */
@Deprecated
@NotNull @NotNull
public static List<Triple<Material, Material, Double>> getUnitRepairs(){ public static List<Triple<Material, Material, Double>> getUnitRepairs() {
List<Triple<Material, Material, Double>> mutableList = new ArrayList<>(); List<Triple<Material, Material, Double>> mutableList = new ArrayList<>();
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig(); FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
for (String unitKey : config.getKeys(false)) { for (String unitKey : config.getKeys(false)) {
// Test if config section exist // Test if config section exist
if(!config.isConfigurationSection(unitKey)) continue; if (!config.isConfigurationSection(unitKey)) continue;
// Test if unit is a material // Test if unit is a material
Material unit = Material.getMaterial(unitKey.toUpperCase()); Material unit = Material.getMaterial(unitKey.toUpperCase());
if(unit == null) continue; if (unit == null) continue;
// Iterate over reparable items // Iterate over reparable items
ConfigurationSection section = config.getConfigurationSection(unitKey); ConfigurationSection section = config.getConfigurationSection(unitKey);
for (String repairableKey : section.getKeys(false)) { for (String repairableKey : section.getKeys(false)) {
// Test if value section exist // Test if value section exist
if(!section.isDouble(repairableKey)) continue; if (!section.isDouble(repairableKey)) continue;
// Test if repairable is valid a material // Test if repairable is valid a material
Material repairable = Material.getMaterial(repairableKey.toUpperCase()); Material repairable = Material.getMaterial(repairableKey.toUpperCase());
if(repairable == null) continue; if (repairable == null) continue;
// Add the values // Add the values
mutableList.add(new Triple<>(unit, repairable, section.getDouble(repairableKey))); mutableList.add(new Triple<>(unit, repairable, section.getDouble(repairableKey)));
@ -215,4 +266,53 @@ public class UnitRepairApi {
return Collections.unmodifiableList(mutableList); return Collections.unmodifiableList(mutableList);
} }
/**
* Get every unit repair recipes.
*
* @return An immutable collection of unit repair recipes.
* <p>
* <li>First map contain a key the unit material used to repair the bellow item and value the second map
* <li>Second map contain as key the item to be repaired and as value the amount to be repaired by every unit. (1% = 0.01)
* </ul>
*/
@NotNull
public static Map<NamespacedKey, Map<NamespacedKey, Double>> getModernUnitRepairs() {
Map<NamespacedKey, Map<NamespacedKey, Double>> mutableList = new HashMap<>();
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
for (String unitKey : config.getKeys(false)) {
// Test if config section exist
if (!config.isConfigurationSection(unitKey)) continue;
// Test if unit is a material
NamespacedKey unit = NamespacedKey.fromString(unitKey.toLowerCase());
if (unit == null) continue;
Map<NamespacedKey, Double> map;
if (!mutableList.containsKey(unit)) {
map = new HashMap<>();
mutableList.put(unit, map);
} else {
map = mutableList.get(unit);
}
// Iterate over reparable items
ConfigurationSection section = config.getConfigurationSection(unitKey);
for (String repairableKey : section.getKeys(false)) {
// Test if value section exist
if (!section.isDouble(repairableKey)) continue;
// Test if repairable is valid a material
NamespacedKey repairable = NamespacedKey.fromString(repairableKey.toLowerCase());
if (repairable == null) continue;
// Add the values
map.put(repairable, section.getDouble(repairableKey));
}
}
return mutableList;
}
} }

View file

@ -1,6 +1,8 @@
package xyz.alexcrea.cuanvil.util package xyz.alexcrea.cuanvil.util
import org.bukkit.NamespacedKey
import org.bukkit.configuration.ConfigurationSection import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.FileConfiguration
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.config.ConfigHolder
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
@ -22,40 +24,36 @@ object UnitRepairUtil {
): Double? { ): Double? {
if (other == null) return null if (other == null) return null
val config = ConfigHolder.UNIT_REPAIR_HOLDER.config val config = ConfigHolder.UNIT_REPAIR_HOLDER.config
// Get configuration section if exist
val otherName = other.customType.key.lowercase()
var section = config.getConfigurationSection(otherName)
if (section == null) {
section = config.getConfigurationSection(otherName.uppercase())
if (section == null) return null
} val material = other.customType
// Get repair amount val selfType = this.customType
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
if (userDefault <= 0) {
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
}
return getRepairAmount(this, section, userDefault) var result = checkSection(config, material.toString(), selfType)
if (result != null) return result
result = checkSection(config, material.key, selfType)
if (result != null) return result
// Get default
val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
if (userDefault <= 0)
return DEFAULT_DEFAULT_UNIT_REPAIR
return userDefault
} }
/** fun checkSection(
* Get the item % repaired by this configuration section of a unit repair. config: FileConfiguration,
* null if not found. path: String,
* If value is set to less than or equal to 0 then it will be set to default material: NamespacedKey
*/ ): Double? {
private fun getRepairAmount(item: ItemStack, section: ConfigurationSection, default: Double): Double? { val section = config.getConfigurationSection(path) ?: return null
val itemName = item.customType.key.lowercase()
val repairValue = if (section.isDouble(itemName)) { if (section.isDouble(material.toString()))
section.getDouble(itemName) return section.getDouble(material.toString())
} else if (section.isDouble(itemName.uppercase())) { if (section.isDouble(material.key))
section.getDouble(itemName.uppercase()) return section.getDouble(material.key)
} else {
return null return null
}
if (repairValue <= 0)
return default
return repairValue
} }
} }