From cc4096ea3f0508f7f5f2ae74c8f5bf66a9929a61 Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 10 Jan 2020 01:09:39 -0400 Subject: [PATCH 1/3] fixes to imports, and fix to #56 and #59 in gnembon/scarpet --- src/main/java/carpet/script/Expression.java | 4 ++-- src/main/java/carpet/script/ScriptHost.java | 16 ++++++++-------- .../carpet/script/value/FunctionValue.java | 19 ++++++++++++++++++- .../carpet/script/value/NumericValue.java | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index 943901b4..03bf2566 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -3338,11 +3338,11 @@ public class Expression } catch (InternalExpressionException exc) { - throw new ExpressionException(c, this, "Your expression result is incorrect:"+exc.getMessage()); + throw new ExpressionException(c, this, "Your expression result is incorrect: "+exc.getMessage()); } catch (ArithmeticException exc) { - throw new ExpressionException(c, this, "The final result is incorrect, "+exc.getMessage()); + throw new ExpressionException(c, this, "The final result is incorrect: "+exc.getMessage()); } } diff --git a/src/main/java/carpet/script/ScriptHost.java b/src/main/java/carpet/script/ScriptHost.java index f104731b..063625e4 100644 --- a/src/main/java/carpet/script/ScriptHost.java +++ b/src/main/java/carpet/script/ScriptHost.java @@ -129,7 +129,7 @@ public abstract class ScriptHost } else { - targetData.functionImports.put(identifier, sourceData); + targetData.futureImports.put(identifier, sourceData); } } } @@ -168,7 +168,7 @@ public abstract class ScriptHost private ModuleData findModuleDataFromFunctionImports(String name, ModuleData source, int ttl) { - if (ttl > 10) throw new InternalExpressionException("Cannot import "+name+", either your imports are too deep or too loopy"); + if (ttl > 64) throw new InternalExpressionException("Cannot import "+name+", either your imports are too deep or too loopy"); if (source.globalFunctions.containsKey(name)) return source; if (source.functionImports.containsKey(name)) @@ -202,7 +202,7 @@ public abstract class ScriptHost private ModuleData findModuleDataFromGlobalImports(String name, ModuleData source, int ttl) { - if (ttl > 10) throw new InternalExpressionException("Cannot import "+name+", either your imports are too deep or too loopy"); + if (ttl > 64) throw new InternalExpressionException("Cannot import "+name+", either your imports are too deep or too loopy"); if (source.globalVariables.containsKey(name)) return source; if (source.globalsImports.containsKey(name)) @@ -251,18 +251,18 @@ public abstract class ScriptHost public Stream globaVariableNames(Module module, Predicate predicate) { - return Stream.concat( + return Stream.concat(Stream.concat( moduleData.get(module).globalVariables.keySet().stream(), moduleData.get(module).globalsImports.keySet().stream() - ).filter(predicate); + ), moduleData.get(module).futureImports.keySet().stream().filter(s -> s.startsWith("global_"))).filter(predicate); } public Stream globaFunctionNames(Module module, Predicate predicate) { - return Stream.concat( + return Stream.concat(Stream.concat( moduleData.get(module).globalFunctions.keySet().stream(), moduleData.get(module).functionImports.keySet().stream() - ).filter(predicate); + ),moduleData.get(module).futureImports.keySet().stream().filter(s -> !s.startsWith("global_"))).filter(predicate); } public ScriptHost retrieveForExecution(String /*Nullable*/ user) @@ -279,7 +279,7 @@ public abstract class ScriptHost // fixing imports userHost.moduleData.forEach((module, data) -> { - data.setImportsBasedOn(this, this.moduleData.get(data.parent)); + data.setImportsBasedOn(userHost, this.moduleData.get(data.parent)); }); userHosts.put(user, userHost); return userHost; diff --git a/src/main/java/carpet/script/value/FunctionValue.java b/src/main/java/carpet/script/value/FunctionValue.java index ecbf7dd0..aeab0d73 100644 --- a/src/main/java/carpet/script/value/FunctionValue.java +++ b/src/main/java/carpet/script/value/FunctionValue.java @@ -2,7 +2,6 @@ package carpet.script.value; import carpet.script.Context; import carpet.script.Expression; -import carpet.script.ExpressionInspector; import carpet.script.Fluff; import carpet.script.LazyValue; import carpet.script.Tokenizer; @@ -14,9 +13,11 @@ import carpet.script.exception.InternalExpressionException; import carpet.script.exception.ReturnStatement; import carpet.script.exception.ThrowStatement; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class FunctionValue extends Value implements Fluff.ILazyFunction { @@ -57,6 +58,16 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction return name; } + @Override + public String getPrettyString() + { + List stringArgs= new ArrayList<>(args); + if (outerState != null) + stringArgs.addAll(outerState.entrySet().stream().map(e -> + "outer("+e.getKey()+") = "+e.getValue().evalValue(null).getPrettyString()).collect(Collectors.toList())); + return (name.equals("_")?"":name) +"("+String.join(", ",stringArgs)+")"; + } + public String fullName() {return expression.module == null?name:name+"["+expression.module.getName()+"]";} @Override @@ -107,6 +118,12 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction throw new InternalExpressionException("Function value has no numeric value"); } + @Override + public String getTypeString() + { + return "function"; + } + @Override public Value slice(long from, long to) { diff --git a/src/main/java/carpet/script/value/NumericValue.java b/src/main/java/carpet/script/value/NumericValue.java index 77ca3ea7..5d31654b 100644 --- a/src/main/java/carpet/script/value/NumericValue.java +++ b/src/main/java/carpet/script/value/NumericValue.java @@ -42,7 +42,7 @@ public class NumericValue extends Value } else { - return String.format("%.3f..", getDouble()); + return String.format("%.1f..", getDouble()); } } From 80b72366eeb30128b7b6a3b204e88ceeefcb3936 Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 10 Jan 2020 01:21:14 -0400 Subject: [PATCH 2/3] attempt to fix #129 --- src/main/java/carpet/mixins/PistonHandler_pushLimitMixin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/carpet/mixins/PistonHandler_pushLimitMixin.java b/src/main/java/carpet/mixins/PistonHandler_pushLimitMixin.java index c170b259..465d3b4b 100644 --- a/src/main/java/carpet/mixins/PistonHandler_pushLimitMixin.java +++ b/src/main/java/carpet/mixins/PistonHandler_pushLimitMixin.java @@ -6,7 +6,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.Constant; import org.spongepowered.asm.mixin.injection.ModifyConstant; -@Mixin(PistonHandler.class) +@Mixin(value = PistonHandler.class, priority = 69420) // piston push limit is important for carpet public class PistonHandler_pushLimitMixin { @ModifyConstant(method = "tryMove", constant = @Constant(intValue = 12), expect = 3) From a841755d61d2762bc0cc2e0617178d7067aa9b37 Mon Sep 17 00:00:00 2001 From: gnembon Date: Fri, 10 Jan 2020 11:31:49 -0400 Subject: [PATCH 3/3] enderchest API support --- .../java/carpet/script/CarpetExpression.java | 6 ++++-- .../script/value/NBTSerializableValue.java | 20 +++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index ea2e78e4..7cf338b0 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -1234,7 +1234,8 @@ public class CarpetExpression *
*

Manipulating inventories of blocks and entities

*

Most functions in this category require inventory as the first argument. Inventory could be specified by - * an entity, or a block, or position (three coordinates) of a potential block with inventory. + * an entity, or a block, or position (three coordinates) of a potential block with inventory. Player enderchest inventory + * require two arguments, keyword 'enderchest', followed by the player entity argument. * If the entity or a block doesn't * have an inventory, they typically do nothing and return null.

*

Most items returned are in the form of @@ -1262,6 +1263,7 @@ public class CarpetExpression * don't have an inventory

*
      *     inventory_size(player()) => 41
+     *     inventory_size('enderchest', player()) => 27 // enderchest
      *     inventory_size(x,y,z) => 27 // chest
      *     inventory_size(block(pos)) => 5 // hopper
      * 
@@ -1585,7 +1587,7 @@ public class CarpetExpression } private void syncPlayerInventory(NBTSerializableValue.InventoryLocator inventory, int int_1) { - if (inventory.owner instanceof ServerPlayerEntity) + if (inventory.owner instanceof ServerPlayerEntity && !inventory.isEnder) { ServerPlayerEntity player = (ServerPlayerEntity) inventory.owner; player.networkHandler.sendPacket(new GuiSlotUpdateS2CPacket( diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index 7cbeaec9..19585081 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -11,7 +11,6 @@ import net.minecraft.block.ChestBlock; import net.minecraft.block.InventoryProvider; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.ChestBlockEntity; -import net.minecraft.block.entity.HopperBlockEntity; import net.minecraft.command.arguments.ItemStackArgument; import net.minecraft.command.arguments.ItemStringReader; import net.minecraft.command.arguments.NbtPathArgumentType; @@ -32,7 +31,6 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Box; -import net.minecraft.world.World; import java.util.Collection; import java.util.HashMap; @@ -201,6 +199,17 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa return null; return new InventoryLocator(pos, pos, inv, offset + 1); } + else if (v1.getString().equalsIgnoreCase("enderchest")) + { + Value v2 = params.get(1 + offset).evalValue(c); + if (!(v2 instanceof EntityValue) || !(((EntityValue) v2).getEntity() instanceof PlayerEntity)) + { + throw new InternalExpressionException("enderchest inventory requires player argument"); + } + PlayerEntity e = (PlayerEntity)((EntityValue) v2).getEntity(); + inv = e.getEnderChestInventory(); + return new InventoryLocator(e, e.getBlockPos(), inv, offset + 2, true); + } BlockPos pos = new BlockPos( NumericValue.asNumber(v1).getDouble(), NumericValue.asNumber(params.get(1 + offset).evalValue(c)).getDouble(), @@ -516,12 +525,19 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa public BlockPos position; public Inventory inventory; public int offset; + public boolean isEnder; InventoryLocator(Object owner, BlockPos pos, Inventory i, int o) + { + this(owner, pos, i, o, false); + } + + InventoryLocator(Object owner, BlockPos pos, Inventory i, int o, boolean isEnder) { this.owner = owner; position = pos; inventory = i; offset = o; + this.isEnder = isEnder; } }