From e12d9f1f94c2ab1f230e5e27eef12be7c4309455 Mon Sep 17 00:00:00 2001 From: Hilligans Date: Fri, 29 Dec 2023 23:00:22 -0600 Subject: [PATCH] Fixed world not being meshed properly --- .../dev/hilligans/ourcraft/client/Client.java | 13 +- .../graphics/api/IGraphicsEngine.java | 3 + .../graphics/tasks/WorldRenderTask.java | 138 +++--------------- .../tasks/WorldTransparentRenderTask.java | 49 +++---- .../ourcraft/network/ClientNetwork.java | 7 +- .../network/ClientNetworkHandler.java | 20 ++- .../packet/server/SSendChunkPacket.java | 3 +- .../newworldsystem/ClientCubicWorld.java | 20 +++ .../newworldsystem/CubicChunkContainer.java | 4 +- .../world/newworldsystem/CubicWorld.java | 2 +- .../ourcraft/world/newworldsystem/IWorld.java | 19 +++ 11 files changed, 121 insertions(+), 157 deletions(-) create mode 100644 src/main/java/dev/hilligans/ourcraft/world/newworldsystem/ClientCubicWorld.java diff --git a/src/main/java/dev/hilligans/ourcraft/client/Client.java b/src/main/java/dev/hilligans/ourcraft/client/Client.java index 5374d747..52fb729e 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/Client.java +++ b/src/main/java/dev/hilligans/ourcraft/client/Client.java @@ -35,6 +35,8 @@ import dev.hilligans.ourcraft.tag.CompoundNBTTag; import dev.hilligans.ourcraft.util.ArgumentContainer; import dev.hilligans.ourcraft.util.Logger; +import dev.hilligans.ourcraft.util.ThreadContext; +import dev.hilligans.ourcraft.world.newworldsystem.ClientCubicWorld; import dev.hilligans.ourcraft.world.newworldsystem.CubicWorld; import dev.hilligans.ourcraft.world.newworldsystem.IWorld; import dev.hilligans.ourcraft.save.WorldLoader; @@ -92,7 +94,7 @@ public class Client implements IClientPacketHandler { public Client(GameInstance gameInstance, ArgumentContainer argumentContainer) { this.gameInstance = gameInstance; - this.newClientWorld = new CubicWorld(gameInstance, 0,"", 64); + this.newClientWorld = new ClientCubicWorld(gameInstance, 0,"", 64); logger = gameInstance.LOGGER.withKey("client"); graphicsEngine = gameInstance.GRAPHICS_ENGINES.get("ourcraft:openglEngine"); ((OpenGLEngine)graphicsEngine).client = this; @@ -163,6 +165,15 @@ public void startClient() { cleanUp(); } + public void tick(ThreadContext threadContext) { + try(var $0 = threadContext.getSection().startSection("tick_world")) { + newClientWorld.tick(); + } + try(var $0 = threadContext.getSection().startSection("process_packets")) { + network.processPackets(); + } + } + public RenderWindow rWindow; public void cleanUp() { diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsEngine.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsEngine.java index 98e38f5a..0409b251 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsEngine.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsEngine.java @@ -52,6 +52,9 @@ default Runnable createRenderLoop(GameInstance gameInstance, RenderWindow w) { break out; } try (var $9 = section.startSection(window.getWindowName())) { + try (var $2 = section.startSection("tick")) { + window.getClient().tick(graphicsContext); + } try (var $2 = section.startSection("render")) { render(window, graphicsContext); } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java index 4aa48831..bc7e6e5e 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java @@ -52,8 +52,6 @@ public RenderTask getDefaultTask() { @Override public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphicsEngine engine, Client client, MatrixStack worldStack, MatrixStack screenStack, float delta) { - int a = 0; - IWorld world = client.newClientWorld; //engine.getDefaultImpl().setState(window, graphicsContext, new PipelineState().setDepth(true)); Vector3d pos = window.camera.getSavedPosition(); @@ -118,10 +116,7 @@ IChunk getChunk(int chunkX, int chunkY, int chunkZ, IWorld world) { public static int rebuildDistance = 10; - - void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client client, IGraphicsEngine engine, MatrixStack matrixStack, Vector3i playerChunkPos, IWorld world, int x, int y, int z, int chunkWidth, int chunkHeight) { - MeshHolder meshHolder = getMesh(x, y, z); for (Tuple tuple : primitiveBuilders) { asyncedChunks.remove(((tuple.getTypeA().getX()) << 32) | (tuple.getTypeA().getZ() & 0xffffffffL)); tuple.typeB.setVertexFormat(shaderSource.vertexFormat); @@ -129,58 +124,28 @@ void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client clie meshes.setChunk(tuple.getTypeA().getX(), tuple.getTypeA().getY(), tuple.getTypeA().getZ(), new MeshHolder().set(meshID, tuple.getTypeB().indices.size())); primitiveBuilders.remove(tuple); } - if (meshHolder != null) { - if (meshHolder.id != -1 && meshHolder.length != 0) { - matrixStack.updateFrustum(); - if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) { - matrixStack.push(); - matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth); - engine.getDefaultImpl().uploadMatrix(graphicsContext, matrixStack, shaderSource); - try(var $ = graphicsContext.getSection().startSection("submitChunkDrawCall")) { - engine.getDefaultImpl().drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length); - } - matrixStack.pop(); + IChunk chunk = getChunk(x, y, z, world); + if (chunk != null) { + MeshHolder meshHolder = getMesh(x, y, z); + if (chunk.isDirty() || meshHolder == null) { + if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) { + buildMesh(window, graphicsContext, chunk); } } - } else { - IChunk chunk = getChunk(x, y, z, world); - if (chunk != null) { - if (chunk.isDirty()) { - if ( getChunk(x + 1, y, z, world) != null && - getChunk(x - 1, y, z, world) != null && - getChunk(x, y + 1, z, world) != null && - getChunk(x, y - 1, z, world) != null && - getChunk(x, y, z + 1, world) != null && - getChunk(x, y, z - 1, world) != null) { - if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) { - buildMesh(window, graphicsContext, chunk); - //System.out.println(chunk.getX() + " " + chunk.getY() + " " + chunk.getZ()); - //System.out.println(chunk.getBlockState1(0,0,0).getBlock().getName()); - // chunk.setDirty(false); + if (meshHolder != null) { + if (meshHolder.id != -1 && meshHolder.length != 0) { + matrixStack.updateFrustum(); + if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) { + matrixStack.push(); + matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth); + + engine.getDefaultImpl().uploadMatrix(graphicsContext, matrixStack, shaderSource); + try (var $ = graphicsContext.getSection().startSection("submitChunkDrawCall")) { + engine.getDefaultImpl().drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length); } + matrixStack.pop(); } - /* if (!asyncedChunks.getOrDefault(((long) x << 32) ^ (int) (z & 0xffffffffL), false)) { - if (getChunk(x + 1, y, z, world) != null && - getChunk(x - 1, y, z, world) != null && - getChunk(x, y, z + 1, world) != null && - getChunk(x, y, z - 1, world) != null) { - if ((x < rebuildDistance && y < rebuildDistance && z < rebuildDistance)) { - buildMesh(window, graphicsContext, chunk); - } else { - if (putChunk(x, z)) { - chunkBuilder.submit(() -> { - PrimitiveBuilder primitiveBuilder = getPrimitiveBuilder(chunk); - primitiveBuilders.add(new Tuple<>(chunk, primitiveBuilder)); - }); - } - } - chunk.setDirty(false); - } - } - } - - */ } } } @@ -189,43 +154,6 @@ void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client clie public ConcurrentLinkedQueue> primitiveBuilders = new ConcurrentLinkedQueue<>(); Long2BooleanOpenHashMap asyncedChunks = new Long2BooleanOpenHashMap(); - boolean putChunk(int chunkX, int chunkZ) { - if (!asyncedChunks.getOrDefault((((long) chunkX) << 32) | (chunkZ & 0xffffffffL), false)) { - asyncedChunks.put((((long)chunkX) << 32) | (chunkZ & 0xffffffffL), true); - return true; - } - return false; - } - - /* - public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk) { - PrimitiveBuilder primitiveBuilder = new PrimitiveBuilder(shaderSource.vertexFormat); - for(int x = 0; x < chunk.getWidth(); x++) { - for(int y = chunk.getHeight() - 1; y >= 0; y--) { - for(int z = 0; z < chunk.getWidth(); z++) { - IBlockState block = chunk.getBlockState1(x,y,z); - if(block.getBlock() != Blocks.AIR && !block.getBlock().blockProperties.translucent) { - for (int a = 0; a < 6; a++) { - BlockPos p = new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a))); - IBlockState newState; - if (!p.inRange(0, 0, 0, 16, 255, 16)) { - newState = chunk.getWorld().getBlockState(p.add(chunk.getBlockX(), chunk.getBlockY(), chunk.getBlockZ())); - } else { - newState = chunk.getBlockState1(new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a)))); - } - if (newState.getBlock().blockProperties.transparent && (Settings.renderSameTransparent || block.getBlock() != newState.getBlock()) || block.getBlock().blockProperties.alwaysRender) { - block.getBlock().addVertices(primitiveBuilder, a, 1f, block, new BlockPos(x + chunk.getX(), y + chunk.getY(), z + chunk.getY()), x, z); - } - } - } - } - } - } - return primitiveBuilder; - } - - */ - public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk, PrimitiveBuilder primitiveBuilder) { BlockPos p = new BlockPos(0, 0, 0); primitiveBuilder = (primitiveBuilder == null ? new PrimitiveBuilder(shaderSource.vertexFormat) : primitiveBuilder); @@ -255,7 +183,6 @@ public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk, PrimitiveBuilder primi public PrimitiveBuilder primitiveBuilder = null; public void buildMesh(RenderWindow window, GraphicsContext graphicsContext, IChunk chunk) { - long start = System.currentTimeMillis(); primitiveBuilder = getPrimitiveBuilder(chunk, primitiveBuilder); primitiveBuilder.setVertexFormat(shaderSource.vertexFormat); int meshID = (int) window.getGraphicsEngine().getDefaultImpl().createMesh(graphicsContext, primitiveBuilder.toVertexMesh()); @@ -263,39 +190,8 @@ public void buildMesh(RenderWindow window, GraphicsContext graphicsContext, IChu primitiveBuilder.size = 0; primitiveBuilder.vertices.size = 0; primitiveBuilder.indices.size = 0; - long end = System.currentTimeMillis(); - //System.out.println("Time to build:" + (end - start)); } - /* - public IPrimitiveBuilder getPrimitiveBuilder1(IChunk chunk) { - BufferPrimitiveBuilder primitiveBuilder = new BufferPrimitiveBuilder(128, 128).setVertexFormat(shaderSource.vertexFormat); - for(int x = 0; x < chunk.getWidth(); x++) { - for(int y = chunk.getHeight() - 1; y >= 0; y--) { - for(int z = 0; z < chunk.getWidth(); z++) { - IBlockState block = chunk.getBlockState1(x,y,z); - if(block.getBlock() != Blocks.AIR && !block.getBlock().blockProperties.translucent) { - for (int a = 0; a < 6; a++) { - BlockPos p = new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a))); - IBlockState newState; - if (!p.inRange(0, 0, 0, 16, 255, 16)) { - newState = chunk.getWorld().getBlockState(p.add(chunk.getBlockX(), chunk.getBlockY(), chunk.getBlockZ())); - } else { - newState = chunk.getBlockState1(new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a)))); - } - if (newState.getBlock().blockProperties.transparent && (Settings.renderSameTransparent || block.getBlock() != newState.getBlock()) || block.getBlock().blockProperties.alwaysRender) { - block.getBlock().addVertices(primitiveBuilder, a, 1f, block, new BlockPos(x + chunk.getX(), y + chunk.getY(), z + chunk.getY()), x, y, z); - } - } - } - } - } - } - return primitiveBuilder; - } - - */ - @Override public void cleanup(GameInstance gameInstance, IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) { super.cleanup(gameInstance, graphicsEngine, graphicsContext); diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java index 7b25ff04..f4f9a15a 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java @@ -122,7 +122,6 @@ IChunk getChunk(int chunkX, int chunkY, int chunkZ, IWorld world) { public static int rebuildDistance = 4; void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client client, IGraphicsEngine engine, MatrixStack matrixStack, Vector3i playerChunkPos, IWorld world, int x, int y, int z, int chunkWidth, int chunkHeight) { - MeshHolder meshHolder = getMesh(x, y, z); for (Tuple tuple : primitiveBuilders) { asyncedChunks.remove(((tuple.getTypeA().getX()) << 32) | (tuple.getTypeA().getZ() & 0xffffffffL)); tuple.typeB.setVertexFormat(shaderSource.vertexFormat); @@ -130,32 +129,32 @@ void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client clie meshes.setChunk(tuple.getTypeA().getX(), tuple.getTypeA().getY(), tuple.getTypeA().getZ(), new MeshHolder().set(meshID, tuple.getTypeB().indices.size())); primitiveBuilders.remove(tuple); } - if (meshHolder != null) { - if (meshHolder.id != -1 && meshHolder.length != 0) { - matrixStack.updateFrustum(); - if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) { - matrixStack.push(); - matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth); - IDefaultEngineImpl impl = engine.getDefaultImpl(); - impl.uploadMatrix(graphicsContext, matrixStack, shaderSource); - impl.drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length); - matrixStack.pop(); + IChunk chunk = getChunk(x, y, z, world); + if (chunk != null) { + MeshHolder meshHolder = getMesh(x, y, z); + if(chunk.isDirty() || meshHolder == null) { + /*if (getChunk(x + 1, y, z, world) != null && + getChunk(x - 1, y, z, world) != null && + getChunk(x, y + 1, z, world) != null && + getChunk(x, y - 1, z, world) != null && + getChunk(x, y, z + 1, world) != null && + getChunk(x, y, z - 1, world) != null) { + + */ + if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) { + buildMesh(window, graphicsContext, chunk); } } - } else { - IChunk chunk = getChunk(x, y, z, world); - if (chunk != null) { - if (chunk.isDirty()) { - if (getChunk(x + 1, y, z, world) != null && - getChunk(x - 1, y, z, world) != null && - getChunk(x, y + 1, z, world) != null && - getChunk(x, y - 1, z, world) != null && - getChunk(x, y, z + 1, world) != null && - getChunk(x, y, z - 1, world) != null) { - if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) { - buildMesh(window, graphicsContext, chunk); - chunk.setDirty(false); - } + if (meshHolder != null) { + if (meshHolder.id != -1 && meshHolder.length != 0) { + matrixStack.updateFrustum(); + if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) { + matrixStack.push(); + matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth); + IDefaultEngineImpl impl = engine.getDefaultImpl(); + impl.uploadMatrix(graphicsContext, matrixStack, shaderSource); + impl.drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length); + matrixStack.pop(); } } } diff --git a/src/main/java/dev/hilligans/ourcraft/network/ClientNetwork.java b/src/main/java/dev/hilligans/ourcraft/network/ClientNetwork.java index 32b47b6c..2e8de4bb 100644 --- a/src/main/java/dev/hilligans/ourcraft/network/ClientNetwork.java +++ b/src/main/java/dev/hilligans/ourcraft/network/ClientNetwork.java @@ -30,7 +30,6 @@ public void joinServer(String ip, String port, Client client) throws Exception { this.gameInstance = client.gameInstance; networkHandler = new ClientNetworkHandler(this); - ClientNetworkHandler.clientNetworkHandler = (ClientNetworkHandler) networkHandler; final String HOST = System.getProperty("host", ip); final int PORT = Integer.parseInt(System.getProperty("port", port)); @@ -72,4 +71,10 @@ public void flush() { public void sendPacketDirect(PacketBase packetBase) { ((ClientNetworkHandler)networkHandler).sendPacket(packetBase); } + + public void processPackets() { + if(networkHandler != null) { + ((ClientNetworkHandler) networkHandler).processPackets(); + } + } } diff --git a/src/main/java/dev/hilligans/ourcraft/network/ClientNetworkHandler.java b/src/main/java/dev/hilligans/ourcraft/network/ClientNetworkHandler.java index c90ed873..ff110eac 100644 --- a/src/main/java/dev/hilligans/ourcraft/network/ClientNetworkHandler.java +++ b/src/main/java/dev/hilligans/ourcraft/network/ClientNetworkHandler.java @@ -4,11 +4,15 @@ import dev.hilligans.ourcraft.network.packet.client.CHandshakePacket; import io.netty.channel.*; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.Consumer; + public class ClientNetworkHandler extends NetworkHandler { public static ClientNetworkHandler clientNetworkHandler; public ClientNetwork network; + public ConcurrentLinkedQueue packets = new ConcurrentLinkedQueue<>(); public ClientNetworkHandler(ClientNetwork network) { this.network = network; @@ -33,11 +37,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { @Override protected void channelRead0(ChannelHandlerContext ctx, IPacketByteArray msg) throws Exception { PacketBase packetBase = msg.createPacket(network.receiveProtocol); - if(packetBase instanceof PacketBaseNew packetBaseNew) { - packetBaseNew.handle(network.client); - } else { - packetBase.handle(); - } + packets.add(packetBase); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { @@ -47,4 +47,14 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { network.client.openScreen(new DisconnectScreen(network.client,cause.getMessage())); } + public void processPackets() { + PacketBase packetBase; + while((packetBase = packets.poll()) != null) { + if(packetBase instanceof PacketBaseNew packetBaseNew) { + packetBaseNew.handle(network.client); + } else { + packetBase.handle(); + } + } + } } diff --git a/src/main/java/dev/hilligans/ourcraft/network/packet/server/SSendChunkPacket.java b/src/main/java/dev/hilligans/ourcraft/network/packet/server/SSendChunkPacket.java index e2d2980a..ada5db32 100644 --- a/src/main/java/dev/hilligans/ourcraft/network/packet/server/SSendChunkPacket.java +++ b/src/main/java/dev/hilligans/ourcraft/network/packet/server/SSendChunkPacket.java @@ -190,8 +190,7 @@ public void decode(IPacketByteArray packetData) { public void handle(IClientPacketHandler clientPacketHandler) { try { clientPacketHandler.getWorld().setChunk(newChunk.getBlockX(), newChunk.getBlockY(), newChunk.getBlockZ(), newChunk.setWorld(clientPacketHandler.getWorld())); - //chunk.world.setChunk(chunk); - //System.out.println(chunk.x + ":" + chunk.z); + clientPacketHandler.getWorld().markDirtyAround(newChunk.getBlockX(), newChunk.getBlockY(), newChunk.getBlockZ()); } catch (Exception ignored) { ignored.printStackTrace(); } diff --git a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/ClientCubicWorld.java b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/ClientCubicWorld.java new file mode 100644 index 00000000..1bdfbce7 --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/ClientCubicWorld.java @@ -0,0 +1,20 @@ +package dev.hilligans.ourcraft.world.newworldsystem; + +import dev.hilligans.ourcraft.GameInstance; + +import java.util.function.Consumer; + +public class ClientCubicWorld extends CubicWorld { + public ClientCubicWorld(GameInstance gameInstance, int id, String worldName, int radius) { + super(gameInstance, id, worldName, radius); + } + + @Override + public void tick() { + chunkContainer.forEach(iChunk -> { + if(iChunk.isDirty()) { + iChunk.setDirty(false); + } + }); + } +} diff --git a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicChunkContainer.java b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicChunkContainer.java index db9b7174..90b1d719 100644 --- a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicChunkContainer.java +++ b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicChunkContainer.java @@ -26,7 +26,9 @@ public synchronized IChunk getChunk(long x, long y, long z) { return null; } chunk = c.getOrDefault((int)z, null); - cachedChunk = chunk; + if(chunk != null) { + cachedChunk = chunk; + } return chunk; } diff --git a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicWorld.java b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicWorld.java index c82b5dc3..0b08eb1e 100644 --- a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicWorld.java +++ b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/CubicWorld.java @@ -54,8 +54,8 @@ public IChunk getChunkNonNull(long blockX, long blockY, long blockZ) { @Override public void setChunk(long blockX, long blockY, long blockZ, IChunk chunk) { - chunkContainer.setChunk(blockX >> 5, blockY >> 5, blockZ >> 5, chunk); chunk.setDirty(true); + chunkContainer.setChunk(blockX >> 5, blockY >> 5, blockZ >> 5, chunk); } @Override diff --git a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/IWorld.java b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/IWorld.java index 0b0c12dd..512a1c74 100644 --- a/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/IWorld.java +++ b/src/main/java/dev/hilligans/ourcraft/world/newworldsystem/IWorld.java @@ -137,6 +137,25 @@ default Vector3fc getBlockGravity(int x, int y, int z) { Entity removeEntity(long l1, long l2); + default void markDirtyAround(long blockX, long blockY, long blockZ) { + int width = getChunkWidth(); + int height = getChunkHeight(); + IChunk chunk = getChunk(blockX + width, blockY, blockZ); + if(chunk != null) {chunk.setDirty(true);} + chunk = getChunk(blockX - width, blockY, blockZ); + if(chunk != null) {chunk.setDirty(true);} + + chunk = getChunk(blockX, blockY + height, blockZ); + if(chunk != null) {chunk.setDirty(true);} + chunk = getChunk(blockX, blockY - height, blockZ); + if(chunk != null) {chunk.setDirty(true);} + + chunk = getChunk(blockX, blockY, blockZ + width); + if(chunk != null) {chunk.setDirty(true);} + chunk = getChunk(blockX, blockY, blockZ - width); + if(chunk != null) {chunk.setDirty(true);} + } + default Iterable getChunks(BlockPos min, BlockPos max) { return getChunks(min.getX(), min.getY(), min.getZ(), max.getX(), max.getY(), max.getZ()); }