Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
Possible optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
emortaldev committed Dec 22, 2022
1 parent 723cb52 commit 4ef1b53
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ hs_err_pid*
.idea
.gradle
build
world
world.tnt
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
69 changes: 31 additions & 38 deletions src/main/java/dev/emortal/tnt/TNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
64 changes: 30 additions & 34 deletions src/main/java/dev/emortal/tnt/TNTLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TNTChunk> 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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/emortal/tnt/source/FileTNTSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 4ef1b53

Please sign in to comment.