forked from BLOCKFANTASY/LOOHP-Limbo
Fixed importing schem incorrectly after certain x/y/z value
This commit is contained in:
parent
ae11665312
commit
5c69de1637
2
pom.xml
2
pom.xml
|
|
@ -4,7 +4,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.loohp</groupId>
|
<groupId>com.loohp</groupId>
|
||||||
<artifactId>Limbo</artifactId>
|
<artifactId>Limbo</artifactId>
|
||||||
<version>0.3.3-ALPHA</version>
|
<version>0.3.4-ALPHA</version>
|
||||||
<build>
|
<build>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<sourceDirectory>src</sourceDirectory>
|
||||||
<resources>
|
<resources>
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,13 @@ package com.loohp.limbo.Server.Packets;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.BitSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.loohp.limbo.Utils.ChunkDataUtils;
|
import com.loohp.limbo.Utils.BitsUtils;
|
||||||
import com.loohp.limbo.Utils.DataTypeIO;
|
import com.loohp.limbo.Utils.DataTypeIO;
|
||||||
import com.loohp.limbo.World.Environment;
|
import com.loohp.limbo.World.Environment;
|
||||||
import com.loohp.limbo.World.GeneratedBlockDataMappings;
|
import com.loohp.limbo.World.GeneratedBlockDataMappings;
|
||||||
|
|
@ -105,8 +109,9 @@ public class PacketPlayOutMapChunk extends PacketOut {
|
||||||
|
|
||||||
int newBits = 32 - Integer.numberOfLeadingZeros(section.getPalette().size() - 1);
|
int newBits = 32 - Integer.numberOfLeadingZeros(section.getPalette().size() - 1);
|
||||||
newBits = Math.max(newBits, 4);
|
newBits = Math.max(newBits, 4);
|
||||||
|
//Limbo.getInstance().getConsole().sendMessage(i + " " + newBits);
|
||||||
if (newBits <= 8) {
|
if (newBits <= 8) {
|
||||||
|
/*
|
||||||
if (newBits == 4) {
|
if (newBits == 4) {
|
||||||
dataOut.writeByte(4);
|
dataOut.writeByte(4);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -114,25 +119,79 @@ public class PacketPlayOutMapChunk extends PacketOut {
|
||||||
ChunkDataUtils.adjustBlockStateBits(newBits, section, chunk.getDataVersion());
|
ChunkDataUtils.adjustBlockStateBits(newBits, section, chunk.getDataVersion());
|
||||||
dataOut.writeByte(8);
|
dataOut.writeByte(8);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
dataOut.writeByte(newBits);
|
||||||
|
|
||||||
DataTypeIO.writeVarInt(dataOut, section.getPalette().size());
|
DataTypeIO.writeVarInt(dataOut, section.getPalette().size());
|
||||||
//Limbo.getInstance().getConsole().sendMessage(section.getPalette().size());
|
//Limbo.getInstance().getConsole().sendMessage(section.getPalette().size());
|
||||||
Iterator<CompoundTag> itr1 = section.getPalette().iterator();
|
Iterator<CompoundTag> itr1 = section.getPalette().iterator();
|
||||||
//Limbo.getInstance().getConsole().sendMessage("Nonnull -> " + i + " " + newBits);
|
//Limbo.getInstance().getConsole().sendMessage("Nonnull -> " + i + " " + newBits);
|
||||||
counter = 0;
|
|
||||||
while (itr1.hasNext()) {
|
while (itr1.hasNext()) {
|
||||||
CompoundTag tag = itr1.next();
|
CompoundTag tag = itr1.next();
|
||||||
DataTypeIO.writeVarInt(dataOut, GeneratedBlockDataMappings.getGlobalPaletteIDFromState(tag));
|
DataTypeIO.writeVarInt(dataOut, GeneratedBlockDataMappings.getGlobalPaletteIDFromState(tag));
|
||||||
//Limbo.getInstance().getConsole().sendMessage(tag + " -> " + GeneratedDataUtils.getGlobalPaletteIDFromState(tag));
|
//Limbo.getInstance().getConsole().sendMessage(tag + " -> " + GeneratedDataUtils.getGlobalPaletteIDFromState(tag));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
dataOut.writeByte(14);
|
|
||||||
}
|
|
||||||
|
|
||||||
DataTypeIO.writeVarInt(dataOut, section.getBlockStates().length);
|
BitSet bits = BitSet.valueOf(section.getBlockStates());
|
||||||
for (int u = 0; u < section.getBlockStates().length; u++) {
|
int shift = 64 % newBits;
|
||||||
dataOut.writeLong(section.getBlockStates()[u]);
|
int longsNeeded = (int) Math.ceil(4096 / (double) (64 / newBits));
|
||||||
//Limbo.getInstance().getConsole().sendMessage(Arrays.toString(section.getBlockStates()));
|
for (int u = 64; u <= bits.length(); u += 64) {
|
||||||
|
bits = BitsUtils.shiftAfter(bits, u - shift, shift);
|
||||||
|
}
|
||||||
|
|
||||||
|
long[] formattedLongs = bits.toLongArray();
|
||||||
|
//Limbo.getInstance().getConsole().sendMessage(longsNeeded + "");
|
||||||
|
|
||||||
|
DataTypeIO.writeVarInt(dataOut, longsNeeded);
|
||||||
|
for (int u = 0; u < longsNeeded; u++) {
|
||||||
|
if (u < formattedLongs.length) {
|
||||||
|
dataOut.writeLong(formattedLongs[u]);
|
||||||
|
} else {
|
||||||
|
dataOut.writeLong(0);
|
||||||
|
}
|
||||||
|
//Limbo.getInstance().getConsole().sendMessage(Arrays.toString(section.getBlockStates()));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
dataOut.writeByte(15);
|
||||||
|
section.getBlockStates();
|
||||||
|
int longsNeeded = 1024;
|
||||||
|
List<Integer> list = new LinkedList<>();
|
||||||
|
for (int y = 0; y < 16; y++) {
|
||||||
|
for (int z = 0; z < 16; z++) {
|
||||||
|
for (int x = 0; x < 16; x++) {
|
||||||
|
list.add(GeneratedBlockDataMappings.getGlobalPaletteIDFromState(section.getBlockStateAt(x, y, z)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
List<Long> globalLongs = new ArrayList<>();
|
||||||
|
long currentLong = 0;
|
||||||
|
int pos = 0;
|
||||||
|
int u = 0;
|
||||||
|
while (pos < longsNeeded) {
|
||||||
|
if (u == 3) {
|
||||||
|
globalLongs.add(currentLong);
|
||||||
|
currentLong = 0;
|
||||||
|
u = 0;
|
||||||
|
pos++;
|
||||||
|
} else {
|
||||||
|
u++;
|
||||||
|
}
|
||||||
|
int id = list.isEmpty() ? 0 : list.remove(0);
|
||||||
|
currentLong = currentLong << 15;
|
||||||
|
currentLong |= (long) id;
|
||||||
|
}
|
||||||
|
DataTypeIO.writeVarInt(dataOut, longsNeeded);
|
||||||
|
for (int j = 0; j < longsNeeded; j++) {
|
||||||
|
if (j < globalLongs.size()) {
|
||||||
|
dataOut.writeLong(globalLongs.get(j));
|
||||||
|
} else {
|
||||||
|
dataOut.writeLong(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.loohp.limbo.Utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.BitSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BitsUtils {
|
||||||
|
|
||||||
|
public static BitSet shiftAfter(BitSet bitset, int from, int shift) {
|
||||||
|
BitSet subset = bitset.get(from, bitset.length());
|
||||||
|
for (int i = 0; i < subset.length(); i++) {
|
||||||
|
bitset.set(from + shift + i, subset.get(i));
|
||||||
|
}
|
||||||
|
if (shift > 0) {
|
||||||
|
for (int i = 0; i < shift; i++) {
|
||||||
|
bitset.set(from + i, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bitset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toLongString(BitSet bitset) {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
for (long l : bitset.toLongArray()) {
|
||||||
|
list.add(Long.toBinaryString(l));
|
||||||
|
}
|
||||||
|
return Arrays.toString(list.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,8 +16,8 @@ public class Schematic {
|
||||||
public static World toWorld(String name, Environment environment, CompoundTag nbt) {
|
public static World toWorld(String name, Environment environment, CompoundTag nbt) {
|
||||||
short width = nbt.getShort("Width");
|
short width = nbt.getShort("Width");
|
||||||
short length = nbt.getShort("Length");
|
short length = nbt.getShort("Length");
|
||||||
short height = nbt.getShort("Height");
|
//short height = nbt.getShort("Height");
|
||||||
byte[] blocks = nbt.getByteArray("BlockData");
|
byte[] blockdata = nbt.getByteArray("BlockData");
|
||||||
CompoundTag palette = nbt.getCompoundTag("Palette");
|
CompoundTag palette = nbt.getCompoundTag("Palette");
|
||||||
ListTag<CompoundTag> blockEntities = nbt.getListTag("BlockEntities").asTypedList(CompoundTag.class);
|
ListTag<CompoundTag> blockEntities = nbt.getListTag("BlockEntities").asTypedList(CompoundTag.class);
|
||||||
Map<Integer, String> mapping = new HashMap<>();
|
Map<Integer, String> mapping = new HashMap<>();
|
||||||
|
|
@ -26,29 +26,50 @@ public class Schematic {
|
||||||
}
|
}
|
||||||
|
|
||||||
World world = new World(name, width, length, environment);
|
World world = new World(name, width, length, environment);
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
for (int z = 0; z < length; z++) {
|
|
||||||
int blockIndex = x + z * width + y * width * length;
|
|
||||||
world.setBlock(x, y, z, mapping.get(blocks[blockIndex] < 0 ? blocks[blockIndex] + 256 : blocks[blockIndex]));
|
|
||||||
Chunk chunk = world.getChunkAtWorldPos(x, z);
|
|
||||||
|
|
||||||
Iterator<CompoundTag> itr = blockEntities.iterator();
|
int index = 0;
|
||||||
while (itr.hasNext()) {
|
int i = 0;
|
||||||
CompoundTag tag = itr.next();
|
int value = 0;
|
||||||
int[] pos = tag.getIntArray("Pos");
|
int varint_length = 0;
|
||||||
|
while (i < blockdata.length) {
|
||||||
|
value = 0;
|
||||||
|
varint_length = 0;
|
||||||
|
|
||||||
if (pos[0] == x && pos[1] == y && pos[2] == z) {
|
while (true) {
|
||||||
ListTag<CompoundTag> newTag = chunk.getTileEntities();
|
value |= (blockdata[i] & 127) << (varint_length++ * 7);
|
||||||
newTag.add(SchematicConvertionUtils.toTileEntityTag(tag));
|
if (varint_length > 5) {
|
||||||
chunk.setTileEntities(newTag);
|
throw new RuntimeException("VarInt too big (probably corrupted data)");
|
||||||
itr.remove();
|
}
|
||||||
break;
|
if ((blockdata[i] & 128) != 128) {
|
||||||
}
|
i++;
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
// index = (y * length + z) * width + x
|
||||||
|
int y = index / (width * length);
|
||||||
|
int z = (index % (width * length)) / width;
|
||||||
|
int x = (index % (width * length)) % width;
|
||||||
|
world.setBlock(x, y, z, mapping.get(value));
|
||||||
|
|
||||||
|
Chunk chunk = world.getChunkAtWorldPos(x, z);
|
||||||
|
|
||||||
|
Iterator<CompoundTag> itr = blockEntities.iterator();
|
||||||
|
while (itr.hasNext()) {
|
||||||
|
CompoundTag tag = itr.next();
|
||||||
|
int[] pos = tag.getIntArray("Pos");
|
||||||
|
|
||||||
|
if (pos[0] == x && pos[1] == y && pos[2] == z) {
|
||||||
|
ListTag<CompoundTag> newTag = chunk.getTileEntities();
|
||||||
|
newTag.add(SchematicConvertionUtils.toTileEntityTag(tag));
|
||||||
|
chunk.setTileEntities(newTag);
|
||||||
|
itr.remove();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
for (Chunk[] chunkarray : world.getChunks()) {
|
for (Chunk[] chunkarray : world.getChunks()) {
|
||||||
for (Chunk chunk : chunkarray) {
|
for (Chunk chunk : chunkarray) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue