| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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<String, Object> values;
- public ResolvedParams(Map<String, Object> values) {
- this.values = Map.copyOf(values);
- }
- public static ResolvedParams resolve(
- AbilityDefinition ability,
- ItemDefinition def,
- ItemInstance instance) {
- Map<String, Object> resolved = new HashMap<>();
- for (Map.Entry<String, String> 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");
- }
- }
|