|
| 1 | +package pro.cloudnode.smp.enchantbookplus; |
| 2 | + |
| 3 | +import org.bukkit.NamespacedKey; |
| 4 | +import org.bukkit.enchantments.Enchantment; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.jetbrains.annotations.Nullable; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +public final class ConfigEnchantmentEntry { |
| 16 | + /** |
| 17 | + * Name of the enchantment. |
| 18 | + */ |
| 19 | + private final @NotNull String name; |
| 20 | + |
| 21 | + /** |
| 22 | + * Maximum level of the enchantment. |
| 23 | + */ |
| 24 | + private final @Nullable Integer maxLevel; |
| 25 | + |
| 26 | + /** |
| 27 | + * Max level relative |
| 28 | + */ |
| 29 | + private final boolean maxLevelRelative; |
| 30 | + |
| 31 | + /** |
| 32 | + * Cost of the enchantment. |
| 33 | + */ |
| 34 | + private final int cost; |
| 35 | + |
| 36 | + /** |
| 37 | + * Multiply cost by level. |
| 38 | + */ |
| 39 | + private final boolean multiplyCostByLevel; |
| 40 | + |
| 41 | + /** |
| 42 | + * Name of the enchantment. |
| 43 | + */ |
| 44 | + public @NotNull String getName() { |
| 45 | + return name; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Maximum level of the enchantment. |
| 50 | + */ |
| 51 | + public @NotNull Optional<Integer> getMaxLevel() { |
| 52 | + if (Optional.ofNullable(maxLevel).isEmpty()) return Optional.empty(); |
| 53 | + if (maxLevelRelative) return Optional.of(getEnchantment().getMaxLevel() + maxLevel); |
| 54 | + return Optional.of(maxLevel); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Cost of the enchantment. |
| 59 | + */ |
| 60 | + public int getCost() { |
| 61 | + return cost; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Multiply cost by level. |
| 66 | + */ |
| 67 | + public boolean getMultiplyCostByLevel() { |
| 68 | + return multiplyCostByLevel; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get enchantment |
| 73 | + */ |
| 74 | + public Enchantment getEnchantment() { |
| 75 | + return Enchantment.getByKey(NamespacedKey.minecraft(name)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Is enchantment |
| 80 | + * |
| 81 | + * @param enchantment The enchantment |
| 82 | + */ |
| 83 | + public boolean isEnchantment(final @NotNull Enchantment enchantment) { |
| 84 | + return name.equalsIgnoreCase(enchantment.getKey().getKey()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param name Name of the enchantment. |
| 89 | + * @param maxLevel Maximum level of the enchantment. |
| 90 | + * @param maxLevelRelative Max level relative |
| 91 | + * @param cost Cost of the enchantment. |
| 92 | + * @param multiplyCostByLevel Multiply cost by level. |
| 93 | + */ |
| 94 | + public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Integer maxLevel, final boolean maxLevelRelative, final int cost, final boolean multiplyCostByLevel) { |
| 95 | + this.name = name; |
| 96 | + this.maxLevel = maxLevel; |
| 97 | + this.maxLevelRelative = maxLevelRelative; |
| 98 | + this.cost = cost; |
| 99 | + this.multiplyCostByLevel = multiplyCostByLevel; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * From config object |
| 104 | + * |
| 105 | + * @param configValue Config object |
| 106 | + */ |
| 107 | + public static ConfigEnchantmentEntry configValue(final @NotNull HashMap<@NotNull String, @NotNull Object> configValue) { |
| 108 | + final @NotNull String name = (String) Objects.requireNonNull(configValue.get("name")); |
| 109 | + final @Nullable Integer maxLevel; |
| 110 | + final boolean maxLevelRelative; |
| 111 | + if (configValue.containsKey("max-level")) { |
| 112 | + if (configValue.get("max-level") instanceof final @NotNull String string) { |
| 113 | + if (string.startsWith("+")) { |
| 114 | + maxLevel = Integer.parseInt(string.substring(1)); |
| 115 | + maxLevelRelative = true; |
| 116 | + } |
| 117 | + else { |
| 118 | + maxLevel = Integer.parseInt(string); |
| 119 | + maxLevelRelative = false; |
| 120 | + } |
| 121 | + } |
| 122 | + else { |
| 123 | + maxLevel = (Integer) configValue.get("max-level"); |
| 124 | + maxLevelRelative = false; |
| 125 | + } |
| 126 | + } |
| 127 | + else { |
| 128 | + maxLevel = null; |
| 129 | + maxLevelRelative = false; |
| 130 | + } |
| 131 | + final boolean multiplyCostByLevel; |
| 132 | + final int cost; |
| 133 | + if (configValue.containsKey("cost")) { |
| 134 | + if (configValue.get("cost") instanceof final @NotNull String costString) { |
| 135 | + if (costString.startsWith("*")) { |
| 136 | + multiplyCostByLevel = true; |
| 137 | + cost = Integer.parseInt(costString.substring(1)); |
| 138 | + } |
| 139 | + else { |
| 140 | + multiplyCostByLevel = false; |
| 141 | + cost = Integer.parseInt(costString); |
| 142 | + } |
| 143 | + } |
| 144 | + else { |
| 145 | + multiplyCostByLevel = false; |
| 146 | + cost = (Integer) configValue.get("cost"); |
| 147 | + } |
| 148 | + } |
| 149 | + else { |
| 150 | + multiplyCostByLevel = false; |
| 151 | + cost = 0; |
| 152 | + } |
| 153 | + return new ConfigEnchantmentEntry(name, maxLevel, maxLevelRelative, cost, multiplyCostByLevel); |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + /** |
| 158 | + * From config object array |
| 159 | + * |
| 160 | + * @param configValue Config object array |
| 161 | + */ |
| 162 | + public static @NotNull List<@NotNull ConfigEnchantmentEntry> configArray(final @NotNull ArrayList<@NotNull HashMap<@NotNull String, @NotNull Object>> configValue) { |
| 163 | + return configValue.stream().map(ConfigEnchantmentEntry::configValue).collect(Collectors.toList()); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Check if valid config object |
| 168 | + * |
| 169 | + * @param configValue Config object |
| 170 | + */ |
| 171 | + private static boolean isValidConfigValue(final @Nullable Object configValue) { |
| 172 | + if (configValue == null) return false; |
| 173 | + if (!(configValue instanceof final @NotNull ArrayList<?> arrayList)) return false; |
| 174 | + for (final @NotNull Object object : arrayList) { |
| 175 | + if (!(object instanceof final @NotNull HashMap<?, ?> hashMap)) return false; |
| 176 | + if (!hashMap.containsKey("name")) return false; |
| 177 | + if (!(hashMap.get("name") instanceof String)) return false; |
| 178 | + if (hashMap.containsKey("max-level") && !(hashMap.get("max-level") instanceof String) && !(hashMap.get("max-level") instanceof Integer)) |
| 179 | + return false; |
| 180 | + if (hashMap.containsKey("cost") && !(hashMap.get("cost") instanceof String) && !(hashMap.get("cost") instanceof Integer)) |
| 181 | + return false; |
| 182 | + } |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * From config |
| 188 | + * |
| 189 | + * @param configValue Config object |
| 190 | + */ |
| 191 | + public static @NotNull List<@NotNull ConfigEnchantmentEntry> config(final @Nullable Object configValue) throws IllegalArgumentException { |
| 192 | + if (!isValidConfigValue(configValue)) throw new IllegalArgumentException("Invalid config value"); |
| 193 | + return configArray((ArrayList<HashMap<String, Object>>) configValue); |
| 194 | + } |
| 195 | +} |
0 commit comments