refactor access to context data

This commit is contained in:
gnembon committed 2023-01-16 19:01:10 +01:00
1 parent bc31faca21
commit 8dd08d5cb1
15 files changed
+239 -194

No files matched your search

+42 -40
View File
@@ -167,8 +167,9 @@ public class Auxiliary {
Vec3 vec = locator.vec;
double d0 = Math.pow(volume > 1.0F ? (double)(volume * 16.0F) : 16.0D, 2.0D);
int count = 0;
long seed = cc.s.getLevel().getRandom().nextLong();
for (ServerPlayer player : cc.s.getLevel().getPlayers( (p) -> p.distanceToSqr(vec) < d0))
final ServerLevel level = cc.level();
long seed = level.getRandom().nextLong();
for (ServerPlayer player : level.getPlayers( (p) -> p.distanceToSqr(vec) < d0))
{
count++;
player.connection.send(new ClientboundSoundPacket(soundHolder, mixer, vec.x, vec.y, vec.z, volume, pitch, seed));
@@ -180,8 +181,8 @@ public class Auxiliary {
{
CarpetContext cc = (CarpetContext)c;
if (lv.size() == 0) return ListValue.wrap(BuiltInRegistries.PARTICLE_TYPE.keySet().stream().map(ValueConversions::of));
MinecraftServer ms = cc.s.getServer();
ServerLevel world = cc.s.getLevel();
MinecraftServer ms = cc.server();
ServerLevel world = cc.level();
Vector3Argument locator = Vector3Argument.findIn(lv, 1);
String particleName = lv.get(0).getString();
int count = 10;
@@ -227,7 +228,7 @@ public class Auxiliary {
expression.addContextFunction("particle_line", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerLevel world = cc.s.getLevel();
ServerLevel world = cc.level();
String particleName = lv.get(0).getString();
ParticleOptions particle = ShapeDispatcher.getParticleData(particleName, world.registryAccess());
Vector3Argument pos1 = Vector3Argument.findIn(lv, 1);
@@ -252,7 +253,7 @@ public class Auxiliary {
}
else
{
player = cc.s.getServer().getPlayerList().getPlayerByName(playerValue.getString());
player = cc.server().getPlayerList().getPlayerByName(playerValue.getString());
}
}
}
@@ -263,12 +264,12 @@ public class Auxiliary {
));
});
expression.addContextFunction("item_display_name", 1, (c, t, lv) -> new FormattedTextValue(ValueConversions.getItemStackFromValue(lv.get(0), false, ((CarpetContext)c).s.registryAccess() ).getHoverName()));
expression.addContextFunction("item_display_name", 1, (c, t, lv) -> new FormattedTextValue(ValueConversions.getItemStackFromValue(lv.get(0), false, ((CarpetContext)c).registryAccess()).getHoverName()));
expression.addContextFunction("particle_box", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerLevel world = cc.s.getLevel();
ServerLevel world = cc.level();
String particleName = lv.get(0).getString();
ParticleOptions particle = ShapeDispatcher.getParticleData(particleName, world.registryAccess() );
Vector3Argument pos1 = Vector3Argument.findIn(lv, 1);
@@ -294,7 +295,7 @@ public class Auxiliary {
}
else
{
player = cc.s.getServer().getPlayerList().getPlayerByName(playerValue.getString());
player = cc.server().getPlayerList().getPlayerByName(playerValue.getString());
}
}
}
@@ -315,7 +316,7 @@ public class Auxiliary {
expression.addContextFunction("draw_shape", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerLevel world = cc.s.getLevel();
ServerLevel world = cc.level();
MinecraftServer server = world.getServer();
Set<ServerPlayer> playerTargets = new HashSet<>();
List<ShapeDispatcher.ShapeWithConfig> shapes = new ArrayList<>();
@@ -335,7 +336,7 @@ public class Auxiliary {
}
ShapeDispatcher.sendShape(
(playerTargets.isEmpty())?cc.s.getLevel().players():playerTargets,
(playerTargets.isEmpty())?cc.level().players():playerTargets,
shapes
);
return Value.TRUE;
@@ -366,8 +367,8 @@ public class Auxiliary {
{
throw new InternalExpressionException("'create_marker' requires a name and three coordinates, with optional direction, and optional block on its head");
}
ArmorStand armorstand = new ArmorStand(EntityType.ARMOR_STAND, cc.s.getLevel());
Level level = cc.level();
ArmorStand armorstand = new ArmorStand(EntityType.ARMOR_STAND, level);
double yoffset;
if (targetBlock == null && name == null)
{
@@ -410,7 +411,7 @@ public class Auxiliary {
armorstand.setInvisible(true);
armorstand.setInvulnerable(true);
armorstand.getEntityData().set(ArmorStand.DATA_CLIENT_FLAGS, (byte)(interactable?8 : 16|8));
cc.s.getLevel().addFreshEntity(armorstand);
level.addFreshEntity(armorstand);
return new EntityValue(armorstand);
});
@@ -418,7 +419,7 @@ public class Auxiliary {
CarpetContext cc = (CarpetContext)c;
int total = 0;
String markerName = MARKER_STRING+"_"+((cc.host.getName()==null)?"":cc.host.getName());
for (Entity e : cc.s.getLevel().getEntities(EntityType.ARMOR_STAND, (as) -> as.getTags().contains(markerName)))
for (Entity e : cc.level().getEntities(EntityType.ARMOR_STAND, (as) -> as.getTags().contains(markerName)))
{
total ++;
e.discard(); // discard // remove();
@@ -468,7 +469,7 @@ public class Auxiliary {
expression.addContextFunction("print", -1, (c, t, lv) ->
{
if (lv.size() == 0 || lv.size() > 2) throw new InternalExpressionException("'print' takes one or two arguments");
CommandSourceStack s = ((CarpetContext)c).s;
CommandSourceStack s = ((CarpetContext)c).source();
MinecraftServer server = s.getServer();
Value res = lv.get(0);
List<ServerPlayer> targets = null;
@@ -500,7 +501,7 @@ public class Auxiliary {
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);
if (!(pVal instanceof ListValue)) pVal = ListValue.of(pVal);
MinecraftServer server = ((CarpetContext)c).s.getServer();
MinecraftServer server = ((CarpetContext)c).server();
Stream<ServerPlayer> targets = ((ListValue) pVal).getItems().stream().map(v ->
{
ServerPlayer player = EntityValue.getPlayerByValue(server, v);
@@ -574,7 +575,7 @@ public class Auxiliary {
map.put(target.getScoreboardName(), (MutableComponent) title);
total.getAndIncrement();
});
HUDController.update_hud(((CarpetContext)c).s.getServer(), targetList);
HUDController.update_hud(((CarpetContext)c).server(), targetList);
return NumericValue.of(total.get());
}
ClientboundSetTitlesAnimationPacket timesPacket; // TimesPacket
@@ -609,7 +610,7 @@ public class Auxiliary {
expression.addContextFunction("run", 1, (c, t, lv) ->
{
CommandSourceStack s = ((CarpetContext)c).s;
CommandSourceStack s = ((CarpetContext)c).source();
try
{
Component[] error = {null};
@@ -632,7 +633,7 @@ public class Auxiliary {
expression.addContextFunction("save", 0, (c, t, lv) ->
{
CommandSourceStack s = ((CarpetContext)c).s;
CommandSourceStack s = ((CarpetContext)c).source();
s.getServer().getPlayerList().saveAll();
s.getServer().saveAllChunks(true,true,true);
for (ServerLevel world : s.getServer().getAllLevels())
@@ -644,21 +645,21 @@ public class Auxiliary {
});
expression.addContextFunction("tick_time", 0, (c, t, lv) ->
new NumericValue(((CarpetContext) c).s.getServer().getTickCount()));
new NumericValue(((CarpetContext) c).server().getTickCount()));
expression.addContextFunction("world_time", 0, (c, t, lv) -> {
c.host.issueDeprecation("world_time()");
return new NumericValue(((CarpetContext) c).s.getLevel().getGameTime());
return new NumericValue(((CarpetContext) c).level().getGameTime());
});
expression.addContextFunction("day_time", -1, (c, t, lv) ->
{
Value time = new NumericValue(((CarpetContext) c).s.getLevel().getDayTime());
Value time = new NumericValue(((CarpetContext) c).level().getDayTime());
if (lv.size() > 0)
{
long newTime = NumericValue.asNumber(lv.get(0)).getLong();
if (newTime < 0) newTime = 0;
((CarpetContext) c).s.getLevel().setDayTime(newTime);// setTimeOfDay(newTime);
((CarpetContext) c).level().setDayTime(newTime);
}
return time;
});
@@ -672,14 +673,15 @@ public class Auxiliary {
expression.addContextFunction("game_tick", -1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext)c;
CommandSourceStack s = cc.s;
//CommandSourceStack s = cc.source();
MinecraftServer server = cc.server();
if (CarpetServer.scriptServer == null) return Value.NULL;
if (!s.getServer().isSameThread()) throw new InternalExpressionException("Unable to run ticks from threads");
if (!server.isSameThread()) throw new InternalExpressionException("Unable to run ticks from threads");
if (CarpetServer.scriptServer.tickDepth > 16) throw new InternalExpressionException("'game_tick' function caused other 'game_tick' functions to run. You should not allow that.");
try
{
CarpetServer.scriptServer.tickDepth ++;
((MinecraftServerInterface) s.getServer()).forceTick(() -> System.nanoTime() - CarpetServer.scriptServer.tickStart < 50000000L);
((MinecraftServerInterface) server).forceTick(() -> System.nanoTime() - CarpetServer.scriptServer.tickStart < 50000000L);
if (lv.size() > 0)
{
long ms_total = NumericValue.asNumber(lv.get(0)).getLong();
@@ -710,7 +712,7 @@ public class Auxiliary {
});
expression.addContextFunction("seed", -1, (c, t, lv) -> {
CommandSourceStack s = ((CarpetContext)c).s;
CommandSourceStack s = ((CarpetContext)c).source();
c.host.issueDeprecation("seed()");
return new NumericValue(s.getLevel().getSeed());
});
@@ -720,7 +722,7 @@ public class Auxiliary {
CarpetContext cc = (CarpetContext) c;
BlockArgument locator = BlockArgument.findIn(cc, lv, 0);
BlockPos pos = locator.block.getPos();
ServerLevel world = cc.s.getLevel();
ServerLevel world = cc.level();
((ThreadedAnvilChunkStorageInterface) world.getChunkSource().chunkMap).relightChunk(new ChunkPos(pos));
WorldTools.forceChunkUpdate(pos, world);
return Value.TRUE;
@@ -728,22 +730,22 @@ public class Auxiliary {
// Should this be deprecated for system_info('source_dimension')?
expression.addContextFunction("current_dimension", 0, (c, t, lv) ->
ValueConversions.of( ((CarpetContext)c).s.getLevel()));
ValueConversions.of( ((CarpetContext)c).level()));
expression.addContextFunction("view_distance", 0, (c, t, lv) -> {
c.host.issueDeprecation("view_distance()");
return new NumericValue(((CarpetContext)c).s.getServer().getPlayerList().getViewDistance());
return new NumericValue(((CarpetContext)c).server().getPlayerList().getViewDistance());
});
// lazy due to passthrough and context changing ability
expression.addLazyFunction("in_dimension", 2, (c, t, lv) -> {
CommandSourceStack outerSource = ((CarpetContext)c).s;
CommandSourceStack outerSource = ((CarpetContext)c).source();
Value dimensionValue = lv.get(0).evalValue(c);
Level world = ValueConversions.dimFromValue(dimensionValue, outerSource.getServer());
if (world == outerSource.getLevel()) return lv.get(1);
CommandSourceStack innerSource = outerSource.withLevel((ServerLevel)world);
Context newCtx = c.recreate();
((CarpetContext) newCtx).s = innerSource;
((CarpetContext) newCtx).swapSource(innerSource);
newCtx.variables = c.variables;
Value retval = lv.get(1).evalValue(newCtx);
return (cc, tt) -> retval;
@@ -754,7 +756,7 @@ public class Auxiliary {
{
Map<Value, Value> plopData = new HashMap<>();
CarpetContext cc = (CarpetContext)c;
RegistryAccess registryManager = cc.s.getLevel().registryAccess();
RegistryAccess registryManager = cc.registryAccess();
plopData.put(StringValue.of("scarpet_custom"),
ListValue.wrap(FeatureGenerator.featureMap.keySet().stream().sorted().map(StringValue::of).collect(Collectors.toList()))
);
@@ -777,14 +779,14 @@ public class Auxiliary {
throw new InternalExpressionException("'plop' needs extra argument indicating what to plop");
String what = lv.get(locator.offset).getString();
Value [] result = new Value[]{Value.NULL};
((CarpetContext)c).s.getServer().executeBlocking( () ->
((CarpetContext)c).server().executeBlocking( () ->
{
Boolean res = FeatureGenerator.plop(what, ((CarpetContext) c).s.getLevel(), locator.block.getPos());
Boolean res = FeatureGenerator.plop(what, ((CarpetContext) c).level(), locator.block.getPos());
if (res == null)
return;
if (what.equalsIgnoreCase("forest_rock")) // there might be more of those
WorldTools.forceChunkUpdate(locator.block.getPos(), ((CarpetContext) c).s.getLevel());
WorldTools.forceChunkUpdate(locator.block.getPos(), ((CarpetContext) c).level());
result[0] = BooleanValue.of(res);
});
return result[0];
@@ -958,7 +960,7 @@ public class Auxiliary {
expression.addContextFunction("statistic", 3, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext)c;
ServerPlayer player = EntityValue.getPlayerByValue(cc.s.getServer(), lv.get(0));
ServerPlayer player = EntityValue.getPlayerByValue(cc.server(), lv.get(0));
if (player == null) return Value.NULL;
ResourceLocation category;
ResourceLocation statName;
@@ -1012,7 +1014,7 @@ public class Auxiliary {
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;
CommandStorage storage = cc.s.getServer().getCommandStorage();
CommandStorage storage = cc.server().getCommandStorage();
if (lv.size() == 0)
return ListValue.wrap(storage.keys().map(i -> new StringValue(nameFromRegistryId(i))).collect(Collectors.toList()));
String key = lv.get(0).getString();
@@ -1031,7 +1033,7 @@ public class Auxiliary {
CarpetContext cc = (CarpetContext)c;
String origName = lv.get(0).getString();
String name = InputValidator.validateSimpleString(origName, true);
MinecraftServer server = cc.s.getServer();
MinecraftServer server = cc.server();
for (String dpName : server.getPackRepository().getAvailableIds())
{
if (dpName.equalsIgnoreCase("file/"+name+".zip") ||