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 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;
|
||||||
|
|
||||||
|
|
@ -39,6 +38,19 @@ public class UnitRepairApi {
|
||||||
return addUnitRepair(unit, repairable, 0.25, false);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write and add a custom anvil unit repair recipe.
|
* Write and add a custom anvil unit repair recipe.
|
||||||
* Will not write the recipe if it already exists or was deleted.
|
* Will not write the recipe if it already exists or was deleted.
|
||||||
|
|
@ -63,8 +75,22 @@ public class UnitRepairApi {
|
||||||
* @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) {
|
||||||
|
return addUnitRepair(unit.getKey(), repairable.getKey(), value, overrideDeleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
FileConfiguration config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
|
||||||
String path = unit.name().toLowerCase() + "." + repairable.name().toLowerCase();
|
String path = unit.toString().toLowerCase() + "." + repairable.toString().toLowerCase();
|
||||||
|
|
||||||
if (!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false;
|
if (!overrideDeleted && ConfigHolder.UNIT_REPAIR_HOLDER.isDeleted(path)) return false;
|
||||||
if (config.contains(path)) return false;
|
if (config.contains(path)) return false;
|
||||||
|
|
@ -83,10 +109,23 @@ public class UnitRepairApi {
|
||||||
* @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);
|
||||||
|
|
@ -97,7 +136,7 @@ public class UnitRepairApi {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,38 +151,46 @@ public class UnitRepairApi {
|
||||||
* @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
|
||||||
|
|
@ -151,7 +198,7 @@ public class UnitRepairApi {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
@ -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,7 +230,10 @@ 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<>();
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
var result = checkSection(config, material.toString(), selfType)
|
||||||
userDefault = DEFAULT_DEFAULT_UNIT_REPAIR
|
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
|
return null
|
||||||
}
|
}
|
||||||
if (repairValue <= 0)
|
|
||||||
return default
|
|
||||||
return repairValue
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue