
net.minecraft.server.ChunkRegionLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of walk-server Show documentation
Show all versions of walk-server Show documentation
A spigot fork to kotlin structure and news.
package net.minecraft.server;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ChunkRegionLoader implements IChunkLoader, IAsyncChunkSaver {
private static final Logger a = LogManager.getLogger();
private Map b = new ConcurrentHashMap<>();
private Set c = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final File d;
private boolean e = false;
public ChunkRegionLoader(File file) {
this.d = file;
}
// CraftBukkit start
public boolean chunkExists(World world, int i, int j) {
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i, j);
if (this.c.contains(chunkcoordintpair)) {
if (this.b.containsKey(chunkcoordintpair)) {
return true;
}
}
return RegionFileCache.a(this.d, i, j).chunkExists(i & 31, j & 31);
}
// CraftBukkit end
// CraftBukkit start - Add async variant, provide compatibility
public Chunk a(World world, int i, int j) throws IOException {
world.timings.syncChunkLoadDataTimer.startTiming(); // Spigot
Object[] data = loadChunk(world, i, j);
world.timings.syncChunkLoadDataTimer.stopTiming(); // Spigot
if (data != null) {
Chunk chunk = (Chunk) data[0];
NBTTagCompound nbttagcompound = (NBTTagCompound) data[1];
loadEntities(chunk, nbttagcompound.getCompound("Level"), world);
return chunk;
}
return null;
}
public Object[] loadChunk(World world, int i, int j) throws IOException {
// CraftBukkit end
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i, j);
NBTTagCompound nbttagcompound = this.b.get(chunkcoordintpair);
if (nbttagcompound == null) {
DataInputStream datainputstream = RegionFileCache.c(this.d, i, j);
if (datainputstream == null) {
return null;
}
nbttagcompound = NBTCompressedStreamTools.a(datainputstream);
}
return this.a(world, i, j, nbttagcompound);
}
protected Object[] a(World world, int i, int j, NBTTagCompound nbttagcompound) { // CraftBukkit - return Chunk -> Object[]
if (!nbttagcompound.hasKeyOfType("Level", 10)) {
ChunkRegionLoader.a.error("Chunk file at " + i + "," + j + " is missing level data, skipping");
return null;
} else {
NBTTagCompound nbttagcompound1 = nbttagcompound.getCompound("Level");
if (!nbttagcompound1.hasKeyOfType("Sections", 9)) {
ChunkRegionLoader.a.error("Chunk file at " + i + "," + j + " is missing block data, skipping");
return null;
} else {
Chunk chunk = this.a(world, nbttagcompound1);
if (!chunk.a(i, j)) {
ChunkRegionLoader.a.error("Chunk file at " + i + "," + j + " is in the wrong location; relocating. (Expected " + i + ", " + j + ", got " + chunk.locX + ", " + chunk.locZ + ")");
nbttagcompound1.setInt("xPos", i);
nbttagcompound1.setInt("zPos", j);
// CraftBukkit start - Have to move tile entities since we don't load them at this stage
NBTTagList tileEntities = nbttagcompound.getCompound("Level").getList("TileEntities", 10);
if (tileEntities != null) {
for (int te = 0; te < tileEntities.size(); te++) {
NBTTagCompound tileEntity = tileEntities.get(te);
int x = tileEntity.getInt("x") - chunk.locX * 16;
int z = tileEntity.getInt("z") - chunk.locZ * 16;
tileEntity.setInt("x", i * 16 + x);
tileEntity.setInt("z", j * 16 + z);
}
}
// CraftBukkit end
chunk = this.a(world, nbttagcompound1);
}
// CraftBukkit start
Object[] data = new Object[2];
data[0] = chunk;
data[1] = nbttagcompound;
return data;
// CraftBukkit end
}
}
}
public void a(World world, Chunk chunk) throws IOException, ExceptionWorldConflict {
world.checkSession();
try {
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound.set("Level", nbttagcompound1);
this.a(chunk, world, nbttagcompound1);
this.a(chunk.j(), nbttagcompound);
} catch (Exception exception) {
ChunkRegionLoader.a.error("Failed to save chunk", exception);
}
}
protected void a(ChunkCoordIntPair chunkcoordintpair, NBTTagCompound nbttagcompound) {
if (!this.c.contains(chunkcoordintpair)) {
this.b.put(chunkcoordintpair, nbttagcompound);
}
FileIOThread.a().a(this);
}
public boolean c() {
if (this.b.isEmpty()) {
if (this.e) {
ChunkRegionLoader.a.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", this.d.getName());
}
return false;
} else {
ChunkCoordIntPair chunkcoordintpair = this.b.keySet().iterator().next();
boolean flag;
try {
this.c.add(chunkcoordintpair);
NBTTagCompound nbttagcompound = this.b.remove(chunkcoordintpair);
if (nbttagcompound != null) {
try {
this.b(chunkcoordintpair, nbttagcompound);
} catch (Exception exception) {
a.error("Failed to save chunk", exception);
}
}
flag = true;
} finally {
this.c.remove(chunkcoordintpair);
}
return flag;
}
}
private void b(ChunkCoordIntPair chunkcoordintpair, NBTTagCompound nbttagcompound) throws IOException {
DataOutputStream dataoutputstream = RegionFileCache.d(this.d, chunkcoordintpair.x, chunkcoordintpair.z);
NBTCompressedStreamTools.a(nbttagcompound, (DataOutput) dataoutputstream);
dataoutputstream.close();
}
public void b(World world, Chunk chunk) throws IOException {
}
public void a() {
}
public void b() {
try {
this.e = true;
while (true) {
this.c();
}
} finally {
this.e = false;
}
}
private void a(Chunk chunk, World world, NBTTagCompound root) {
root.setByte("V", (byte) 1);
root.setInt("xPos", chunk.locX);
root.setInt("zPos", chunk.locZ);
root.setLong("LastUpdate", world.getTime());
root.setIntArray("HeightMap", chunk.q());
root.setBoolean("TerrainPopulated", chunk.isDone());
root.setBoolean("LightPopulated", chunk.u());
root.setLong("InhabitedTime", chunk.w());
ChunkSection[] achunksection = chunk.getSections();
NBTTagList nbttaglist = new NBTTagList();
boolean flag = !world.worldProvider.o();
int i = achunksection.length;
NBTTagCompound nbttagcompound1;
for (int j = 0; j < i; ++j) {
ChunkSection chunksection = achunksection[j];
if (chunksection != null) {
nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Y", (byte) (chunksection.getYPosition() >> 4 & 255));
byte[] abyte = new byte[chunksection.getIdArray().length];
NibbleArray nibblearray = new NibbleArray();
NibbleArray nibblearray1 = null;
for (int k = 0; k < chunksection.getIdArray().length; ++k) {
char c0 = chunksection.getIdArray()[k];
int l = k & 15;
int i1 = k >> 8 & 15;
int j1 = k >> 4 & 15;
if (c0 >> 12 != 0) {
if (nibblearray1 == null) {
nibblearray1 = new NibbleArray();
}
nibblearray1.a(l, i1, j1, c0 >> 12);
}
abyte[k] = (byte) (c0 >> 4 & 255);
nibblearray.a(l, i1, j1, c0 & 15);
}
nbttagcompound1.setByteArray("Blocks", abyte);
nbttagcompound1.setByteArray("Data", nibblearray.a());
if (nibblearray1 != null) {
nbttagcompound1.setByteArray("Add", nibblearray1.a());
}
nbttagcompound1.setByteArray("BlockLight", chunksection.getEmittedLightArray().a());
if (flag) {
nbttagcompound1.setByteArray("SkyLight", chunksection.getSkyLightArray().a());
} else {
nbttagcompound1.setByteArray("SkyLight", new byte[chunksection.getEmittedLightArray().a().length]);
}
nbttaglist.add(nbttagcompound1);
}
}
root.set("Sections", nbttaglist);
root.setByteArray("Biomes", chunk.getBiomeIndex());
chunk.g(false);
NBTTagList nbttaglist1 = new NBTTagList();
Iterator iterator;
for (i = 0; i < chunk.getEntitySlices().length; ++i) {
iterator = chunk.getEntitySlices()[i].iterator();
while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next();
nbttagcompound1 = new NBTTagCompound();
if (entity.d(nbttagcompound1)) {
chunk.g(true);
nbttaglist1.add(nbttagcompound1);
}
}
}
root.set("Entities", nbttaglist1);
NBTTagList nbttaglist2 = new NBTTagList();
iterator = chunk.getTileEntities().values().iterator();
while (iterator.hasNext()) {
TileEntity tileentity = (TileEntity) iterator.next();
nbttagcompound1 = new NBTTagCompound();
tileentity.b(nbttagcompound1);
nbttaglist2.add(nbttagcompound1);
}
root.set("TileEntities", nbttaglist2);
List list = world.a(chunk, false);
if (list != null) {
long k1 = world.getTime();
NBTTagList nbttaglist3 = new NBTTagList();
for (NextTickListEntry nextticklistentry : list) {
NBTTagCompound nbttagcompound2 = new NBTTagCompound();
MinecraftKey minecraftkey = Block.REGISTRY.c(nextticklistentry.a());
nbttagcompound2.setString("i", minecraftkey == null ? "" : minecraftkey.toString());
nbttagcompound2.setInt("x", nextticklistentry.a.getX());
nbttagcompound2.setInt("y", nextticklistentry.a.getY());
nbttagcompound2.setInt("z", nextticklistentry.a.getZ());
nbttagcompound2.setInt("t", (int) (nextticklistentry.b - k1));
nbttagcompound2.setInt("p", nextticklistentry.c);
nbttaglist3.add(nbttagcompound2);
}
root.set("TileTicks", nbttaglist3);
}
}
private Chunk a(World world, NBTTagCompound root) {
int i = root.getInt("xPos");
int j = root.getInt("zPos");
Chunk chunk = new Chunk(world, i, j);
chunk.a(root.getIntArray("HeightMap"));
chunk.d(root.getBoolean("TerrainPopulated"));
chunk.e(root.getBoolean("LightPopulated"));
chunk.c(root.getLong("InhabitedTime"));
NBTTagList nbttaglist = root.getList("Sections", 10);
byte b0 = 16;
ChunkSection[] achunksection = new ChunkSection[b0];
boolean flag = !world.worldProvider.o();
for (int k = 0; k < nbttaglist.size(); ++k) {
NBTTagCompound nbttagcompound1 = nbttaglist.get(k);
byte b1 = nbttagcompound1.getByte("Y");
ChunkSection chunksection = new ChunkSection(b1 << 4, flag);
byte[] abyte = nbttagcompound1.getByteArray("Blocks");
NibbleArray nibblearray = new NibbleArray(nbttagcompound1.getByteArray("Data"));
NibbleArray nibblearray1 = nbttagcompound1.hasKeyOfType("Add", 7) ? new NibbleArray(nbttagcompound1.getByteArray("Add")) : null;
char[] achar = new char[abyte.length];
for (int l = 0; l < achar.length; ++l) {
int i1 = l & 15;
int j1 = l >> 8 & 15;
int k1 = l >> 4 & 15;
// CraftBukkit start - fix broken blocks
// achar[l] = (char) (l1 << 12 | (abyte[l] & 255) << 4 | nibblearray.a(i1, j1, k1));
int ex = nibblearray1 != null ? nibblearray1.a(i1, j1, k1) : 0;
int id = (abyte[l] & 255);
int data = nibblearray.a(i1, j1, k1);
int packed = ex << 12 | id << 4 | data;
if (Block.d.a(packed) == null) {
Block block = Block.getById(ex << 8 | id);
if (block != null) {
try {
data = block.toLegacyData(block.fromLegacyData(data));
} catch (Exception ignored) {
data = block.toLegacyData(block.getBlockData());
}
packed = ex << 12 | id << 4 | data;
}
}
achar[l] = (char) packed;
// CraftBukkit end
}
chunksection.a(achar);
chunksection.a(new NibbleArray(nbttagcompound1.getByteArray("BlockLight")));
if (flag) {
chunksection.b(new NibbleArray(nbttagcompound1.getByteArray("SkyLight")));
}
chunksection.recalcBlockCounts();
achunksection[b1] = chunksection;
}
chunk.a(achunksection);
if (root.hasKeyOfType("Biomes", 7)) {
chunk.a(root.getByteArray("Biomes"));
}
// CraftBukkit start - End this method here and split off entity loading to another method
return chunk;
}
public void loadEntities(Chunk chunk, NBTTagCompound root, World world) {
// CraftBukkit end
NBTTagList nbttaglist1 = root.getList("Entities", 10);
if (nbttaglist1 != null) {
for (int i2 = 0; i2 < nbttaglist1.size(); ++i2) {
NBTTagCompound nbttagcompound2 = nbttaglist1.get(i2);
Entity entity = EntityTypes.a(nbttagcompound2, world);
chunk.g(true);
if (entity != null) {
chunk.a(entity);
Entity entity1 = entity;
for (NBTTagCompound nbttagcompound3 = nbttagcompound2; nbttagcompound3.hasKeyOfType("Riding", 10); nbttagcompound3 = nbttagcompound3.getCompound("Riding")) {
Entity entity2 = EntityTypes.a(nbttagcompound3.getCompound("Riding"), world);
if (entity2 != null) {
chunk.a(entity2);
entity1.mount(entity2);
}
entity1 = entity2;
}
}
}
}
NBTTagList nbttaglist2 = root.getList("TileEntities", 10);
if (nbttaglist2 != null) {
for (int j2 = 0; j2 < nbttaglist2.size(); ++j2) {
NBTTagCompound nbttagcompound4 = nbttaglist2.get(j2);
TileEntity tileentity = TileEntity.c(nbttagcompound4);
if (tileentity != null) {
chunk.a(tileentity);
}
}
}
if (root.hasKeyOfType("TileTicks", 9)) {
NBTTagList nbttaglist3 = root.getList("TileTicks", 10);
if (nbttaglist3 != null) {
for (int k2 = 0; k2 < nbttaglist3.size(); ++k2) {
NBTTagCompound nbttagcompound5 = nbttaglist3.get(k2);
Block block;
if (nbttagcompound5.hasKeyOfType("i", 8)) {
block = Block.getByName(nbttagcompound5.getString("i"));
} else {
block = Block.getById(nbttagcompound5.getInt("i"));
}
world.b(new BlockPosition(nbttagcompound5.getInt("x"), nbttagcompound5.getInt("y"), nbttagcompound5.getInt("z")), block, nbttagcompound5.getInt("t"), nbttagcompound5.getInt("p"));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy