Merge branch 'master' into 1.17
# Conflicts: # src/main/java/carpet/script/api/Auxiliary.java # src/main/java/carpet/script/api/Entities.java # src/main/java/carpet/script/api/Inventories.java # src/main/java/carpet/script/api/Scoreboards.java # src/main/java/carpet/script/api/WorldAccess.java # src/main/java/carpet/script/value/FunctionValue.java # src/main/java/carpet/script/value/NBTSerializableValue.java
This commit is contained in:
commit
5e3812e1f0
35 files changed
+1250
-1416
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import carpet.CarpetServer;
|
||||
import carpet.fakes.BrainInterface;
|
||||
import carpet.fakes.EntityInterface;
|
||||
import carpet.fakes.ItemEntityInterface;
|
||||
@@ -13,6 +14,7 @@ import carpet.helpers.Tracer;
|
||||
import carpet.network.ServerNetworkHandler;
|
||||
import carpet.patches.EntityPlayerMPFake;
|
||||
import carpet.script.CarpetContext;
|
||||
import carpet.script.CarpetScriptServer;
|
||||
import carpet.script.EntityEventsGroup;
|
||||
import carpet.script.argument.Vector3Argument;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
@@ -933,9 +935,11 @@ public class EntityValue extends Value
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateVelocity(Entity e)
|
||||
private static void updateVelocity(Entity e, double scale)
|
||||
{
|
||||
e.velocityModified = true;
|
||||
if (Math.abs(scale) > 10000)
|
||||
CarpetScriptServer.LOG.warn("Moved entity "+e.getEntityName()+" "+e.getName()+" at " +e.getPos()+" extremly fast: "+e.getVelocity());
|
||||
//((ServerWorld)e.getEntityWorld()).method_14178().sendToNearbyPlayers(e, new EntityVelocityUpdateS2CPacket(e));
|
||||
}
|
||||
|
||||
@@ -1067,30 +1071,32 @@ public class EntityValue extends Value
|
||||
throw new InternalExpressionException("Expected a list of 3 parameters as a second argument");
|
||||
}
|
||||
List<Value> coords = ((ListValue) v).getItems();
|
||||
e.setVelocity(
|
||||
NumericValue.asNumber(coords.get(0)).getDouble(),
|
||||
NumericValue.asNumber(coords.get(1)).getDouble(),
|
||||
NumericValue.asNumber(coords.get(2)).getDouble()
|
||||
);
|
||||
updateVelocity(e);
|
||||
double dx = NumericValue.asNumber(coords.get(0)).getDouble();
|
||||
double dy = NumericValue.asNumber(coords.get(0)).getDouble();
|
||||
double dz = NumericValue.asNumber(coords.get(2)).getDouble();
|
||||
e.setVelocity(dx, dy, dz);
|
||||
updateVelocity(e, MathHelper.absMax(MathHelper.absMax(dx, dy), dz));
|
||||
});
|
||||
put("motion_x", (e, v) ->
|
||||
{
|
||||
Vec3d velocity = e.getVelocity();
|
||||
e.setVelocity(NumericValue.asNumber(v).getDouble(), velocity.y, velocity.z);
|
||||
updateVelocity(e);
|
||||
double dv = NumericValue.asNumber(v).getDouble();
|
||||
e.setVelocity(dv, velocity.y, velocity.z);
|
||||
updateVelocity(e, dv);
|
||||
});
|
||||
put("motion_y", (e, v) ->
|
||||
{
|
||||
Vec3d velocity = e.getVelocity();
|
||||
e.setVelocity(velocity.x, NumericValue.asNumber(v).getDouble(), velocity.z);
|
||||
updateVelocity(e);
|
||||
double dv = NumericValue.asNumber(v).getDouble();
|
||||
e.setVelocity(velocity.x, dv, velocity.z);
|
||||
updateVelocity(e, dv);
|
||||
});
|
||||
put("motion_z", (e, v) ->
|
||||
{
|
||||
Vec3d velocity = e.getVelocity();
|
||||
e.setVelocity(velocity.x, velocity.y, NumericValue.asNumber(v).getDouble());
|
||||
updateVelocity(e);
|
||||
double dv = NumericValue.asNumber(v).getDouble();
|
||||
e.setVelocity(velocity.x, velocity.y, dv);
|
||||
updateVelocity(e, dv);
|
||||
});
|
||||
|
||||
put("accelerate", (e, v) ->
|
||||
@@ -1105,7 +1111,7 @@ public class EntityValue extends Value
|
||||
NumericValue.asNumber(coords.get(1)).getDouble(),
|
||||
NumericValue.asNumber(coords.get(2)).getDouble()
|
||||
);
|
||||
updateVelocity(e);
|
||||
updateVelocity(e, e.getVelocity().length());
|
||||
|
||||
});
|
||||
put("custom_name", (e, v) -> {
|
||||
@@ -1224,7 +1230,7 @@ public class EntityValue extends Value
|
||||
else if (v instanceof ListValue)
|
||||
{
|
||||
List<Value> lv = ((ListValue) v).getItems();
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 0, false);
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 0, false, false);
|
||||
pos = new BlockPos(locator.vec.x, locator.vec.y, locator.vec.z);
|
||||
if (lv.size() > locator.offset)
|
||||
{
|
||||
@@ -1253,7 +1259,7 @@ public class EntityValue extends Value
|
||||
else if (a instanceof ListValue)
|
||||
{
|
||||
List<Value> params= ((ListValue) a).getItems();
|
||||
Vector3Argument blockLocator = Vector3Argument.findIn(params, 0, false);
|
||||
Vector3Argument blockLocator = Vector3Argument.findIn(params, 0, false, false);
|
||||
BlockPos pos = new BlockPos(blockLocator.vec);
|
||||
RegistryKey<World> world = spe.getEntityWorld().getRegistryKey();
|
||||
float angle = spe.getHeadYaw();
|
||||
|
||||
@@ -5,10 +5,12 @@ import net.minecraft.nbt.NbtElement;
|
||||
|
||||
public class FunctionAnnotationValue extends Value
|
||||
{
|
||||
// 0 global
|
||||
// 1 vararg
|
||||
public int type;
|
||||
public FunctionAnnotationValue(Value variable, int type)
|
||||
public enum Type
|
||||
{
|
||||
GLOBAL, VARARG
|
||||
}
|
||||
public Type type;
|
||||
public FunctionAnnotationValue(Value variable, Type type)
|
||||
{
|
||||
if (variable.boundVariable == null)
|
||||
throw new InternalExpressionException("You can only borrow variables from the outer scope");
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FunctionUnpackedArgumentsValue extends ListValue
|
||||
{
|
||||
public FunctionUnpackedArgumentsValue(List<Value> list) {
|
||||
super(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value clone()
|
||||
{
|
||||
return new FunctionUnpackedArgumentsValue(items);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Value deepcopy()
|
||||
{
|
||||
ListValue copy = (ListValue)super.deepcopy();
|
||||
return new FunctionUnpackedArgumentsValue(copy.items);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import carpet.CarpetSettings;
|
||||
import carpet.script.Context;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.Fluff;
|
||||
@@ -11,7 +12,6 @@ import carpet.script.exception.ContinueStatement;
|
||||
import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ReturnStatement;
|
||||
import carpet.script.exception.ThrowStatement;
|
||||
import net.minecraft.nbt.NbtString;
|
||||
import net.minecraft.nbt.NbtElement;
|
||||
|
||||
@@ -150,11 +150,11 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
return varArgs != null;
|
||||
}
|
||||
|
||||
public LazyValue callInContext(Context c, Integer type, List<LazyValue> lazyParams)
|
||||
public LazyValue callInContext(Context c, Integer type, List<Value> params)
|
||||
{
|
||||
try
|
||||
{
|
||||
return lazyEval(c, type, expression, token, lazyParams);
|
||||
return execute(c, type, expression, token, params);
|
||||
}
|
||||
catch (ExpressionException exc)
|
||||
{
|
||||
@@ -173,15 +173,50 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
|
||||
public void checkArgs(int candidates)
|
||||
{
|
||||
assertArgsOk(lazyParams, (fixedArgs) ->{
|
||||
int actual = getArguments().size();
|
||||
|
||||
if (candidates < actual)
|
||||
throw new InternalExpressionException("Function " + getPrettyString() + " requires at least " + actual + " arguments");
|
||||
if (candidates > actual && getVarArgs() == null)
|
||||
throw new InternalExpressionException("Function " + getPrettyString() + " requires " + actual + " arguments");
|
||||
}
|
||||
|
||||
public static List<Value> unpackArgs(List<LazyValue> lazyParams, Context c)
|
||||
{
|
||||
// TODO we shoudn't need that if all fuctions are not lazy really
|
||||
List<Value> params = new ArrayList<>();
|
||||
for (LazyValue lv : lazyParams)
|
||||
{
|
||||
Value param = lv.evalValue(c, Context.NONE);
|
||||
if (param instanceof FunctionUnpackedArgumentsValue)
|
||||
{
|
||||
CarpetSettings.LOG.error("How did we get here?");
|
||||
params.addAll(((ListValue) param).getItems());
|
||||
}
|
||||
else
|
||||
{
|
||||
params.add(param);
|
||||
}
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) {
|
||||
List<Value> resolvedParams = unpackArgs(lazyParams, c);
|
||||
return execute(c, type, e, t, resolvedParams);
|
||||
}
|
||||
|
||||
public LazyValue execute(Context c, Integer type, Expression e, Tokenizer.Token t, List<Value> params)
|
||||
{
|
||||
assertArgsOk(params, (fixedArgs) ->{
|
||||
if (fixedArgs) // wrong number of args for fixed args
|
||||
{
|
||||
throw new ExpressionException(c, e, t,
|
||||
"Incorrect number of arguments for function "+name+
|
||||
". Should be "+args.size()+", not "+lazyParams.size()+" like "+args
|
||||
". Should be "+args.size()+", not "+params.size()+" like "+args
|
||||
);
|
||||
}
|
||||
else // too few args for varargs
|
||||
@@ -190,7 +225,7 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
argList.add("... "+varArgs);
|
||||
throw new ExpressionException(c, e, t,
|
||||
"Incorrect number of arguments for function "+name+
|
||||
". Should be at least "+args.size()+", not "+lazyParams.size()+" like "+argList
|
||||
". Should be at least "+args.size()+", not "+params.size()+" like "+argList
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -200,15 +235,15 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
for (int i=0; i<args.size(); i++)
|
||||
{
|
||||
String arg = args.get(i);
|
||||
Value val = lazyParams.get(i).evalValue(c).reboundedTo(arg); // todo check if we need to copy that
|
||||
Value val = params.get(i).reboundedTo(arg); // todo check if we need to copy that
|
||||
newFrame.setVariable(arg, (cc, tt) -> val);
|
||||
}
|
||||
if (varArgs != null)
|
||||
{
|
||||
List<Value> extraParams = new ArrayList<>();
|
||||
for (int i = args.size(), mx = lazyParams.size(); i < mx; i++)
|
||||
for (int i = args.size(), mx = params.size(); i < mx; i++)
|
||||
{
|
||||
extraParams.add(lazyParams.get(i).evalValue(c).reboundedTo(null)); // copy by value I guess
|
||||
extraParams.add(params.get(i).reboundedTo(null)); // copy by value I guess
|
||||
}
|
||||
Value rest = ListValue.wrap(extraParams).bindTo(varArgs); // didn't we just copied that?
|
||||
newFrame.setVariable(varArgs, (cc, tt) -> rest);
|
||||
@@ -265,12 +300,6 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
feedback.accept(false);
|
||||
}
|
||||
}
|
||||
public static List<Value> resolveArgs(List<LazyValue> lzargs, Context c, Integer t)
|
||||
{
|
||||
List<Value> args = new ArrayList<>(lzargs.size());
|
||||
lzargs.forEach( v -> args.add( v.evalValue(c, t)));
|
||||
return args;
|
||||
}
|
||||
|
||||
public static List<LazyValue> lazify(List<Value> args)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ public class LContainerValue extends FrameworkValue
|
||||
{
|
||||
private ContainerValueInterface container;
|
||||
private Value address;
|
||||
public static final LContainerValue NULL_CONTAINER = new LContainerValue(null, null);
|
||||
|
||||
public LContainerValue(ContainerValueInterface c, Value v)
|
||||
{
|
||||
|
||||
@@ -70,7 +70,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
||||
items.addAll(list);
|
||||
}
|
||||
|
||||
private ListValue(List<Value> list)
|
||||
protected ListValue(List<Value> list)
|
||||
{
|
||||
items = list;
|
||||
}
|
||||
|
||||
@@ -83,6 +83,12 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
|
||||
owned = true;
|
||||
}
|
||||
|
||||
public static Value of(NbtElement tag)
|
||||
{
|
||||
if (tag == null) return Value.NULL;
|
||||
return new NBTSerializableValue(tag);
|
||||
}
|
||||
|
||||
public NBTSerializableValue(Supplier<NbtElement> tagSupplier)
|
||||
{
|
||||
nbtSupplier = tagSupplier;
|
||||
@@ -180,29 +186,29 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public static InventoryLocator locateInventory(CarpetContext c, List<LazyValue> params, int offset)
|
||||
public static InventoryLocator locateInventory(CarpetContext c, List<Value> params, int offset)
|
||||
{
|
||||
try
|
||||
{
|
||||
Value v1 = params.get(offset).evalValue(c);
|
||||
Value v1 = params.get(offset);
|
||||
if (v1.isNull())
|
||||
{
|
||||
offset ++;
|
||||
v1 = params.get(offset).evalValue(c);
|
||||
v1 = params.get(offset);
|
||||
}
|
||||
else if (v1 instanceof StringValue)
|
||||
{
|
||||
String strVal = v1.getString().toLowerCase(Locale.ROOT);
|
||||
if (strVal.equals("enderchest"))
|
||||
{
|
||||
Value v2 = params.get(1 + offset).evalValue(c);
|
||||
Value v2 = params.get(1 + offset);
|
||||
ServerPlayerEntity player = EntityValue.getPlayerByValue(c.s.getMinecraftServer(), v2);
|
||||
if (player == null) throw new InternalExpressionException("enderchest inventory requires player argument");
|
||||
return new InventoryLocator(player, player.getBlockPos(), player.getEnderChestInventory(), offset + 2, true);
|
||||
}
|
||||
if (strVal.equals("equipment"))
|
||||
{
|
||||
Value v2 = params.get(1 + offset).evalValue(c);
|
||||
Value v2 = params.get(1 + offset);
|
||||
if (!(v2 instanceof EntityValue)) throw new InternalExpressionException("Equipment inventory requires a living entity argument");
|
||||
Entity e = ((EntityValue) v2).getEntity();
|
||||
if (!(e instanceof LivingEntity)) throw new InternalExpressionException("Equipment inventory requires a living entity argument");
|
||||
@@ -258,8 +264,8 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
|
||||
}
|
||||
BlockPos pos = new BlockPos(
|
||||
NumericValue.asNumber(v1).getDouble(),
|
||||
NumericValue.asNumber(params.get(1 + offset).evalValue(c)).getDouble(),
|
||||
NumericValue.asNumber(params.get(2 + offset).evalValue(c)).getDouble());
|
||||
NumericValue.asNumber(params.get(1 + offset)).getDouble(),
|
||||
NumericValue.asNumber(params.get(2 + offset)).getDouble());
|
||||
Inventory inv = getInventoryAt(c.s.getWorld(), pos);
|
||||
if (inv == null)
|
||||
return null;
|
||||
|
||||
@@ -35,7 +35,7 @@ public class ThreadValue extends Value
|
||||
{
|
||||
try
|
||||
{
|
||||
return function.lazyEval(ctx, Context.NONE, expr, token, FunctionValue.lazify(args)).evalValue(ctx);
|
||||
return function.execute(ctx, Context.NONE, expr, token, args).evalValue(ctx);
|
||||
}
|
||||
catch (ExitStatement exit)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user