mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
Add unit repair api.
This commit is contained in:
parent
e43f6276cd
commit
e89fff951e
7 changed files with 240 additions and 6 deletions
|
|
@ -135,7 +135,7 @@ public class ConflictAPI {
|
||||||
// Remove from registry
|
// Remove from registry
|
||||||
ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict);
|
ConfigHolder.CONFLICT_HOLDER.getConflictManager().removeConflict(conflict);
|
||||||
|
|
||||||
// Write as null and save to file
|
// Delete and save to file
|
||||||
ConfigHolder.CONFLICT_HOLDER.delete(conflict.getName());
|
ConfigHolder.CONFLICT_HOLDER.delete(conflict.getName());
|
||||||
prepareSaveTask();
|
prepareSaveTask();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ public class CustomAnvilRecipeApi {
|
||||||
// Remove from registry
|
// Remove from registry
|
||||||
ConfigHolder.CUSTOM_RECIPE_HOLDER.getRecipeManager().cleanRemove(recipe);
|
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());
|
ConfigHolder.CUSTOM_RECIPE_HOLDER.delete(recipe.getName());
|
||||||
prepareSaveTask();
|
prepareSaveTask();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,7 @@ public class MaterialGroupApi {
|
||||||
// Remove from registry
|
// Remove from registry
|
||||||
ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().groupMap.remove(group.getName());
|
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());
|
ConfigHolder.ITEM_GROUP_HOLDER.delete(group.getName());
|
||||||
prepareSaveTask();
|
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);
|
}, 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.
|
@Override // Not used in this implementation.
|
||||||
protected String genericDisplayedName() {
|
protected String genericDisplayedName() {
|
||||||
return "this function Should not be used.";
|
return "this function Should not be used.";
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
String materialName = type.name().toLowerCase();
|
String materialName = type.name().toLowerCase();
|
||||||
|
|
||||||
// Add new material
|
// 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) {
|
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
|
||||||
ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
|
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 xyz.alexcrea.cuanvil.util.Metrics
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileReader
|
import java.io.FileReader
|
||||||
|
import java.util.logging.Level
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bukkit/Spigot/Paper plugin to alter anvil feature
|
* Bukkit/Spigot/Paper plugin to alter anvil feature
|
||||||
|
|
@ -113,7 +114,10 @@ class CustomAnvil : JavaPlugin() {
|
||||||
|
|
||||||
private fun loadEnchantmentSystem(){
|
private fun loadEnchantmentSystem(){
|
||||||
// Load default configuration
|
// Load default configuration
|
||||||
if (!ConfigHolder.loadDefaultConfig()) return
|
if (!ConfigHolder.loadDefaultConfig()) {
|
||||||
|
logger.log(Level.SEVERE,"could not load default config.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Register enchantments
|
// Register enchantments
|
||||||
CAEnchantmentRegistry.getInstance().registerBukkit()
|
CAEnchantmentRegistry.getInstance().registerBukkit()
|
||||||
|
|
@ -123,7 +127,10 @@ class CustomAnvil : JavaPlugin() {
|
||||||
server.pluginManager.callEvent(enchantReadyEvent)
|
server.pluginManager.callEvent(enchantReadyEvent)
|
||||||
|
|
||||||
// Load config
|
// 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
|
// temporary: handle 1.21 update
|
||||||
Update_1_21.handleUpdate()
|
Update_1_21.handleUpdate()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue