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 all 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 @@ -268,7 +268,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
151 changes: 145 additions & 6 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,165 @@
*/
package ch.njol.skript.bukkitutil;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.localization.Language;
import ch.njol.util.StringUtils;
import ch.njol.yggdrasil.Fields;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.Nullable;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import ch.njol.skript.Skript;
import java.io.StreamCorruptedException;
import java.util.Collection;
import java.util.HashMap;
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 = BukkitUtils.registryExists("ENCHANTMENT");

static {
if (!HAS_REGISTRY) {
Language.addListener(() -> {
NAMES.clear();
PATTERNS.clear();
for (Enchantment enchantment : Enchantment.values()) {
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(NamespacedKey.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);
}

public static ClassInfo<Enchantment> createClassInfo() {
return new ClassInfo<>(Enchantment.class, "enchantment")
.parser(new Parser<>() {
@Override
@Nullable
public Enchantment parse(final String s, final ParseContext context) {
return EnchantmentUtils.parseEnchantment(s);
}

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

@Override
public String toVariableNameString(final Enchantment e) {
return "" + EnchantmentUtils.getKey(e);
}
}).serializer(new Serializer<>() {
@Override
public Fields serialize(final Enchantment ench) {
final Fields f = new Fields();
f.putObject("key", EnchantmentUtils.getKey(ench));
return f;
}

@Override
public boolean canBeInstantiated() {
return false;
}

@Override
public void deserialize(final Enchantment o, final Fields f) {
assert false;
}

@Override
protected Enchantment deserialize(final Fields fields) throws StreamCorruptedException {
final String key = fields.getObject("key", String.class);
assert key != null; // If a key happens to be null, something went really wrong...
final Enchantment e = EnchantmentUtils.getByKey(key);
if (e == null)
throw new StreamCorruptedException("Invalid enchantment " + key);
return e;
}

@Override
@Nullable
public Enchantment deserialize(String s) {
return Enchantment.getByName(s);
}

@Override
public boolean mustSyncDeserialization() {
return false;
}
})
.usage(StringUtils.join(EnchantmentUtils.getNames(), ", "))
.supplier(Enchantment.values());
}

}
84 changes: 16 additions & 68 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
import ch.njol.skript.localization.Language;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.BlockUtils;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.PotionEffectUtils;
import ch.njol.skript.util.StringMode;
import ch.njol.util.StringUtils;
Expand Down Expand Up @@ -1078,12 +1077,12 @@ protected boolean canBeInstantiated() {
public PotionEffectType parse(final String s, final ParseContext context) {
return PotionEffectUtils.parseType(s);
}

@Override
public String toString(final PotionEffectType p, final int flags) {
return PotionEffectUtils.toString(p, flags);
}

@Override
public String toVariableNameString(final PotionEffectType p) {
return "" + p.getName();
Expand Down Expand Up @@ -1223,74 +1222,23 @@ public boolean mustSyncDeserialization() {
return true;
}
}));

Classes.registerClass(new ClassInfo<>(Enchantment.class, "enchantment")

ClassInfo<Enchantment> enchantmentClassInfo;
if (BukkitUtils.registryExists("ENCHANTMENT")) {
enchantmentClassInfo = new RegistryClassInfo<>(Enchantment.class, Registry.ENCHANTMENT, "enchantment", "enchantments");
} else {
enchantmentClassInfo = EnchantmentUtils.createClassInfo();
}
Classes.registerClass(enchantmentClassInfo
.user("enchantments?")
.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(), ", "))
"this type has no level, but you usually don't need to use this type anyway.",
"NOTE: Minecraft namespaces are supported, ex: 'minecraft:basalt_deltas'.",
"As of Minecraft 1.21 this will also support custom enchantments using namespaces, ex: 'myenchants:explosive'.")
.examples("")
.since("1.4.6")
.before("enchantmenttype")
.supplier(Enchantment.values())
.parser(new Parser<Enchantment>() {
@Override
@Nullable
public Enchantment parse(final String s, final ParseContext context) {
return EnchantmentType.parseEnchantment(s);
}

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

@Override
public String toVariableNameString(final Enchantment e) {
return "" + EnchantmentUtils.getKey(e);
}
})
.serializer(new Serializer<Enchantment>() {
@Override
public Fields serialize(final Enchantment ench) {
final Fields f = new Fields();
f.putObject("key", EnchantmentUtils.getKey(ench));
return f;
}

@Override
public boolean canBeInstantiated() {
return false;
}

@Override
public void deserialize(final Enchantment o, final Fields f) {
assert false;
}

@Override
protected Enchantment deserialize(final Fields fields) throws StreamCorruptedException {
final String key = fields.getObject("key", String.class);
assert key != null; // If a key happens to be null, something went really wrong...
final Enchantment e = EnchantmentUtils.getByKey(key);
if (e == null)
throw new StreamCorruptedException("Invalid enchantment " + key);
return e;
}

// return "" + e.getId();
@Override
@Nullable
public Enchantment deserialize(String s) {
return Enchantment.getByName(s);
}

@Override
public boolean mustSyncDeserialization() {
return false;
}
}));
.before("enchantmenttype"));

Material[] allMaterials = Material.values();
Classes.registerClass(new ClassInfo<>(Material.class, "material")
Expand Down Expand Up @@ -1516,12 +1464,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
Loading