refactor
This commit is contained in:
1 parent
59575ec607
commit
33f400e904
17 files changed
+369
-303
No files matched your search
@@ -15,9 +15,9 @@ import carpet.script.argument.BlockArgument;
|
||||
import carpet.script.argument.FunctionArgument;
|
||||
import carpet.script.argument.Vector3Argument;
|
||||
import carpet.script.exception.ExitStatement;
|
||||
import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.utils.ShapeDispatcher;
|
||||
import carpet.script.utils.WorldTools;
|
||||
import carpet.script.value.BlockValue;
|
||||
import carpet.script.value.EntityValue;
|
||||
import carpet.script.value.FormattedTextValue;
|
||||
@@ -26,7 +26,6 @@ import carpet.script.value.NBTSerializableValue;
|
||||
import carpet.script.value.NullValue;
|
||||
import carpet.script.value.NumericValue;
|
||||
import carpet.script.value.StringValue;
|
||||
import carpet.script.value.ThreadValue;
|
||||
import carpet.script.value.Value;
|
||||
import carpet.script.value.ValueConversions;
|
||||
import carpet.utils.Messenger;
|
||||
@@ -66,7 +65,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
@@ -459,60 +457,6 @@ public class Auxiliary {
|
||||
return (cc, tt) -> ret;
|
||||
});
|
||||
|
||||
//"overidden" native call to cancel if on main thread
|
||||
expression.addLazyFunction("task_join", 1, (c, t, lv) -> {
|
||||
if (((CarpetContext)c).s.getMinecraftServer().isOnThread())
|
||||
throw new InternalExpressionException("'task_join' cannot be called from main thread to avoid deadlocks");
|
||||
Value v = lv.get(0).evalValue(c);
|
||||
if (!(v instanceof ThreadValue))
|
||||
throw new InternalExpressionException("'task_join' could only be used with a task value");
|
||||
Value ret = ((ThreadValue) v).join();
|
||||
return (_c, _t) -> ret;
|
||||
});
|
||||
|
||||
expression.addLazyFunctionWithDelegation("task_dock", 1, (c, t, expr, tok, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
MinecraftServer server = cc.s.getMinecraftServer();
|
||||
if (server.isOnThread()) return lv.get(0); // pass through for on thread tasks
|
||||
Value[] result = new Value[]{Value.NULL};
|
||||
RuntimeException[] internal = new RuntimeException[]{null};
|
||||
try
|
||||
{
|
||||
((CarpetContext) c).s.getMinecraftServer().submitAndJoin(() ->
|
||||
{
|
||||
try
|
||||
{
|
||||
result[0] = lv.get(0).evalValue(c, t);
|
||||
}
|
||||
catch (ExpressionException exc)
|
||||
{
|
||||
internal[0] = exc;
|
||||
}
|
||||
catch (InternalExpressionException exc)
|
||||
{
|
||||
internal[0] = new ExpressionException(c, expr, tok, exc.getMessage(), exc.stack);
|
||||
}
|
||||
|
||||
catch (ArithmeticException exc)
|
||||
{
|
||||
internal[0] = new ExpressionException(c, expr, tok, "Your math is wrong, "+exc.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (CompletionException exc)
|
||||
{
|
||||
throw new InternalExpressionException("Error while executing docked task section, internal stack trace is gone");
|
||||
}
|
||||
if (internal[0] != null)
|
||||
{
|
||||
throw internal[0];
|
||||
}
|
||||
Value ret = result[0]; // preventing from lazy evaluating of the result in case a future completes later
|
||||
return (_c, _t) -> ret;
|
||||
// pass through placeholder
|
||||
// implmenetation should dock the task on the main thread.
|
||||
});
|
||||
|
||||
expression.addLazyFunction("run", 1, (c, t, lv) -> {
|
||||
BlockPos target = ((CarpetContext)c).origin;
|
||||
Vec3d posf = new Vec3d((double)target.getX()+0.5D,(double)target.getY(),(double)target.getZ()+0.5D);
|
||||
@@ -616,7 +560,7 @@ public class Auxiliary {
|
||||
BlockPos pos = locator.block.getPos();
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
((ThreadedAnvilChunkStorageInterface) world.getChunkManager().threadedAnvilChunkStorage).relightChunk(new ChunkPos(pos));
|
||||
WorldAccess.forceChunkUpdate(pos, world);
|
||||
WorldTools.forceChunkUpdate(pos, world);
|
||||
return LazyValue.TRUE;
|
||||
});
|
||||
|
||||
@@ -708,7 +652,7 @@ public class Auxiliary {
|
||||
if (res == null)
|
||||
return;
|
||||
if (what.equalsIgnoreCase("boulder")) // there might be more of those
|
||||
WorldAccess.forceChunkUpdate(locator.block.getPos(), ((CarpetContext) c).s.getWorld());
|
||||
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
|
||||
|
||||
@@ -39,7 +39,8 @@ import java.util.stream.Collectors;
|
||||
public class Entities {
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
expression.addLazyFunction("player", -1, (c, t, lv) -> {
|
||||
expression.addLazyFunction("player", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() ==0)
|
||||
{
|
||||
|
||||
@@ -106,7 +107,8 @@ public class Entities {
|
||||
return (cc, tt) -> finalVar;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("spawn", -1, (c, t, lv) -> {
|
||||
expression.addLazyFunction("spawn", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
if (lv.size() < 2)
|
||||
throw new InternalExpressionException("'spawn' function takes mob name, and position to spawn");
|
||||
@@ -176,7 +178,8 @@ public class Entities {
|
||||
return (cc, tt) -> new EntityValue(e);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("entity_list", 1, (c, t, lv) -> {
|
||||
expression.addLazyFunction("entity_list", 1, (c, t, lv) ->
|
||||
{
|
||||
String who = lv.get(0).evalValue(c).getString();
|
||||
Pair<EntityType<?>, Predicate<? super Entity>> pair = EntityValue.getPredicate(who);
|
||||
if (pair == null)
|
||||
@@ -188,7 +191,8 @@ public class Entities {
|
||||
return (_c, _t ) -> retval;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("entity_area", 7, (c, t, lv) -> {
|
||||
expression.addLazyFunction("entity_area", 7, (c, t, lv) ->
|
||||
{
|
||||
Vec3d center = new Vec3d(
|
||||
NumericValue.asNumber(lv.get(1).evalValue(c)).getDouble(),
|
||||
NumericValue.asNumber(lv.get(2).evalValue(c)).getDouble(),
|
||||
@@ -221,7 +225,8 @@ public class Entities {
|
||||
return (c_, t_) -> ListValue.wrap(retlist);
|
||||
});
|
||||
|
||||
expression.addLazyFunction("query", -1, (c, t, lv) -> {
|
||||
expression.addLazyFunction("query", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size()<2)
|
||||
{
|
||||
throw new InternalExpressionException("'query' takes entity as a first argument, and queried feature as a second");
|
||||
@@ -241,7 +246,8 @@ public class Entities {
|
||||
});
|
||||
|
||||
// or update
|
||||
expression.addLazyFunction("modify", -1, (c, t, lv) -> {
|
||||
expression.addLazyFunction("modify", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size()<2)
|
||||
{
|
||||
throw new InternalExpressionException("'modify' takes entity as a first argument, and queried feature as a second");
|
||||
|
||||
@@ -309,7 +309,8 @@ public class Inventories {
|
||||
});
|
||||
|
||||
//inventory_remove(<b, e>, <item>, <amount=1>) -> bool
|
||||
expression.addLazyFunction("inventory_remove", -1, (c, t, lv) -> {
|
||||
expression.addLazyFunction("inventory_remove", -1, (c, t, lv) ->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext) c;
|
||||
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
|
||||
if (inventoryLocator == null)
|
||||
|
||||
@@ -74,7 +74,8 @@ public class Scoreboards {
|
||||
return (_c, _t) -> retval;
|
||||
});
|
||||
|
||||
expression.addLazyFunction("scoreboard_remove", -1, (c, t, lv)-> {
|
||||
expression.addLazyFunction("scoreboard_remove", -1, (c, t, lv)->
|
||||
{
|
||||
if (lv.size()==0) throw new InternalExpressionException("'scoreboard_remove' requires at least one parameter");
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
|
||||
@@ -98,7 +99,8 @@ public class Scoreboards {
|
||||
// objective_add('lvl','level')
|
||||
// objective_add('counter')
|
||||
|
||||
expression.addLazyFunction("scoreboard_add", -1, (c, t, lv)-> {
|
||||
expression.addLazyFunction("scoreboard_add", -1, (c, t, lv)->
|
||||
{
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
Scoreboard scoreboard = cc.s.getMinecraftServer().getScoreboard();
|
||||
if (lv.size() == 0 || lv.size()>2) throw new InternalExpressionException("'scoreboard_add' should have one or two parameters");
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package carpet.script.api;
|
||||
|
||||
import carpet.script.CarpetContext;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.exception.ExpressionException;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.value.ThreadValue;
|
||||
import carpet.script.value.Value;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
public class Threading
|
||||
{
|
||||
public static void apply(Expression expression)
|
||||
{
|
||||
//"overidden" native call to cancel if on main thread
|
||||
expression.addLazyFunction("task_join", 1, (c, t, lv) -> {
|
||||
if (((CarpetContext)c).s.getMinecraftServer().isOnThread())
|
||||
throw new InternalExpressionException("'task_join' cannot be called from main thread to avoid deadlocks");
|
||||
Value v = lv.get(0).evalValue(c);
|
||||
if (!(v instanceof ThreadValue))
|
||||
throw new InternalExpressionException("'task_join' could only be used with a task value");
|
||||
Value ret = ((ThreadValue) v).join();
|
||||
return (_c, _t) -> ret;
|
||||
});
|
||||
|
||||
expression.addLazyFunctionWithDelegation("task_dock", 1, (c, t, expr, tok, lv) -> {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
MinecraftServer server = cc.s.getMinecraftServer();
|
||||
if (server.isOnThread()) return lv.get(0); // pass through for on thread tasks
|
||||
Value[] result = new Value[]{Value.NULL};
|
||||
RuntimeException[] internal = new RuntimeException[]{null};
|
||||
try
|
||||
{
|
||||
((CarpetContext) c).s.getMinecraftServer().submitAndJoin(() ->
|
||||
{
|
||||
try
|
||||
{
|
||||
result[0] = lv.get(0).evalValue(c, t);
|
||||
}
|
||||
catch (ExpressionException exc)
|
||||
{
|
||||
internal[0] = exc;
|
||||
}
|
||||
catch (InternalExpressionException exc)
|
||||
{
|
||||
internal[0] = new ExpressionException(c, expr, tok, exc.getMessage(), exc.stack);
|
||||
}
|
||||
|
||||
catch (ArithmeticException exc)
|
||||
{
|
||||
internal[0] = new ExpressionException(c, expr, tok, "Your math is wrong, "+exc.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (CompletionException exc)
|
||||
{
|
||||
throw new InternalExpressionException("Error while executing docked task section, internal stack trace is gone");
|
||||
}
|
||||
if (internal[0] != null)
|
||||
{
|
||||
throw internal[0];
|
||||
}
|
||||
Value ret = result[0]; // preventing from lazy evaluating of the result in case a future completes later
|
||||
return (_c, _t) -> ret;
|
||||
// pass through placeholder
|
||||
// implmenetation should dock the task on the main thread.
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import carpet.script.value.NullValue;
|
||||
import carpet.script.value.NumericValue;
|
||||
import carpet.script.value.StringValue;
|
||||
import carpet.script.value.Value;
|
||||
import carpet.script.value.ValueConversions;
|
||||
import carpet.utils.BlockInfo;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
@@ -56,7 +57,6 @@ import net.minecraft.item.SwordItem;
|
||||
import net.minecraft.item.TridentItem;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.packet.s2c.play.ChunkDataS2CPacket;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ChunkTicket;
|
||||
import net.minecraft.server.world.ChunkTicketType;
|
||||
@@ -65,7 +65,6 @@ import net.minecraft.sound.BlockSoundGroup;
|
||||
import net.minecraft.sound.SoundCategory;
|
||||
import net.minecraft.state.StateManager;
|
||||
import net.minecraft.state.property.Property;
|
||||
import net.minecraft.structure.StructurePiece;
|
||||
import net.minecraft.structure.StructureStart;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.Clearable;
|
||||
@@ -118,23 +117,6 @@ public class WorldAccess {
|
||||
// dummy entity for dummy requirements in the loot tables (see snowball)
|
||||
private static FallingBlockEntity DUMMY_ENTITY = new FallingBlockEntity(EntityType.FALLING_BLOCK, null);
|
||||
|
||||
public static void forceChunkUpdate(BlockPos pos, ServerWorld world)
|
||||
{
|
||||
WorldChunk worldChunk = world.getChunkManager().getWorldChunk(pos.getX()>>4, pos.getZ()>>4, false);
|
||||
if (worldChunk != null)
|
||||
{
|
||||
int vd = world.getServer().getPlayerManager().getViewDistance() * 16;
|
||||
int vvd = vd * vd;
|
||||
List<ServerPlayerEntity> nearbyPlayers = world.getPlayers(p -> pos.getSquaredDistance(p.getX(), pos.getY(), p.getZ(), true) < vvd);
|
||||
if (!nearbyPlayers.isEmpty())
|
||||
{
|
||||
ChunkDataS2CPacket packet = new ChunkDataS2CPacket(worldChunk, 65535);
|
||||
ChunkPos chpos = new ChunkPos(pos);
|
||||
nearbyPlayers.forEach(p -> p.networkHandler.sendPacket(packet));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static LazyValue booleanStateTest(
|
||||
Context c,
|
||||
String name,
|
||||
@@ -270,30 +252,6 @@ public class WorldAccess {
|
||||
}
|
||||
}
|
||||
|
||||
private static Value structureToValue(StructureStart<?> structure)
|
||||
{
|
||||
if (structure == null || structure == StructureStart.DEFAULT) return Value.NULL;
|
||||
List<Value> pieces = new ArrayList<>();
|
||||
for (StructurePiece piece : structure.getChildren())
|
||||
{
|
||||
BlockBox box = piece.getBoundingBox();
|
||||
pieces.add(ListValue.of(
|
||||
new StringValue( NBTSerializableValue.nameFromRegistryId(Registry.STRUCTURE_PIECE.getId(piece.getType()))),
|
||||
(piece.getFacing()== null)?Value.NULL: new StringValue(piece.getFacing().getName()),
|
||||
ListValue.fromTriple(box.minX, box.minY, box.minZ),
|
||||
ListValue.fromTriple(box.maxX, box.maxY, box.maxZ)
|
||||
));
|
||||
};
|
||||
BlockBox boundingBox = structure.getBoundingBox();
|
||||
Map<Value, Value> ret = new HashMap<>();
|
||||
ret.put(new StringValue("box"), ListValue.of(
|
||||
ListValue.fromTriple(boundingBox.minX, boundingBox.minY, boundingBox.minZ),
|
||||
ListValue.fromTriple(boundingBox.maxX, boundingBox.maxY, boundingBox.maxZ)
|
||||
));
|
||||
ret.put(new StringValue("pieces"), ListValue.wrap(pieces));
|
||||
return MapValue.wrap(ret);
|
||||
}
|
||||
|
||||
private static void BooYah(ChunkGenerator generator)
|
||||
{
|
||||
synchronized (generator)
|
||||
@@ -1058,7 +1016,7 @@ public class WorldAccess {
|
||||
BlockPos pos = locator.block.getPos();
|
||||
Chunk chunk = world.getChunk(pos.getX() >> 4, pos.getZ() >> 4, ChunkStatus.BIOMES);
|
||||
((BiomeArrayInterface)chunk.getBiomeArray()).setBiomeAtIndex(pos, world, biome);
|
||||
if (doImmediateUpdate) forceChunkUpdate(pos, world);
|
||||
if (doImmediateUpdate) WorldTools.forceChunkUpdate(pos, world);
|
||||
return LazyValue.TRUE;
|
||||
});
|
||||
|
||||
@@ -1066,7 +1024,7 @@ public class WorldAccess {
|
||||
CarpetContext cc = (CarpetContext)c;
|
||||
BlockPos pos = BlockArgument.findIn(cc, lv, 0).block.getPos();
|
||||
ServerWorld world = cc.s.getWorld();
|
||||
cc.s.getMinecraftServer().submitAndJoin( () -> forceChunkUpdate(pos, world));
|
||||
cc.s.getMinecraftServer().submitAndJoin( () -> WorldTools.forceChunkUpdate(pos, world));
|
||||
return LazyValue.TRUE;
|
||||
});
|
||||
|
||||
@@ -1130,7 +1088,7 @@ public class WorldAccess {
|
||||
StructureStart<?> start = FeatureGenerator.shouldStructureStartAt(world, pos, structure, needSize);
|
||||
if (start == null) return LazyValue.NULL;
|
||||
if (!needSize) return LazyValue.TRUE;
|
||||
Value ret = structureToValue(start);
|
||||
Value ret = ValueConversions.fromStructure(start);
|
||||
return (_c, _t) -> ret;
|
||||
}
|
||||
Map<Value, Value> ret = new HashMap<>();
|
||||
@@ -1140,7 +1098,7 @@ public class WorldAccess {
|
||||
if (start == null) continue;
|
||||
|
||||
Value key = new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.STRUCTURE_FEATURE.getId(str)));
|
||||
ret.put(key, (!needSize)?Value.NULL:structureToValue(start));
|
||||
ret.put(key, (!needSize)?Value.NULL: ValueConversions.fromStructure(start));
|
||||
}
|
||||
Value retMap = MapValue.wrap(ret);
|
||||
return (_c, _t) -> retMap;
|
||||
@@ -1172,7 +1130,7 @@ public class WorldAccess {
|
||||
}
|
||||
String structureName = lv.get(locator.offset).evalValue(c).getString().toLowerCase(Locale.ROOT);
|
||||
StructureStart start = structures.get(Registry.STRUCTURE_FEATURE.get(new Identifier(structureName)));
|
||||
Value ret = structureToValue(start);
|
||||
Value ret = ValueConversions.fromStructure(start);
|
||||
return (_c, _t) -> ret;
|
||||
});
|
||||
|
||||
@@ -1321,7 +1279,7 @@ public class WorldAccess {
|
||||
{
|
||||
if (world.getChunk(chpos.x, chpos.z, ChunkStatus.FULL, false) != null)
|
||||
{
|
||||
forceChunkUpdate(chpos.getStartPos(), world);
|
||||
WorldTools.forceChunkUpdate(chpos.getStartPos(), world);
|
||||
}
|
||||
}
|
||||
result[0] = MapValue.wrap(report.entrySet().stream().collect(Collectors.toMap(
|
||||
|
||||
Reference in new issue
Block a user