Lookup cleanup
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 <T> The type of testable object, eg {@link com.willfp.eco.core.items.TestableItem}.
|
||||
*/
|
||||
public interface LookupHandler<T extends Testable<?>> {
|
||||
/**
|
||||
* Parse lookup string completely.
|
||||
* <p>
|
||||
* 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.
|
||||
*
|
||||
|
||||
@@ -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<SegmentParser> SEGMENT_PARSERS = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Parse key to object.
|
||||
*
|
||||
* @param key The key.
|
||||
* @param handler The handler.
|
||||
* @param <T> The object type.
|
||||
* @return The object.
|
||||
*/
|
||||
@NotNull
|
||||
public static <T extends Testable<?>> T parseWith(@NotNull final String key,
|
||||
@NotNull final LookupHandler<T> 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");
|
||||
}
|
||||
}
|
||||
@@ -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<SegmentParser> 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 <T> The object type.
|
||||
* @return The returned object.
|
||||
* @return The parser.
|
||||
*/
|
||||
protected abstract <T extends Testable<?>> T handleSegments(@NotNull String[] segments,
|
||||
@NotNull LookupHandler<T> 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 <T> The object type.
|
||||
* @return The returned object.
|
||||
*/
|
||||
protected abstract <T extends Testable<?>> T handleSegments(@NotNull String[] segments,
|
||||
@NotNull LookupHandler<T> handler);
|
||||
|
||||
/**
|
||||
* Get all segment parsers.
|
||||
*
|
||||
* @return All parsers.
|
||||
*/
|
||||
public static Collection<SegmentParser> values() {
|
||||
return new ArrayList<>(REGISTERED);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user