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:
altrisi committed 2022-10-02 15:29:34 +02:00
1 parent b1c29efa2b
commit 3247b83272
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");
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) ->
@@ -255,7 +255,7 @@ public class Entities {
? ((ListValue) entityValue).getItems().stream().map(Value::getString).collect(Collectors.toList())
: Collections.singletonList(entityValue.getString());
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);
CarpetEventServer events = ((CarpetScriptHost)c.host).scriptServer().events;
if (funArg.function == null)
@@ -283,29 +283,15 @@ public class EntityValue extends Value
public static class EntityClassDescriptor
{
public EntityTypeTest<Entity, ? extends Entity> directType; // interface of EntityType
public Predicate<? super Entity> filteringPredicate;
public List<EntityType<? extends Entity>> typeList;
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()));
}
public final EntityTypeTest<Entity, ? extends Entity> directType; // interface of EntityType
public final Predicate<? super Entity> filteringPredicate;
public final List<EntityType<? extends Entity>> types;
EntityClassDescriptor( EntityTypeTest<Entity, ?> type, Predicate<? super Entity> predicate, List<EntityType<?>> types)
EntityClassDescriptor(EntityTypeTest<Entity, ?> 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(EntityType<?> type, Predicate<? super Entity> predicate, Stream<EntityType<?>> types)
{
this(type, predicate, types.toList());
this.directType = type;
this.filteringPredicate = predicate;
this.types = 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());
}
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<>() {{
List<EntityType<?>> allTypes = Registry.ENTITY_TYPE.stream().toList();
// nonliving types