From 4c031ba2ef1a2499efdbe07cd51a606471b130a8 Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 9 Jul 2019 16:51:18 -0300 Subject: [PATCH] mark chunks for resend --- src/main/java/carpet/CarpetServer.java | 2 +- .../java/carpet/script/CarpetExpression.java | 54 ++++++++++++------- .../carpet/script/CarpetScriptServer.java | 33 +++++++++--- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/main/java/carpet/CarpetServer.java b/src/main/java/carpet/CarpetServer.java index 5c411f08..a0da2265 100644 --- a/src/main/java/carpet/CarpetServer.java +++ b/src/main/java/carpet/CarpetServer.java @@ -64,7 +64,7 @@ public class CarpetServer // static for now - easier to handle all around the co { TickSpeed.tick(server); HUDController.update_hud(server); - scriptServer.events.tick(); + scriptServer.tick(); //in case something happens CarpetSettings.impendingFillSkipUpdates = false; diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 19389f1c..5a04b640 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -65,6 +65,8 @@ import net.minecraft.util.registry.Registry; import net.minecraft.world.Heightmap; import net.minecraft.world.LightType; import net.minecraft.world.World; +import net.minecraft.world.biome.Biome; +import net.minecraft.world.chunk.Chunk; import net.minecraft.world.loot.context.LootContext; import net.minecraft.world.loot.context.LootContextParameters; import org.apache.commons.lang3.tuple.Pair; @@ -926,6 +928,40 @@ public class CarpetExpression return (_c, _t) -> retval; }); + this.expr.addLazyFunction("trace_block", -1, (c, t, lv) -> { + Value entityValue = lv.get(0).evalValue(c); + if (!(entityValue instanceof EntityValue)) + throw new InternalExpressionException("First argument of trace_block should be an entity"); + Entity e = ((EntityValue) entityValue).getEntity(); + HitResult hitres = Tracer.traceEntityLook(e, 20); + switch (hitres.getType()) + { + case MISS: + return (_c, _t) -> Value.NULL; + case BLOCK: + return (_c, _t) -> new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() ); + case ENTITY: + return (_c, _t) -> new EntityValue(((EntityHitResult)hitres).getEntity()); + } + return LazyValue.NULL; + }); + + this.expr.addLazyFunction("set_biome", -1, (c, t, lv) -> { + CarpetContext cc = (CarpetContext)c; + BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0); + String biomeName = lv.get(locator.offset+0).evalValue(c).getString(); + Biome biome = Registry.BIOME.get(new Identifier(biomeName)); + byte biomeId = (byte) (Registry.BIOME.getRawId(biome) & 255); + ServerWorld world = cc.s.getWorld(); + BlockPos pos = locator.block.getPos(); + Chunk chunk = world.getChunk(pos); + chunk.getBiomeArray()[(pos.getX() & 15) | (pos.getZ() & 15) << 4] = biome; + chunk.setShouldSave(true); + //world.method_14178().markForUpdate(pos); + CarpetServer.scriptServer.markChunkForUpdate(world, pos); + return LazyValue.NULL; + + }); } @@ -1245,24 +1281,6 @@ public class CarpetExpression Value res = new NumericValue(item.getStack().getCount()); return (_c, _t) -> res; }); - - this.expr.addLazyFunction("trace_block", -1, (c, t, lv) -> { - Value entityValue = lv.get(0).evalValue(c); - if (!(entityValue instanceof EntityValue)) - throw new InternalExpressionException("First argument of trace_block should be an entity"); - Entity e = ((EntityValue) entityValue).getEntity(); - HitResult hitres = Tracer.traceEntityLook(e, 20); - switch (hitres.getType()) - { - case MISS: - return (_c, _t) -> Value.NULL; - case BLOCK: - return (_c, _t) -> new BlockValue(null, e.getEntityWorld(), ((BlockHitResult)hitres).getBlockPos() ); - case ENTITY: - return (_c, _t) -> new EntityValue(((EntityHitResult)hitres).getEntity()); - } - return LazyValue.NULL; - }); } diff --git a/src/main/java/carpet/script/CarpetScriptServer.java b/src/main/java/carpet/script/CarpetScriptServer.java index 0b686ad3..8c96ea7d 100644 --- a/src/main/java/carpet/script/CarpetScriptServer.java +++ b/src/main/java/carpet/script/CarpetScriptServer.java @@ -14,16 +14,16 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.tree.CommandNode; +import net.minecraft.client.network.packet.ChunkDataS2CPacket; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.chunk.Chunk; +import org.apache.commons.lang3.tuple.Pair; import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import static net.minecraft.server.command.CommandManager.argument; @@ -38,6 +38,7 @@ public class CarpetScriptServer public boolean stopAll; Set holyMoly; public CarpetEventServer events; + private Set> modifiedChunks = new HashSet<>(); public static List bundledModuleData = new ArrayList(){{ add(new CameraPathModule()); @@ -330,4 +331,24 @@ public class CarpetScriptServer } return true; } + + void markChunkForUpdate(ServerWorld world, BlockPos pos) + { + modifiedChunks.add(Pair.of(world, new ChunkPos(pos))); + } + + public void tick() + { + for (Pair tuple : modifiedChunks) + { + //ChunkDataS2CPacket + ServerWorld world = tuple.getLeft(); + ChunkPos chpos = tuple.getRight(); + Chunk chunk = world.getChunk(chpos.x, chpos.z); + //..... + + } + modifiedChunks.clear(); + events.tick(); + } }