package me.lethunderhawk.custom.item.abstraction.handling; import me.lethunderhawk.custom.item.abstraction.ability.AbilityDefinition; import me.lethunderhawk.custom.item.abstraction.definition.ItemDefinition; import me.lethunderhawk.custom.item.abstraction.instance.ItemInstance; import java.util.HashMap; import java.util.Map; public final class ResolvedParams { private final Map values; public ResolvedParams(Map values) { this.values = Map.copyOf(values); } public static ResolvedParams resolve( AbilityDefinition ability, ItemDefinition def, ItemInstance instance) { Map resolved = new HashMap<>(); for (Map.Entry entry : ability.getParams().entrySet()) { String key = entry.getKey(); String rawValue = entry.getValue(); Object value; if (rawValue.startsWith("{") && rawValue.endsWith("}")) { value = resolvePlaceholder(rawValue, def, instance); } else { value = parseLiteral(rawValue); } if (value == null) { throw new IllegalStateException( "Unable to resolve parameter '" + key + "' with value '" + rawValue + "' for ability handler '" + ability.handlerId() + "'" ); } resolved.put(key, value); } return new ResolvedParams(resolved); } private static Object resolvePlaceholder( String placeholder, ItemDefinition def, ItemInstance instance ) { // Strip { and } String content = placeholder.substring(1, placeholder.length() - 1); String[] parts = content.split("\\.", 2); if (parts.length != 2) { throw new IllegalArgumentException("Invalid placeholder format: " + placeholder); } String scope = parts[0]; String key = parts[1]; return switch (scope) { case "stat" -> def.stats().get(key); case "instance" -> instance.data().get(key); default -> throw new IllegalArgumentException( "Unknown placeholder scope '" + scope + "' in " + placeholder ); }; } private static Object parseLiteral(String raw) { // Boolean if (raw.equalsIgnoreCase("true") || raw.equalsIgnoreCase("false")) { return Boolean.parseBoolean(raw); } // Integer try { if (!raw.contains(".")) { return Integer.parseInt(raw); } } catch (NumberFormatException ignored) {} // Double try { return Double.parseDouble(raw); } catch (NumberFormatException ignored) {} // Fallback: string return raw; } public double getDouble(String key) { Object val = values.get(key); if (val instanceof Number number) { return number.doubleValue(); } throw new IllegalArgumentException("Value for key '" + key + "' is not a number"); } public double getDoubleOrDefault(String key, double defaultValue) { Object val = values.get(key); if (val instanceof Number number) { return number.doubleValue(); } return defaultValue; } public int getInt(String key) { Object val = values.get(key); if (val instanceof Number number) { return number.intValue(); } throw new IllegalArgumentException("Value for key '" + key + "' is not a number"); } public int getIntOrDefault(String key, int defaultValue) { Object val = values.get(key); if (val instanceof Number number) { return number.intValue(); } return defaultValue; } public boolean getBoolean(String key) { Object val = values.get(key); if (val instanceof Boolean bool) { return bool; } throw new IllegalArgumentException("Value for key '" + key + "' is not a boolean"); } public boolean getBooleanOrDefault(String key, boolean defaultValue) { Object val = values.get(key); if (val instanceof Boolean bool) { return bool; } return defaultValue; } public String getString(String key) { Object val = values.get(key); if (val instanceof String str) { return str; } throw new IllegalArgumentException("Value for key '" + key + "' is not a String"); } }