Skip to content

Commit

Permalink
EnchantmentType - prepare for MC 1.21 custom enchantments
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed May 11, 2024
1 parent 389dbf9 commit c8f6b45
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 103 deletions.
103 changes: 55 additions & 48 deletions src/main/java/ch/njol/skript/util/EnchantmentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,31 @@
*/
package ch.njol.skript.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.util.regex.Pattern;

import ch.njol.skript.Skript;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.EnchantmentUtils;
import ch.njol.skript.localization.Language;
import ch.njol.yggdrasil.YggdrasilSerializable;
import org.jetbrains.annotations.NotNull;

/**
* @author Peter Güttinger
*/
public class EnchantmentType implements YggdrasilSerializable {
private final static String LANGUAGE_NODE = "enchantments";

private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT");

private final Enchantment type;
private final int level;

/**
* Used for deserialisation only
*/
Expand All @@ -50,7 +51,7 @@ private EnchantmentType() {
type = null;
level = -1;
}

public EnchantmentType(final Enchantment type) {
assert type != null;
this.type = type;
Expand All @@ -61,26 +62,26 @@ public EnchantmentType(final Enchantment type, final int level) {
this.type = type;
this.level = level;
}

/**
* @return level or 1 if level == -1
*/
public int getLevel() {
return level == -1 ? 1 : level;
}

/**
* @return the internal level, can be -1
*/
public int getInternalLevel() {
return level;
}

@Nullable
public Enchantment getType() {
return type;
}

/**
* Checks whether the given item type has this enchantment.
* @param item the item to be checked.
Expand All @@ -90,43 +91,32 @@ public Enchantment getType() {
public boolean has(final ItemType item) {
return item.hasEnchantments(type);
}

@Override
public String toString() {
return toString(type) + (level == -1 ? "" : " " + level);
}

@SuppressWarnings("null")
public static String toString(final Enchantment e) {
return NAMES.get(e);
public static String toString(final Enchantment enchantment) {
NamespacedKey key = enchantment.getKey();
// If it's a minecraft enchant, just return the key
if (key.getNamespace().equalsIgnoreCase("minecraft"))
return key.getKey();
// Else if it's a custom enchant, return with the namespace
// ex: `my_key:explosive`
return key.toString();
}

// REMIND flags?
@SuppressWarnings("null")
public static String toString(final Enchantment e, final int flags) {
return NAMES.get(e);
public static String toString(final Enchantment enchantment, final int flags) {
return toString(enchantment);
}

private final static Map<Enchantment, String> NAMES = new HashMap<>();
private final static Map<String, Enchantment> PATTERNS = new HashMap<>();

static {
Language.addListener(() -> {
NAMES.clear();
for (Enchantment e : Enchantment.values()) {
assert e != null;
final String[] names = Language.getList(LANGUAGE_NODE + ".names." + EnchantmentUtils.getKey(e));
NAMES.put(e, names[0]);

for (String name : names)
PATTERNS.put(name.toLowerCase(Locale.ENGLISH), e);
}
});
}


@SuppressWarnings("null")
private final static Pattern pattern = Pattern.compile(".+ \\d+");

/**
* Parses an enchantment type from string. This includes an {@link Enchantment}
* and its level.
Expand All @@ -150,17 +140,34 @@ public static EnchantmentType parse(final String s) {
return null;
return new EnchantmentType(ench, -1);
}


@SuppressWarnings("deprecation")
@Nullable
public static Enchantment parseEnchantment(final String s) {
return PATTERNS.get(s.toLowerCase(Locale.ENGLISH));
public static Enchantment parseEnchantment(String s) {
s = s.replace(" ", "_");
NamespacedKey key;
try {
if (s.contains(":"))
key = NamespacedKey.fromString(s);
else
key = NamespacedKey.minecraft(s);
} catch (IllegalArgumentException ignore) {
return null;
}
if (key == null)
return null;
if (HAS_REGISTRY) // Registry added in Bukkit 1.14
return Registry.ENCHANTMENT.get(key);
return Enchantment.getByKey(key);
}

@SuppressWarnings("null")
public static Collection<String> getNames() {
return NAMES.values();
List<String> names = new ArrayList<>();
Registry.ENCHANTMENT.forEach(enchantment -> names.add(enchantment.getKey().getKey().replace("_", " ")));
return names;
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -169,7 +176,7 @@ public int hashCode() {
result = prime * result + type.hashCode();
return result;
}

@Override
public boolean equals(final @Nullable Object obj) {
if (this == obj)
Expand All @@ -183,5 +190,5 @@ public boolean equals(final @Nullable Object obj) {
return false;
return type.equals(other.type);
}

}
55 changes: 0 additions & 55 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -45,61 +45,6 @@ aliases:

named: named

# -- Enchantments --
enchantments:
of: of
names:
protection: Protection
fire_protection: Fire Protection
feather_falling: Feather Falling
blast_protection: Blast Protection
projectile_protection: Projectile Protection
respiration: Respiration
aqua_affinity: Aqua Affinity
sharpness: Sharpness
smite: Smite
bane_of_arthropods: Bane of Arthropods
knockback: Knockback
fire_aspect: Fire Aspect
looting: Looting
efficiency: Efficiency
silk_touch: Silk Touch
unbreaking: Unbreaking
fortune: Fortune
power: Power
punch: Punch
flame: Flame
infinity: Infinity
thorns: Thorns
luck: Luck of the Sea
lure: Lure
depth_strider: Depth Strider
mending: Mending
frost_walker: Frost Walker
vanishing_curse: Curse of Vanishing
binding_curse: Curse of Binding
# It's sweeping on 1.10 and below for some reason.
sweeping: Sweeping Edge
sweeping_edge: Sweeping Edge
# New 1.13 Enchantments
channeling: Channeling, Channelling
riptide: Riptide
impaling: Impaling
loyalty: Loyalty
luck_of_the_sea: Luck of The Sea
# new 1.14 Enchantments
multishot: Multishot, Multi-Shot
piercing: Piercing
quick_charge: Quick Charge
# new 1.16 Enchantment
soul_speed: Soul Speed
# new 1.19 Enchantment
swift_sneak: Swift Sneak
# new 1.21 Enchantments (added in 1.20.5 experimental)
density: Density
breach: Breach
wind_burst: Wind Burst

# -- Potion Effects --
potions:
speed: speed, swiftness
Expand Down

0 comments on commit c8f6b45

Please sign in to comment.