fixes to scarpet 1.5
This commit is contained in:
1 parent
701a717135
commit
6524725245
11 files changed
+129
-30
No files matched your search
@@ -18,6 +18,7 @@ import net.minecraft.util.hit.BlockHitResult;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.dimension.DimensionType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -148,7 +149,10 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onTick()
|
||||
{
|
||||
handler.call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
|
||||
handler.call(Collections::emptyList, () ->
|
||||
CarpetServer.minecraft_server.getCommandSource().
|
||||
withWorld(CarpetServer.minecraft_server.getWorld(DimensionType.OVERWORLD))
|
||||
);
|
||||
}
|
||||
},
|
||||
NETHER_TICK("tick_nether",new CallbackList(0))
|
||||
@@ -156,7 +160,10 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onTick()
|
||||
{
|
||||
handler.call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
|
||||
handler.call(Collections::emptyList, () ->
|
||||
CarpetServer.minecraft_server.getCommandSource().
|
||||
withWorld(CarpetServer.minecraft_server.getWorld(DimensionType.THE_NETHER))
|
||||
);
|
||||
}
|
||||
},
|
||||
ENDER_TICK("tick_ender",new CallbackList(0))
|
||||
@@ -164,7 +171,10 @@ public class CarpetEventServer
|
||||
@Override
|
||||
public void onTick()
|
||||
{
|
||||
handler.call(Collections::emptyList, CarpetServer.minecraft_server::getCommandSource);
|
||||
handler.call(Collections::emptyList, () ->
|
||||
CarpetServer.minecraft_server.getCommandSource().
|
||||
withWorld(CarpetServer.minecraft_server.getWorld(DimensionType.THE_END))
|
||||
);
|
||||
}
|
||||
},
|
||||
PLAYER_JUMPS("player_jumps", new CallbackList(1))
|
||||
|
||||
@@ -67,6 +67,7 @@ import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Box;
|
||||
import net.minecraft.util.math.ChunkPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.EulerAngle;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
@@ -686,6 +687,24 @@ public class CarpetExpression
|
||||
}
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("pos_offset", -1, (c, t, lv) ->
|
||||
{
|
||||
BlockValue.LocatorResult locator = BlockValue.fromParams((CarpetContext)c, lv, 0);
|
||||
BlockPos pos = locator.block.getPos();
|
||||
if (lv.size() <= locator.offset)
|
||||
throw new InternalExpressionException("block_offset needs at least position, and direction");
|
||||
String directionString = lv.get(locator.offset).evalValue(c).getString();
|
||||
Direction dir = Direction.byName(directionString);
|
||||
if (dir == null)
|
||||
throw new InternalExpressionException("Unknown direction: "+directionString);
|
||||
int howMuch = 1;
|
||||
if (lv.size() > locator.offset+1)
|
||||
howMuch = (int) NumericValue.asNumber(lv.get(locator.offset+1).evalValue(c)).getLong();
|
||||
BlockPos retpos = pos.offset(dir, howMuch);
|
||||
Value ret = ListValue.of(new NumericValue(retpos.getX()), new NumericValue(retpos.getY()), new NumericValue(retpos.getZ()));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("solid", -1, (c, t, lv) ->
|
||||
genericStateTest(c, "solid", lv, (s, p, w) -> new NumericValue(s.isSimpleFullBlock(w, p))));
|
||||
|
||||
@@ -1146,6 +1165,16 @@ public class CarpetExpression
|
||||
return (_c, _t) -> res;
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("inventory_has_items", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null)
|
||||
return (_c, _t) -> Value.NULL;
|
||||
Value res = new NumericValue(!inventoryLocator.inventory.isInvEmpty());
|
||||
return (_c, _t) -> res;
|
||||
});
|
||||
|
||||
//inventory_get(<b, e>, <n>) -> item_triple
|
||||
this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) ->
|
||||
{
|
||||
@@ -2737,6 +2766,12 @@ public class CarpetExpression
|
||||
return (_c, _t) -> res; // pass through for variables
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("logger", 1, (c, t, lv) ->
|
||||
{
|
||||
Value res = lv.get(0).evalValue(c);
|
||||
CarpetSettings.LOG.error(res.getString());
|
||||
return (_c, _t) -> res; // pass through for variables
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("run", 1, (c, t, lv) -> {
|
||||
BlockPos target = ((CarpetContext)c).origin;
|
||||
@@ -2875,7 +2910,7 @@ public class CarpetExpression
|
||||
return (c_, t_) -> Value.TRUE;
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("get_global_state", -1, (c, t, lv) ->
|
||||
this.expr.addLazyFunction("load_app_data", -1, (c, t, lv) ->
|
||||
{
|
||||
String file = null;
|
||||
if (lv.size()>0)
|
||||
@@ -2887,10 +2922,10 @@ public class CarpetExpression
|
||||
return (cc, tt) -> retVal;
|
||||
});
|
||||
|
||||
this.expr.addLazyFunction("set_global_state", -1, (c, t, lv) ->
|
||||
this.expr.addLazyFunction("store_app_data", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
throw new InternalExpressionException("save_state needs nbt tag and optional file");
|
||||
throw new InternalExpressionException("store_app_data needs nbt tag and optional file");
|
||||
Value val = lv.get(0).evalValue(c);
|
||||
String file = null;
|
||||
if (lv.size()>1)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package carpet.script;
|
||||
|
||||
import carpet.script.bundled.BundledModule;
|
||||
import carpet.script.value.MapValue;
|
||||
import carpet.script.value.StringValue;
|
||||
import carpet.settings.CarpetSettings;
|
||||
import carpet.CarpetServer;
|
||||
import carpet.script.bundled.FileModule;
|
||||
@@ -19,6 +21,7 @@ import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -59,9 +62,10 @@ public class CarpetScriptServer
|
||||
{
|
||||
if (CarpetSettings.scriptsAutoload)
|
||||
{
|
||||
Messenger.m(CarpetServer.minecraft_server.getCommandSource(), "Autoloading world scarpet apps");
|
||||
for (String moduleName: listAvailableModules(false))
|
||||
{
|
||||
addScriptHost(CarpetServer.minecraft_server.getCommandSource(), moduleName, true);
|
||||
addScriptHost(CarpetServer.minecraft_server.getCommandSource(), moduleName, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +160,7 @@ public class CarpetScriptServer
|
||||
return host;
|
||||
}
|
||||
|
||||
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer)
|
||||
public boolean addScriptHost(ServerCommandSource source, String name, boolean perPlayer, boolean autoload)
|
||||
{
|
||||
//TODO add per player modules to support player actions better on a server
|
||||
name = name.toLowerCase(Locale.ROOT);
|
||||
@@ -164,32 +168,56 @@ public class CarpetScriptServer
|
||||
ScriptHost newHost = createMinecraftScriptHost(name, module, perPlayer, source);
|
||||
if (newHost == null)
|
||||
{
|
||||
Messenger.m(source, "r Failed to add a module");
|
||||
Messenger.m(source, "r Failed to add "+name+" app");
|
||||
return false;
|
||||
}
|
||||
if (module == null)
|
||||
{
|
||||
Messenger.m(source, "r Unable to locate the package, but created empty host "+name+" instead");
|
||||
Messenger.m(source, "r Unable to locate the app, but created empty "+name+" app instead");
|
||||
modules.put(name, newHost);
|
||||
return true;
|
||||
}
|
||||
String code = module.getCode();
|
||||
if (module.getCode() == null)
|
||||
{
|
||||
Messenger.m(source, "r Unable to load the package - not found");
|
||||
Messenger.m(source, "r Unable to load "+name+" app - not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
modules.put(name, newHost);
|
||||
|
||||
if (!addConfig(source, name) && autoload)
|
||||
{
|
||||
removeScriptHost(source, name);
|
||||
return false;
|
||||
}
|
||||
addEvents(source, name);
|
||||
|
||||
addCommand(source, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void addEvents(ServerCommandSource source, String hostName)
|
||||
private boolean addConfig(ServerCommandSource source, String hostName)
|
||||
{
|
||||
ScriptHost host = modules.get(hostName);
|
||||
if (host == null || !host.globalFunctions.containsKey("__config"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
Value ret = host.callUDF(BlockPos.ORIGIN, source, host.globalFunctions.get("__config"), Collections.emptyList());
|
||||
if (!(ret instanceof MapValue)) return false;
|
||||
Map<Value, Value> config = ((MapValue) ret).getMap();
|
||||
host.setPerPlayer(config.getOrDefault(new StringValue("scope"), new StringValue("player")).getString().equalsIgnoreCase("player"));
|
||||
return config.getOrDefault(new StringValue("stay_loaded"), Value.FALSE).getBoolean();
|
||||
}
|
||||
catch (NullPointerException | InvalidCallbackException ignored)
|
||||
{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private void addEvents(ServerCommandSource source, String hostName)
|
||||
{
|
||||
ScriptHost host = modules.get(hostName);
|
||||
if (host == null)
|
||||
@@ -208,7 +236,7 @@ public class CarpetScriptServer
|
||||
}
|
||||
|
||||
|
||||
public void addCommand(ServerCommandSource source, String hostName)
|
||||
private void addCommand(ServerCommandSource source, String hostName)
|
||||
{
|
||||
ScriptHost host = modules.get(hostName);
|
||||
if (host == null)
|
||||
|
||||
@@ -1323,7 +1323,7 @@ public class Expression implements Cloneable
|
||||
|
||||
addLazyBinaryOperator("+=", precedence.get("assign=<>"), false, (c, t, lv1, lv2) ->
|
||||
{
|
||||
Value v1 = lv1.evalValue(c);
|
||||
Value v1 = lv1.evalValue(c, Context.LVALUE);
|
||||
Value v2 = lv2.evalValue(c);
|
||||
if (v1 instanceof ListValue.ListConstructorValue && v2 instanceof ListValue)
|
||||
{
|
||||
@@ -1343,6 +1343,18 @@ public class Expression implements Cloneable
|
||||
}
|
||||
return (cc, tt) -> Value.TRUE;
|
||||
}
|
||||
if (v1 instanceof LContainerValue)
|
||||
{
|
||||
ContainerValueInterface cvi = ((LContainerValue) v1).getContainer();
|
||||
if (cvi == null)
|
||||
{
|
||||
throw new InternalExpressionException("Failed to resolve left hand side of the += operation");
|
||||
}
|
||||
Value key = ((LContainerValue) v1).getAddress();
|
||||
Value res = cvi.get(key).add(v2);
|
||||
cvi.put(key, res);
|
||||
return (cc, tt) -> res;
|
||||
}
|
||||
v1.assertAssignable();
|
||||
String varname = v1.getVariable();
|
||||
LazyValue boundedLHS;
|
||||
@@ -2360,7 +2372,10 @@ public class Expression implements Cloneable
|
||||
return (cc, tt) -> innerLValue;
|
||||
}
|
||||
if (!(container instanceof ContainerValueInterface))
|
||||
return (cc, tt) -> Value.NULL;
|
||||
if (t == Context.LVALUE)
|
||||
return (cc, tt) -> new LContainerValue(null, null);
|
||||
else
|
||||
return (cc, tt) -> Value.NULL;
|
||||
Value address = key_lv.evalValue(c);
|
||||
if (t != Context.LVALUE)
|
||||
{
|
||||
@@ -2456,7 +2471,7 @@ public class Expression implements Cloneable
|
||||
Value address = ((LContainerValue) container).getAddress();
|
||||
Value what = lv.get(1).evalValue(c);
|
||||
Value retVal = new NumericValue( (lv.size() > 2)
|
||||
? internalContainer.put(address, what, lv.get(3).evalValue(c))
|
||||
? internalContainer.put(address, what, lv.get(2).evalValue(c))
|
||||
: internalContainer.put(address, what));
|
||||
return (cc, tt) -> retVal;
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class ScriptHost
|
||||
public String getName() {return name;}
|
||||
|
||||
private final ModuleInterface myCode;
|
||||
private final boolean perUser;
|
||||
private boolean perUser;
|
||||
|
||||
ScriptHost(String name, ModuleInterface code, boolean perUser, ScriptHost parent)
|
||||
{
|
||||
@@ -207,10 +207,10 @@ public class ScriptHost
|
||||
}
|
||||
}
|
||||
|
||||
public void callUDF(BlockPos pos, ServerCommandSource source, UserDefinedFunction acf, List<LazyValue> argv) throws InvalidCallbackException
|
||||
public Value callUDF(BlockPos pos, ServerCommandSource source, UserDefinedFunction acf, List<LazyValue> argv) throws InvalidCallbackException
|
||||
{
|
||||
if (CarpetServer.scriptServer.stopAll)
|
||||
return;
|
||||
return Value.NULL;
|
||||
|
||||
List<String> args = acf.getArguments();
|
||||
if (argv.size() != args.size())
|
||||
@@ -221,7 +221,7 @@ public class ScriptHost
|
||||
{
|
||||
// TODO: this is just for now - invoke would be able to invoke other hosts scripts
|
||||
Context context = new CarpetContext(this, source, pos);
|
||||
Expression.evalValue(
|
||||
return Expression.evalValue(
|
||||
() -> acf.lazyEval(context, Context.VOID, acf.expression, acf.token, argv),
|
||||
context,
|
||||
Context.VOID);
|
||||
@@ -230,6 +230,7 @@ public class ScriptHost
|
||||
{
|
||||
CarpetSettings.LOG.error("Callback failed: "+e.getMessage());
|
||||
}
|
||||
return Value.NULL;
|
||||
}
|
||||
|
||||
private void dumpState()
|
||||
@@ -288,4 +289,10 @@ public class ScriptHost
|
||||
if (this.saveTimeout > 0)
|
||||
dumpState();
|
||||
}
|
||||
|
||||
public void setPerPlayer(boolean isPerUser)
|
||||
{
|
||||
CarpetSettings.LOG.error("Setting per player for "+name+" of "+isPerUser);
|
||||
perUser = isPerUser;
|
||||
}
|
||||
}
|
||||
@@ -227,10 +227,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
||||
return items;
|
||||
}
|
||||
|
||||
public Iterator<Value> iterator()
|
||||
{
|
||||
return items.iterator();
|
||||
}
|
||||
public Iterator<Value> iterator() { return new ArrayList<>(items).iterator(); }
|
||||
|
||||
public void extend(List<Value> subList)
|
||||
{
|
||||
|
||||
@@ -30,7 +30,7 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
@Override
|
||||
public Iterator<Value> iterator()
|
||||
{
|
||||
return map.keySet().iterator();
|
||||
return new ArrayList<>(map.keySet()).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user