tracking and tracing

This commit is contained in:
gnembon committed 2019-07-20 18:53:17 -03:00
1 parent eee23e4ba6
commit 3c96345a60
20 files changed
+404 -128

No files matched your search

@@ -241,6 +241,12 @@ public class BlockValue extends Value
return this != NULL && !getBlockState().isAir();
}
@Override
public String getTypeString()
{
return "block";
}
@Override
public Value clone()
{
@@ -1,6 +1,7 @@
package carpet.script.value;
import carpet.fakes.MobEntityInterface;
import carpet.helpers.Tracer;
import carpet.script.exception.InternalExpressionException;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -30,6 +31,9 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
@@ -124,6 +128,12 @@ public class EntityValue extends Value
return this.get(what, null);
}
@Override
public String getTypeString()
{
return "entity";
}
public static Pair<EntityType<?>, Predicate<? super Entity>> getPredicate(String who)
{
Pair<EntityType<?>, Predicate<? super Entity>> res = entityPredicates.get(who);
@@ -310,20 +320,65 @@ public class EntityValue extends Value
return new StringValue(Direction.getEntityFacingOrder(e)[index].asString());
});
put("trace", (e, a) ->
{
float reach = 4.5f;
boolean entities = true;
boolean liquids = false;
if (a!=null)
{
if (!(a instanceof ListValue))
{
reach = (float) NumericValue.asNumber(a).getDouble();
}
else
{
List<Value> args = ((ListValue) a).getItems();
if (args.size()==0)
throw new InternalExpressionException("trace needs more arguments");
reach = (float) NumericValue.asNumber(args.get(0)).getDouble();
if (args.size() > 1)
{
entities = false;
for (int i = 1; i < args.size(); i++)
{
String what = args.get(i).getString();
if (what.equalsIgnoreCase("entities"))
entities = true;
else if (what.equalsIgnoreCase("blocks")) {}
else if (what.equalsIgnoreCase("liquids"))
liquids = true;
else throw new InternalExpressionException("Incorrect tracing: "+what);
}
}
}
}
else if (e instanceof ServerPlayerEntity && ((ServerPlayerEntity) e).interactionManager.isCreative())
{
reach = 5.0f;
}
HitResult hitres;
if (entities)
hitres = Tracer.rayTrace(e, 1, reach, liquids);
else
hitres = Tracer.rayTraceBlocks(e, 1, reach, liquids);
switch (hitres.getType())
{
case MISS: return Value.NULL;
case BLOCK: return new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() );
case ENTITY: return new EntityValue(((EntityHitResult)hitres).getEntity());
}
return Value.NULL;
});
put("nbt",(e, a) -> {
CompoundTag nbttagcompound = e.toTag((new CompoundTag()));
if (a==null)
return new NBTSerializableValue(nbttagcompound);//StringValue(nbttagcompound.toString());
NbtPathArgumentType.NbtPath path;
try
{
path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(a.getString()));
}
catch (CommandSyntaxException exc)
{
throw new InternalExpressionException("Incorrect path: "+a.getString());
}
String res = null;
NbtPathArgumentType.NbtPath path = NBTSerializableValue.cachePath(a.getString());
try
{
List<Tag> tags = path.get(nbttagcompound);
@@ -331,7 +386,7 @@ public class EntityValue extends Value
return Value.NULL;
if (tags.size()==1)
return NBTSerializableValue.decodeTag(tags.get(0));
return ListValue.wrap(tags.stream().map(t -> NBTSerializableValue.decodeTag(t)).collect(Collectors.toList()));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList()));
}
catch (CommandSyntaxException ignored) { }
return Value.NULL;
@@ -504,9 +559,13 @@ public class EntityValue extends Value
});
put("custom_name", (e, v) -> {
String name = v.getString();
if (name.isEmpty())
if (v instanceof NullValue)
{
e.setCustomNameVisible(false);
e.setCustomName(null);
return;
}
e.setCustomNameVisible(true);
e.setCustomName(new LiteralText(v.getString()));
});
put("dismount", (e, v) -> e.stopRiding() );
@@ -1,7 +1,7 @@
package carpet.script.value;
import carpet.script.exception.ExpressionException;
import carpet.script.value.Value;
import carpet.script.exception.InternalExpressionException;
import java.util.List;
@@ -23,6 +23,12 @@ public class FunctionSignatureValue extends Value
throw new ExpressionException("Function "+identifier+" is not defined yet");
}
@Override
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct.");
}
@Override
public Value clone()
{
@@ -1,6 +1,6 @@
package carpet.script.value;
import carpet.script.value.Value;
import carpet.script.exception.InternalExpressionException;
public class GlobalValue extends Value
{
@@ -21,4 +21,10 @@ public class GlobalValue extends Value
{
return false;
}
@Override
public String getTypeString()
{
throw new InternalExpressionException("How did you get here? Cannot get type of an internal struct.");
}
}
@@ -111,5 +111,10 @@ public abstract class LazyListValue extends ListValue implements Iterator<Value>
{
throw new InternalExpressionException("get element can only be obtained for regular lists");
}
@Override
public String getTypeString()
{
return "iterator";
}
}
@@ -326,6 +326,11 @@ public class ListValue extends Value
index = index % numitems;
return items.get((int)index);
}
@Override
public String getTypeString()
{
return "list";
}
@@ -26,8 +26,6 @@ import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static java.lang.Math.abs;
public class NBTSerializableValue extends Value
{
private String nbtString = null;
@@ -224,31 +222,38 @@ public class NBTSerializableValue extends Value
}
}
@Override
public Value getElementAt(Value value)
private static Map<String, NbtPathArgumentType.NbtPath> pathCache = new HashMap<>();
public static NbtPathArgumentType.NbtPath cachePath(String arg)
{
String pathString = value.getString();
//return Value.NULL;
NbtPathArgumentType.NbtPath path;
NbtPathArgumentType.NbtPath res = pathCache.get(arg);
if (res != null)
return res;
try
{
path = NbtPathArgumentType.nbtPath().method_9362(new StringReader(pathString));
res = NbtPathArgumentType.nbtPath().method_9362(new StringReader(arg));
}
catch (CommandSyntaxException exc)
{
throw new InternalExpressionException("Incorrect nbt path: "+pathString);
throw new InternalExpressionException("Incorrect nbt path: "+arg);
}
String res = null;
if (pathCache.size() > 1024)
pathCache.clear();
pathCache.put(arg, res);
return res;
}
@Override
public Value getElementAt(Value value)
{
NbtPathArgumentType.NbtPath path = cachePath(value.getString());
try
{
List<Tag> tags = path.get(getTag());
if (tags.size()==0)
return Value.NULL;
if (tags.size()==1)
return new NBTSerializableValue(tags.get(0));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::new).collect(Collectors.toList()));
return NBTSerializableValue.decodeTag(tags.get(0));
return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList()));
}
catch (CommandSyntaxException ignored) { }
return Value.NULL;
@@ -259,4 +264,10 @@ public class NBTSerializableValue extends Value
index = index % numitems;
return items.get((int)index); */
}
@Override
public String getTypeString()
{
return "nbt";
}
}
@@ -38,4 +38,10 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
{
return o instanceof NullValue ? 0 : -1;
}
@Override
public String getTypeString()
{
return "null";
}
}
@@ -167,4 +167,10 @@ public class NumericValue extends Value
return getLong();
}
@Override
public String getTypeString()
{
return "number";
}
}
@@ -24,4 +24,10 @@ public class StringValue extends Value
{
this.str = str;
}
@Override
public String getTypeString()
{
return "number";
}
}
@@ -177,4 +177,6 @@ public abstract class Value implements Comparable<Value>, Cloneable
{
throw new InternalExpressionException("get element can only be obtained for lists and nbt values");
}
public abstract String getTypeString();
}