mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-08-28 08:25:56 +02:00
reworked unit repair
This commit is contained in:
parent
71568f8a0b
commit
96ecc93f76
2 changed files with 172 additions and 74 deletions
|
|
@ -3,18 +3,16 @@ package xyz.alexcrea.cuanvil.api;
|
|||
import io.delilaheve.CustomAnvil;
|
||||
import kotlin.Triple;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
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.dependency.DependencyManager;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Custom Anvil api for unit repair.
|
||||
|
|
@ -22,7 +20,8 @@ import java.util.List;
|
|||
@SuppressWarnings("unused")
|
||||
public class UnitRepairApi {
|
||||
|
||||
private UnitRepairApi(){}
|
||||
private UnitRepairApi() {
|
||||
}
|
||||
|
||||
private static Object saveChangeTask = null;
|
||||
|
||||
|
|
@ -35,7 +34,20 @@ public class UnitRepairApi {
|
|||
* @param repairable The item to be repaired.
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +60,7 @@ public class UnitRepairApi {
|
|||
* @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){
|
||||
public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value) {
|
||||
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.
|
||||
* @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();
|
||||
public static boolean addUnitRepair(@NotNull Material unit, @NotNull Material repairable, double value, boolean overrideDeleted) {
|
||||
return addUnitRepair(unit.getKey(), repairable.getKey(), value, overrideDeleted);
|
||||
}
|
||||
|
||||
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
|
||||
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)
|
||||
* @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();
|
||||
|
||||
String repairableName = repairable.name().toLowerCase();
|
||||
String path = unit.name().toLowerCase() + "." + repairableName;
|
||||
String repairableName = repairable.toString().toLowerCase();
|
||||
String path = unit.toString().toLowerCase() + "." + repairableName;
|
||||
|
||||
// Add to config then prepare save
|
||||
config.set(path, value);
|
||||
|
|
@ -94,10 +133,10 @@ public class UnitRepairApi {
|
|||
|
||||
// Add to gui
|
||||
UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance();
|
||||
if(repairConfigGui != null) {
|
||||
if (repairConfigGui != null) {
|
||||
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
|
||||
|
||||
if(elementGui != null) elementGui.updateValueForGeneric(repairableName, true);
|
||||
if (elementGui != null) elementGui.updateValueForGeneric(repairable, true);
|
||||
repairConfigGui.updateValueForGeneric(unit, true);
|
||||
}
|
||||
|
||||
|
|
@ -111,48 +150,56 @@ public class UnitRepairApi {
|
|||
* @param repairable The item used to be repaired.
|
||||
* @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
|
||||
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);
|
||||
config.set(unitName.toLowerCase() + "." + repairableName.toLowerCase(), null);
|
||||
config.set(unit.getKey() + "." + repairable.getKey(), null);
|
||||
config.set(unit.getKey() + "." + repairable, null);
|
||||
config.set(unit + "." + repairable.getKey(), null);
|
||||
config.set(unit + "." + repairable, 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 (config.isConfigurationSection(unit.toString())) {
|
||||
ConfigurationSection section = config.getConfigurationSection(unit.toString());
|
||||
|
||||
if(section != null && section.getKeys(false).isEmpty()) {
|
||||
if (section != null && section.getKeys(false).isEmpty()) {
|
||||
lastValue = true;
|
||||
config.set(unitName.toLowerCase(), null);
|
||||
config.set(unit.toString(), null);
|
||||
}
|
||||
|
||||
} else if (config.isConfigurationSection(unitName.toUpperCase())) {
|
||||
ConfigurationSection section = config.getConfigurationSection(unitName.toUpperCase());
|
||||
if(section != null && section.getKeys(false).isEmpty()) {
|
||||
} else if (config.isConfigurationSection(unit.getKey())) {
|
||||
ConfigurationSection section = config.getConfigurationSection(unit.getKey());
|
||||
if (section != null && section.getKeys(false).isEmpty()) {
|
||||
lastValue = true;
|
||||
config.set(unitName.toUpperCase(), null);
|
||||
config.set(unit.getKey(), 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());
|
||||
ConfigHolder.UNIT_REPAIR_HOLDER.delete(unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase());
|
||||
prepareSaveTask();
|
||||
|
||||
// Remove from gui
|
||||
UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance();
|
||||
if(repairConfigGui != null) {
|
||||
if (repairConfigGui != null) {
|
||||
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
|
||||
|
||||
if(elementGui != null) elementGui.removeGeneric(repairableName);
|
||||
if(lastValue){
|
||||
if (elementGui != null) elementGui.removeGeneric(repairable);
|
||||
if (lastValue) {
|
||||
repairConfigGui.removeGeneric(unit);
|
||||
}
|
||||
}
|
||||
|
|
@ -164,9 +211,9 @@ public class UnitRepairApi {
|
|||
* Prepare a task to save custom unit repair recipe configuration.
|
||||
*/
|
||||
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);
|
||||
saveChangeTask = null;
|
||||
});
|
||||
|
|
@ -174,6 +221,7 @@ public class UnitRepairApi {
|
|||
|
||||
/**
|
||||
* Get every unit repair recipes.
|
||||
*
|
||||
* @return An immutable collection of unit repair recipes.
|
||||
* <p>
|
||||
* 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>Last object is the amount to be repaired by every unit. (1% = 0.01)
|
||||
* </ul>
|
||||
* @deprecated some may be missing. use {@link #getModernUnitRepairs()}
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
@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<>();
|
||||
|
||||
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
|
||||
for (String unitKey : config.getKeys(false)) {
|
||||
// Test if config section exist
|
||||
if(!config.isConfigurationSection(unitKey)) continue;
|
||||
if (!config.isConfigurationSection(unitKey)) continue;
|
||||
|
||||
// Test if unit is a material
|
||||
Material unit = Material.getMaterial(unitKey.toUpperCase());
|
||||
if(unit == null) continue;
|
||||
if (unit == null) continue;
|
||||
|
||||
// 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;
|
||||
if (!section.isDouble(repairableKey)) continue;
|
||||
|
||||
// Test if repairable is valid a material
|
||||
Material repairable = Material.getMaterial(repairableKey.toUpperCase());
|
||||
if(repairable == null) continue;
|
||||
if (repairable == null) continue;
|
||||
|
||||
// Add the values
|
||||
mutableList.add(new Triple<>(unit, repairable, section.getDouble(repairableKey)));
|
||||
|
|
@ -215,4 +266,53 @@ public class UnitRepairApi {
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package xyz.alexcrea.cuanvil.util
|
||||
|
||||
import org.bukkit.NamespacedKey
|
||||
import org.bukkit.configuration.ConfigurationSection
|
||||
import org.bukkit.configuration.file.FileConfiguration
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder
|
||||
import xyz.alexcrea.cuanvil.util.MaterialUtil.customType
|
||||
|
|
@ -22,40 +24,36 @@ object UnitRepairUtil {
|
|||
): Double? {
|
||||
if (other == null) return null
|
||||
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
|
||||
|
||||
}
|
||||
// Get repair amount
|
||||
var userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
|
||||
if (userDefault <= 0) {
|
||||
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
|
||||
val material = other.customType
|
||||
val selfType = this.customType
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return getRepairAmount(this, section, userDefault)
|
||||
}
|
||||
fun checkSection(
|
||||
config: FileConfiguration,
|
||||
path: String,
|
||||
material: NamespacedKey
|
||||
): Double? {
|
||||
val section = config.getConfigurationSection(path) ?: return null
|
||||
|
||||
if (section.isDouble(material.toString()))
|
||||
return section.getDouble(material.toString())
|
||||
if (section.isDouble(material.key))
|
||||
return section.getDouble(material.key)
|
||||
|
||||
/**
|
||||
* Get the item % repaired by this configuration section of a unit repair.
|
||||
* null if not found.
|
||||
* 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.customType.key.lowercase()
|
||||
val repairValue = if (section.isDouble(itemName)) {
|
||||
section.getDouble(itemName)
|
||||
} else if (section.isDouble(itemName.uppercase())) {
|
||||
section.getDouble(itemName.uppercase())
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
if (repairValue <= 0)
|
||||
return default
|
||||
return repairValue
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue