Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pop4959 committed Jan 26, 2024
1 parent c15c600 commit 346be4c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ subprojects {
repositories {
mavenCentral()
maven("https://oss.sonatype.org/content/repositories/snapshots/")
maven("https://jitpack.io")
}

java {
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies {
compileOnly(group = "com.google.code.gson", name = "gson", version = "2.8.9")
testImplementation(group = "junit", name = "junit", version = "4.13.2")
testImplementation(group = "com.google.code.gson", name = "gson", version = "2.8.9")
implementation(group = "com.github.BlueMap-Minecraft", name = "BlueNBT", version = "v1.3.0")
implementation(project(":chunky-nbt"))
}

Expand Down
13 changes: 3 additions & 10 deletions common/src/main/java/org/popcraft/chunky/command/TrimCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import org.popcraft.chunky.Chunky;
import org.popcraft.chunky.Selection;
import org.popcraft.chunky.nbt.CompoundTag;
import org.popcraft.chunky.nbt.LongTag;
import org.popcraft.chunky.nbt.util.RegionFile;
import org.popcraft.chunky.platform.Sender;
import org.popcraft.chunky.platform.World;
import org.popcraft.chunky.shape.Shape;
Expand All @@ -13,6 +10,7 @@
import org.popcraft.chunky.util.ChunkCoordinate;
import org.popcraft.chunky.util.Formatting;
import org.popcraft.chunky.util.Input;
import org.popcraft.chunky.util.RegionFile;
import org.popcraft.chunky.util.TranslationKey;

import java.io.IOException;
Expand Down Expand Up @@ -227,15 +225,10 @@ private int trimRegion(final World world, final String regionFileName, final Sha
}
final boolean trimInhabited = regionData == null || regionData.getChunk(offsetChunkX, offsetChunkZ)
.map(chunk -> {
final CompoundTag compoundTag = chunk.getData();
if (compoundTag == null) {
if (chunk.inhabitedTime < 0) {
return true;
}
final LongTag inhabited = compoundTag.getLong("InhabitedTime").orElse(null);
if (inhabited == null) {
return true;
}
return inhabited.value() <= inhabitedTime;
return chunk.inhabitedTime <= inhabitedTime;
})
.orElse(true);
if (trimChunk && trimInhabited) {
Expand Down
86 changes: 86 additions & 0 deletions common/src/main/java/org/popcraft/chunky/util/RegionFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.popcraft.chunky.util;

import com.google.gson.reflect.TypeToken;
import de.bluecolored.bluenbt.BlueNBT;
import de.bluecolored.bluenbt.NBTName;
import org.popcraft.chunky.nbt.util.ChunkPos;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.zip.InflaterInputStream;

public final class RegionFile {
private static final int ENTRIES = 1024;
private static final int SECTOR_SIZE = 4096;
private final Set<Chunk> chunks = new HashSet<>();
private final Map<ChunkPos, Chunk> chunkMap = new HashMap<>();

public RegionFile(final File file) {
final BlueNBT blueNBT = new BlueNBT();
try (final RandomAccessFile region = new RandomAccessFile(file, "r")) {
if (region.length() < 4096) {
return;
}
final int[] offsetTable = new int[ENTRIES];
final int[] sizeTable = new int[ENTRIES];
for (int i = 0; i < ENTRIES; ++i) {
final int location = region.readInt();
offsetTable[i] = (location >> 8) & 0xFFFFFF;
sizeTable[i] = location & 0xFF;
}
final int[] timestampTable = new int[ENTRIES];
for (int i = 0; i < ENTRIES; ++i) {
timestampTable[i] = region.readInt();
}
for (int i = 0; i < ENTRIES; ++i) {
final int offset = offsetTable[i] * SECTOR_SIZE;
final int size = sizeTable[i] * SECTOR_SIZE;
if (offset == 0 && size == 0) {
continue;
}
region.seek(offset);
final int length = region.readInt();
final byte compressionType = region.readByte();
if (compressionType != 2) {
throw new UnsupportedOperationException("Not in zlib format");
}
final byte[] compressed = new byte[length - 1];
region.readFully(compressed);
final DataInputStream input = new DataInputStream(new InflaterInputStream(new ByteArrayInputStream(compressed)));
final Chunk chunk = blueNBT.read(input, TypeToken.get(Chunk.class));
chunks.add(chunk);
}
} catch (IOException e) {
e.printStackTrace();
}
for (final Chunk chunk : chunks) {
chunkMap.put(ChunkPos.of(chunk.x, chunk.z), chunk);
}
}

public Collection<Chunk> getChunks() {
return chunks;
}

public Optional<Chunk> getChunk(final int x, final int z) {
return Optional.ofNullable(chunkMap.get(ChunkPos.of(x, z)));
}

public static class Chunk {
@NBTName("xPos")
public int x;
@NBTName("zPos")
public int z;
@NBTName("InhabitedTime")
public long inhabitedTime = -1;
}
}

0 comments on commit 346be4c

Please sign in to comment.