From 208d07bf47e474614a27ed2acde9532c644fcb26 Mon Sep 17 00:00:00 2001 From: gnembon Date: Thu, 3 Sep 2020 05:04:17 -0300 Subject: [PATCH] added 'exact' to 'trace' in entity API --- docs/scarpet/api/Entities.md | 12 ++++++++---- src/main/java/carpet/script/value/EntityValue.java | 5 +++++ .../java/carpet/script/value/ValueConversions.java | 6 ++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/scarpet/api/Entities.md b/docs/scarpet/api/Entities.md index 46d7cd2b..aee2f2fd 100644 --- a/docs/scarpet/api/Entities.md +++ b/docs/scarpet/api/Entities.md @@ -434,13 +434,17 @@ where entity is looking at. From most prominent (order 0) to opposite (order 5, Returns the result of ray tracing from entity perspective, indicating what it is looking at. Default reach is 4.5 blocks (5 for creative players), and by default it traces for blocks and entities, identical to player attack tracing -action. This can be customized with `options`, use 'blocks' to trace for blocks, 'liquids' to include liquid blocks -as possible results, and 'entities' to trace entities. Any combination of the above is possible. When tracing +action. This can be customized with `options`, use `'blocks'` to trace for blocks, `'liquids'` to include liquid blocks +as possible results, and `'entities'` to trace entities. You can also specify `'exact'` which returns the actual hit + coordinate as a triple, instead of a block or entity value. Any combination of the above is possible. When tracing entities and blocks, blocks will take over the priority even if transparent or non-colliding (aka fighting chickens in tall grass). -Regardless of the options selected, the result could be `null` if nothing is in reach, entity, if look targets an -entity, and block value if block is in reach. +Regardless of the options selected, the result could be: + - `null` if nothing is in reach + - an entity if look targets an entity + - block value if block is in reach, or + - a coordinate triple if `'exact'` option was used and hit was successful. ### `query(e, 'brain', memory)` diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index cc9e9641..4d4fe964 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -552,6 +552,7 @@ public class EntityValue extends Value boolean entities = true; boolean liquids = false; boolean blocks = true; + boolean exact = false; if (a!=null) { @@ -578,6 +579,9 @@ public class EntityValue extends Value blocks = true; else if (what.equalsIgnoreCase("liquids")) liquids = true; + else if (what.equalsIgnoreCase("exact")) + exact = true; + else throw new InternalExpressionException("Incorrect tracing: "+what); } } @@ -597,6 +601,7 @@ public class EntityValue extends Value hitres = Tracer.rayTraceBlocks(e, 1, reach, liquids); if (hitres == null) return Value.NULL; + if (exact && hitres.getType() != HitResult.Type.MISS) return ValueConversions.fromVec(hitres.getPos()); switch (hitres.getType()) { case MISS: return Value.NULL; diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index c192bf15..afcf4a67 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -12,6 +12,7 @@ import net.minecraft.util.Identifier; import net.minecraft.util.dynamic.GlobalPos; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; @@ -29,6 +30,11 @@ public class ValueConversions return ListValue.of(new NumericValue(pos.getX()), new NumericValue(pos.getY()), new NumericValue(pos.getZ())); } + public static Value fromVec(Vec3d vec) + { + return ListValue.of(new NumericValue(vec.x), new NumericValue(vec.y), new NumericValue(vec.z)); + } + public static Value dimName(ServerWorld world) { return ofId(world.getRegistryKey().getValue());