Skip to content

Commit

Permalink
Use enchantment key names for 1.13+
Browse files Browse the repository at this point in the history
Enchantment#getName is deprecated and gives the wrong names for enchantments starting in 1.21
  • Loading branch information
JRoy authored and mdcfe committed Jun 29, 2024
1 parent 64531aa commit b392f03
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
11 changes: 11 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/Enchantments.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ public final class Enchantments {
private Enchantments() {
}

public static String getRealName(final Enchantment enchantment) {
if (enchantment == null) {
return null;
}

if (isFlat) { // 1.13+ only
return enchantment.getKey().getKey();
}
return enchantment.getName().toLowerCase(Locale.ENGLISH);
}

public static Enchantment getByName(final String name) {
if (name == null || name.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ private boolean parseEnchantmentStrings(final CommandSource sender, final boolea
if (enchantment == null) {
return false;
}
if (hasMetaPermission(sender, "enchantments." + enchantment.getName().toLowerCase(Locale.ENGLISH), false, false, ess)) {
if (hasMetaPermission(sender, "enchantments." + Enchantments.getRealName(enchantment), false, false, ess)) {
int level = -1;
if (split.length > 1) {
try {
Expand Down Expand Up @@ -619,7 +619,7 @@ public void addEnchantment(final CommandSource sender, final boolean allowUnsafe
}
}
} catch (final Exception ex) {
throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex);
throw new Exception("Enchantment " + Enchantments.getRealName(enchantment) + ": " + ex.getMessage(), ex);
}
}

Expand All @@ -629,7 +629,7 @@ public Enchantment getEnchantment(final User user, final String name) throws Exc
return null;
}

final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
final String enchantmentName = Enchantments.getRealName(enchantment);

if (!hasMetaPermission(user, "enchantments." + enchantmentName, true, false)) {
throw new TranslatableException("enchantmentPerm", enchantmentName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -35,7 +34,7 @@ protected void run(final Server server, final User user, final String commandLab
if (args.length == 0) {
final Set<String> usableEnchants = new TreeSet<>();
for (final Map.Entry<String, Enchantment> entry : Enchantments.entrySet()) {
final String name = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
final String name = Enchantments.getRealName(entry.getValue());
if (usableEnchants.contains(name) || (user.isAuthorized("essentials.enchantments." + name) && entry.getValue().canEnchantItem(stack))) {
usableEnchants.add(entry.getKey());
}
Expand All @@ -57,7 +56,7 @@ protected void run(final Server server, final User user, final String commandLab
metaStack.addEnchantment(user.getSource(), ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchantments.allowunsafe"), enchantment, level);
stack.setItemMeta(metaStack.getItemStack().getItemMeta());
user.getBase().updateInventory();
final String enchantName = enchantment.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
final String enchantName = Enchantments.getRealName(enchantment).replace('_', ' ');
if (level == 0) {
user.sendTl("enchantmentRemoved", enchantName);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.earth2me.essentials.items;

import com.earth2me.essentials.Enchantments;
import com.earth2me.essentials.IConf;
import com.earth2me.essentials.User;
import com.earth2me.essentials.craftbukkit.Inventories;
Expand Down Expand Up @@ -218,7 +219,7 @@ public String serialize(final ItemStack is, final boolean useResolvers) {

if (meta.hasEnchants()) {
for (final Enchantment e : meta.getEnchants().keySet()) {
sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" ");
sb.append(Enchantments.getRealName(e)).append(":").append(meta.getEnchantLevel(e)).append(" ");
}
}

Expand Down Expand Up @@ -263,7 +264,7 @@ public String serialize(final ItemStack is, final boolean useResolvers) {
case ENCHANTED_BOOK:
final EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta();
for (final Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) {
sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" ");
sb.append(Enchantments.getRealName(e)).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" ");
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected boolean onSignInteract(final ISign sign, final User player, final Stri
throw new SignException(ex, "errorWithMessage", ex.getMessage());
}

final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
final String enchantmentName = Enchantments.getRealName(enchantment);
if (level == 0) {
player.sendTl("enchantmentRemoved", enchantmentName.replace('_', ' '));
} else {
Expand Down

0 comments on commit b392f03

Please sign in to comment.