From 4ef1b53482897dbaf87b70a1f1109f0b58b8e984 Mon Sep 17 00:00:00 2001 From: emortal Date: Thu, 22 Dec 2022 02:00:19 +0000 Subject: [PATCH] Possible optimisations --- .gitignore | 2 + build.gradle.kts | 3 +- src/main/java/dev/emortal/tnt/TNT.java | 69 +++++++++---------- src/main/java/dev/emortal/tnt/TNTLoader.java | 64 ++++++++--------- .../dev/emortal/tnt/source/FileTNTSource.java | 2 - 5 files changed, 65 insertions(+), 75 deletions(-) diff --git a/.gitignore b/.gitignore index 6a9eae7..54913f7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ hs_err_pid* .idea .gradle build +world +world.tnt diff --git a/build.gradle.kts b/build.gradle.kts index 6b08c1b..c30298e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,8 @@ dependencies { //compileOnly(kotlin("stdlib")) //compileOnly(kotlin("reflect")) - compileOnly("com.github.Minestom:Minestom:d596992c0e") + compileOnly("com.github.Minestom:Minestom:eb06ba8664") +// implementation("com.github.Minestom:Minestom:eb06ba8664") implementation("com.github.luben:zstd-jni:1.5.2-5") } diff --git a/src/main/java/dev/emortal/tnt/TNT.java b/src/main/java/dev/emortal/tnt/TNT.java index b342715..11ac801 100644 --- a/src/main/java/dev/emortal/tnt/TNT.java +++ b/src/main/java/dev/emortal/tnt/TNT.java @@ -15,7 +15,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.stream.Collectors; @@ -32,56 +31,50 @@ private static byte[] convertChunk(Chunk chunk) throws IOException { writer.writeByte((byte) chunk.getMaxSection()); // LOGGER.info("Chunk {} {} min max {} {}", chunk.getChunkX(), chunk.getChunkZ(), chunk.getMinSection(), chunk.getMaxSection()); - int sectionIter = 0; - for (Section section : chunk.getSections()) { - int airSkip = 0; - boolean needsEnding = false; + int airSkip = 0; + boolean needsEnding = false; + for (int y = chunk.getMinSection() * Chunk.CHUNK_SECTION_SIZE; y < chunk.getMaxSection() * Chunk.CHUNK_SECTION_SIZE; y++) { for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) { - for (int y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) { - for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) { - Block block = chunk.getBlock(x, y + ((sectionIter + chunk.getMinSection()) * Chunk.CHUNK_SECTION_SIZE), z); - - if (block.compare(Block.AIR)) { - airSkip++; - if (airSkip == 1) { - writer.writeShort((short )0); -// LOGGER.info("Wrote short 0"); - needsEnding = true; - } - - continue; - } - if (airSkip > 0) { - writer.writeInt(airSkip); -// LOGGER.info("Wrote int {}", airSkip); - needsEnding = false; + for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) { + Block block = chunk.getBlock(x, y, z); + + // check for the air block state + if (block.stateId() == 0) { + airSkip++; + if (airSkip == 1) { + writer.writeShort((short) 0); + needsEnding = true; } - airSkip = 0; + continue; + } + if (airSkip > 0) { + writer.writeInt(airSkip); + needsEnding = false; + } - writer.writeShort(block.stateId()); -// LOGGER.info("Wrote short {}", block.stateId()); + airSkip = 0; - NBTCompound nbt = block.nbt(); - writer.writeBoolean(block.hasNbt()); -// LOGGER.info("Wrote bool {}", block.hasNbt()); - if (nbt != null) { - writer.writeNBT("blockNBT", nbt); - } + writer.writeShort(block.stateId()); + + NBTCompound nbt = block.nbt(); + writer.writeBoolean(block.hasNbt()); + if (nbt != null) { + writer.writeNBT("blockNBT", nbt); } } } + } - // Air skip sometimes isn't written, maybe there is a cleaner way? - if (needsEnding) { - writer.writeInt(airSkip); - } + // Air skip sometimes isn't written, maybe there is a cleaner way? + if (needsEnding) { + writer.writeInt(airSkip); + } + for (Section section : chunk.getSections()) { writer.writeByteArray(section.getBlockLight()); writer.writeByteArray(section.getSkyLight()); - - sectionIter++; } byte[] bytes = writer.toByteArray(); diff --git a/src/main/java/dev/emortal/tnt/TNTLoader.java b/src/main/java/dev/emortal/tnt/TNTLoader.java index f183db1..cc209f0 100644 --- a/src/main/java/dev/emortal/tnt/TNTLoader.java +++ b/src/main/java/dev/emortal/tnt/TNTLoader.java @@ -4,8 +4,8 @@ import dev.emortal.tnt.source.TNTSource; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import net.minestom.server.MinecraftServer; -import net.minestom.server.coordinate.Point; import net.minestom.server.instance.*; +import net.minestom.server.instance.batch.BatchOption; import net.minestom.server.instance.batch.ChunkBatch; import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.BlockManager; @@ -22,14 +22,12 @@ public final class TNTLoader implements IChunkLoader { - private static Logger LOGGER = LoggerFactory.getLogger("TNTLoader"); + private static final Logger LOGGER = LoggerFactory.getLogger("TNTLoader"); - private final Instance instance; private final TNTSource source; public final Long2ObjectOpenHashMap chunksMap = new Long2ObjectOpenHashMap<>(); - public TNTLoader(Instance instance, TNTSource source) throws IOException, NBTException { - this.instance = instance; + public TNTLoader(TNTSource source) throws IOException, NBTException { this.source = source; BlockManager blockManager = MinecraftServer.getBlockManager(); @@ -43,7 +41,8 @@ public TNTLoader(Instance instance, TNTSource source) throws IOException, NBTExc // LOGGER.info("Reading {} chunks", chunks); for (int chunkI = 0; chunkI < chunks; chunkI++) { - ChunkBatch batch = new ChunkBatch(); + ChunkBatch batch = new ChunkBatch(new BatchOption()/*.setSendUpdate(false)*/.setUnsafeApply(true)); +// ChunkBatch batch = new ChunkBatch(); int chunkX = reader.readInt(); int chunkZ = reader.readInt(); @@ -55,46 +54,43 @@ public TNTLoader(Instance instance, TNTSource source) throws IOException, NBTExc TNTChunk mstChunk = new TNTChunk(batch, maxSection, minSection); - for (int sectionY = minSection; sectionY < maxSection; sectionY++) { - int airSkip = 0; - Section section = mstChunk.sections[sectionY - minSection]; + int airSkip = 0; + for (int y = minSection * Chunk.CHUNK_SECTION_SIZE; y < maxSection * Chunk.CHUNK_SECTION_SIZE; y++) { for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) { - for (int y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) { - for (int z = 0; z < Chunk.CHUNK_SIZE_X; z++) { - if (airSkip > 0) { - airSkip--; - continue; - } - - short stateId = reader.readShort(); -// LOGGER.info("Read short {}", stateId); + for (int z = 0; z < Chunk.CHUNK_SIZE_X; z++) { + if (airSkip > 0) { + airSkip--; + continue; + } - if (stateId == 0) { - airSkip = reader.readInt() - 1; -// LOGGER.info("Read int {}", airSkip); - continue; - } + short stateId = reader.readShort(); - boolean hasNbt = reader.readBoolean(); -// LOGGER.info("Read bool {}", hasNbt); + if (stateId == 0) { + airSkip = reader.readInt() - 1; + continue; + } - Block block; + boolean hasNbt = reader.readBoolean(); - if (hasNbt) { - NBT nbt = nbtReader.read(); + Block block; - Block b = Block.fromStateId(stateId); - block = b.withHandler(blockManager.getHandlerOrDummy(b.name())).withNbt((NBTCompound) nbt); - } else { - block = Block.fromStateId(stateId); - } + if (hasNbt) { + NBT nbt = nbtReader.read(); - batch.setBlock(x, y + (sectionY * 16), z, block); + Block b = Block.fromStateId(stateId); + block = b.withHandler(blockManager.getHandlerOrDummy(b.name())).withNbt((NBTCompound) nbt); + } else { + block = Block.fromStateId(stateId); } + + batch.setBlock(x, y, z, block); } } + } + for (int sectionY = minSection; sectionY < maxSection; sectionY++) { + Section section = mstChunk.sections[sectionY - minSection]; byte[] blockLights = reader.readByteArray(); byte[] skyLights = reader.readByteArray(); section.setBlockLight(blockLights); diff --git a/src/main/java/dev/emortal/tnt/source/FileTNTSource.java b/src/main/java/dev/emortal/tnt/source/FileTNTSource.java index 3154a87..7dd8ed2 100644 --- a/src/main/java/dev/emortal/tnt/source/FileTNTSource.java +++ b/src/main/java/dev/emortal/tnt/source/FileTNTSource.java @@ -4,10 +4,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.nio.file.AccessDeniedException; import java.nio.file.Files; import java.nio.file.Path;