ResolvedParams.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package me.lethunderhawk.custom.item.abstraction.handling;
  2. import me.lethunderhawk.custom.item.abstraction.ability.AbilityDefinition;
  3. import me.lethunderhawk.custom.item.abstraction.definition.ItemDefinition;
  4. import me.lethunderhawk.custom.item.abstraction.instance.ItemInstance;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. public final class ResolvedParams {
  8. private final Map<String, Object> values;
  9. public ResolvedParams(Map<String, Object> values) {
  10. this.values = Map.copyOf(values);
  11. }
  12. public static ResolvedParams resolve(
  13. AbilityDefinition ability,
  14. ItemDefinition def,
  15. ItemInstance instance) {
  16. Map<String, Object> resolved = new HashMap<>();
  17. for (Map.Entry<String, String> entry : ability.getParams().entrySet()) {
  18. String key = entry.getKey();
  19. String rawValue = entry.getValue();
  20. Object value;
  21. if (rawValue.startsWith("{") && rawValue.endsWith("}")) {
  22. value = resolvePlaceholder(rawValue, def, instance);
  23. } else {
  24. value = parseLiteral(rawValue);
  25. }
  26. if (value == null) {
  27. throw new IllegalStateException(
  28. "Unable to resolve parameter '" + key + "' with value '" + rawValue +
  29. "' for ability handler '" + ability.handlerId() + "'"
  30. );
  31. }
  32. resolved.put(key, value);
  33. }
  34. return new ResolvedParams(resolved);
  35. }
  36. private static Object resolvePlaceholder(
  37. String placeholder,
  38. ItemDefinition def,
  39. ItemInstance instance
  40. ) {
  41. // Strip { and }
  42. String content = placeholder.substring(1, placeholder.length() - 1);
  43. String[] parts = content.split("\\.", 2);
  44. if (parts.length != 2) {
  45. throw new IllegalArgumentException("Invalid placeholder format: " + placeholder);
  46. }
  47. String scope = parts[0];
  48. String key = parts[1];
  49. return switch (scope) {
  50. case "stat" -> def.stats().get(key);
  51. case "instance" -> instance.data().get(key);
  52. default -> throw new IllegalArgumentException(
  53. "Unknown placeholder scope '" + scope + "' in " + placeholder
  54. );
  55. };
  56. }
  57. private static Object parseLiteral(String raw) {
  58. // Boolean
  59. if (raw.equalsIgnoreCase("true") || raw.equalsIgnoreCase("false")) {
  60. return Boolean.parseBoolean(raw);
  61. }
  62. // Integer
  63. try {
  64. if (!raw.contains(".")) {
  65. return Integer.parseInt(raw);
  66. }
  67. } catch (NumberFormatException ignored) {}
  68. // Double
  69. try {
  70. return Double.parseDouble(raw);
  71. } catch (NumberFormatException ignored) {}
  72. // Fallback: string
  73. return raw;
  74. }
  75. public double getDouble(String key) {
  76. Object val = values.get(key);
  77. if (val instanceof Number number) {
  78. return number.doubleValue();
  79. }
  80. throw new IllegalArgumentException("Value for key '" + key + "' is not a number");
  81. }
  82. public double getDoubleOrDefault(String key, double defaultValue) {
  83. Object val = values.get(key);
  84. if (val instanceof Number number) {
  85. return number.doubleValue();
  86. }
  87. return defaultValue;
  88. }
  89. public int getInt(String key) {
  90. Object val = values.get(key);
  91. if (val instanceof Number number) {
  92. return number.intValue();
  93. }
  94. throw new IllegalArgumentException("Value for key '" + key + "' is not a number");
  95. }
  96. public int getIntOrDefault(String key, int defaultValue) {
  97. Object val = values.get(key);
  98. if (val instanceof Number number) {
  99. return number.intValue();
  100. }
  101. return defaultValue;
  102. }
  103. public boolean getBoolean(String key) {
  104. Object val = values.get(key);
  105. if (val instanceof Boolean bool) {
  106. return bool;
  107. }
  108. throw new IllegalArgumentException("Value for key '" + key + "' is not a boolean");
  109. }
  110. public boolean getBooleanOrDefault(String key, boolean defaultValue) {
  111. Object val = values.get(key);
  112. if (val instanceof Boolean bool) {
  113. return bool;
  114. }
  115. return defaultValue;
  116. }
  117. public String getString(String key) {
  118. Object val = values.get(key);
  119. if (val instanceof String str) {
  120. return str;
  121. }
  122. throw new IllegalArgumentException("Value for key '" + key + "' is not a String");
  123. }
  124. }