Merge branch 'master' into 1.16
This commit is contained in:
commit
33651de9cd
3 files changed
+191
-1
No files matched your search
@@ -13,6 +13,8 @@ import carpet.script.argument.Vector3Argument;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.entity.ai.brain.MemoryModuleType;
|
||||
import net.minecraft.entity.ai.pathing.Path;
|
||||
import net.minecraft.nbt.StringTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.packet.s2c.play.EntityPassengersSetS2CPacket;
|
||||
@@ -60,6 +62,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
@@ -200,7 +203,14 @@ public class EntityValue extends Value
|
||||
{
|
||||
if (!(featureAccessors.containsKey(what)))
|
||||
throw new InternalExpressionException("Unknown entity feature: "+what);
|
||||
return featureAccessors.get(what).apply(entity, arg);
|
||||
try
|
||||
{
|
||||
return featureAccessors.get(what).apply(entity, arg);
|
||||
}
|
||||
catch (NullPointerException npe)
|
||||
{
|
||||
throw new InternalExpressionException("Cannot fetch '"+what+"' with these arguments");
|
||||
}
|
||||
}
|
||||
private static final Map<String, EquipmentSlot> inventorySlots = new HashMap<String, EquipmentSlot>(){{
|
||||
put("mainhand", EquipmentSlot.MAINHAND);
|
||||
@@ -210,6 +220,7 @@ public class EntityValue extends Value
|
||||
put("legs", EquipmentSlot.LEGS);
|
||||
put("feet", EquipmentSlot.FEET);
|
||||
}};
|
||||
|
||||
private static final Map<String, BiFunction<Entity, Value, Value>> featureAccessors = new HashMap<String, BiFunction<Entity, Value, Value>>() {{
|
||||
//put("test", (e, a) -> a == null ? Value.NULL : new StringValue(a.getString()));
|
||||
put("removed", (entity, arg) -> new NumericValue(entity.removed));
|
||||
@@ -321,6 +332,30 @@ public class EntityValue extends Value
|
||||
}
|
||||
return Value.NULL;
|
||||
});
|
||||
|
||||
put("path", (e, a) -> {
|
||||
if (e instanceof MobEntity)
|
||||
{
|
||||
Path path = ((MobEntity)e).getNavigation().getCurrentPath();
|
||||
if (path == null) return Value.NULL;
|
||||
return ValueConversions.fromPath((ServerWorld)e.getEntityWorld(), path);
|
||||
}
|
||||
return Value.NULL;
|
||||
});
|
||||
|
||||
put("brain", (e, a) -> {
|
||||
String module = a.getString();
|
||||
MemoryModuleType<?> moduleType = Registry.MEMORY_MODULE_TYPE.get(new Identifier(module));
|
||||
if (moduleType == MemoryModuleType.DUMMY) return Value.NULL;
|
||||
if (e instanceof LivingEntity)
|
||||
{
|
||||
LivingEntity livingEntity = (LivingEntity)e;
|
||||
Optional<?> memory = livingEntity.getBrain().getOptionalMemory(moduleType);
|
||||
if (memory==null || !memory.isPresent()) return Value.NULL;
|
||||
return ValueConversions.fromEntityMemory(e, memory.get());
|
||||
}
|
||||
return Value.NULL;
|
||||
});
|
||||
put("gamemode_id", (e, a) -> {
|
||||
if (e instanceof ServerPlayerEntity)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.ai.brain.LookTarget;
|
||||
import net.minecraft.entity.ai.brain.WalkTarget;
|
||||
import net.minecraft.entity.ai.pathing.Path;
|
||||
import net.minecraft.entity.ai.pathing.PathNode;
|
||||
import net.minecraft.entity.damage.DamageSource;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.GlobalPos;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.Timestamp;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.dimension.DimensionType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ValueConversions
|
||||
{
|
||||
public static Value fromPos(BlockPos pos)
|
||||
{
|
||||
return ListValue.of(new NumericValue(pos.getX()), new NumericValue(pos.getY()), new NumericValue(pos.getZ()));
|
||||
}
|
||||
|
||||
public static Value dimName(ServerWorld world)
|
||||
{
|
||||
return ofId(Registry.DIMENSION_TYPE.getId(world.getDimension().getType()));
|
||||
}
|
||||
|
||||
public static Value dimName(DimensionType dim)
|
||||
{
|
||||
return ofId(Registry.DIMENSION_TYPE.getId(dim));
|
||||
}
|
||||
|
||||
public static Value ofId(Identifier id)
|
||||
{
|
||||
if (id == null) // should be Value.NULL
|
||||
return Value.NULL;
|
||||
if (id.getNamespace().equals("minecraft"))
|
||||
return new StringValue(id.getPath());
|
||||
return new StringValue(id.toString());
|
||||
}
|
||||
public static Value ofGlobalPos(GlobalPos pos)
|
||||
{
|
||||
return ListValue.of(
|
||||
ValueConversions.dimName(pos.getDimension()),
|
||||
ValueConversions.fromPos(pos.getPos())
|
||||
);
|
||||
}
|
||||
|
||||
public static Value fromPath(ServerWorld world, Path path)
|
||||
{
|
||||
List<Value> nodes = new ArrayList<>();
|
||||
for (PathNode node: path.getNodes())
|
||||
{
|
||||
nodes.add( ListValue.of(
|
||||
new BlockValue(null, world, node.getPos()),
|
||||
new StringValue(node.type.name().toLowerCase(Locale.ROOT)),
|
||||
new NumericValue(node.penalty),
|
||||
new NumericValue(node.visited)
|
||||
));
|
||||
}
|
||||
return ListValue.wrap(nodes);
|
||||
}
|
||||
|
||||
public static Value fromEntityMemory(Entity e, Object v)
|
||||
{
|
||||
if (v instanceof GlobalPos)
|
||||
{
|
||||
GlobalPos pos = (GlobalPos)v;
|
||||
return ofGlobalPos(pos);
|
||||
};
|
||||
if (v instanceof Entity)
|
||||
{
|
||||
return new EntityValue((Entity)v);
|
||||
};
|
||||
if (v instanceof BlockPos)
|
||||
{
|
||||
return new BlockValue(null, (ServerWorld) e.getEntityWorld(), (BlockPos) v);
|
||||
};
|
||||
if (v instanceof Timestamp)
|
||||
{
|
||||
return new NumericValue(((Timestamp) v).getTime());
|
||||
}
|
||||
if (v instanceof Long)
|
||||
{
|
||||
return new NumericValue((Long)v);
|
||||
}
|
||||
if (v instanceof DamageSource)
|
||||
{
|
||||
DamageSource source = (DamageSource) v;
|
||||
return ListValue.of(
|
||||
new StringValue(source.getName()),
|
||||
source.getAttacker()==null?Value.NULL:new EntityValue(source.getAttacker())
|
||||
);
|
||||
}
|
||||
if (v instanceof Path)
|
||||
{
|
||||
return fromPath((ServerWorld)e.getEntityWorld(), (Path)v);
|
||||
}
|
||||
if (v instanceof LookTarget)
|
||||
{
|
||||
return new BlockValue(null, (ServerWorld) e.getEntityWorld(), ((LookTarget)v).getBlockPos());
|
||||
}
|
||||
if (v instanceof WalkTarget)
|
||||
{
|
||||
return ListValue.of(
|
||||
new BlockValue(null, (ServerWorld) e.getEntityWorld(), ((LookTarget)v).getBlockPos()),
|
||||
new NumericValue(((WalkTarget) v).getSpeed()),
|
||||
new NumericValue(((WalkTarget) v).getCompletionRange())
|
||||
);
|
||||
}
|
||||
if (v instanceof Set)
|
||||
{
|
||||
v = new ArrayList(((Set) v));
|
||||
}
|
||||
if (v instanceof List)
|
||||
{
|
||||
List l = (List)v;
|
||||
if (l.isEmpty()) return ListValue.of();
|
||||
Object el = l.get(0);
|
||||
if (el instanceof Entity)
|
||||
{
|
||||
return ListValue.wrap((List<Value>) l.stream().map(o -> new EntityValue((Entity)o)).collect(Collectors.toList()));
|
||||
}
|
||||
if (el instanceof GlobalPos)
|
||||
{
|
||||
return ListValue.wrap((List<Value>) l.stream().map(o -> ofGlobalPos((GlobalPos) o)).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
return Value.NULL;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user