less restricting use of shapes

This commit is contained in:
gnembon committed 2021-04-12 07:57:04 -03:00
1 parent cd9a5b96c2
commit 92cc545896
2 files changed
+31 -28

No files matched your search

@@ -331,6 +331,8 @@ public class Auxiliary {
expression.addLazyFunction("draw_shape", -1, (c, t, lv) -> expression.addLazyFunction("draw_shape", -1, (c, t, lv) ->
{ {
CarpetContext cc = (CarpetContext)c; CarpetContext cc = (CarpetContext)c;
ServerWorld world = cc.s.getWorld();
MinecraftServer server = world.getServer();
ServerPlayerEntity player[] = {null}; ServerPlayerEntity player[] = {null};
List<Pair<ShapeDispatcher.ExpiringShape, Map<String,Value>>> shapes = new ArrayList<>(); List<Pair<ShapeDispatcher.ExpiringShape, Map<String,Value>>> shapes = new ArrayList<>();
if (lv.size() == 1) // bulk if (lv.size() == 1) // bulk
@@ -340,14 +342,14 @@ public class Auxiliary {
for (Value list : ((ListValue) specLoad).getItems()) for (Value list : ((ListValue) specLoad).getItems())
{ {
if (!(list instanceof ListValue)) throw new InternalExpressionException("In bulk mode - shapes need to be provided as a list of shape specs"); if (!(list instanceof ListValue)) throw new InternalExpressionException("In bulk mode - shapes need to be provided as a list of shape specs");
shapes.add( ShapeDispatcher.fromFunctionArgs(cc, ((ListValue) list).getItems(), player)); shapes.add( ShapeDispatcher.fromFunctionArgs(server, world, ((ListValue) list).getItems(), player));
} }
} }
else else
{ {
List<Value> params = new ArrayList<>(); List<Value> params = new ArrayList<>();
for (LazyValue v : lv) params.add(v.evalValue(c)); for (LazyValue v : lv) params.add(v.evalValue(c));
shapes.add(ShapeDispatcher.fromFunctionArgs(cc, params, player)); shapes.add(ShapeDispatcher.fromFunctionArgs(server, world, params, player));
} }
ShapeDispatcher.sendShape( ShapeDispatcher.sendShape(
@@ -31,6 +31,7 @@ import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag; import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag; import net.minecraft.nbt.Tag;
import net.minecraft.particle.ParticleEffect; import net.minecraft.particle.ParticleEffect;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
@@ -62,7 +63,7 @@ public class ShapeDispatcher
private static final Map<String, ParticleEffect> particleCache = new HashMap<>(); private static final Map<String, ParticleEffect> particleCache = new HashMap<>();
public static Pair<ExpiringShape,Map<String, Value>> fromFunctionArgs( public static Pair<ExpiringShape,Map<String, Value>> fromFunctionArgs(
CarpetContext cc, MinecraftServer server, ServerWorld world,
List<Value> lv, List<Value> lv,
ServerPlayerEntity[] playerRef ServerPlayerEntity[] playerRef
) )
@@ -91,18 +92,18 @@ public class ShapeDispatcher
for (int i=2; i < lv.size(); i++) paramList.add(lv.get(i)); for (int i=2; i < lv.size(); i++) paramList.add(lv.get(i));
params = ShapeDispatcher.parseParams(paramList); params = ShapeDispatcher.parseParams(paramList);
} }
params.putIfAbsent("dim", new StringValue(cc.s.getWorld().getRegistryKey().getValue().toString())); params.putIfAbsent("dim", new StringValue(world.getRegistryKey().getValue().toString()));
params.putIfAbsent("duration", duration); params.putIfAbsent("duration", duration);
if (params.containsKey("player")) if (params.containsKey("player"))
{ {
ServerPlayerEntity player = EntityValue.getPlayerByValue(cc.s.getMinecraftServer(), params.get("player")); ServerPlayerEntity player = EntityValue.getPlayerByValue(server, params.get("player"));
if (player == null) if (player == null)
throw new InternalExpressionException("'player' parameter needs to represent an existing player"); throw new InternalExpressionException("'player' parameter needs to represent an existing player");
params.remove("player"); params.remove("player");
playerRef[0] = player; playerRef[0] = player;
} }
return Pair.of(ShapeDispatcher.create(cc, shapeType, params), params); return Pair.of(ShapeDispatcher.create(server, shapeType, params), params);
} }
public static void sendShape(List<ServerPlayerEntity> players, List<Pair<ExpiringShape,Map<String, Value>>> shapes) public static void sendShape(List<ServerPlayerEntity> players, List<Pair<ExpiringShape,Map<String, Value>>> shapes)
@@ -180,13 +181,13 @@ public class ShapeDispatcher
return param; return param;
} }
public static ExpiringShape create(CarpetContext cc, String shapeType, Map<String, Value> userParams) public static ExpiringShape create(MinecraftServer server, String shapeType, Map<String, Value> userParams)
{ {
userParams.put("shape", new StringValue(shapeType)); userParams.put("shape", new StringValue(shapeType));
userParams.keySet().forEach(key -> { userParams.keySet().forEach(key -> {
Param param = Param.of.get(key); Param param = Param.of.get(key);
if (param==null) throw new InternalExpressionException("Unknown feature for shape: "+key); if (param==null) throw new InternalExpressionException("Unknown feature for shape: "+key);
userParams.put(key, param.validate(userParams, cc, userParams.get(key))); userParams.put(key, param.validate(userParams, server, userParams.get(key)));
}); });
Function<Map<String, Value>,ExpiringShape> factory = ExpiringShape.shapeProviders.get(shapeType); Function<Map<String, Value>,ExpiringShape> factory = ExpiringShape.shapeProviders.get(shapeType);
if (factory == null) throw new InternalExpressionException("Unknown shape: "+shapeType); if (factory == null) throw new InternalExpressionException("Unknown shape: "+shapeType);
@@ -891,7 +892,7 @@ public class ShapeDispatcher
} }
public abstract Tag toTag(Value value); //validates value, returning null if not necessary to keep it and serialize public abstract Tag toTag(Value value); //validates value, returning null if not necessary to keep it and serialize
public abstract Value validate(Map<String, Value> options, CarpetContext cc, Value value); // makes sure the value is proper public abstract Value validate(Map<String, Value> options, MinecraftServer server, Value value); // makes sure the value is proper
public abstract Value decode(Tag tag); public abstract Value decode(Tag tag);
} }
@@ -910,7 +911,7 @@ public class ShapeDispatcher
super(id); super(id);
} }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
return value; return value;
} }
@@ -923,7 +924,7 @@ public class ShapeDispatcher
super(id); super(id);
} }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
if (!(value instanceof FormattedTextValue)) if (!(value instanceof FormattedTextValue))
value = new FormattedTextValue(new LiteralText(value.getString())); value = new FormattedTextValue(new LiteralText(value.getString()));
@@ -957,7 +958,7 @@ public class ShapeDispatcher
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
if (this.options.contains(value.getString())) return value; if (this.options.contains(value.getString())) return value;
return null; return null;
@@ -969,14 +970,14 @@ public class ShapeDispatcher
protected DimensionParam() { super("dim"); } protected DimensionParam() { super("dim"); }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) { return value; } public Value validate(Map<String, Value> options, MinecraftServer server, Value value) { return value; }
} }
public static class ShapeParam extends StringParam public static class ShapeParam extends StringParam
{ {
protected ShapeParam() { super("shape"); } protected ShapeParam() { super("shape"); }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
String shape = value.getString(); String shape = value.getString();
if (!ExpiringShape.shapeProviders.containsKey(shape)) if (!ExpiringShape.shapeProviders.containsKey(shape))
@@ -989,7 +990,7 @@ public class ShapeDispatcher
protected NumericParam(String id) { super(id); } protected NumericParam(String id) { super(id); }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
if (!(value instanceof NumericValue)) if (!(value instanceof NumericValue))
throw new InternalExpressionException("'" + id + "' needs to be a number"); throw new InternalExpressionException("'" + id + "' needs to be a number");
@@ -1027,9 +1028,9 @@ public class ShapeDispatcher
public static abstract class PositiveParam extends NumericParam public static abstract class PositiveParam extends NumericParam
{ {
protected PositiveParam(String id) { super(id); } protected PositiveParam(String id) { super(id); }
@Override public Value validate(Map<String, Value> options, CarpetContext cc, Value value) @Override public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
Value ret = super.validate(options, cc, value); Value ret = super.validate(options, server, value);
if (((NumericValue)ret).getDouble()<=0) throw new InternalExpressionException("'"+id+"' should be positive"); if (((NumericValue)ret).getDouble()<=0) throw new InternalExpressionException("'"+id+"' should be positive");
return ret; return ret;
} }
@@ -1059,9 +1060,9 @@ public class ShapeDispatcher
public Value decode(Tag tag) { return new NumericValue(((IntTag)tag).getInt()); } public Value decode(Tag tag) { return new NumericValue(((IntTag)tag).getInt()); }
@Override @Override
public Tag toTag(Value value) { return IntTag.of(NumericValue.asNumber(value, id).getInt()); } public Tag toTag(Value value) { return IntTag.of(NumericValue.asNumber(value, id).getInt()); }
@Override public Value validate(Map<String, Value> options, CarpetContext cc, Value value) @Override public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
Value ret = super.validate(options, cc, value); Value ret = super.validate(options, server, value);
if (((NumericValue)ret).getDouble()<0) throw new InternalExpressionException("'"+id+"' should be non-negative"); if (((NumericValue)ret).getDouble()<0) throw new InternalExpressionException("'"+id+"' should be non-negative");
return ret; return ret;
} }
@@ -1073,9 +1074,9 @@ public class ShapeDispatcher
public Value decode(Tag tag) { return new NumericValue(((FloatTag)tag).getFloat()); } public Value decode(Tag tag) { return new NumericValue(((FloatTag)tag).getFloat()); }
@Override @Override
public Tag toTag(Value value) { return FloatTag.of(NumericValue.asNumber(value, id).getFloat()); } public Tag toTag(Value value) { return FloatTag.of(NumericValue.asNumber(value, id).getFloat()); }
@Override public Value validate(Map<String, Value> options, CarpetContext cc, Value value) @Override public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
Value ret = super.validate(options, cc, value); Value ret = super.validate(options, server, value);
if (((NumericValue)ret).getDouble()<0) throw new InternalExpressionException("'"+id+"' should be non-negative"); if (((NumericValue)ret).getDouble()<0) throw new InternalExpressionException("'"+id+"' should be non-negative");
return ret; return ret;
} }
@@ -1093,11 +1094,11 @@ public class ShapeDispatcher
roundsUpForBlocks = doesRoundUpForBlocks; roundsUpForBlocks = doesRoundUpForBlocks;
} }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
return validate(this, options, cc, value, roundsUpForBlocks); return validate(this, options, value, roundsUpForBlocks);
} }
public static Value validate(Param p, Map<String, Value> options, CarpetContext cc, Value value, boolean roundsUp) public static Value validate(Param p, Map<String, Value> options, Value value, boolean roundsUp)
{ {
if (value instanceof BlockValue) if (value instanceof BlockValue)
{ {
@@ -1165,13 +1166,13 @@ public class ShapeDispatcher
super(id); super(id);
} }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
if (!(value instanceof ListValue)) if (!(value instanceof ListValue))
throw new InternalExpressionException(id+ " parameter should be a list"); throw new InternalExpressionException(id+ " parameter should be a list");
List<Value> points = new ArrayList<>(); List<Value> points = new ArrayList<>();
for (Value point: ((ListValue) value).getItems()) for (Value point: ((ListValue) value).getItems())
points.add(Vec3Param.validate(this, options, cc, point, false)); points.add(Vec3Param.validate(this, options, point, false));
return ListValue.wrap(points); return ListValue.wrap(points);
} }
@@ -1233,10 +1234,10 @@ public class ShapeDispatcher
} }
@Override @Override
public Value validate(Map<String, Value> options, CarpetContext cc, Value value) public Value validate(Map<String, Value> options, MinecraftServer server, Value value)
{ {
if (value instanceof EntityValue) return new NumericValue(((EntityValue) value).getEntity().getEntityId()); if (value instanceof EntityValue) return new NumericValue(((EntityValue) value).getEntity().getEntityId());
ServerPlayerEntity player = EntityValue.getPlayerByValue(cc.s.getMinecraftServer(), value); ServerPlayerEntity player = EntityValue.getPlayerByValue(server, value);
if (player == null) if (player == null)
throw new InternalExpressionException(id+" parameter needs to represent an entity or player"); throw new InternalExpressionException(id+" parameter needs to represent an entity or player");
return new NumericValue(player.getEntityId()); return new NumericValue(player.getEntityId());