This commit is contained in:
gnembon committed 2025-01-16 12:43:30 +01:00
1 parent c241594eb6
commit fd098151c4
18 files changed
+100 -39

No files matched your search

@@ -141,4 +141,41 @@ public class CarpetExpression
throw new CarpetExpressionException("Your thoughts are too deep", null);
}
}
public Value explain(ScriptHost host, BlockPos pos)
{
// convert to explain
CarpetScriptServer scriptServer = (CarpetScriptServer) host.scriptServer();
try
{
Context context = new CarpetContext(host, source, origin).
with("x", (c, t) -> new NumericValue(pos.getX() - origin.getX()).bindTo("x")).
with("y", (c, t) -> new NumericValue(pos.getY() - origin.getY()).bindTo("y")).
with("z", (c, t) -> new NumericValue(pos.getZ() - origin.getZ()).bindTo("z"));
Entity e = source.getEntity();
if (e == null)
{
Value nullPlayer = Value.NULL.reboundedTo("p");
context.with("p", (cc, tt) -> nullPlayer);
}
else
{
Value playerValue = new EntityValue(e).bindTo("p");
context.with("p", (cc, tt) -> playerValue);
}
return scriptServer.events.handleEvents.getWhileDisabled(() -> this.expr.explain(context));
}
catch (ExpressionException e)
{
throw new CarpetExpressionException(e.getMessage(), e.stack);
}
catch (ArithmeticException ae)
{
throw new CarpetExpressionException("Math doesn't compute... " + ae.getMessage(), null);
}
catch (StackOverflowError soe)
{
throw new CarpetExpressionException("Your thoughts are too deep", null);
}
}
}
@@ -1251,6 +1251,12 @@ public class Expression
return extractOp(optimizeOnlyContext, root, Context.Type.NONE);
}
public Value explain(Context context)
{
//todo convert to explain
return Value.NULL;
}
private int treeSize(ExpressionNode node)
{
return node.op instanceof LazyValue.ContextFreeLazyValue ? 1 : node.args.stream().mapToInt(this::treeSize).sum() + 1;
@@ -5,6 +5,7 @@ import carpet.script.external.Vanilla;
import carpet.script.utils.AppStoreManager;
import carpet.script.exception.CarpetExpressionException;
import carpet.script.value.FunctionValue;
import carpet.script.value.NumericValue;
import carpet.script.value.Value;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
@@ -358,6 +359,12 @@ public class ScriptCommand
boolean success = ss(cc).uninstallApp(cc.getSource(), StringArgumentType.getString(cc, "app"));
return success ? 1 : 0;
}));
LiteralArgumentBuilder<CommandSourceStack> x = literal("explain").
requires(Vanilla::ServerPlayer_canScriptACE).
then(argument("expr", StringArgumentType.greedyString()).suggests(ScriptCommand::suggestCode).
executes((cc) -> explain(
cc,
StringArgumentType.getString(cc, "expr"))));
dispatcher.register(literal("script").
requires(Vanilla::ServerPlayer_canScriptGeneral).
@@ -536,6 +543,19 @@ public class ScriptCommand
});
}
private static int explain(CommandContext<CommandSourceStack> context, String expr) throws CommandSyntaxException
{
CommandSourceStack source = context.getSource();
CarpetScriptHost host = getHost(context);
return handleCall(source, host, () -> {
CarpetExpression ex = new CarpetExpression(host.main, expr, source, new BlockPos(0, 0, 0));
ex.explain(host, BlockPos.containing(source.getPosition()));
Carpet.Messenger_message(source, "w Expression: ", "wb " + expr);
Carpet.Messenger_message(source, "w Tokens: ", "wb " + ex.getExpr().toString());
return NumericValue.ONE;
});
}
private static int scriptScan(CommandContext<CommandSourceStack> context, BlockPos origin, BlockPos a, BlockPos b, String expr) throws CommandSyntaxException
{
CommandSourceStack source = context.getSource();
@@ -401,7 +401,7 @@ public class Auxiliary
yoffset = -armorstand.getBbHeight() + 0.3;
}
}
armorstand.moveTo(
armorstand.snapTo(
pointLocator.vec.x,
//pointLocator.vec.y - ((!interactable && targetBlock == null)?0.41f:((targetBlock==null)?(armorstand.getHeight()+0.41):(armorstand.getHeight()-0.3))),
pointLocator.vec.y + yoffset,
@@ -143,7 +143,7 @@ public class Entities
ServerLevel serverWorld = cc.level();
Entity entity = EntityType.loadEntityRecursive(tag, serverWorld, EntitySpawnReason.COMMAND, e -> {
e.moveTo(vec3d.x, vec3d.y, vec3d.z, e.getYRot(), e.getXRot());
e.snapTo(vec3d.x, vec3d.y, vec3d.z, e.getYRot(), e.getXRot());
return e;
});
if (entity == null)
@@ -911,7 +911,7 @@ public class EntityValue extends Value
}
else
{
e.moveTo(x, y, z, yaw, pitch);
e.snapTo(x, y, z, yaw, pitch);
// we were sending to players for not-living entites, that were untracked. Living entities should be tracked.
//((ServerWorld) e.getEntityWorld()).getChunkManager().sendToNearbyPlayers(e, new EntityS2CPacket.(e));
if (e instanceof LivingEntity le)