Switched entity lookup off to new system
This commit is contained in:
@@ -5,8 +5,8 @@ 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 com.willfp.eco.util.StringUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Entity;
|
||||
@@ -37,6 +37,11 @@ public final class Entities {
|
||||
*/
|
||||
private static final List<EntityArgParser> ARG_PARSERS = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* The lookup handler.
|
||||
*/
|
||||
private static final EntitiesLookupHandler ENTITIES_LOOKUP_HANDLER = new EntitiesLookupHandler(Entities::doParse);
|
||||
|
||||
/**
|
||||
* Register a new custom item.
|
||||
*
|
||||
@@ -87,20 +92,11 @@ public final class Entities {
|
||||
*/
|
||||
@NotNull
|
||||
public static TestableEntity lookup(@NotNull final String key) {
|
||||
if (key.contains(" ? ")) {
|
||||
String[] options = StringUtils.splitAround(key, "?");
|
||||
for (String option : options) {
|
||||
TestableEntity lookup = lookup(option);
|
||||
if (!(lookup instanceof EmptyTestableEntity)) {
|
||||
return lookup;
|
||||
}
|
||||
}
|
||||
|
||||
return new EmptyTestableEntity();
|
||||
}
|
||||
|
||||
String[] args = StringUtils.parseTokens(key);
|
||||
return LookupHelper.parseWith(key, ENTITIES_LOOKUP_HANDLER);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static TestableEntity doParse(@NotNull final String[] args) {
|
||||
if (args.length == 0) {
|
||||
return new EmptyTestableEntity();
|
||||
}
|
||||
@@ -131,7 +127,6 @@ public final class Entities {
|
||||
entity = part;
|
||||
}
|
||||
|
||||
|
||||
String[] modifierArgs = Arrays.copyOfRange(args, 1, args.length);
|
||||
|
||||
List<EntityArgParseResult> parseResults = new ArrayList<>();
|
||||
@@ -172,7 +167,6 @@ public final class Entities {
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Testable Entity from an ItemStack.
|
||||
* <p>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.willfp.eco.core.entities;
|
||||
|
||||
import com.willfp.eco.core.entities.impl.EmptyTestableEntity;
|
||||
import com.willfp.eco.core.entities.impl.GroupedTestableEntities;
|
||||
import com.willfp.eco.core.lookup.LookupHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Handle item lookup strings.
|
||||
*/
|
||||
public class EntitiesLookupHandler implements LookupHandler<TestableEntity> {
|
||||
/**
|
||||
* The parser.
|
||||
*/
|
||||
private final Function<String[], @NotNull TestableEntity> parser;
|
||||
|
||||
/**
|
||||
* Create new lookup handler.
|
||||
*
|
||||
* @param parser The parser.
|
||||
*/
|
||||
public EntitiesLookupHandler(@NotNull final Function<String[], @NotNull TestableEntity> parser) {
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull TestableEntity parse(@NotNull final String[] args) {
|
||||
return parser.apply(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(@NotNull final TestableEntity object) {
|
||||
return !(object instanceof EmptyTestableEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull TestableEntity getFailsafe() {
|
||||
return new EmptyTestableEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull TestableEntity join(@NotNull final Collection<TestableEntity> options) {
|
||||
return new GroupedTestableEntities(options);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.core.entities;
|
||||
|
||||
import com.willfp.eco.core.lookup.test.Testable;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -8,13 +9,14 @@ import org.jetbrains.annotations.Nullable;
|
||||
/**
|
||||
* An item with a test to see if any item is that item.
|
||||
*/
|
||||
public interface TestableEntity {
|
||||
public interface TestableEntity extends Testable<Entity> {
|
||||
/**
|
||||
* If an Entity matches the test.
|
||||
*
|
||||
* @param entity The entity to test.
|
||||
* @return If the entity matches.
|
||||
*/
|
||||
@Override
|
||||
boolean matches(@Nullable Entity entity);
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.willfp.eco.core.entities.impl;
|
||||
|
||||
import com.willfp.eco.core.entities.TestableEntity;
|
||||
import com.willfp.eco.util.NumberUtils;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* A group of testable entities.
|
||||
*
|
||||
* @see com.willfp.eco.core.entities.CustomEntity
|
||||
*/
|
||||
public class GroupedTestableEntities implements TestableEntity {
|
||||
/**
|
||||
* The children.
|
||||
*/
|
||||
private final Collection<TestableEntity> children;
|
||||
|
||||
/**
|
||||
* Create a new group of testable entities.
|
||||
*
|
||||
* @param children The children.
|
||||
*/
|
||||
public GroupedTestableEntities(@NotNull final Collection<TestableEntity> children) {
|
||||
Validate.isTrue(!children.isEmpty(), "Group must have at least one child!");
|
||||
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the item matches any children.
|
||||
*
|
||||
* @param entity The entity to test.
|
||||
* @return If the entity matches the test of any children.
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(@Nullable final Entity entity) {
|
||||
for (TestableEntity child : children) {
|
||||
if (child.matches(entity)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity spawn(@NotNull final Location location) {
|
||||
return new ArrayList<>(children)
|
||||
.get(NumberUtils.randInt(0, children.size() - 1))
|
||||
.spawn(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the children.
|
||||
*
|
||||
* @return The children.
|
||||
*/
|
||||
public Collection<TestableEntity> getChildren() {
|
||||
return new ArrayList<>(children);
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,11 @@ 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;
|
||||
import com.willfp.eco.core.recipe.parts.TestableStack;
|
||||
import com.willfp.eco.lookup.LookupHelper;
|
||||
import com.willfp.eco.util.NamespacedKeyUtils;
|
||||
import com.willfp.eco.util.NumberUtils;
|
||||
import org.bukkit.Material;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.willfp.eco.core.items;
|
||||
|
||||
import com.willfp.eco.core.lookup.LookupHandler;
|
||||
import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
|
||||
import com.willfp.eco.lookup.LookupHandler;
|
||||
import com.willfp.eco.core.recipe.parts.GroupedTestableItems;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -42,6 +43,6 @@ public class ItemsLookupHandler implements LookupHandler<TestableItem> {
|
||||
|
||||
@Override
|
||||
public @NotNull TestableItem join(@NotNull final Collection<TestableItem> options) {
|
||||
throw new UnsupportedOperationException("Joining not supported!");
|
||||
return new GroupedTestableItems(options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package com.willfp.eco.core.items;
|
||||
|
||||
import com.willfp.eco.core.lookup.test.Testable;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* An item with a test to see if any item is that item.
|
||||
*/
|
||||
public interface TestableItem {
|
||||
public interface TestableItem extends Testable<ItemStack> {
|
||||
/**
|
||||
* If an ItemStack matches the recipe part.
|
||||
*
|
||||
* @param itemStack The item to test.
|
||||
* @return If the item matches.
|
||||
*/
|
||||
@Override
|
||||
boolean matches(@Nullable ItemStack itemStack);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.lookup;
|
||||
package com.willfp.eco.core.lookup;
|
||||
|
||||
import com.willfp.eco.core.lookup.test.Testable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -9,7 +10,7 @@ import java.util.Collection;
|
||||
*
|
||||
* @param <T> The type of testable object, eg {@link com.willfp.eco.core.items.TestableItem}.
|
||||
*/
|
||||
public interface LookupHandler<T> {
|
||||
public interface LookupHandler<T extends Testable<?>> {
|
||||
/**
|
||||
* Parse arguments to an object.
|
||||
*
|
||||
@@ -30,7 +31,7 @@ public interface LookupHandler<T> {
|
||||
/**
|
||||
* Get the failsafe object.
|
||||
* <p>
|
||||
* A failsafe object should never pass {@link this#validate(Object)}.
|
||||
* A failsafe object should never pass {@link this#validate(Testable)}.
|
||||
*
|
||||
* @return The failsafe.
|
||||
*/
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.lookup;
|
||||
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;
|
||||
|
||||
@@ -24,8 +25,8 @@ public final class LookupHelper {
|
||||
* @return The object.
|
||||
*/
|
||||
@NotNull
|
||||
public static <T> T parseWith(@NotNull final String key,
|
||||
@NotNull final LookupHandler<T> handler) {
|
||||
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);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.eco.lookup;
|
||||
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 org.jetbrains.annotations.Nullable;
|
||||
@@ -30,8 +31,8 @@ public abstract class SegmentParser {
|
||||
* @param <T> The object type.
|
||||
* @return The returned object.
|
||||
*/
|
||||
protected abstract <T> T handleSegments(@NotNull String[] segments,
|
||||
@NotNull LookupHandler<T> handler);
|
||||
protected abstract <T extends Testable<?>> T handleSegments(@NotNull String[] segments,
|
||||
@NotNull LookupHandler<T> handler);
|
||||
|
||||
/**
|
||||
* Try parse segments from key.
|
||||
@@ -42,8 +43,8 @@ public abstract class SegmentParser {
|
||||
* @return Null if no segments were found, or the object generated from the segments.
|
||||
*/
|
||||
@Nullable
|
||||
public <T> T parse(@NotNull final String key,
|
||||
@NotNull final LookupHandler<T> handler) {
|
||||
public <T extends Testable<?>> T parse(@NotNull final String key,
|
||||
@NotNull final LookupHandler<T> handler) {
|
||||
if (!key.contains(" " + pattern + " ")) {
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.willfp.eco.core.lookup.test;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Interface for testing if any object matches another object.
|
||||
*
|
||||
* @param <T> The type of object.
|
||||
*/
|
||||
public interface Testable<T> {
|
||||
/**
|
||||
* If object matches the test.
|
||||
*
|
||||
* @param other The other object.
|
||||
* @return If matches.
|
||||
*/
|
||||
boolean matches(@Nullable T other);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.willfp.eco.core.recipe.parts;
|
||||
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* A group of testable items.
|
||||
*
|
||||
* @see com.willfp.eco.core.items.CustomItem
|
||||
*/
|
||||
public class GroupedTestableItems implements TestableItem {
|
||||
/**
|
||||
* The children.
|
||||
*/
|
||||
private final Collection<TestableItem> children;
|
||||
|
||||
/**
|
||||
* Create a new group of testable items.
|
||||
*
|
||||
* @param children The children.
|
||||
*/
|
||||
public GroupedTestableItems(@NotNull final Collection<TestableItem> children) {
|
||||
Validate.isTrue(!children.isEmpty(), "Group must have at least one child!");
|
||||
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the item matches any children.
|
||||
*
|
||||
* @param itemStack The item to test.
|
||||
* @return If the item matches the test of any children..
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(@Nullable final ItemStack itemStack) {
|
||||
for (TestableItem child : children) {
|
||||
if (child.matches(itemStack)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItem() {
|
||||
for (TestableItem child : children) {
|
||||
return child.getItem();
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Empty group of children!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the children.
|
||||
*
|
||||
* @return The children.
|
||||
*/
|
||||
public Collection<TestableItem> getChildren() {
|
||||
return new ArrayList<>(children);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user