Skip to content

Commit b1d8e72

Browse files
committed
determine Minecraft enchantment by ConfiEnchantment name
1 parent f11882d commit b1d8e72

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/main/java/pro/cloudnode/smp/enchantbookplus/ConfigEnchantmentEntry.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pro.cloudnode.smp.enchantbookplus;
22

3+
import org.bukkit.NamespacedKey;
34
import org.bukkit.enchantments.Enchantment;
45
import org.jetbrains.annotations.NotNull;
56
import org.jetbrains.annotations.Nullable;
@@ -46,11 +47,10 @@ public final class ConfigEnchantmentEntry {
4647

4748
/**
4849
* Maximum level of the enchantment.
49-
* @param enchantment The enchantment
5050
*/
51-
public @NotNull Optional<Integer> getMaxLevel(final @NotNull Enchantment enchantment) {
51+
public @NotNull Optional<Integer> getMaxLevel() {
5252
if (Optional.ofNullable(maxLevel).isEmpty()) return Optional.empty();
53-
if (maxLevelRelative) return Optional.of(enchantment.getMaxLevel() + maxLevel);
53+
if (maxLevelRelative) return Optional.of(getEnchantment().getMaxLevel() + maxLevel);
5454
return Optional.of(maxLevel);
5555
}
5656

@@ -69,10 +69,26 @@ public boolean getMultiplyCostByLevel() {
6969
}
7070

7171
/**
72-
* @param name Name of the enchantment.
73-
* @param maxLevel Maximum level of the enchantment.
74-
* @param maxLevelRelative Max level relative
75-
* @param cost Cost of the enchantment.
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.
7692
* @param multiplyCostByLevel Multiply cost by level.
7793
*/
7894
public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Integer maxLevel, final boolean maxLevelRelative, final int cost, final boolean multiplyCostByLevel) {
@@ -159,8 +175,10 @@ private static boolean isValidConfigValue(final @Nullable Object configValue) {
159175
if (!(object instanceof final @NotNull HashMap<?, ?> hashMap)) return false;
160176
if (!hashMap.containsKey("name")) return false;
161177
if (!(hashMap.get("name") instanceof String)) return false;
162-
if (hashMap.containsKey("max-level") && !(hashMap.get("max-level") instanceof String) && !(hashMap.get("max-level") instanceof Integer)) return false;
163-
if (hashMap.containsKey("cost") && !(hashMap.get("cost") instanceof String) && !(hashMap.get("cost") instanceof Integer)) 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;
164182
}
165183
return true;
166184
}

src/main/java/pro/cloudnode/smp/enchantbookplus/EnchantBookPlus.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pro.cloudnode.smp.enchantbookplus;
22

3+
import org.bukkit.enchantments.Enchantment;
34
import org.bukkit.plugin.java.JavaPlugin;
45
import org.jetbrains.annotations.NotNull;
56
import pro.cloudnode.smp.enchantbookplus.event.PrepareAnvil;
@@ -40,10 +41,10 @@ private void registerEvents() {
4041
/**
4142
* Get enchantment from cache
4243
*
43-
* @param name Name of the enchantment
44+
* @param enchantment The Minecraft enchantment
4445
*/
45-
public @NotNull Optional<@NotNull ConfigEnchantmentEntry> getConfigEnchantment(final @NotNull String name) {
46-
return getConfigEnchantments().stream().filter(enchantment -> enchantment.getName().equalsIgnoreCase(name) || enchantment.getName().equalsIgnoreCase("ALL")).findFirst();
46+
public @NotNull Optional<@NotNull ConfigEnchantmentEntry> getConfigEnchantment(final @NotNull Enchantment enchantment) {
47+
return getConfigEnchantments().stream().filter(c -> c.isEnchantment(enchantment)).findFirst();
4748
}
4849

4950
/**

src/main/java/pro/cloudnode/smp/enchantbookplus/event/PrepareAnvil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void onPrepareAnvil(final @NotNull PrepareAnvilEvent event) {
3737
for (final @NotNull Map.Entry<@NotNull Enchantment, @NotNull Integer> entry : upgradeEnchants.entrySet()) {
3838
final @NotNull Enchantment enchantment = entry.getKey();
3939
if (enchantment.getMaxLevel() == 1) continue;
40-
final @NotNull Optional<@NotNull ConfigEnchantmentEntry> configEnchantment = EnchantBookPlus.getInstance().getConfigEnchantment(enchantment.getKey().getKey());
40+
final @NotNull Optional<@NotNull ConfigEnchantmentEntry> configEnchantment = EnchantBookPlus.getInstance().getConfigEnchantment(enchantment);
4141
if (configEnchantment.isEmpty()) continue;
4242
final int upgradeLevel = entry.getValue();
4343
final int finalLevel;

0 commit comments

Comments
 (0)