dont be lazy refactor
This commit is contained in:
1 parent
a65d16c9ba
commit
74bd0bb81e
31 files changed
+1078
-1383
No files matched your search
@@ -15,7 +15,6 @@ import carpet.script.CarpetScriptHost;
|
||||
import carpet.script.CarpetScriptServer;
|
||||
import carpet.script.Context;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.LazyValue;
|
||||
import carpet.script.argument.BlockArgument;
|
||||
import carpet.script.argument.FunctionArgument;
|
||||
import carpet.script.argument.Vector3Argument;
|
||||
@@ -28,9 +27,9 @@ import carpet.script.utils.InputValidator;
|
||||
import carpet.script.utils.ScarpetJsonDeserializer;
|
||||
import carpet.script.utils.ShapeDispatcher;
|
||||
import carpet.script.utils.WorldTools;
|
||||
import carpet.script.value.BooleanValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.FormattedTextValue;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import carpet.script.value.ListValue;
|
||||
import carpet.script.value.MapValue;
|
||||
import carpet.script.value.NBTSerializableValue;
|
||||
@@ -147,16 +146,15 @@ public class Auxiliary {
|
||||
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addLazyFunction("sound", -1, (c, t, lv) -> {
|
||||
expression.addContextFunction("sound", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
Value ret = ListValue.wrap(Registry.SOUND_EVENT.getIds().stream().map(ValueConversions::of));
|
||||
return (_c, _t) -> ret;
|
||||
return ListValue.wrap(Registry.SOUND_EVENT.getIds().stream().map(ValueConversions::of));
|
||||
}
|
||||
String rawString = lv.get(0).evalValue(c).getString();
|
||||
String rawString = lv.get(0).getString();
|
||||
Identifier soundName = new Identifier(rawString);
|
||||
Vector3Argument locator = Vector3Argument.findIn(cc, lv, 1);
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 1);
|
||||
if (Registry.SOUND_EVENT.get(soundName) == null)
|
||||
throw new ThrowStatement(rawString, Throwables.UNKNOWN_SOUND);
|
||||
float volume = 1.0F;
|
||||
@@ -164,13 +162,13 @@ public class Auxiliary {
|
||||
SoundCategory mixer = SoundCategory.MASTER;
|
||||
if (lv.size() > 0+locator.offset)
|
||||
{
|
||||
volume = (float) NumericValue.asNumber(lv.get(0+locator.offset).evalValue(c)).getDouble();
|
||||
volume = (float) NumericValue.asNumber(lv.get(0+locator.offset)).getDouble();
|
||||
if (lv.size() > 1+locator.offset)
|
||||
{
|
||||
pitch = (float) NumericValue.asNumber(lv.get(1+locator.offset).evalValue(c)).getDouble();
|
||||
pitch = (float) NumericValue.asNumber(lv.get(1+locator.offset)).getDouble();
|
||||
if (lv.size() > 2+locator.offset)
|
||||
{
|
||||
String mixerName = lv.get(2+locator.offset).evalValue(c).getString();
|
||||
String mixerName = lv.get(2+locator.offset).getString();
|
||||
mixer = mixerMap.get(mixerName.toLowerCase(Locale.ROOT));
|
||||
if (mixer == null) throw new InternalExpressionException(mixerName +" is not a valid mixer name");
|
||||
}
|
||||
@@ -184,39 +182,33 @@ public class Auxiliary {
|
||||
count++;
|
||||
player.networkHandler.sendPacket(new PlaySoundIdS2CPacket(soundName, mixer, vec, volume, pitch));
|
||||
}
|
||||
int totalPlayed = count;
|
||||
return (_c, _t) -> new NumericValue(totalPlayed);
|
||||
return new NumericValue(count);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("particle", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("particle", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
Value ret = ListValue.wrap(Registry.PARTICLE_TYPE.getIds().stream().map(ValueConversions::of));
|
||||
return (_c, _t) -> ret;
|
||||
}
|
||||
|
||||
if (lv.size() == 0) return ListValue.wrap(Registry.PARTICLE_TYPE.getIds().stream().map(ValueConversions::of));
|
||||
MinecraftServer ms = cc.s.getMinecraftServer();
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
Vector3Argument locator = Vector3Argument.findIn(cc, lv, 1);
|
||||
String particleName = lv.get(0).evalValue(c).getString();
|
||||
Vector3Argument locator = Vector3Argument.findIn(lv, 1);
|
||||
String particleName = lv.get(0).getString();
|
||||
int count = 10;
|
||||
double speed = 0;
|
||||
float spread = 0.5f;
|
||||
ServerPlayerEntity player = null;
|
||||
if (lv.size() > locator.offset)
|
||||
{
|
||||
count = (int) NumericValue.asNumber(lv.get(locator.offset).evalValue(c)).getLong();
|
||||
count = (int) NumericValue.asNumber(lv.get(locator.offset)).getLong();
|
||||
if (lv.size() > 1+locator.offset)
|
||||
{
|
||||
spread = (float) NumericValue.asNumber(lv.get(1+locator.offset).evalValue(c)).getDouble();
|
||||
spread = (float) NumericValue.asNumber(lv.get(1+locator.offset)).getDouble();
|
||||
if (lv.size() > 2+locator.offset)
|
||||
{
|
||||
speed = NumericValue.asNumber(lv.get(2 + locator.offset).evalValue(c)).getDouble();
|
||||
speed = NumericValue.asNumber(lv.get(2 + locator.offset)).getDouble();
|
||||
if (lv.size() > 3 + locator.offset) // should accept entity as well as long as it is player
|
||||
{
|
||||
player = ms.getPlayerManager().getPlayer(lv.get(3 + locator.offset).evalValue(c).getString());
|
||||
player = ms.getPlayerManager().getPlayer(lv.get(3 + locator.offset).getString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,29 +230,29 @@ public class Auxiliary {
|
||||
spread, spread, spread, speed);
|
||||
}
|
||||
|
||||
return (c_, t_) -> Value.TRUE;
|
||||
return Value.TRUE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("particle_line", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("particle_line", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
String particleName = lv.get(0).evalValue(c).getString();
|
||||
String particleName = lv.get(0).getString();
|
||||
ParticleEffect particle = ShapeDispatcher.getParticleData(particleName);
|
||||
Vector3Argument pos1 = Vector3Argument.findIn(cc, lv, 1);
|
||||
Vector3Argument pos2 = Vector3Argument.findIn(cc, lv, pos1.offset);
|
||||
Vector3Argument pos1 = Vector3Argument.findIn(lv, 1);
|
||||
Vector3Argument pos2 = Vector3Argument.findIn(lv, pos1.offset);
|
||||
double density = 1.0;
|
||||
ServerPlayerEntity player = null;
|
||||
if (lv.size() > pos2.offset+0 )
|
||||
{
|
||||
density = NumericValue.asNumber(lv.get(pos2.offset+0).evalValue(c)).getDouble();
|
||||
density = NumericValue.asNumber(lv.get(pos2.offset+0)).getDouble();
|
||||
if (density <= 0)
|
||||
{
|
||||
throw new InternalExpressionException("Particle density should be positive");
|
||||
}
|
||||
if (lv.size() > pos2.offset+1)
|
||||
{
|
||||
Value playerValue = lv.get(pos2.offset+1).evalValue(c);
|
||||
Value playerValue = lv.get(pos2.offset+1);
|
||||
if (playerValue instanceof EntityValue)
|
||||
{
|
||||
Entity e = ((EntityValue) playerValue).getEntity();
|
||||
@@ -274,34 +266,33 @@ public class Auxiliary {
|
||||
}
|
||||
}
|
||||
|
||||
Value retval = new NumericValue(ShapeDispatcher.drawParticleLine(
|
||||
return new NumericValue(ShapeDispatcher.drawParticleLine(
|
||||
(player == null)?world.getPlayers(): Collections.singletonList(player),
|
||||
particle, pos1.vec, pos2.vec, density));
|
||||
|
||||
return (c_, t_) -> retval;
|
||||
particle, pos1.vec, pos2.vec, density
|
||||
));
|
||||
});
|
||||
|
||||
expression.addLazyFunction("particle_box", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("particle_box", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
String particleName = lv.get(0).evalValue(c).getString();
|
||||
String particleName = lv.get(0).getString();
|
||||
ParticleEffect particle = ShapeDispatcher.getParticleData(particleName);
|
||||
Vector3Argument pos1 = Vector3Argument.findIn(cc, lv, 1);
|
||||
Vector3Argument pos2 = Vector3Argument.findIn(cc, lv, pos1.offset);
|
||||
Vector3Argument pos1 = Vector3Argument.findIn(lv, 1);
|
||||
Vector3Argument pos2 = Vector3Argument.findIn(lv, pos1.offset);
|
||||
|
||||
double density = 1.0;
|
||||
ServerPlayerEntity player = null;
|
||||
if (lv.size() > pos2.offset+0 )
|
||||
{
|
||||
density = NumericValue.asNumber(lv.get(pos2.offset+0).evalValue(c)).getDouble();
|
||||
density = NumericValue.asNumber(lv.get(pos2.offset+0)).getDouble();
|
||||
if (density <= 0)
|
||||
{
|
||||
throw new InternalExpressionException("Particle density should be positive");
|
||||
}
|
||||
if (lv.size() > pos2.offset+1)
|
||||
{
|
||||
Value playerValue = lv.get(pos2.offset+1).evalValue(c);
|
||||
Value playerValue = lv.get(pos2.offset+1);
|
||||
if (playerValue instanceof EntityValue)
|
||||
{
|
||||
Entity e = ((EntityValue) playerValue).getEntity();
|
||||
@@ -322,13 +313,13 @@ public class Auxiliary {
|
||||
player==null?world.getPlayers():Collections.singletonList(player),
|
||||
particle, density, from, to
|
||||
);
|
||||
return (c_, t_) -> new NumericValue(particleCount);
|
||||
return new NumericValue(particleCount);
|
||||
});
|
||||
// deprecated
|
||||
expression.alias("particle_rect", "particle_box");
|
||||
|
||||
|
||||
expression.addLazyFunction("draw_shape", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("draw_shape", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
@@ -337,7 +328,7 @@ public class Auxiliary {
|
||||
List<Pair<ShapeDispatcher.ExpiringShape, Map<String,Value>>> shapes = new ArrayList<>();
|
||||
if (lv.size() == 1) // bulk
|
||||
{
|
||||
Value specLoad = lv.get(0).evalValue(c);
|
||||
Value specLoad = lv.get(0);
|
||||
if (!(specLoad instanceof ListValue)) throw new InternalExpressionException("In bulk mode - shapes need to be provided as a list of shape specs");
|
||||
for (Value list : ((ListValue) specLoad).getItems())
|
||||
{
|
||||
@@ -347,19 +338,17 @@ public class Auxiliary {
|
||||
}
|
||||
else
|
||||
{
|
||||
List<Value> params = new ArrayList<>();
|
||||
for (LazyValue v : lv) params.add(v.evalValue(c));
|
||||
shapes.add(ShapeDispatcher.fromFunctionArgs(server, world, params, player));
|
||||
shapes.add(ShapeDispatcher.fromFunctionArgs(server, world, lv, player));
|
||||
}
|
||||
|
||||
ShapeDispatcher.sendShape(
|
||||
(player[0]==null)?cc.s.getWorld().getPlayers():Collections.singletonList(player[0]),
|
||||
shapes
|
||||
);
|
||||
return LazyValue.TRUE;
|
||||
return Value.TRUE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("create_marker", -1, (c, t, lv) ->{
|
||||
expression.addContextFunction("create_marker", -1, (c, t, lv) ->{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
BlockState targetBlock = null;
|
||||
Vector3Argument pointLocator;
|
||||
@@ -367,16 +356,16 @@ public class Auxiliary {
|
||||
String name;
|
||||
try
|
||||
{
|
||||
Value nameValue = lv.get(0).evalValue(c);
|
||||
Value nameValue = lv.get(0);
|
||||
name = nameValue instanceof NullValue ? "" : nameValue.getString();
|
||||
pointLocator = Vector3Argument.findIn(cc, lv, 1, true, false);
|
||||
pointLocator = Vector3Argument.findIn(lv, 1, true, false);
|
||||
if (lv.size()>pointLocator.offset)
|
||||
{
|
||||
BlockArgument blockLocator = BlockArgument.findIn(cc, lv, pointLocator.offset, true, true, false);
|
||||
if (blockLocator.block != null) targetBlock = blockLocator.block.getBlockState();
|
||||
if (lv.size() > blockLocator.offset)
|
||||
{
|
||||
interactable = lv.get(blockLocator.offset).evalValue(c, Context.BOOLEAN).getBoolean();
|
||||
interactable = lv.get(blockLocator.offset).getBoolean();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -429,11 +418,10 @@ public class Auxiliary {
|
||||
armorstand.setInvulnerable(true);
|
||||
armorstand.getDataTracker().set(ArmorStandEntity.ARMOR_STAND_FLAGS, (byte)(interactable?8 : 16|8));
|
||||
cc.s.getWorld().spawnEntity(armorstand);
|
||||
EntityValue result = new EntityValue(armorstand);
|
||||
return (_c, _t) -> result;
|
||||
return new EntityValue(armorstand);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("remove_all_markers", 0, (c, t, lv) -> {
|
||||
expression.addContextFunction("remove_all_markers", 0, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
int total = 0;
|
||||
String markerName = MARKER_STRING+"_"+((cc.host.getName()==null)?"":cc.host.getName());
|
||||
@@ -442,34 +430,18 @@ public class Auxiliary {
|
||||
total ++;
|
||||
e.remove();
|
||||
}
|
||||
int finalTotal = total;
|
||||
return (_cc, _tt) -> new NumericValue(finalTotal);
|
||||
return new NumericValue(total);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("nbt", 1, (c, t, lv) -> {
|
||||
Value ret = NBTSerializableValue.fromValue(lv.get(0).evalValue(c));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
expression.addUnaryFunction("nbt", NBTSerializableValue::fromValue);
|
||||
|
||||
expression.addLazyFunction("escape_nbt", 1, (c, t, lv) -> {
|
||||
Value v = lv.get(0).evalValue(c);
|
||||
String string = v.getString();
|
||||
Value ret = new StringValue(StringTag.escape(string));
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
expression.addUnaryFunction("escape_nbt", v -> new StringValue(StringTag.escape(v.getString())));
|
||||
|
||||
expression.addLazyFunction("parse_nbt", 1, (c, t, lv) -> {
|
||||
Value v = lv.get(0).evalValue(c);
|
||||
if (v instanceof NBTSerializableValue)
|
||||
{
|
||||
Value parsed = ((NBTSerializableValue) v).toValue();
|
||||
return (cc, tt) -> parsed;
|
||||
}
|
||||
expression.addUnaryFunction("parse_nbt", v -> {
|
||||
if (v instanceof NBTSerializableValue) return ((NBTSerializableValue) v).toValue();
|
||||
NBTSerializableValue ret = NBTSerializableValue.parseString(v.getString(), false);
|
||||
if (ret == null)
|
||||
return LazyValue.NULL;
|
||||
Value parsed = ret.toValue();
|
||||
return (cc, tt) -> parsed;
|
||||
if (ret == null) return Value.NULL;
|
||||
return ret.toValue();
|
||||
});
|
||||
|
||||
expression.addFunction("tag_matches", (lv) -> {
|
||||
@@ -482,11 +454,11 @@ public class Auxiliary {
|
||||
return new NumericValue(NbtHelper.matches(match, source, numParam == 2 || lv.get(2).getBoolean()));
|
||||
});
|
||||
|
||||
expression.addLazyFunction("encode_nbt", -1, (c, t, lv) -> {
|
||||
expression.addFunction("encode_nbt", lv -> {
|
||||
int argSize = lv.size();
|
||||
if (argSize==0 || argSize > 2) throw new InternalExpressionException("'encode_nbt' requires 1 or 2 parameters");
|
||||
Value v = lv.get(0).evalValue(c);
|
||||
boolean force = (argSize > 1) && lv.get(1).evalValue(c).getBoolean();
|
||||
Value v = lv.get(0);
|
||||
boolean force = (argSize > 1) && lv.get(1).getBoolean();
|
||||
Tag tag;
|
||||
try
|
||||
{
|
||||
@@ -496,17 +468,16 @@ public class Auxiliary {
|
||||
{
|
||||
throw new InternalExpressionException("cannot reliably encode to a tag the value of '"+ignored.val.getPrettyString()+"'");
|
||||
}
|
||||
Value tagValue = new NBTSerializableValue(tag);
|
||||
return (cc, tt) -> tagValue;
|
||||
return new NBTSerializableValue(tag);
|
||||
});
|
||||
|
||||
//"overridden" native call that prints to stderr
|
||||
expression.addLazyFunction("print", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("print", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0 || lv.size() > 2) throw new InternalExpressionException("'print' takes one or two arguments");
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
MinecraftServer server = s.getMinecraftServer();
|
||||
Value res = lv.get(0).evalValue(c);
|
||||
Value res = lv.get(0);
|
||||
List<ServerPlayerEntity> targets = null;
|
||||
if (lv.size() == 2)
|
||||
{
|
||||
@@ -518,7 +489,7 @@ public class Auxiliary {
|
||||
playerTargets.add(player);
|
||||
});
|
||||
targets = playerTargets;
|
||||
res = lv.get(1).evalValue(c);
|
||||
res = lv.get(1);
|
||||
}
|
||||
Text message = FormattedTextValue.getTextByValue(res);
|
||||
if (targets == null)
|
||||
@@ -529,12 +500,12 @@ public class Auxiliary {
|
||||
{
|
||||
targets.forEach(p -> p.getCommandSource().sendFeedback(message, false));
|
||||
}
|
||||
Value finalRes = res;
|
||||
return (_c, _t) -> finalRes; // pass through for variables
|
||||
return res; // pass through for variables
|
||||
});
|
||||
expression.addLazyFunction("display_title", -1, (c, t, lv) -> {
|
||||
|
||||
expression.addContextFunction("display_title", -1, (c, t, lv) -> {
|
||||
if (lv.size() < 2) throw new InternalExpressionException("'display_title' needs at least a target, type and message, and optionally times");
|
||||
Value pVal = lv.get(0).evalValue(c);
|
||||
Value pVal = lv.get(0);
|
||||
if (!(pVal instanceof ListValue)) pVal = ListValue.of(pVal);
|
||||
MinecraftServer server = ((CarpetContext)c).s.getMinecraftServer();
|
||||
Stream<ServerPlayerEntity> targets = ((ListValue) pVal).getItems().stream().map(v ->
|
||||
@@ -544,7 +515,7 @@ public class Auxiliary {
|
||||
return player;
|
||||
});
|
||||
TitleS2CPacket.Action action;
|
||||
String actionString = lv.get(1).evalValue(c).getString().toLowerCase(Locale.ROOT);
|
||||
String actionString = lv.get(1).getString().toLowerCase(Locale.ROOT);
|
||||
switch (actionString)
|
||||
{
|
||||
case "title":
|
||||
@@ -572,7 +543,7 @@ public class Auxiliary {
|
||||
boolean soundsTrue = false;
|
||||
if (lv.size() > 2)
|
||||
{
|
||||
pVal = lv.get(2).evalValue(c);
|
||||
pVal = lv.get(2);
|
||||
title = FormattedTextValue.getTextByValue(pVal);
|
||||
soundsTrue = pVal.getBoolean();
|
||||
}
|
||||
@@ -598,15 +569,15 @@ public class Auxiliary {
|
||||
total.getAndIncrement();
|
||||
});
|
||||
HUDController.update_hud(((CarpetContext)c).s.getMinecraftServer(), targetList);
|
||||
Value ret = NumericValue.of(total.get());
|
||||
return (cc, tt) -> ret;
|
||||
return NumericValue.of(total.get());
|
||||
}
|
||||
TitleS2CPacket timesPacket;
|
||||
if (lv.size() > 3)
|
||||
{
|
||||
int in = NumericValue.asNumber(lv.get(3).evalValue(c),"fade in for display_title" ).getInt();
|
||||
int stay = NumericValue.asNumber(lv.get(4).evalValue(c),"stay for display_title" ).getInt();
|
||||
int out = NumericValue.asNumber(lv.get(5).evalValue(c),"fade out for display_title" ).getInt();
|
||||
if (lv.size() != 6) throw new InternalExpressionException("'display_title' needs all fade-in, stay and fade-out times");
|
||||
int in = NumericValue.asNumber(lv.get(3),"fade in for display_title" ).getInt();
|
||||
int stay = NumericValue.asNumber(lv.get(4),"stay for display_title" ).getInt();
|
||||
int out = NumericValue.asNumber(lv.get(5),"fade out for display_title" ).getInt();
|
||||
timesPacket = new TitleS2CPacket(Action.TIMES, null, in, stay, out);
|
||||
}
|
||||
else timesPacket = null;
|
||||
@@ -618,20 +589,17 @@ public class Auxiliary {
|
||||
p.networkHandler.sendPacket(packet);
|
||||
total.getAndIncrement();
|
||||
});
|
||||
Value ret = NumericValue.of(total.get());
|
||||
return (cc, tt) -> ret;
|
||||
return NumericValue.of(total.get());
|
||||
});
|
||||
|
||||
expression.addLazyFunction("format", -1, (c, t, lv) -> {
|
||||
if (lv.size() == 0 ) throw new InternalExpressionException("'format' requires at least one component");
|
||||
List<Value> values = lv.stream().map(lazy -> lazy.evalValue(c)).collect(Collectors.toList());
|
||||
expression.addFunction("format", values -> {
|
||||
if (values.size() == 0 ) throw new InternalExpressionException("'format' requires at least one component");
|
||||
if (values.get(0) instanceof ListValue && values.size()==1)
|
||||
values = ((ListValue) values.get(0)).getItems();
|
||||
Value ret = new FormattedTextValue(Messenger.c(values.stream().map(Value::getString).toArray()));
|
||||
return (cc, tt) -> ret;
|
||||
return new FormattedTextValue(Messenger.c(values.stream().map(Value::getString).toArray()));
|
||||
});
|
||||
|
||||
expression.addLazyFunction("run", 1, (c, t, lv) -> {
|
||||
expression.addContextFunction("run", 1, (c, t, lv) -> {
|
||||
BlockPos target = ((CarpetContext)c).origin;
|
||||
Vec3d posf = new Vec3d((double)target.getX()+0.5D, target.getY(),(double)target.getZ()+0.5D);
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
@@ -641,56 +609,48 @@ public class Auxiliary {
|
||||
List<Text> output = new ArrayList<>();
|
||||
Value retval = new NumericValue(s.getMinecraftServer().getCommandManager().execute(
|
||||
new SnoopyCommandSource(s, posf, error, output),
|
||||
lv.get(0).evalValue(c).getString())
|
||||
lv.get(0).getString())
|
||||
);
|
||||
Value ret = ListValue.of(
|
||||
return ListValue.of(
|
||||
retval,
|
||||
ListValue.wrap(output.stream().map(FormattedTextValue::new).collect(Collectors.toList())),
|
||||
FormattedTextValue.of(error[0])
|
||||
);
|
||||
return (c_, t_) -> ret;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Value ret = ListValue.of(Value.NULL, ListValue.of(), new FormattedTextValue(new LiteralText(exc.getMessage())));
|
||||
return (c_, t_) -> ret;
|
||||
return ListValue.of(Value.NULL, ListValue.of(), new FormattedTextValue(new LiteralText(exc.getMessage())));
|
||||
}
|
||||
});
|
||||
|
||||
expression.addLazyFunction("save", 0, (c, t, lv) -> {
|
||||
expression.addContextFunction("save", 0, (c, t, lv) -> {
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
s.getMinecraftServer().getPlayerManager().saveAllPlayerData();
|
||||
s.getMinecraftServer().save(true,true,true);
|
||||
s.getWorld().getChunkManager().tick(() -> true);
|
||||
CarpetSettings.LOG.warn("Saved chunks");
|
||||
return (cc, tt) -> Value.TRUE;
|
||||
return Value.TRUE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("tick_time", 0, (c, t, lv) ->
|
||||
{
|
||||
Value time = new NumericValue(((CarpetContext) c).s.getMinecraftServer().getTicks());
|
||||
return (cc, tt) -> time;
|
||||
});
|
||||
expression.addContextFunction("tick_time", 0, (c, t, lv) ->
|
||||
new NumericValue(((CarpetContext) c).s.getMinecraftServer().getTicks()));
|
||||
|
||||
expression.addLazyFunction("world_time", 0, (c, t, lv) ->
|
||||
{
|
||||
Value time = new NumericValue(((CarpetContext) c).s.getWorld().getTime());
|
||||
return (cc, tt) -> time;
|
||||
});
|
||||
expression.addContextFunction("world_time", 0, (c, t, lv) ->
|
||||
new NumericValue(((CarpetContext) c).s.getWorld().getTime()));
|
||||
|
||||
expression.addLazyFunction("day_time", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("day_time", -1, (c, t, lv) ->
|
||||
{
|
||||
Value time = new NumericValue(((CarpetContext) c).s.getWorld().getTimeOfDay());
|
||||
if (lv.size() > 0)
|
||||
{
|
||||
long newTime = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||
long newTime = NumericValue.asNumber(lv.get(0)).getLong();
|
||||
if (newTime < 0) newTime = 0;
|
||||
((CarpetContext) c).s.getWorld().setTimeOfDay(newTime);// setTimeOfDay(newTime);
|
||||
}
|
||||
return (cc, tt) -> time;
|
||||
return time;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("last_tick_times", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("last_tick_times", -1, (c, t, lv) ->
|
||||
{
|
||||
//assuming we are in the tick world section
|
||||
// might be off one tick when run in the off tasks or asynchronously.
|
||||
@@ -701,12 +661,11 @@ public class Auxiliary {
|
||||
{
|
||||
ticks.add(new NumericValue(((double)tickArray[i % 100])/1000000.0));
|
||||
}
|
||||
Value ret = ListValue.wrap(ticks);
|
||||
return (cc, tt) -> ret;
|
||||
return ListValue.wrap(ticks);
|
||||
});
|
||||
|
||||
|
||||
expression.addLazyFunction("game_tick", -1, (c, t, lv) -> {
|
||||
expression.addContextFunction("game_tick", -1, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
ServerCommandSource s = cc.s;
|
||||
if (!s.getMinecraftServer().isOnThread()) throw new InternalExpressionException("Unable to run ticks from threads");
|
||||
@@ -717,7 +676,7 @@ public class Auxiliary {
|
||||
((MinecraftServerInterface) s.getMinecraftServer()).forceTick(() -> System.nanoTime() - CarpetServer.scriptServer.tickStart < 50000000L);
|
||||
if (lv.size() > 0)
|
||||
{
|
||||
long ms_total = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||
long ms_total = NumericValue.asNumber(lv.get(0)).getLong();
|
||||
long end_expected = CarpetServer.scriptServer.tickStart + ms_total * 1000000L;
|
||||
long wait = end_expected - System.nanoTime();
|
||||
if (wait > 0L)
|
||||
@@ -740,17 +699,16 @@ public class Auxiliary {
|
||||
}
|
||||
if(CarpetServer.scriptServer.stopAll)
|
||||
throw new ExitStatement(Value.NULL);
|
||||
return (_c, _t) -> Value.TRUE;
|
||||
return Value.TRUE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("seed", -1, (c, t, lv) -> {
|
||||
expression.addContextFunction("seed", -1, (c, t, lv) -> {
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
c.host.issueDeprecation("seed()");
|
||||
Value ret = new NumericValue(s.getWorld().getSeed());
|
||||
return (cc, tt) -> ret;
|
||||
return new NumericValue(s.getWorld().getSeed());
|
||||
});
|
||||
|
||||
expression.addLazyFunction("relight", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("relight", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
BlockArgument locator = BlockArgument.findIn(cc, lv, 0);
|
||||
@@ -758,21 +716,16 @@ public class Auxiliary {
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
((ThreadedAnvilChunkStorageInterface) world.getChunkManager().threadedAnvilChunkStorage).relightChunk(new ChunkPos(pos));
|
||||
WorldTools.forceChunkUpdate(pos, world);
|
||||
return LazyValue.TRUE;
|
||||
return Value.TRUE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("current_dimension", 0, (c, t, lv) -> {
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
Value retval = ValueConversions.of(s.getWorld());
|
||||
return (cc, tt) -> retval;
|
||||
});
|
||||
expression.addContextFunction("current_dimension", 0, (c, t, lv) ->
|
||||
ValueConversions.of( ((CarpetContext)c).s.getWorld()));
|
||||
|
||||
expression.addLazyFunction("view_distance", 0, (c, t, lv) -> {
|
||||
ServerCommandSource s = ((CarpetContext)c).s;
|
||||
Value retval = new NumericValue(s.getMinecraftServer().getPlayerManager().getViewDistance());
|
||||
return (cc, tt) -> retval;
|
||||
});
|
||||
expression.addContextFunction("view_distance", 0, (c, t, lv) ->
|
||||
new NumericValue(((CarpetContext)c).s.getMinecraftServer().getPlayerManager().getViewDistance()));
|
||||
|
||||
// lazy due to passthrough and context changing ability
|
||||
expression.addLazyFunction("in_dimension", 2, (c, t, lv) -> {
|
||||
ServerCommandSource outerSource = ((CarpetContext)c).s;
|
||||
Value dimensionValue = lv.get(0).evalValue(c);
|
||||
@@ -786,7 +739,7 @@ public class Auxiliary {
|
||||
return (cc, tt) -> retval;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("plop", -1, (c, t, lv) ->{
|
||||
expression.addContextFunction("plop", -1, (c, t, lv) ->{
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
Map<Value, Value> plopData = new HashMap<>();
|
||||
@@ -807,13 +760,12 @@ public class Auxiliary {
|
||||
plopData.put(StringValue.of("configured_structures"),
|
||||
ListValue.wrap(registryManager.get(Registry.CONFIGURED_STRUCTURE_FEATURE_WORLDGEN).getIds().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
|
||||
);
|
||||
Value ret = MapValue.wrap(plopData);
|
||||
return (_c, _t) -> ret;
|
||||
return MapValue.wrap(plopData);
|
||||
}
|
||||
BlockArgument locator = BlockArgument.findIn((CarpetContext)c, lv, 0);
|
||||
if (lv.size() <= locator.offset)
|
||||
throw new InternalExpressionException("'plop' needs extra argument indicating what to plop");
|
||||
String what = lv.get(locator.offset).evalValue(c).getString();
|
||||
String what = lv.get(locator.offset).getString();
|
||||
Value [] result = new Value[]{Value.NULL};
|
||||
((CarpetContext)c).s.getMinecraftServer().submitAndJoin( () ->
|
||||
{
|
||||
@@ -825,41 +777,38 @@ public class Auxiliary {
|
||||
WorldTools.forceChunkUpdate(locator.block.getPos(), ((CarpetContext) c).s.getWorld());
|
||||
result[0] = new NumericValue(res);
|
||||
});
|
||||
Value ret = result[0]; // preventing from lazy evaluating of the result in case a future completes later
|
||||
return (_c, _t) -> ret;
|
||||
return result[0];
|
||||
});
|
||||
|
||||
expression.addLazyFunction("schedule", -1, (c, t, lv) -> {
|
||||
expression.addContextFunction("schedule", -1, (c, t, lv) -> {
|
||||
if (lv.size()<2)
|
||||
throw new InternalExpressionException("'schedule' should have at least 2 arguments, delay and call name");
|
||||
long delay = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||
|
||||
FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, false);
|
||||
List<Value> args = functionArgument.unpackArgs(c);
|
||||
long delay = NumericValue.asNumber(lv.get(0)).getLong();
|
||||
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, false);
|
||||
CarpetServer.scriptServer.events.scheduleCall(
|
||||
(CarpetContext) c,
|
||||
functionArgument.function,
|
||||
args,
|
||||
functionArgument.checkedArgs(),
|
||||
delay
|
||||
);
|
||||
return (c_, t_) -> Value.TRUE;
|
||||
return Value.TRUE;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("logger", -1, (c, t, lv) ->
|
||||
expression.addFunction("logger", lv ->
|
||||
{
|
||||
//CarpetSettings.LOG.error(Registry.ENTITY_TYPE.getIds().stream().sorted().map(ValueConversions::simplify).collect(Collectors.joining("`, `")));
|
||||
Value res;
|
||||
|
||||
if(lv.size()==1)
|
||||
{
|
||||
res = lv.get(0).evalValue(c);
|
||||
res = lv.get(0);
|
||||
CarpetSettings.LOG.info(res.getString());
|
||||
}
|
||||
else if(lv.size()==2)
|
||||
{
|
||||
String level = lv.get(0).evalValue(c).getString().toLowerCase(Locale.ROOT);
|
||||
res = lv.get(1).evalValue(c);
|
||||
String level = lv.get(0).getString().toLowerCase(Locale.ROOT);
|
||||
res = lv.get(1);
|
||||
switch(level){
|
||||
case "error": CarpetSettings.LOG.error(res.getString()); break;
|
||||
case "warn": CarpetSettings.LOG.warn(res.getString()); break;
|
||||
@@ -871,26 +820,25 @@ public class Auxiliary {
|
||||
}
|
||||
else throw new InternalExpressionException("logger takes 1 or 2 arguments");
|
||||
|
||||
return (_c, _t) -> res; // pass through for variables
|
||||
return res; // pass through for variables
|
||||
});
|
||||
|
||||
expression.addLazyFunction("list_files", 2, (c, t, lv) ->
|
||||
expression.addContextFunction("list_files", 2, (c, t, lv) ->
|
||||
{
|
||||
FileArgument fdesc = FileArgument.from(lv, c, true, FileArgument.Reason.READ);
|
||||
FileArgument fdesc = FileArgument.from(lv,true, FileArgument.Reason.READ);
|
||||
Stream<String> files = ((CarpetScriptHost) c.host).listFolder(fdesc);
|
||||
if (files == null) return LazyValue.NULL;
|
||||
Value ret = ListValue.wrap(files.map(StringValue::of).collect(Collectors.toList()));
|
||||
return (cc, tt) -> ret;
|
||||
if (files == null) return Value.NULL;
|
||||
return ListValue.wrap(files.map(StringValue::of).collect(Collectors.toList()));
|
||||
});
|
||||
|
||||
expression.addLazyFunction("read_file", 2, (c, t, lv) ->
|
||||
expression.addContextFunction("read_file", 2, (c, t, lv) ->
|
||||
{
|
||||
FileArgument fdesc = FileArgument.from(lv, c, false, FileArgument.Reason.READ);
|
||||
FileArgument fdesc = FileArgument.from(lv,false, FileArgument.Reason.READ);
|
||||
Value retVal;
|
||||
if (fdesc.type == FileArgument.Type.NBT)
|
||||
{
|
||||
Tag state = ((CarpetScriptHost) c.host).readFileTag(fdesc);
|
||||
if (state == null) return LazyValue.NULL;
|
||||
if (state == null) return Value.NULL;
|
||||
retVal = new NBTSerializableValue(state);
|
||||
}
|
||||
else if (fdesc.type == FileArgument.Type.JSON)
|
||||
@@ -906,25 +854,23 @@ public class Auxiliary {
|
||||
else
|
||||
{
|
||||
List<String> content = ((CarpetScriptHost) c.host).readTextResource(fdesc);
|
||||
if (content == null) return LazyValue.NULL;
|
||||
if (content == null) return Value.NULL;
|
||||
retVal = ListValue.wrap(content.stream().map(StringValue::new).collect(Collectors.toList()));
|
||||
}
|
||||
return (cc, tt) -> retVal;
|
||||
return retVal;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("delete_file", 2, (c, t, lv) -> {
|
||||
FileArgument fdesc = FileArgument.from(lv, c, false, FileArgument.Reason.DELETE);
|
||||
return ((CarpetScriptHost) c.host).removeResourceFile(fdesc) ? LazyValue.TRUE : LazyValue.FALSE;
|
||||
});
|
||||
expression.addContextFunction("delete_file", 2, (c, t, lv) ->
|
||||
BooleanValue.of(((CarpetScriptHost) c.host).removeResourceFile(FileArgument.from(lv,false, FileArgument.Reason.DELETE))));
|
||||
|
||||
expression.addLazyFunction("write_file", -1, (c, t, lv) -> {
|
||||
expression.addContextFunction("write_file", -1, (c, t, lv) -> {
|
||||
if (lv.size() < 3) throw new InternalExpressionException("'write_file' requires three or more arguments");
|
||||
FileArgument fdesc = FileArgument.from(lv, c, false, FileArgument.Reason.CREATE);
|
||||
FileArgument fdesc = FileArgument.from(lv, false, FileArgument.Reason.CREATE);
|
||||
|
||||
boolean success;
|
||||
if (fdesc.type == FileArgument.Type.NBT)
|
||||
{
|
||||
Value val = lv.get(2).evalValue(c);
|
||||
Value val = lv.get(2);
|
||||
NBTSerializableValue tagValue = (val instanceof NBTSerializableValue)
|
||||
? (NBTSerializableValue) val
|
||||
: new NBTSerializableValue(val.getString());
|
||||
@@ -933,7 +879,7 @@ public class Auxiliary {
|
||||
}
|
||||
else if (fdesc.type == FileArgument.Type.JSON)
|
||||
{
|
||||
List<String> data = Collections.singletonList(GSON.toJson(lv.get(2).evalValue(c).toJson()));
|
||||
List<String> data = Collections.singletonList(GSON.toJson(lv.get(2).toJson()));
|
||||
((CarpetScriptHost) c.host).removeResourceFile(fdesc);
|
||||
success = ((CarpetScriptHost) c.host).appendLogFile(fdesc, data);
|
||||
}
|
||||
@@ -942,7 +888,7 @@ public class Auxiliary {
|
||||
List<String> data = new ArrayList<>();
|
||||
if (lv.size()==3)
|
||||
{
|
||||
Value val = lv.get(2).evalValue(c);
|
||||
Value val = lv.get(2);
|
||||
if (val instanceof ListValue)
|
||||
{
|
||||
List<Value> lval = ((ListValue) val).getItems();
|
||||
@@ -957,166 +903,144 @@ public class Auxiliary {
|
||||
{
|
||||
for(int i = 2; i < lv.size(); i++)
|
||||
{
|
||||
data.add(lv.get(i).evalValue(c).getString());
|
||||
data.add(lv.get(i).getString());
|
||||
}
|
||||
}
|
||||
success = ((CarpetScriptHost) c.host).appendLogFile(fdesc, data);
|
||||
}
|
||||
return success?LazyValue.TRUE:LazyValue.FALSE;
|
||||
return BooleanValue.of(success);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("load_app_data", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("load_app_data", -1, (c, t, lv) ->
|
||||
{
|
||||
FileArgument fdesc = new FileArgument(null, FileArgument.Type.NBT, null, false, false, FileArgument.Reason.READ);
|
||||
if (lv.size()>0)
|
||||
{
|
||||
c.host.issueDeprecation("load_app_data(...) with arguments");
|
||||
String resource = recognizeResource(lv.get(0).evalValue(c), false);
|
||||
boolean shared = lv.size() > 1 && lv.get(1).evalValue(c).getBoolean();
|
||||
String resource = recognizeResource(lv.get(0), false);
|
||||
boolean shared = lv.size() > 1 && lv.get(1).getBoolean();
|
||||
fdesc = new FileArgument(resource, FileArgument.Type.NBT, null, false, shared, FileArgument.Reason.READ);
|
||||
}
|
||||
Tag state = ((CarpetScriptHost) c.host).readFileTag(fdesc);
|
||||
if (state == null)
|
||||
return (cc, tt) -> Value.NULL;
|
||||
Value retVal = new NBTSerializableValue(state);
|
||||
return (cc, tt) -> retVal;
|
||||
return NBTSerializableValue.of(((CarpetScriptHost) c.host).readFileTag(fdesc));
|
||||
});
|
||||
|
||||
expression.addLazyFunction("store_app_data", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("store_app_data", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
throw new InternalExpressionException("'store_app_data' needs NBT tag and an optional file");
|
||||
Value val = lv.get(0).evalValue(c);
|
||||
Value val = lv.get(0);
|
||||
FileArgument fdesc = new FileArgument(null, FileArgument.Type.NBT, null, false, false, FileArgument.Reason.CREATE);
|
||||
if (lv.size()>1)
|
||||
{
|
||||
c.host.issueDeprecation("store_app_data(...) with more than one argument");
|
||||
String resource = recognizeResource(lv.get(1).evalValue(c), false);
|
||||
boolean shared = lv.size() > 2 && lv.get(2).evalValue(c).getBoolean();
|
||||
String resource = recognizeResource(lv.get(1), false);
|
||||
boolean shared = lv.size() > 2 && lv.get(2).getBoolean();
|
||||
fdesc = new FileArgument(resource, FileArgument.Type.NBT, null, false, shared, FileArgument.Reason.CREATE);
|
||||
}
|
||||
NBTSerializableValue tagValue = (val instanceof NBTSerializableValue)
|
||||
? (NBTSerializableValue) val
|
||||
: new NBTSerializableValue(val.getString());
|
||||
Tag tag = tagValue.getTag();
|
||||
boolean success = ((CarpetScriptHost) c.host).writeTagFile(tag, fdesc);
|
||||
return success?LazyValue.TRUE:LazyValue.FALSE;
|
||||
return BooleanValue.of(((CarpetScriptHost) c.host).writeTagFile(tagValue.getTag(), fdesc));
|
||||
});
|
||||
|
||||
expression.addLazyFunction("statistic", 3, (c, t, lv) ->
|
||||
expression.addContextFunction("statistic", 3, (c, t, lv) ->
|
||||
{
|
||||
Value playerValue = lv.get(0).evalValue(c);
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
ServerPlayerEntity player = EntityValue.getPlayerByValue(cc.s.getMinecraftServer(), playerValue);
|
||||
if (player == null) return LazyValue.NULL;
|
||||
ServerPlayerEntity player = EntityValue.getPlayerByValue(cc.s.getMinecraftServer(), lv.get(0));
|
||||
if (player == null) return Value.NULL;
|
||||
Identifier category;
|
||||
Identifier statName;
|
||||
try
|
||||
{
|
||||
category = new Identifier(lv.get(1).evalValue(c).getString());
|
||||
statName = new Identifier(lv.get(2).evalValue(c).getString());
|
||||
category = new Identifier(lv.get(1).getString());
|
||||
statName = new Identifier(lv.get(2).getString());
|
||||
}
|
||||
catch (InvalidIdentifierException e)
|
||||
{
|
||||
return LazyValue.NULL;
|
||||
return Value.NULL;
|
||||
}
|
||||
StatType<?> type = Registry.STAT_TYPE.get(category);
|
||||
if (type == null)
|
||||
return LazyValue.NULL;
|
||||
if (type == null) return Value.NULL;
|
||||
Stat<?> stat = getStat(type, statName);
|
||||
if (stat == null)
|
||||
return LazyValue.NULL;
|
||||
return (_c, _t) -> new NumericValue(player.getStatHandler().getStat(stat));
|
||||
if (stat == null) return Value.NULL;
|
||||
return new NumericValue(player.getStatHandler().getStat(stat));
|
||||
});
|
||||
|
||||
//handle_event('event', function...)
|
||||
expression.addLazyFunction("handle_event", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("handle_event", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() < 2)
|
||||
throw new InternalExpressionException("'handle_event' requires at least two arguments, event name, and a callback");
|
||||
String event = lv.get(0).evalValue(c).getString();
|
||||
FunctionArgument<LazyValue> callback = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||
String event = lv.get(0).getString();
|
||||
FunctionArgument callback = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||
CarpetScriptHost host = ((CarpetScriptHost)c.host);
|
||||
Value success;
|
||||
if (callback.function == null)
|
||||
{
|
||||
success = host.getScriptServer().events.removeBuiltInEvent(event, host)?Value.TRUE:Value.FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
success = host.getScriptServer().events.handleCustomEvent(event, host, callback.function, callback.unpackArgs(c) )?Value.TRUE:Value.FALSE;
|
||||
}
|
||||
return (cc, tt) -> success;
|
||||
return BooleanValue.of(host.getScriptServer().events.removeBuiltInEvent(event, host));
|
||||
// args don't need to be checked will be checked at the event
|
||||
return BooleanValue.of( host.getScriptServer().events.handleCustomEvent(event, host, callback.function, callback.args ));
|
||||
});
|
||||
//signal_event('event', player or null, args.... ) -> number of apps notified
|
||||
expression.addLazyFunction("signal_event", -1, (c, t, lv) ->
|
||||
expression.addContextFunction("signal_event", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
throw new InternalExpressionException("'signal' requires at least one argument");
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
CarpetScriptServer server = ((CarpetScriptHost)c.host).getScriptServer();
|
||||
String eventName = lv.get(0).evalValue(c).getString();
|
||||
String eventName = lv.get(0).getString();
|
||||
// no such event yet
|
||||
if (CarpetEventServer.Event.getEvent(eventName, server) == null) return LazyValue.NULL;
|
||||
if (CarpetEventServer.Event.getEvent(eventName, server) == null) return Value.NULL;
|
||||
ServerPlayerEntity player = null;
|
||||
List<Value> args = Collections.emptyList();
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
player = EntityValue.getPlayerByValue(server.server, lv.get(1).evalValue(c));
|
||||
if (lv.size() > 2)
|
||||
{
|
||||
args = FunctionValue.resolveArgs(lv.subList(2, lv.size()), c, t);
|
||||
}
|
||||
player = EntityValue.getPlayerByValue(server.server, lv.get(1));
|
||||
if (lv.size() > 2) args = lv.subList(2, lv.size());
|
||||
}
|
||||
int counts = ((CarpetScriptHost)c.host).getScriptServer().events.signalEvent(eventName, cc, player, args);
|
||||
if (counts < 0) return LazyValue.NULL;
|
||||
Value ret = new NumericValue(counts);
|
||||
return (c_, t_) -> ret;
|
||||
if (counts < 0) return Value.NULL;
|
||||
return new NumericValue(counts);
|
||||
});
|
||||
|
||||
// nbt_storage()
|
||||
// nbt_storage(key)
|
||||
// nbt_storage(key, nbt)
|
||||
expression.addLazyFunction("nbt_storage", -1, (c, t, lv) -> {
|
||||
expression.addContextFunction("nbt_storage", -1, (c, t, lv) -> {
|
||||
if (lv.size() > 2) throw new InternalExpressionException("'nbt_storage' requires 0, 1 or 2 arguments.");
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
DataCommandStorage storage = cc.s.getMinecraftServer().getDataCommandStorage();
|
||||
if (lv.size() == 0) {
|
||||
Value ret = ListValue.wrap(storage.getIds().map(i -> new StringValue(nameFromRegistryId(i))).collect(Collectors.toList()));
|
||||
return (_c, _t) -> ret;
|
||||
}
|
||||
String key = lv.get(0).evalValue(c).getString();
|
||||
if (lv.size() == 0)
|
||||
return ListValue.wrap(storage.getIds().map(i -> new StringValue(nameFromRegistryId(i))).collect(Collectors.toList()));
|
||||
String key = lv.get(0).getString();
|
||||
CompoundTag old_nbt = storage.get(new Identifier(key));
|
||||
if (lv.size() == 2) {
|
||||
Value nbt = lv.get(1).evalValue(c);
|
||||
Value nbt = lv.get(1);
|
||||
NBTSerializableValue new_nbt = (nbt instanceof NBTSerializableValue) ? (NBTSerializableValue) nbt
|
||||
: NBTSerializableValue.parseString(nbt.getString(), true);
|
||||
storage.set(new Identifier(key), new_nbt.getCompoundTag());
|
||||
}
|
||||
if (old_nbt == null) return LazyValue.NULL;
|
||||
return (_c, _t) -> new NBTSerializableValue(old_nbt);
|
||||
return NBTSerializableValue.of(old_nbt);
|
||||
});
|
||||
|
||||
// script run create_datapack('foo', {'foo' -> {'bar.json' -> {'c' -> true,'d' -> false,'e' -> {'foo' -> [1,2,3]},'a' -> 'foobar','b' -> 5}}})
|
||||
expression.addLazyFunction("create_datapack", 2, (c, t, lv) -> {
|
||||
expression.addContextFunction("create_datapack", 2, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
String origName = lv.get(0).evalValue(c).getString();
|
||||
String origName = lv.get(0).getString();
|
||||
String name = InputValidator.validateSimpleString(origName, true);
|
||||
MinecraftServer server = cc.s.getMinecraftServer();
|
||||
for (String dpName : server.getDataPackManager().getNames())
|
||||
{
|
||||
if (dpName.equalsIgnoreCase("file/"+name+".zip") ||
|
||||
dpName.equalsIgnoreCase("file/"+name))
|
||||
return LazyValue.NULL;
|
||||
return Value.NULL;
|
||||
|
||||
}
|
||||
Value dpdata = lv.get(1).evalValue(c);
|
||||
Value dpdata = lv.get(1);
|
||||
if (!(dpdata instanceof MapValue))
|
||||
throw new InternalExpressionException("datapack data needs to be a valid map type");
|
||||
ResourcePackManager packManager = server.getDataPackManager();
|
||||
Path dbFloder = server.getSavePath(WorldSavePath.DATAPACKS);
|
||||
Path packFloder = dbFloder.resolve(name+".zip");
|
||||
if (Files.exists(packFloder) || Files.exists(dbFloder.resolve(name))) return LazyValue.NULL;
|
||||
if (Files.exists(packFloder) || Files.exists(dbFloder.resolve(name))) return Value.NULL;
|
||||
Boolean [] successful = new Boolean[]{true};
|
||||
server.submitAndJoin( () ->
|
||||
{
|
||||
@@ -1161,10 +1085,10 @@ public class Auxiliary {
|
||||
|
||||
}
|
||||
});
|
||||
return successful[0]?LazyValue.TRUE:LazyValue.FALSE;
|
||||
return BooleanValue.of(successful[0]);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("enable_hidden_dimensions", 0, (c, t, lv) -> {
|
||||
expression.addContextFunction("enable_hidden_dimensions", 0, (c, t, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
// from minecraft.server.Main.main
|
||||
MinecraftServer server = cc.s.getMinecraftServer();
|
||||
@@ -1184,7 +1108,7 @@ public class Auxiliary {
|
||||
|
||||
RegistryOps<Tag> registryOps = RegistryOps.of(NbtOps.INSTANCE, serverRM.getResourceManager(), (DynamicRegistryManager.Impl) server.getRegistryManager());
|
||||
SaveProperties saveProperties = session.readLevelProperties(registryOps, dataPackSettings2);
|
||||
if (saveProperties == null) return LazyValue.NULL;
|
||||
if (saveProperties == null) return Value.NULL;
|
||||
//session.backupLevelDataFile(server.getRegistryManager(), saveProperties); // no need
|
||||
|
||||
// MinecraftServer.createWorlds
|
||||
@@ -1209,8 +1133,7 @@ public class Auxiliary {
|
||||
existing_worlds.put(registryKey2, serverWorld2);
|
||||
}
|
||||
}
|
||||
Value ret = ListValue.wrap(addeds);
|
||||
return (_c, _t) -> ret;
|
||||
return ListValue.wrap(addeds);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1267,8 +1190,6 @@ public class Auxiliary {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static <T> Stat<T> getStat(StatType<T> type, Identifier id)
|
||||
{
|
||||
T key = type.getRegistry().get(id);
|
||||
|
||||
Reference in new issue
Block a user