Simplify and slightly optimize EntityClassDescriptor
Could probably also be a record, though I'll look into that at another time
This commit is contained in:
2 files changed
+13
-23
No files matched your search
@@ -244,7 +244,7 @@ public class Entities {
|
|||||||
{
|
{
|
||||||
if (lv.size() > 1) throw new InternalExpressionException("'entity_types' requires one or no arguments");
|
if (lv.size() > 1) throw new InternalExpressionException("'entity_types' requires one or no arguments");
|
||||||
String desc = (lv.size() == 1)?lv.get(0).getString():"*";
|
String desc = (lv.size() == 1)?lv.get(0).getString():"*";
|
||||||
return EntityValue.getEntityDescriptor(desc, ((CarpetContext) c).s.getServer()).listValue;
|
return EntityValue.getEntityDescriptor(desc, ((CarpetContext) c).s.getServer()).listValue();
|
||||||
});
|
});
|
||||||
|
|
||||||
expression.addContextFunction("entity_load_handler", -1, (c, t, lv) ->
|
expression.addContextFunction("entity_load_handler", -1, (c, t, lv) ->
|
||||||
@@ -255,7 +255,7 @@ public class Entities {
|
|||||||
? ((ListValue) entityValue).getItems().stream().map(Value::getString).collect(Collectors.toList())
|
? ((ListValue) entityValue).getItems().stream().map(Value::getString).collect(Collectors.toList())
|
||||||
: Collections.singletonList(entityValue.getString());
|
: Collections.singletonList(entityValue.getString());
|
||||||
Set<EntityType<? extends Entity>> types = new HashSet<>();
|
Set<EntityType<? extends Entity>> types = new HashSet<>();
|
||||||
descriptors.forEach(s -> types.addAll(EntityValue.getEntityDescriptor(s, ((CarpetContext) c).s.getServer()).typeList));
|
descriptors.forEach(s -> types.addAll(EntityValue.getEntityDescriptor(s, ((CarpetContext) c).s.getServer()).types));
|
||||||
FunctionArgument funArg = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
FunctionArgument funArg = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||||
CarpetEventServer events = ((CarpetScriptHost)c.host).scriptServer().events;
|
CarpetEventServer events = ((CarpetScriptHost)c.host).scriptServer().events;
|
||||||
if (funArg.function == null)
|
if (funArg.function == null)
|
||||||
|
|||||||
@@ -283,29 +283,15 @@ public class EntityValue extends Value
|
|||||||
|
|
||||||
public static class EntityClassDescriptor
|
public static class EntityClassDescriptor
|
||||||
{
|
{
|
||||||
public EntityTypeTest<Entity, ? extends Entity> directType; // interface of EntityType
|
public final EntityTypeTest<Entity, ? extends Entity> directType; // interface of EntityType
|
||||||
public Predicate<? super Entity> filteringPredicate;
|
public final Predicate<? super Entity> filteringPredicate;
|
||||||
public List<EntityType<? extends Entity>> typeList;
|
public final List<EntityType<? extends Entity>> types;
|
||||||
public Value listValue;
|
|
||||||
EntityClassDescriptor(EntityType<?> type, Predicate<? super Entity> predicate, List<EntityType<?>> types)
|
|
||||||
{
|
|
||||||
directType = type;
|
|
||||||
filteringPredicate = predicate;
|
|
||||||
typeList = types;
|
|
||||||
listValue = (types==null)?Value.NULL:ListValue.wrap(types.stream().map(et -> StringValue.of(nameFromRegistryId(Registry.ENTITY_TYPE.getKey(et)))).collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityClassDescriptor( EntityTypeTest<Entity, ?> type, Predicate<? super Entity> predicate, List<EntityType<?>> types)
|
EntityClassDescriptor(EntityTypeTest<Entity, ?> type, Predicate<? super Entity> predicate, List<EntityType<?>> types)
|
||||||
{
|
{
|
||||||
directType = type;
|
this.directType = type;
|
||||||
filteringPredicate = predicate;
|
this.filteringPredicate = predicate;
|
||||||
typeList = types;
|
this.types = types;
|
||||||
listValue = (types==null)?Value.NULL:ListValue.wrap(types.stream().map(et -> StringValue.of(nameFromRegistryId(Registry.ENTITY_TYPE.getKey(et)))).collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
|
|
||||||
EntityClassDescriptor(EntityType<?> type, Predicate<? super Entity> predicate, Stream<EntityType<?>> types)
|
|
||||||
{
|
|
||||||
this(type, predicate, types.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityClassDescriptor(EntityTypeTest<Entity, ?> type, Predicate<? super Entity> predicate, Stream<EntityType<?>> types)
|
EntityClassDescriptor(EntityTypeTest<Entity, ?> type, Predicate<? super Entity> predicate, Stream<EntityType<?>> types)
|
||||||
@@ -313,6 +299,10 @@ public class EntityValue extends Value
|
|||||||
this(type, predicate, types.toList());
|
this(type, predicate, types.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Value listValue() {
|
||||||
|
return ListValue.wrap(types.stream().map(et -> StringValue.of(nameFromRegistryId(Registry.ENTITY_TYPE.getKey(et)))));
|
||||||
|
}
|
||||||
|
|
||||||
public final static Map<String, EntityClassDescriptor> byName = new HashMap<>() {{
|
public final static Map<String, EntityClassDescriptor> byName = new HashMap<>() {{
|
||||||
List<EntityType<?>> allTypes = Registry.ENTITY_TYPE.stream().toList();
|
List<EntityType<?>> allTypes = Registry.ENTITY_TYPE.stream().toList();
|
||||||
// nonliving types
|
// nonliving types
|
||||||
|
|||||||
Reference in new issue
Block a user