Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnchantmentType - prepare for MC 1.21 custom enchantments #6687

Merged
merged 29 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c8f6b45
EnchantmentType - prepare for MC 1.21 custom enchantments
ShaneBeee May 11, 2024
fc2ad09
EnchantmentType - change wording, key to namespace
ShaneBeee May 11, 2024
10f20a0
EnchantmentType - clean up repetitive code
ShaneBeee May 11, 2024
6b694df
EnchantmentType - to lower case
ShaneBeee May 11, 2024
4d4f56b
default.lang - add this back and deal with it later
ShaneBeee May 12, 2024
09760cb
Aliases/default.lang - change "of" location in lang file
ShaneBeee May 12, 2024
bcdae08
Merge branch 'dev/feature' into feature/enchantment_prep
sovdeeth May 13, 2024
f56f529
EnchantmentUtils - more changes
ShaneBeee May 13, 2024
e69289c
Merge remote-tracking branch 'origin/feature/enchantment_prep' into f…
ShaneBeee May 13, 2024
620b354
EnchantmentUtils - fix stream issue on older java versions
ShaneBeee May 13, 2024
b9ac52c
EnchantmentUtils - small cleanup
ShaneBeee May 13, 2024
2412afa
Update src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java
ShaneBeee May 13, 2024
8fc3246
Update src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java
ShaneBeee May 13, 2024
126607f
EnchantmentUtils - small changes as suggested
ShaneBeee May 13, 2024
60b45b8
EnchantmentUtils - more changes
ShaneBeee May 14, 2024
843c009
EnchantmentUtils - update serialization
ShaneBeee May 14, 2024
392f9d2
Merge branch 'dev/feature' into feature/enchantment_prep
Moderocky Jun 1, 2024
84ac0ac
Merge branch 'dev/feature' into feature/enchantment_prep
ShaneBeee Jun 21, 2024
3576a31
Merge branch 'dev/feature' into feature/enchantment_prep
ShaneBeee Jun 27, 2024
2c107e4
Merge branch 'dev/feature' into feature/enchantment_prep
ShaneBeee Jul 1, 2024
ad81354
EnchantmentUtils - change up how classinfo is done
ShaneBeee Jul 1, 2024
f4c3a7a
BukkitClasses - move usage to EnchantmentUtils
ShaneBeee Jul 1, 2024
50b13bd
pull-6687-enchantment-update.sk - add test
ShaneBeee Jul 1, 2024
70eff27
BukkitClasses - add note regarding namespaces and custom enchants
ShaneBeee Jul 1, 2024
928d595
Merge branch 'dev/feature' into feature/enchantment_prep
sovdeeth Jul 1, 2024
80e81ff
EnchantmentUtils - only register listener if Registry is not found
ShaneBeee Jul 1, 2024
77195f2
Merge remote-tracking branch 'origin/feature/enchantment_prep' into f…
ShaneBeee Jul 1, 2024
c28da37
Update src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java
ShaneBeee Jul 1, 2024
0d5dc52
Merge branch 'dev/feature' into feature/enchantment_prep
sovdeeth Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/aliases/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static ItemType parseItemType(String s) {
}

String lc = s.toLowerCase(Locale.ENGLISH);
String of = Language.getSpaced("enchantments.of").toLowerCase();
String of = Language.getSpaced("of").toLowerCase();
int c = -1;
outer: while ((c = lc.indexOf(of, c + 1)) != -1) {
ItemType t2 = t.clone();
Expand Down
88 changes: 84 additions & 4 deletions src/main/java/ch/njol/skript/bukkitutil/EnchantmentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,106 @@
*/
package ch.njol.skript.bukkitutil;

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

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import ch.njol.skript.Skript;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
* Maps enchantments to their keys.
*/
public class EnchantmentUtils {

private static final Map<Enchantment, String> NAMES = new HashMap<>();
private static final Map<String, Enchantment> PATTERNS = new HashMap<>();
private static final boolean HAS_REGISTRY = Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "ENCHANTMENT");

static {
Language.addListener(() -> {
NAMES.clear();
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
PATTERNS.clear();
List<Enchantment> enchantments = new ArrayList<>();
if (HAS_REGISTRY) {
Registry.ENCHANTMENT.forEach(enchantments::add);
} else {
enchantments.addAll(Arrays.asList(Enchantment.values()));
}
for (Enchantment enchantment : enchantments) {
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
NamespacedKey key = enchantment.getKey();
final String[] names = Language.getList("enchantments." + key.getKey());

if (!names[0].startsWith("enchantments.")) {
NAMES.put(enchantment, names[0]);
// Add lang file names
for (String name : names)
PATTERNS.put(name.toLowerCase(Locale.ENGLISH), enchantment);
}
// If Minecraft provided, add key without namespace and underscores (ex: "fire aspect")
if (key.getNamespace().equalsIgnoreCase("minecraft"))
PATTERNS.put(key.getKey().replace("_", " "), enchantment);
// Add full namespaced key as pattern (ex: "minecraft:fire_aspect", "custom:floopy_floopy")
PATTERNS.put(key.toString(), enchantment);
}
});
}

public static String getKey(Enchantment enchantment) {
return enchantment.getKey().getKey();
return enchantment.getKey().toString();
}

@Nullable
public static Enchantment getByKey(String key) {
return Enchantment.getByKey(NamespacedKey.minecraft(key));
if (!key.contains(":")) {
// Old method for old variables
return Enchantment.getByKey(NamespacedKey.minecraft(key));
} else {
NamespacedKey namespacedKey = NamespacedKey.fromString(key);
if (namespacedKey == null)
return null;

if (HAS_REGISTRY) {
return Registry.ENCHANTMENT.get(namespacedKey);
} else {
return Enchantment.getByKey(namespacedKey);
}
}
}

@Nullable
public static Enchantment parseEnchantment(String s) {
return PATTERNS.get(s);
}

@SuppressWarnings("null")
public static Collection<String> getNames() {
return NAMES.values();
}

@SuppressWarnings("null")
public static String toString(final Enchantment enchantment) {
// If we have a name in the lang file, return that first
if (NAMES.containsKey(enchantment))
return NAMES.get(enchantment);
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved

// If no name is available, return the namespaced key
return enchantment.getKey().toString();
}

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

}
10 changes: 5 additions & 5 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ public boolean mustSyncDeserialization() {
.name("Enchantment")
.description("An enchantment, e.g. 'sharpness' or 'fortune'. Unlike <a href='#enchantmenttype'>enchantment type</a> " +
"this type has no level, but you usually don't need to use this type anyway.")
.usage(StringUtils.join(EnchantmentType.getNames(), ", "))
.usage(StringUtils.join(EnchantmentUtils.getNames(), ", "))
.examples("")
.since("1.4.6")
.before("enchantmenttype")
Expand All @@ -1235,12 +1235,12 @@ public boolean mustSyncDeserialization() {
@Override
@Nullable
public Enchantment parse(final String s, final ParseContext context) {
return EnchantmentType.parseEnchantment(s);
return EnchantmentUtils.parseEnchantment(s);
}

@Override
public String toString(final Enchantment e, final int flags) {
return EnchantmentType.toString(e, flags);
return EnchantmentUtils.toString(e, flags);
}

@Override
Expand Down Expand Up @@ -1505,12 +1505,12 @@ public boolean canParse(ParseContext context) {

@Override
public String toString(EnchantmentOffer eo, int flags) {
return EnchantmentType.toString(eo.getEnchantment(), flags) + " " + eo.getEnchantmentLevel();
return EnchantmentUtils.toString(eo.getEnchantment(), flags) + " " + eo.getEnchantmentLevel();
}

@Override
public String toVariableNameString(EnchantmentOffer eo) {
return "offer:" + EnchantmentType.toString(eo.getEnchantment()) + "=" + eo.getEnchantmentLevel();
return "offer:" + EnchantmentUtils.toString(eo.getEnchantment()) + "=" + eo.getEnchantmentLevel();
}
}));

Expand Down
79 changes: 17 additions & 62 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,23 @@
*/
package ch.njol.skript.util;

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

import ch.njol.skript.bukkitutil.EnchantmentUtils;
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;

/**
* @author Peter Güttinger
*/
public class EnchantmentType implements YggdrasilSerializable {

private final static String LANGUAGE_NODE = "enchantments";


private final Enchantment type;
private final int level;

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

public EnchantmentType(final Enchantment type) {
assert type != null;
this.type = type;
Expand All @@ -61,26 +54,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 +83,15 @@ 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);
return EnchantmentUtils.toString(type) + (level == -1 ? "" : " " + level);
}

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

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 @@ -138,29 +103,19 @@ public static EnchantmentType parse(final String s) {
if (pattern.matcher(s).matches()) {
String name = s.substring(0, s.lastIndexOf(' '));
assert name != null;
final Enchantment ench = parseEnchantment(name);
final Enchantment ench = EnchantmentUtils.parseEnchantment(name);
if (ench == null)
return null;
String level = s.substring(s.lastIndexOf(' ') + 1);
assert level != null;
return new EnchantmentType(ench, Utils.parseInt(level));
}
final Enchantment ench = parseEnchantment(s);
final Enchantment ench = EnchantmentUtils.parseEnchantment(s);
if (ench == null)
return null;
return new EnchantmentType(ench, -1);
}

@Nullable
public static Enchantment parseEnchantment(final String s) {
return PATTERNS.get(s.toLowerCase(Locale.ENGLISH));
}

@SuppressWarnings("null")
public static Collection<String> getNames() {
return NAMES.values();
}


@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -169,7 +124,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 +138,5 @@ public boolean equals(final @Nullable Object obj) {
return false;
return type.equals(other.type);
}

}
Loading