unit repair gui and internal update

This commit is contained in:
alexcrea 2026-07-01 02:39:14 +02:00
parent af9050eb15
commit dc84ab7303
Signed by: alexcrea
GPG key ID: E346CD16413450E3
5 changed files with 129 additions and 71 deletions

View file

@ -2,7 +2,6 @@ package xyz.alexcrea.cuanvil.gui.config.global;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui; import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
@ -119,4 +118,16 @@ public class ItemConfigGui extends ChestGui {
return groupConfigGui; return groupConfigGui;
} }
/*private UnitRepairConfigGui getUnitRepairConfigGui(NamespacedKey material) {
if (unitRepairConfigGui == null) {
unitRepairConfigGui = new UnitRepairConfigGui(this);
unitRepairConfigGui.setFilter(otherMat ->
group.contain(material) //TODO check material & what inside
);
unitRepairConfigGui.init();
}
return unitRepairConfigGui;
}*/
} }

View file

@ -1,9 +1,10 @@
package xyz.alexcrea.cuanvil.gui.config.global; package xyz.alexcrea.cuanvil.gui.config.global;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,13 +14,14 @@ import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui;
import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui; 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 xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import xyz.alexcrea.cuanvil.util.MaterialUtil;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
public class UnitRepairConfigGui extends public class UnitRepairConfigGui extends
MappedGuiListConfigGui<Material, MappedGuiListConfigGui.LazyElement<UnitRepairElementListGui>> { MappedGuiListConfigGui<NamespacedKey, MappedGuiListConfigGui.LazyElement<UnitRepairElementListGui>> {
private static UnitRepairConfigGui INSTANCE; private static UnitRepairConfigGui INSTANCE;
@ -41,8 +43,12 @@ public class UnitRepairConfigGui extends
init(); init();
} }
public UnitRepairConfigGui(Gui parent) {
super("Unit Repair Config", parent);
}
@Override @Override
protected LazyElement<UnitRepairElementListGui> newInstanceOfGui(Material material, GuiItem item) { protected LazyElement<UnitRepairElementListGui> newInstanceOfGui(NamespacedKey material, GuiItem item) {
return new LazyElement<>(item, () -> { return new LazyElement<>(item, () -> {
UnitRepairElementListGui element = new UnitRepairElementListGui(material, this); UnitRepairElementListGui element = new UnitRepairElementListGui(material, this);
element.init(); element.init();
@ -51,17 +57,28 @@ public class UnitRepairConfigGui extends
} }
@Override @Override
protected ItemStack createItemForGeneric(Material material) { protected ItemStack createItemForGeneric(@NotNull NamespacedKey material) {
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(material.name().toLowerCase()); var unitConfig = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()); var section = unitConfig.getConfigurationSection(material.toString().toLowerCase());
var legacySection = unitConfig.getConfigurationSection(material.toString().toLowerCase());
if(material.isAir()){ String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.getKey().toLowerCase());
material = Material.BARRIER;
var display = MaterialUtil.INSTANCE.getMatFromKey(material);
if (display == null || display.isAir()) {
display = Material.BARRIER;
} }
int reparableItemCount = materialSection == null ? 0 : materialSection.getKeys(false).size(); // Probably an expensive call but... why not var reparable = new HashSet<String>();
if (section != null)
reparable.addAll(section.getKeys(false));
if (legacySection != null)
reparable.addAll(legacySection.getKeys(false));
ItemStack item = new ItemStack(material); var reparableItemCount = reparable.size();
ItemStack item = new ItemStack(display);
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
assert meta != null; assert meta != null;
@ -77,13 +94,16 @@ public class UnitRepairConfigGui extends
} }
@Override @Override
protected Collection<Material> getEveryInstanceOfGeneric() { protected Collection<NamespacedKey> getEveryInstanceOfGeneric() {
ArrayList<Material> materials = new ArrayList<>(); var materials = new HashSet<NamespacedKey>();
var config = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig();
for (String matName : ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getKeys(false)) { for (String matName : config.getKeys(false)) {
Material mat = Material.getMaterial(matName.toUpperCase()); if(!config.isConfigurationSection(matName)) continue;
if(mat != null){
materials.add(mat); NamespacedKey material = NamespacedKey.fromString(matName.toLowerCase());
if (material != null) {
materials.add(material);
} }
} }
return materials; return materials;
@ -113,7 +133,7 @@ public class UnitRepairConfigGui extends
"§7You like to be an unit repair item", "§7You like to be an unit repair item",
this, this,
(itemStack, player) -> { (itemStack, player) -> {
Material type = itemStack.getType(); NamespacedKey type = MaterialUtil.INSTANCE.getCustomType(itemStack);
// Add new material // Add new material
updateValueForGeneric(type, true); updateValueForGeneric(type, true);
@ -126,7 +146,7 @@ public class UnitRepairConfigGui extends
} }
@NotNull @NotNull
public LazyElement<UnitRepairElementListGui> getInstanceOrCreate(Material mat){ public LazyElement<UnitRepairElementListGui> getInstanceOrCreate(NamespacedKey mat) {
LazyElement<UnitRepairElementListGui> element = this.elementGuiMap.get(mat); LazyElement<UnitRepairElementListGui> element = this.elementGuiMap.get(mat);
if (element == null) { if (element == null) {
updateValueForGeneric(mat, false); updateValueForGeneric(mat, false);
@ -141,8 +161,10 @@ public class UnitRepairConfigGui extends
protected String genericDisplayedName() { protected String genericDisplayedName() {
return "this function Should not be used."; return "this function Should not be used.";
} }
@Override // Not used in this implementation. @Override // Not used in this implementation.
protected Material createAndSaveNewEmptyGeneric(String name) { protected NamespacedKey createAndSaveNewEmptyGeneric(String name) {
return null; return null;
} }
} }

View file

@ -3,12 +3,14 @@ package xyz.alexcrea.cuanvil.gui.config.list;
import com.github.stefvanschie.inventoryframework.gui.GuiItem; import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
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.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.alexcrea.cuanvil.config.ConfigHolder; import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui; import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui;
import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui; import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui;
@ -17,26 +19,25 @@ import xyz.alexcrea.cuanvil.gui.config.settings.DoubleSettingGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import xyz.alexcrea.cuanvil.util.MaterialUtil;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, DoubleSettingGui.DoubleSettingFactory> implements ElementMappedToListGui { public class UnitRepairElementListGui extends SettingGuiListConfigGui<NamespacedKey, DoubleSettingGui.DoubleSettingFactory> implements ElementMappedToListGui {
private final Material parentMaterial; private final NamespacedKey parentMaterial;
private final UnitRepairConfigGui parentGui; private final UnitRepairConfigGui parentGui;
private final String materialName; private final String materialName;
private boolean shouldWork = true; private boolean shouldWork = true;
public UnitRepairElementListGui(@NotNull Material parentMaterial,
public UnitRepairElementListGui(@NotNull NamespacedKey parentMaterial,
@NotNull UnitRepairConfigGui parentGui) { @NotNull UnitRepairConfigGui parentGui) {
super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()) + " §rUnit repair"); super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.getKey().toLowerCase()) + " §rUnit repair");
this.parentMaterial = parentMaterial; this.parentMaterial = parentMaterial;
this.parentGui = parentGui; this.parentGui = parentGui;
this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()); this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.getKey().toLowerCase());
GuiGlobalItems.addBackItem(this.backgroundPane, parentGui); GuiGlobalItems.addBackItem(this.backgroundPane, parentGui);
} }
@ -66,28 +67,28 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
this, this,
(itemStack, player) -> { (itemStack, player) -> {
ItemMeta meta = itemStack.getItemMeta(); ItemMeta meta = itemStack.getItemMeta();
Material type = itemStack.getType(); NamespacedKey type = MaterialUtil.INSTANCE.getCustomType(itemStack);
if(!(meta instanceof Damageable) || (type.getMaxDurability() <= 0)) { if (!(meta instanceof Damageable)) {
player.sendMessage("§cThis item can't be damaged, so it can't be repaired."); player.sendMessage("§cThis item can't be damaged, so it can't be repaired.");
return; return;
} }
if(type == this.parentMaterial){ if (type.equals(this.parentMaterial)) {
player.sendMessage("§cItem can't repair something of the same type."); player.sendMessage("§cItem can't repair something of the same type.");
return; return;
} }
String materialName = type.name().toLowerCase(); String materialName = type.toString();
// 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.toString().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);
} }
// Update gui // Update gui
updateValueForGeneric(materialName, true); updateValueForGeneric(type, true);
this.parentGui.updateValueForGeneric(this.parentMaterial, true); this.parentGui.updateValueForGeneric(this.parentMaterial, true);
@ -106,14 +107,14 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
} }
@Override @Override
protected DoubleSettingGui.DoubleSettingFactory createFactory(String materialName) { protected DoubleSettingGui.DoubleSettingFactory createFactory(NamespacedKey materialName) {
String materialDisplayName = CasedStringUtil.snakeToUpperSpacedCase(materialName); String materialDisplayName = CasedStringUtil.snakeToUpperSpacedCase(materialName.getKey());
return new DoubleSettingGui.DoubleSettingFactory( return new DoubleSettingGui.DoubleSettingFactory(
"§0%§8" + materialDisplayName + " Repair", "§0%§8" + materialDisplayName + " Repair",
this, this,
ConfigHolder.UNIT_REPAIR_HOLDER, ConfigHolder.UNIT_REPAIR_HOLDER,
this.parentMaterial.name().toLowerCase()+"."+materialName, this.parentMaterial.toString().toLowerCase() + "." + materialName,
Arrays.asList( Arrays.asList(
"§7Click here to change how many §e% §7of §a" + materialDisplayName, "§7Click here to change how many §e% §7of §a" + materialDisplayName,
"§7Should get repaired by §e" + this.materialName "§7Should get repaired by §e" + this.materialName
@ -128,28 +129,39 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
} }
@Override @Override
protected GuiItem itemFromFactory(String materialName, DoubleSettingGui.DoubleSettingFactory factory) { protected GuiItem itemFromFactory(NamespacedKey materialName, DoubleSettingGui.DoubleSettingFactory factory) {
return factory.getItem(materialFromName(materialName), return factory.getItem(materialFromName(materialName),
"§7%§a" + CasedStringUtil.snakeToUpperSpacedCase(materialName)+ " §erepaired by §a" + this.materialName); "§7%§a" + CasedStringUtil.snakeToUpperSpacedCase(materialName.getKey()) + " §erepaired by §a" + this.materialName);
} }
@Override @Override
protected Collection<String> getEveryInstanceOfGeneric() { protected Collection<NamespacedKey> getEveryInstanceOfGeneric() {
ArrayList<String> keys = new ArrayList<>(); Set<NamespacedKey> keys = new HashSet<>();
if (!this.shouldWork) { if (!this.shouldWork) {
return keys; return keys;
} }
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.name().toLowerCase()); ConfigurationSection legacySection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.getKey().toLowerCase());
if(materialSection == null){ ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(parentMaterial.toString().toLowerCase());
return keys;
} addAllKeys(legacySection, keys);
keys.addAll(materialSection.getKeys(false)); addAllKeys(materialSection, keys);
return keys; return keys;
} }
private Material materialFromName(String materialName){ private void addAllKeys(@Nullable ConfigurationSection section, @NotNull Set<NamespacedKey> keys) {
Material mat = Material.getMaterial(materialName.toUpperCase()); if (section == null) return;
for (var key : section.getKeys(false)) {
var material = NamespacedKey.fromString(key);
if (material == null) continue;
keys.add(material);
}
}
private Material materialFromName(NamespacedKey material) {
Material mat = MaterialUtil.INSTANCE.getMatFromKey(material);
if (mat == null || mat.isAir()) return Material.BARRIER; if (mat == null || mat.isAir()) return Material.BARRIER;
return mat; return mat;
} }
@ -163,7 +175,8 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
// ElementMappedToListGui methods // ElementMappedToListGui methods
@Override // Not used in this implementation @Override // Not used in this implementation
public void updateLocal() {} public void updateLocal() {
}
@Override @Override
public void cleanAndBeUnusable() { public void cleanAndBeUnusable() {

View file

@ -6,6 +6,7 @@ import org.bukkit.NamespacedKey
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import xyz.alexcrea.cuanvil.dependency.DependencyManager import xyz.alexcrea.cuanvil.dependency.DependencyManager
import xyz.alexcrea.cuanvil.dependency.plugins.EcoItemDependencyUtil import xyz.alexcrea.cuanvil.dependency.plugins.EcoItemDependencyUtil
import javax.annotation.Nullable
object MaterialUtil { object MaterialUtil {
@ -35,11 +36,13 @@ object MaterialUtil {
return this.type.key return this.type.key
} }
@Nullable
private fun bukkitMaterialFromKey(key: NamespacedKey): Material? { private fun bukkitMaterialFromKey(key: NamespacedKey): Material? {
//TODO on paper only transition Registry.MATERIAL.get(key) //TODO on paper only transition Registry.MATERIAL.get(key)
return Material.matchMaterial(key.toString()) return Material.matchMaterial(key.toString())
} }
@Nullable
fun getMatFromKey(key: NamespacedKey): Material? { fun getMatFromKey(key: NamespacedKey): Material? {
if(DependencyManager.ecoEnchantCompatibility != null) { if(DependencyManager.ecoEnchantCompatibility != null) {
val result = EcoItemDependencyUtil.ecoItemMaterialFromKey(key) val result = EcoItemDependencyUtil.ecoItemMaterialFromKey(key)

View file

@ -13,7 +13,7 @@ object UnitRepairUtil {
private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25 private const val DEFAULT_DEFAULT_UNIT_REPAIR = 0.25
// Path to user default unit repair value // Path to user default unit repair value
private const val UNIT_REPAIR_DEFAULT_PATH = "default_repair_amount" public const val UNIT_REPAIR_DEFAULT_PATH = "default_repair_amount"
/** /**
* Get the % of repair by unit [other] will do to this [ItemStack]. * Get the % of repair by unit [other] will do to this [ItemStack].
@ -25,14 +25,9 @@ object UnitRepairUtil {
if (other == null) return null if (other == null) return null
val config = ConfigHolder.UNIT_REPAIR_HOLDER.config val config = ConfigHolder.UNIT_REPAIR_HOLDER.config
val material = other.customType val result = findRepairValue(this, other, config) ?: return null
val selfType = this.customType
var result = checkSection(config, material.toString(), selfType) if(result > 0) return result
if (result != null) return result
result = checkSection(config, material.key, selfType)
if (result != null) return result
// Get default // Get default
val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR) val userDefault = config.getDouble(UNIT_REPAIR_DEFAULT_PATH, DEFAULT_DEFAULT_UNIT_REPAIR)
@ -41,6 +36,20 @@ object UnitRepairUtil {
return userDefault return userDefault
} }
private fun findRepairValue(
self: ItemStack,
other: ItemStack,
config: FileConfiguration
): Double? {
val material = other.customType
val selfType = self.customType
val result = checkSection(config, material.toString(), selfType)
if (result != null) return result
return checkSection(config, material.key, selfType)
}
fun checkSection( fun checkSection(
config: FileConfiguration, config: FileConfiguration,
path: String, path: String,