From eabce9c6efd94ab4539fa6b8b915e5ce451b00ba Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 20:04:55 -0700 Subject: [PATCH 01/12] Registry - new registry based ClassInfo/Serializer/Utils --- .../classes/registry/RegistryClassInfo.java | 66 +++++++++ .../classes/registry/RegistrySerializer.java | 78 +++++++++++ .../classes/registry/RegistryUtils.java | 129 ++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java create mode 100644 src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java create mode 100644 src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java new file mode 100644 index 00000000000..822955d1e1d --- /dev/null +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -0,0 +1,66 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.classes.registry; + +import ch.njol.skript.classes.ClassInfo; +import ch.njol.skript.classes.Parser; +import ch.njol.skript.expressions.base.EventValueExpression; +import ch.njol.skript.lang.DefaultExpression; +import ch.njol.skript.lang.ParseContext; +import org.bukkit.Keyed; +import org.bukkit.Registry; +import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.NotNull; + +public class RegistryClassInfo extends ClassInfo { + + public RegistryClassInfo(Class registryClass, Registry registry, String codeName, String languageNode) { + this(registryClass, registry, codeName, languageNode, new EventValueExpression<>(registryClass)); + } + + /** + * @param registry The registry + * @param codeName The name used in patterns + */ + public RegistryClassInfo(Class registryClass, Registry registry, String codeName, String languageNode, DefaultExpression defaultExpression) { + super(registryClass, codeName); + RegistryUtils registryUtils = new RegistryUtils<>(registry, languageNode); + usage(registryUtils.getAllNames()) + .serializer(new RegistrySerializer(registry)) + .defaultExpression(defaultExpression) + .parser(new Parser() { + + @Override + public @Nullable R parse(String string, ParseContext context) { + return registryUtils.parse(string); + } + + @Override + public @NotNull String toString(R object, int flags) { + return registryUtils.toString(object, flags); + } + + @Override + public @NotNull String toVariableNameString(R object) { + return toString(object, 0); + } + }); + } + +} diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java b/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java new file mode 100644 index 00000000000..dc8a7a74bbe --- /dev/null +++ b/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java @@ -0,0 +1,78 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.classes.registry; + +import ch.njol.skript.classes.Serializer; +import ch.njol.yggdrasil.Fields; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; + +import java.io.StreamCorruptedException; + +public class RegistrySerializer extends Serializer { + + private final Registry registry; + + public RegistrySerializer(Registry registry) { + this.registry = registry; + } + + @Override + public Fields serialize(R o) { + Fields fields = new Fields(); + fields.putPrimitive("name", o.getKey().toString()); + return null; + } + + @Override + protected R deserialize(Fields fields) { + try { + String name = fields.getAndRemovePrimitive("name", String.class); + NamespacedKey namespacedKey; + if (!name.contains(":")) { + // Old variables + namespacedKey = NamespacedKey.minecraft(name); + } else { + namespacedKey = NamespacedKey.fromString(name); + } + if (namespacedKey == null) + return null; + + return registry.get(namespacedKey); + } catch (StreamCorruptedException e) { + return null; + } + } + + @Override + public boolean mustSyncDeserialization() { + return false; + } + + @Override + protected boolean canBeInstantiated() { + return false; + } + + @Override + public void deserialize(R o, Fields f) { + } + +} diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java b/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java new file mode 100644 index 00000000000..6630f624f17 --- /dev/null +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java @@ -0,0 +1,129 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.classes.registry; + +import ch.njol.skript.localization.Language; +import ch.njol.skript.localization.Noun; +import ch.njol.util.NonNullPair; +import ch.njol.util.StringUtils; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.eclipse.jdt.annotation.Nullable; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class RegistryUtils { + + private final Registry registry; + private final String languageNode; + + private final Map names = new HashMap<>(); + private final Map parseMap = new HashMap<>(); + + public RegistryUtils(Registry registry, String languageNode) { + assert !languageNode.isEmpty() && !languageNode.endsWith(".") : languageNode; + this.registry = registry; + this.languageNode = languageNode; + refresh(); + Language.addListener(this::refresh); + } + + void refresh() { + names.clear(); + parseMap.clear(); + for (R registryObject : registry) { + NamespacedKey namespacedKey = registryObject.getKey(); + String namespace = namespacedKey.getNamespace(); + String key = namespacedKey.getKey(); + String keyWithSpaces = key.replace("_", " "); + String languageKey = languageNode + "." + key; + + // Put the full namespaced key as a pattern + parseMap.put(namespacedKey.toString(), registryObject); + + // If the object is a vanilla Minecraft object, we'll add the key with spaces as a pattern + if (namespace.equalsIgnoreCase("minecraft")) { + parseMap.put(keyWithSpaces, registryObject); + } + + String[] options = Language.getList(languageKey); + // Missing/Custom registry objects + if (options.length == 1 && options[0].equals(languageKey.toLowerCase(Locale.ENGLISH))) { + if (namespace.equalsIgnoreCase("minecraft")) { + // If the object is a vanilla Minecraft object, we'll use the key with spaces as a name + names.put(registryObject, keyWithSpaces); + } else { + // If the object is a custom object, we'll use the full namespaced key as a name + names.put(registryObject, namespacedKey.toString()); + } + } else { + for (String option : options) { + option = option.toLowerCase(Locale.ENGLISH); + + // Isolate the gender if one is present + NonNullPair strippedOption = Noun.stripGender(option, languageKey); + String first = strippedOption.getFirst(); + Integer second = strippedOption.getSecond(); + + // Add to name map if needed + names.putIfAbsent(registryObject, first); + + parseMap.put(first, registryObject); + if (second != -1) { // There is a gender present + parseMap.put(Noun.getArticleWithSpace(second, Language.F_INDEFINITE_ARTICLE) + first, registryObject); + } + } + } + } + } + + /** + * This method attempts to match the string input against one of the string representations of the enumerators. + * + * @param input a string to attempt to match against one the enumerators. + * @return The enumerator matching the input, or null if no match could be made. + */ + @Nullable + public R parse(String input) { + return parseMap.get(input.toLowerCase(Locale.ENGLISH)); + } + + /** + * This method returns the string representation of an enumerator. + * + * @param object The object to represent as a string. + * @param flags not currently used + * @return A string representation of the enumerator. + */ + public String toString(R object, int flags) { + return names.get(object); + } + + /** + * @return A comma-separated string containing a list of all names representing the registry. + * Note that some entries may represent the same registry object. + */ + public String getAllNames() { + return StringUtils.join(parseMap.keySet(), ", "); + } + +} From 2d9f264faa79b1ef8a84aa34a0e8462b6a8ff412 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Mon, 13 May 2024 20:05:48 -0700 Subject: [PATCH 02/12] BukkitClasses - test registry stuff on biomes --- .../ch/njol/skript/classes/data/BukkitClasses.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index 25000a2390d..e606a369c0e 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -38,6 +38,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.Registry; import org.bukkit.SoundCategory; import org.bukkit.World; import org.bukkit.World.Environment; @@ -90,6 +91,7 @@ import ch.njol.skript.classes.EnumClassInfo; import ch.njol.skript.classes.Parser; import ch.njol.skript.classes.Serializer; +import ch.njol.skript.classes.registry.RegistryClassInfo; import ch.njol.skript.entity.EntityData; import ch.njol.skript.expressions.ExprDamageCause; import ch.njol.skript.expressions.base.EventValueExpression; @@ -976,8 +978,14 @@ public String toVariableNameString(final ItemStack i) { .name(ClassInfo.NO_DOC) .since("2.0") .changer(DefaultChangers.itemChanger)); - - Classes.registerClass(new EnumClassInfo<>(Biome.class, "biome", "biomes") + + ClassInfo biomeClassInfo; + if (Skript.classExists("org.bukkit.Registry") && Skript.fieldExists(Registry.class, "BIOME")) { + biomeClassInfo = new RegistryClassInfo<>(Biome.class, Registry.BIOME, "biome", "biomes"); + } else { + biomeClassInfo = new EnumClassInfo<>(Biome.class, "biome", "biomes"); + } + Classes.registerClass(biomeClassInfo .user("biomes?") .name("Biome") .description("All possible biomes Minecraft uses to generate a world.") From b2317c9ce088eae507c8db036154bd0a9d7ddc9c Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Wed, 15 May 2024 13:39:21 -0700 Subject: [PATCH 03/12] RegistryClassInfo - some suggestions from pickle --- .../ch/njol/skript/classes/data/BukkitClasses.java | 2 +- .../skript/classes/registry/RegistryClassInfo.java | 9 ++++++++- .../skript/classes/registry/RegistrySerializer.java | 6 ++++++ .../njol/skript/classes/registry/RegistryUtils.java | 13 +++++++++---- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index e606a369c0e..ba1a419aad0 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -78,7 +78,6 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.util.CachedServerIcon; import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.Skript; import ch.njol.skript.SkriptConfig; @@ -106,6 +105,7 @@ import ch.njol.util.StringUtils; import ch.njol.yggdrasil.Fields; import io.papermc.paper.world.MoonPhase; +import org.jetbrains.annotations.Nullable; /** * @author Peter Güttinger diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java index 822955d1e1d..07e6cdab7c5 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -25,9 +25,16 @@ import ch.njol.skript.lang.ParseContext; import org.bukkit.Keyed; import org.bukkit.Registry; -import org.eclipse.jdt.annotation.Nullable; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +/** + * This class can be used for an easier writing of ClassInfos that are registries, + * it registers a language node with usage, a serializer, default expression and a parser. + * Making it easier to register registry ClassInfos. + * + * @param The Registry class. + */ public class RegistryClassInfo extends ClassInfo { public RegistryClassInfo(Class registryClass, Registry registry, String codeName, String languageNode) { diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java b/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java index dc8a7a74bbe..6fb094bfc1d 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java @@ -26,6 +26,11 @@ import java.io.StreamCorruptedException; +/** + * Serializer for {@link RegistryClassInfo} + * + * @param Registry class + */ public class RegistrySerializer extends Serializer { private final Registry registry; @@ -73,6 +78,7 @@ protected boolean canBeInstantiated() { @Override public void deserialize(R o, Fields f) { + throw new UnsupportedOperationException(); } } diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java b/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java index 6630f624f17..c02d58f586c 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java @@ -25,12 +25,17 @@ import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.Registry; -import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Locale; import java.util.Map; +/** + * Utility class for {@link Registry} + * + * @param Registry class + */ public class RegistryUtils { private final Registry registry; @@ -47,7 +52,7 @@ public RegistryUtils(Registry registry, String languageNode) { Language.addListener(this::refresh); } - void refresh() { + private void refresh() { names.clear(); parseMap.clear(); for (R registryObject : registry) { @@ -61,14 +66,14 @@ void refresh() { parseMap.put(namespacedKey.toString(), registryObject); // If the object is a vanilla Minecraft object, we'll add the key with spaces as a pattern - if (namespace.equalsIgnoreCase("minecraft")) { + if (namespace.equalsIgnoreCase(NamespacedKey.MINECRAFT)) { parseMap.put(keyWithSpaces, registryObject); } String[] options = Language.getList(languageKey); // Missing/Custom registry objects if (options.length == 1 && options[0].equals(languageKey.toLowerCase(Locale.ENGLISH))) { - if (namespace.equalsIgnoreCase("minecraft")) { + if (namespace.equalsIgnoreCase(NamespacedKey.MINECRAFT)) { // If the object is a vanilla Minecraft object, we'll use the key with spaces as a name names.put(registryObject, keyWithSpaces); } else { From b539c0b38b8a93769928d1cb70384cc7c13314c6 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Wed, 15 May 2024 13:42:41 -0700 Subject: [PATCH 04/12] RegistryUtils - fix some old enum stuff --- .../ch/njol/skript/classes/registry/RegistryUtils.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java b/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java index c02d58f586c..fab2bd9da7a 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java @@ -102,10 +102,10 @@ private void refresh() { } /** - * This method attempts to match the string input against one of the string representations of the enumerators. + * This method attempts to match the string input against one of the string representations of the registry. * - * @param input a string to attempt to match against one the enumerators. - * @return The enumerator matching the input, or null if no match could be made. + * @param input a string to attempt to match against one in the registry. + * @return The registry object matching the input, or null if no match could be made. */ @Nullable public R parse(String input) { @@ -113,11 +113,11 @@ public R parse(String input) { } /** - * This method returns the string representation of an enumerator. + * This method returns the string representation of a registry. * * @param object The object to represent as a string. * @param flags not currently used - * @return A string representation of the enumerator. + * @return A string representation of the registry object. */ public String toString(R object, int flags) { return names.get(object); From 840024019e3b6b62b3161de7607b68560be122d3 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Sat, 25 May 2024 11:33:25 -0700 Subject: [PATCH 05/12] Update src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java Co-authored-by: Patrick Miller --- .../java/ch/njol/skript/classes/registry/RegistrySerializer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java b/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java index 6fb094bfc1d..ddec6c3225f 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistrySerializer.java @@ -59,7 +59,6 @@ protected R deserialize(Fields fields) { } if (namespacedKey == null) return null; - return registry.get(namespacedKey); } catch (StreamCorruptedException e) { return null; From ee61a255291d0718a2dc17a51d069c3269a80e79 Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Sat, 25 May 2024 11:33:33 -0700 Subject: [PATCH 06/12] Update src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java Co-authored-by: Patrick Miller --- .../ch/njol/skript/classes/registry/RegistryClassInfo.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java index 07e6cdab7c5..0dc67fed19d 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -29,9 +29,8 @@ import org.jetbrains.annotations.Nullable; /** - * This class can be used for an easier writing of ClassInfos that are registries, - * it registers a language node with usage, a serializer, default expression and a parser. - * Making it easier to register registry ClassInfos. + * This class can be used for easily creating ClassInfos for {@link Registry}s. + * It registers a language node with usage, a serializer, default expression, and a parser. * * @param The Registry class. */ From ae462375ff711838b620005791e7582603a7b98f Mon Sep 17 00:00:00 2001 From: Shane Bee Date: Sat, 25 May 2024 11:34:42 -0700 Subject: [PATCH 07/12] Update src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java Co-authored-by: Patrick Miller --- .../java/ch/njol/skript/classes/registry/RegistryClassInfo.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java index 0dc67fed19d..f62c93dce76 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -48,6 +48,7 @@ public RegistryClassInfo(Class registryClass, Registry registry, String co super(registryClass, codeName); RegistryUtils registryUtils = new RegistryUtils<>(registry, languageNode); usage(registryUtils.getAllNames()) + .supplier(registry::iterator) .serializer(new RegistrySerializer(registry)) .defaultExpression(defaultExpression) .parser(new Parser() { From ee3558a2536a9138a060b5e3929f80c01f806d4f Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 25 May 2024 11:40:20 -0700 Subject: [PATCH 08/12] Registry - more suggestions --- .../classes/registry/RegistryClassInfo.java | 24 ++----------------- ...RegistryUtils.java => RegistryParser.java} | 21 ++++++++++++---- 2 files changed, 19 insertions(+), 26 deletions(-) rename src/main/java/ch/njol/skript/classes/registry/{RegistryUtils.java => RegistryParser.java} (86%) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java index f62c93dce76..2a14ecca32e 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -19,14 +19,10 @@ package ch.njol.skript.classes.registry; import ch.njol.skript.classes.ClassInfo; -import ch.njol.skript.classes.Parser; import ch.njol.skript.expressions.base.EventValueExpression; import ch.njol.skript.lang.DefaultExpression; -import ch.njol.skript.lang.ParseContext; import org.bukkit.Keyed; import org.bukkit.Registry; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; /** * This class can be used for easily creating ClassInfos for {@link Registry}s. @@ -46,28 +42,12 @@ public RegistryClassInfo(Class registryClass, Registry registry, String co */ public RegistryClassInfo(Class registryClass, Registry registry, String codeName, String languageNode, DefaultExpression defaultExpression) { super(registryClass, codeName); - RegistryUtils registryUtils = new RegistryUtils<>(registry, languageNode); + RegistryParser registryUtils = new RegistryParser<>(registry, languageNode); usage(registryUtils.getAllNames()) .supplier(registry::iterator) .serializer(new RegistrySerializer(registry)) .defaultExpression(defaultExpression) - .parser(new Parser() { - - @Override - public @Nullable R parse(String string, ParseContext context) { - return registryUtils.parse(string); - } - - @Override - public @NotNull String toString(R object, int flags) { - return registryUtils.toString(object, flags); - } - - @Override - public @NotNull String toVariableNameString(R object) { - return toString(object, 0); - } - }); + .parser(new RegistryParser(registry, codeName)); } } diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java similarity index 86% rename from src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java rename to src/main/java/ch/njol/skript/classes/registry/RegistryParser.java index fab2bd9da7a..7b5eed73956 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryUtils.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.classes.registry; +import ch.njol.skript.classes.Parser; import ch.njol.skript.localization.Language; import ch.njol.skript.localization.Noun; import ch.njol.util.NonNullPair; @@ -25,6 +26,7 @@ import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.Registry; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; @@ -32,11 +34,11 @@ import java.util.Map; /** - * Utility class for {@link Registry} + * A parser based on a {@link Registry} used to parse data from a string or turn data into a string. * * @param Registry class */ -public class RegistryUtils { +public class RegistryParser extends Parser { private final Registry registry; private final String languageNode; @@ -44,7 +46,7 @@ public class RegistryUtils { private final Map names = new HashMap<>(); private final Map parseMap = new HashMap<>(); - public RegistryUtils(Registry registry, String languageNode) { + public RegistryParser(Registry registry, String languageNode) { assert !languageNode.isEmpty() && !languageNode.endsWith(".") : languageNode; this.registry = registry; this.languageNode = languageNode; @@ -119,10 +121,21 @@ public R parse(String input) { * @param flags not currently used * @return A string representation of the registry object. */ - public String toString(R object, int flags) { + public @NotNull String toString(R object, int flags) { return names.get(object); } + /** + * Returns a registry object's string representation in a variable name. + * + * @param object Object to represent in a variable name. + * @return The given object's representation in a variable name. + */ + @Override + public @NotNull String toVariableNameString(R object) { + return toString(object, 0); + } + /** * @return A comma-separated string containing a list of all names representing the registry. * Note that some entries may represent the same registry object. From 6a8ae6beddcccd5dca1e6b33d251c564dfea0de9 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 25 May 2024 11:42:30 -0700 Subject: [PATCH 09/12] RegistryClassInfo - small cleanup with parser --- .../ch/njol/skript/classes/registry/RegistryClassInfo.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java index 2a14ecca32e..ff82f966af8 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryClassInfo.java @@ -42,12 +42,12 @@ public RegistryClassInfo(Class registryClass, Registry registry, String co */ public RegistryClassInfo(Class registryClass, Registry registry, String codeName, String languageNode, DefaultExpression defaultExpression) { super(registryClass, codeName); - RegistryParser registryUtils = new RegistryParser<>(registry, languageNode); - usage(registryUtils.getAllNames()) + RegistryParser registryParser = new RegistryParser<>(registry, languageNode); + usage(registryParser.getAllNames()) .supplier(registry::iterator) .serializer(new RegistrySerializer(registry)) .defaultExpression(defaultExpression) - .parser(new RegistryParser(registry, codeName)); + .parser(registryParser); } } From ed66aa85175e7d33f04454ed76edfd6e98555572 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 25 May 2024 11:52:53 -0700 Subject: [PATCH 10/12] RegistryParser - fix parse override --- .../ch/njol/skript/classes/registry/RegistryParser.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java index 7b5eed73956..58e65f6c732 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java @@ -19,6 +19,7 @@ package ch.njol.skript.classes.registry; import ch.njol.skript.classes.Parser; +import ch.njol.skript.lang.ParseContext; import ch.njol.skript.localization.Language; import ch.njol.skript.localization.Noun; import ch.njol.util.NonNullPair; @@ -109,8 +110,8 @@ private void refresh() { * @param input a string to attempt to match against one in the registry. * @return The registry object matching the input, or null if no match could be made. */ - @Nullable - public R parse(String input) { + @Override + public @Nullable R parse(String input, ParseContext context) { return parseMap.get(input.toLowerCase(Locale.ENGLISH)); } @@ -121,6 +122,7 @@ public R parse(String input) { * @param flags not currently used * @return A string representation of the registry object. */ + @Override public @NotNull String toString(R object, int flags) { return names.get(object); } From 8adcb445ad960fed3688d225b144fc0499e376f9 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sat, 25 May 2024 11:54:13 -0700 Subject: [PATCH 11/12] RegistryParser - fix docs --- .../java/ch/njol/skript/classes/registry/RegistryParser.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java index 58e65f6c732..69adc5b491c 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java @@ -108,10 +108,11 @@ private void refresh() { * This method attempts to match the string input against one of the string representations of the registry. * * @param input a string to attempt to match against one in the registry. + * @param context of parsing, may not be null * @return The registry object matching the input, or null if no match could be made. */ @Override - public @Nullable R parse(String input, ParseContext context) { + public @Nullable R parse(String input, @NotNull ParseContext context) { return parseMap.get(input.toLowerCase(Locale.ENGLISH)); } From f045e2f174fdee5ee05b0a6e8e9ad7cb8a4edca4 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sun, 26 May 2024 09:11:26 -0700 Subject: [PATCH 12/12] RegistryParser - sort getAllNames() --- .../ch/njol/skript/classes/registry/RegistryParser.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java index 69adc5b491c..51cb4637434 100644 --- a/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java +++ b/src/main/java/ch/njol/skript/classes/registry/RegistryParser.java @@ -30,7 +30,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; @@ -144,7 +147,9 @@ private void refresh() { * Note that some entries may represent the same registry object. */ public String getAllNames() { - return StringUtils.join(parseMap.keySet(), ", "); + List strings = new ArrayList<>(parseMap.keySet()); + Collections.sort(strings); + return StringUtils.join(strings, ", "); } }