diff --git a/eco-api/src/main/java/com/willfp/eco/core/entities/Entities.java b/eco-api/src/main/java/com/willfp/eco/core/entities/Entities.java index 1ba38490..28336bba 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/entities/Entities.java +++ b/eco-api/src/main/java/com/willfp/eco/core/entities/Entities.java @@ -5,7 +5,6 @@ import com.willfp.eco.core.entities.args.EntityArgParser; import com.willfp.eco.core.entities.impl.EmptyTestableEntity; import com.willfp.eco.core.entities.impl.ModifiedTestableEntity; import com.willfp.eco.core.entities.impl.SimpleTestableEntity; -import com.willfp.eco.core.lookup.LookupHelper; import com.willfp.eco.util.NamespacedKeyUtils; import org.bukkit.Location; import org.bukkit.NamespacedKey; @@ -92,7 +91,7 @@ public final class Entities { */ @NotNull public static TestableEntity lookup(@NotNull final String key) { - return LookupHelper.parseWith(key, ENTITIES_LOOKUP_HANDLER); + return ENTITIES_LOOKUP_HANDLER.parseKey(key); } @NotNull diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java index 2851ef6b..6671af61 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java @@ -4,7 +4,6 @@ import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import com.willfp.eco.core.items.args.LookupArgParser; import com.willfp.eco.core.items.provider.ItemProvider; -import com.willfp.eco.core.lookup.LookupHelper; import com.willfp.eco.core.recipe.parts.EmptyTestableItem; import com.willfp.eco.core.recipe.parts.MaterialTestableItem; import com.willfp.eco.core.recipe.parts.ModifiedTestableItem; @@ -136,7 +135,7 @@ public final class Items { */ @NotNull public static TestableItem lookup(@NotNull final String key) { - return LookupHelper.parseWith(key, ITEMS_LOOKUP_HANDLER); + return ITEMS_LOOKUP_HANDLER.parseKey(key); } @NotNull diff --git a/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHandler.java b/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHandler.java index 7b49722b..d9759b03 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHandler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHandler.java @@ -1,6 +1,7 @@ package com.willfp.eco.core.lookup; import com.willfp.eco.core.lookup.test.Testable; +import com.willfp.eco.util.StringUtils; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -11,6 +12,31 @@ import java.util.Collection; * @param The type of testable object, eg {@link com.willfp.eco.core.items.TestableItem}. */ public interface LookupHandler> { + /** + * Parse lookup string completely. + *

+ * You shouldn't override this method unless you're doing something + * technically interesting or weird. This is the entry point for all + * lookup parsers, {@link this#parse(String[])} is to specify implementation-specific + * parsing. + * + * @param key The key. + * @return The object. + */ + default T parseKey(@NotNull String key) { + for (SegmentParser parser : SegmentParser.values()) { + T generated = parser.parse(key, this); + + if (generated != null) { + return generated; + } + } + + String[] args = StringUtils.parseTokens(key); + + return this.parse(args); + } + /** * Parse arguments to an object. * diff --git a/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHelper.java b/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHelper.java deleted file mode 100644 index ec0766cd..00000000 --- a/eco-api/src/main/java/com/willfp/eco/core/lookup/LookupHelper.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.willfp.eco.core.lookup; - -import com.willfp.eco.core.lookup.test.Testable; -import com.willfp.eco.util.StringUtils; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; - -/** - * Helper for lookups. - */ -public final class LookupHelper { - /** - * All segment parsers. - */ - private static final List SEGMENT_PARSERS = new ArrayList<>(); - - /** - * Parse key to object. - * - * @param key The key. - * @param handler The handler. - * @param The object type. - * @return The object. - */ - @NotNull - public static > T parseWith(@NotNull final String key, - @NotNull final LookupHandler handler) { - for (SegmentParser parser : SEGMENT_PARSERS) { - T generated = parser.parse(key, handler); - - if (generated != null) { - return generated; - } - } - - String[] args = StringUtils.parseTokens(key); - - return handler.parse(args); - } - - /** - * Register segment parser. - * - * @param parser The parser. - */ - public static void registerSegmentParser(@NotNull final SegmentParser parser) { - SEGMENT_PARSERS.add(parser); - } - - private LookupHelper() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); - } -} diff --git a/eco-api/src/main/java/com/willfp/eco/core/lookup/SegmentParser.java b/eco-api/src/main/java/com/willfp/eco/core/lookup/SegmentParser.java index c0de3e0f..61b4ab4a 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/lookup/SegmentParser.java +++ b/eco-api/src/main/java/com/willfp/eco/core/lookup/SegmentParser.java @@ -5,10 +5,19 @@ import com.willfp.eco.util.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + /** * Parse a key into segments. */ public abstract class SegmentParser { + /** + * All segment parsers. + */ + private static final List REGISTERED = new ArrayList<>(); + /** * The pattern to split keys on. */ @@ -24,15 +33,14 @@ public abstract class SegmentParser { } /** - * Handle segments from key. + * Register the parser. * - * @param segments The key segments. - * @param handler The handler. - * @param The object type. - * @return The returned object. + * @return The parser. */ - protected abstract > T handleSegments(@NotNull String[] segments, - @NotNull LookupHandler handler); + public SegmentParser register() { + REGISTERED.add(this); + return this; + } /** * Try parse segments from key. @@ -53,4 +61,24 @@ public abstract class SegmentParser { return handleSegments(segments, handler); } + + /** + * Handle segments from key. + * + * @param segments The key segments. + * @param handler The handler. + * @param The object type. + * @return The returned object. + */ + protected abstract > T handleSegments(@NotNull String[] segments, + @NotNull LookupHandler handler); + + /** + * Get all segment parsers. + * + * @return All parsers. + */ + public static Collection values() { + return new ArrayList<>(REGISTERED); + } } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserGroup.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserGroup.kt index 2256ab52..0057279e 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserGroup.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserGroup.kt @@ -1,7 +1,6 @@ package com.willfp.eco.internal.lookup import com.willfp.eco.core.lookup.LookupHandler -import com.willfp.eco.core.lookup.LookupHelper import com.willfp.eco.core.lookup.SegmentParser import com.willfp.eco.core.lookup.test.Testable @@ -10,7 +9,7 @@ class SegmentParserGroup : SegmentParser("||") { val possibleOptions = mutableListOf() for (option in segments) { - val lookup = LookupHelper.parseWith(option, handler) + val lookup = handler.parseKey(option) if (handler.validate(lookup)) { possibleOptions.add(lookup) } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserUseIfPresent.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserUseIfPresent.kt index 4b9460eb..10ad28a6 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserUseIfPresent.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/lookup/SegmentParserUseIfPresent.kt @@ -1,14 +1,13 @@ package com.willfp.eco.internal.lookup import com.willfp.eco.core.lookup.LookupHandler -import com.willfp.eco.core.lookup.LookupHelper import com.willfp.eco.core.lookup.SegmentParser import com.willfp.eco.core.lookup.test.Testable class SegmentParserUseIfPresent : SegmentParser("?") { override fun > handleSegments(segments: Array, handler: LookupHandler): T { for (option in segments) { - val lookup = LookupHelper.parseWith(option, handler) + val lookup = handler.parseKey(option) if (handler.validate(lookup)) { return lookup } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt index 5cc85a97..e4edb715 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt @@ -17,7 +17,6 @@ import com.willfp.eco.core.integrations.hologram.HologramManager import com.willfp.eco.core.integrations.mcmmo.McmmoManager import com.willfp.eco.core.integrations.shop.ShopManager import com.willfp.eco.core.items.Items -import com.willfp.eco.core.lookup.LookupHelper import com.willfp.eco.internal.display.EcoDisplayHandler import com.willfp.eco.internal.drops.DropManager import com.willfp.eco.internal.entities.EntityArgParserAdult @@ -158,13 +157,13 @@ abstract class EcoSpigotPlugin : EcoPlugin() { Entities.registerArgParser(EntityArgParserSilent()) Entities.registerArgParser(EntityArgParserEquipment()) - LookupHelper.registerSegmentParser(SegmentParserGroup()) - LookupHelper.registerSegmentParser(SegmentParserUseIfPresent()) - ShapedRecipeListener.registerListener(ComplexInComplex()) ShapedRecipeListener.registerListener(ComplexInEco()) ShapedRecipeListener.registerListener(ComplexInVanilla()) + SegmentParserGroup().register() + SegmentParserUseIfPresent().register() + val skullProxy = getProxy(SkullProxy::class.java) SkullUtils.initialize( { meta, base64 -> skullProxy.setSkullTexture(meta, base64) },