mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
Add unit repair api (#16)
Also optimize (not by a lot) unit repair default case.
This commit is contained in:
commit
e48eb95aa8
8 changed files with 245 additions and 11 deletions
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
215
src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java
Normal file
215
src/main/java/xyz/alexcrea/cuanvil/api/UnitRepairApi.java
Normal file
|
|
@ -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.
|
||||
* <p>
|
||||
* Each element of the provided triple represent a part of the recipe
|
||||
* <ul>
|
||||
* <li>First object is the unit material used to repair the bellow item.
|
||||
* <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>
|
||||
*/
|
||||
@NotNull
|
||||
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;
|
||||
|
||||
// Test if unit is a material
|
||||
Material unit = Material.getMaterial(unitKey.toUpperCase());
|
||||
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;
|
||||
|
||||
// Test if repairable is valid a material
|
||||
Material repairable = Material.getMaterial(repairableKey.toUpperCase());
|
||||
if(repairable == null) continue;
|
||||
|
||||
// Add the values
|
||||
mutableList.add(new Triple<>(unit, repairable, section.getDouble(repairableKey)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(mutableList);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -122,6 +122,18 @@ public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRe
|
|||
}, CustomAnvil.instance);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public UnitRepairElementListGui getInstanceOrCreate(Material mat){
|
||||
UnitRepairElementListGui element = this.elementGuiMap.get(mat);
|
||||
if(element == null){
|
||||
updateValueForGeneric(mat, false);
|
||||
|
||||
element = this.elementGuiMap.get(mat);
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override // Not used in this implementation.
|
||||
protected String genericDisplayedName() {
|
||||
return "this function Should not be used.";
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
|||
String materialName = type.name().toLowerCase();
|
||||
|
||||
// Add new material
|
||||
ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(parentMaterial.name().toLowerCase()+"."+materialName,0.25);
|
||||
ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(parentMaterial.name().toLowerCase() + "." + materialName,0.25);
|
||||
|
||||
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
|
||||
ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import xyz.alexcrea.cuanvil.update.Update_1_21
|
|||
import xyz.alexcrea.cuanvil.util.Metrics
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.util.logging.Level
|
||||
|
||||
/**
|
||||
* Bukkit/Spigot/Paper plugin to alter anvil feature
|
||||
|
|
@ -113,7 +114,10 @@ class CustomAnvil : JavaPlugin() {
|
|||
|
||||
private fun loadEnchantmentSystem(){
|
||||
// Load default configuration
|
||||
if (!ConfigHolder.loadDefaultConfig()) return
|
||||
if (!ConfigHolder.loadDefaultConfig()) {
|
||||
logger.log(Level.SEVERE,"could not load default config.")
|
||||
return
|
||||
}
|
||||
|
||||
// Register enchantments
|
||||
CAEnchantmentRegistry.getInstance().registerBukkit()
|
||||
|
|
@ -123,7 +127,10 @@ class CustomAnvil : JavaPlugin() {
|
|||
server.pluginManager.callEvent(enchantReadyEvent)
|
||||
|
||||
// Load config
|
||||
if (!ConfigHolder.loadNonDefaultConfig()) return
|
||||
if (!ConfigHolder.loadNonDefaultConfig()) {
|
||||
logger.log(Level.SEVERE,"could not load non default config.")
|
||||
return
|
||||
}
|
||||
|
||||
// temporary: handle 1.21 update
|
||||
Update_1_21.handleUpdate()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue