diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7421a59e..45a80e19 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -89,5 +89,5 @@ jobs: uses: SkriptLang/skript-test-action@v1.2 with: test_script_directory: src/test/scripts - skript_repo_ref: dev/patch + skript_repo_ref: dev/feature extra_plugins_directory: extra-plugins/ diff --git a/build.gradle b/build.gradle index e0acb927..e4b34ef3 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,8 @@ compileJava { options.encoding = "UTF-8" } -version = "1.3.3" + +version = "1.4" def java16 = 16 def java17 = 17 diff --git a/src/main/java/it/jakegblp/lusk/elements/minecraft/namespacedkey/types/NamespacedKeyClassInfos.java b/src/main/java/it/jakegblp/lusk/elements/minecraft/namespacedkey/types/NamespacedKeyClassInfos.java new file mode 100644 index 00000000..2ac447c3 --- /dev/null +++ b/src/main/java/it/jakegblp/lusk/elements/minecraft/namespacedkey/types/NamespacedKeyClassInfos.java @@ -0,0 +1,80 @@ +package it.jakegblp.lusk.elements.minecraft.namespacedkey.types; + +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.registrations.Classes; +import ch.njol.yggdrasil.Fields; +import it.jakegblp.lusk.utils.LuskUtils; +import org.bukkit.NamespacedKey; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.StreamCorruptedException; + +public class NamespacedKeyClassInfos { + static { + if (Classes.getExactClassInfo(NamespacedKey.class) == null) { + Classes.registerClass(new ClassInfo<>(NamespacedKey.class, "namespacedkey") + .user("namespaced ?keys?") + .name("Namespaced Key") + .description(""" + A String based key which consists of a namespace and a key; used to declare and specify game objects in Minecraft without without potential ambiguity or conflicts. + + Namespaces may only contain lowercase alphanumeric characters, periods, underscores, and hyphens. + Keys may only contain lowercase alphanumeric characters, periods, underscores, hyphens, and forward slashes. + + More Info: [**Resource Location**](https://minecraft.wiki/w/Resource_location) + """) + .since("1.4") + .parser(new Parser<>() { + @Override + public @Nullable NamespacedKey parse(String s, ParseContext context) { + return LuskUtils.getNamespacedKey(s); + } + + @Override + public String toString(NamespacedKey o, int flags) { + return o.toString(); + } + + @Override + public String toVariableNameString(NamespacedKey o) { + return o.toString(); + } + }) + .serializer(new Serializer<>() { + @Override + public @NotNull Fields serialize(NamespacedKey namespacedKey) { + Fields fields = new Fields(); + fields.putObject("key", namespacedKey.toString()); + return fields; + } + + @Override + public void deserialize(NamespacedKey o, Fields f) { + } + + @Override + protected NamespacedKey deserialize(Fields fields) throws StreamCorruptedException { + String key = fields.getObject("key", String.class); + if (key == null) { + throw new StreamCorruptedException("NamespacedKey string is null"); + } + return NamespacedKey.fromString(key); + } + + @Override + public boolean mustSyncDeserialization() { + return true; + } + + @Override + protected boolean canBeInstantiated() { + return false; + } + })); + } + } +} \ No newline at end of file diff --git a/src/main/java/it/jakegblp/lusk/utils/Constants.java b/src/main/java/it/jakegblp/lusk/utils/Constants.java index 67ca2563..1de7821e 100644 --- a/src/main/java/it/jakegblp/lusk/utils/Constants.java +++ b/src/main/java/it/jakegblp/lusk/utils/Constants.java @@ -35,6 +35,8 @@ public class Constants { public static final double EPSILON = 1e-7; + public static final short MAX_NAMESPACED_KEY_LENGTH = isPaper() ? Short.MAX_VALUE : 255; + public static final String[] LUSK_COLORS = new String[]{"&7", "&9"}; public static final Pattern diff --git a/src/main/java/it/jakegblp/lusk/utils/LuskUtils.java b/src/main/java/it/jakegblp/lusk/utils/LuskUtils.java index e2f4fa2c..44a48c7f 100644 --- a/src/main/java/it/jakegblp/lusk/utils/LuskUtils.java +++ b/src/main/java/it/jakegblp/lusk/utils/LuskUtils.java @@ -7,8 +7,10 @@ import net.md_5.bungee.api.ChatColor; import org.bukkit.Bukkit; import org.bukkit.Color; +import org.bukkit.NamespacedKey; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.text.MessageFormat; import java.util.List; @@ -17,6 +19,7 @@ import java.util.stream.IntStream; import static it.jakegblp.lusk.utils.Constants.*; +import static it.jakegblp.lusk.utils.Constants.MAX_NAMESPACED_KEY_LENGTH; public class LuskUtils { @@ -125,4 +128,18 @@ public static ColorRGB getColorAsRGB(@NotNull Color color) { return new ColorRGB(color.getRed(), color.getGreen(), color.getBlue()); } + + @Nullable + public static NamespacedKey getNamespacedKey(@NotNull String key) { + if (key.isEmpty()) return null; + if (!key.contains(":")) key = "minecraft:" + key; + if (key.length() > MAX_NAMESPACED_KEY_LENGTH) { + warning("Namespacedkey {0} with length {1} exceeds the max length of {2}!", key, key.length(), MAX_NAMESPACED_KEY_LENGTH); + return null; + } + key = key.toLowerCase(); + if (key.contains(" ")) key = key.replace(" ", "_"); + return NamespacedKey.fromString(key); + } + } diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 35b96a97..d2993f1c 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -37,6 +37,7 @@ types: ignitecause: ignition cause¦s @an entitysnapshot: entity snapshot¦s @an blockaction: block action¦s @a + namespacedkey: namespaced key¦s @a # -- Rotations -- rotations: