Merge branch 'lenis-patch'

This commit is contained in:
Myles 2016-07-02 22:06:39 +01:00
commit 84d006c71d
14 changed files with 313 additions and 60 deletions

View File

@ -223,6 +223,7 @@ public class PacketWrapper {
}
if (readableObjects.size() > 0) {
packetValues.addAll(readableObjects);
readableObjects.clear();
}
int index = 0;
@ -267,7 +268,7 @@ public class PacketWrapper {
private void writeRemaining(ByteBuf output) {
if (inputBuffer != null) {
output.writeBytes(inputBuffer);
output.writeBytes(inputBuffer, inputBuffer.readableBytes());
}
}
@ -410,6 +411,10 @@ public class PacketWrapper {
* Reset the reader, so that it can be read again.
*/
public void resetReader() {
// Move readable objects are packet values
this.packetValues.addAll(readableObjects);
this.readableObjects.clear();
// Move all packet values to the readable for next packet.
this.readableObjects.addAll(packetValues);
this.packetValues.clear();
}

View File

@ -1,4 +1,17 @@
package us.myles.ViaVersion.api.minecraft.chunks;
public interface Chunk {
int getX();
int getZ();
ChunkSection[] getSections();
boolean isGroundUp();
boolean isBiomeData();
byte[] getBiomeData();
int getBitmask();
}

View File

@ -0,0 +1,15 @@
package us.myles.ViaVersion.api.minecraft.chunks;
import io.netty.buffer.ByteBuf;
public interface ChunkSection {
int getBlockId(int x, int y, int z);
void writeBlocks(ByteBuf output) throws Exception;
void writeBlockLight(ByteBuf output) throws Exception;
boolean hasSkyLight();
void writeSkyLight(ByteBuf output) throws Exception;
}

View File

@ -6,7 +6,7 @@ import us.myles.ViaVersion.api.type.TypeConverter;
public class UnsignedByteType extends Type<Short> implements TypeConverter<Short> {
public UnsignedByteType() {
super(Short.class);
super("Unsigned Byte", Short.class);
}
@Override

View File

@ -14,7 +14,16 @@ public class Chunk1_9_3_4 implements Chunk {
private int z;
private boolean groundUp;
private int bitmask;
private byte[] sections;
private ChunkSection1_9_3_4[] sections;
List<CompoundTag> blockEntities;
@Override
public boolean isBiomeData() {
return false;
}
@Override
public byte[] getBiomeData() {
return new byte[0];
}
}

View File

@ -31,7 +31,7 @@ public class Chunk1_9_3_4Type extends BaseChunkType {
for (int i = 0; i < blockEntities; i++) {
nbtData.add(Type.NBT.read(input));
}
return new Chunk1_9_3_4(chunkX, chunkZ, groundUp, primaryBitmask, sections, nbtData);
return new Chunk1_9_3_4(chunkX, chunkZ, groundUp, primaryBitmask, new ChunkSection1_9_3_4[] {new ChunkSection1_9_3_4(sections)}, nbtData);
}
@Override
@ -46,8 +46,8 @@ public class Chunk1_9_3_4Type extends BaseChunkType {
buffer.writeBoolean(chunk.isGroundUp());
Type.VAR_INT.write(buffer, chunk.getBitmask());
Type.VAR_INT.write(buffer, chunk.getSections().length);
buffer.writeBytes(chunk.getSections());
Type.VAR_INT.write(buffer, chunk.getSections()[0].getData().length);
buffer.writeBytes(chunk.getSections()[0].getData());
// no block entities as it's earlier
}

View File

@ -0,0 +1,41 @@
package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4;
import io.netty.buffer.ByteBuf;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
public class ChunkSection1_9_3_4 implements ChunkSection {
private final byte[] data;
public ChunkSection1_9_3_4(byte[] data) {
this.data = data;
}
public byte[] getData() {
return data;
}
@Override
public int getBlockId(int x, int y, int z) {
throw new UnsupportedOperationException("Invalid chunk type!");
}
@Override
public void writeBlocks(ByteBuf output) throws Exception {
throw new UnsupportedOperationException("Invalid chunk type!");
}
@Override
public void writeBlockLight(ByteBuf output) throws Exception {
throw new UnsupportedOperationException("Invalid chunk type!");
}
@Override
public boolean hasSkyLight() {
throw new UnsupportedOperationException("Invalid chunk type!");
}
@Override
public void writeSkyLight(ByteBuf output) throws Exception {
throw new UnsupportedOperationException("Invalid chunk type!");
}
}

View File

@ -2,18 +2,23 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bukkit.World;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.minecraft.BaseChunkType;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks.ChunkSection1_9to1_8;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.ArrayList;
import java.util.BitSet;
public class Chunk1_9_1_2Type extends BaseChunkType {
public Chunk1_9_1_2Type() {
private final ClientWorld clientWorld;
public Chunk1_9_1_2Type(ClientWorld clientWorld) {
super("1.9.1/2 Chunk");
this.clientWorld = clientWorld;
}
@Override
@ -39,29 +44,24 @@ public class Chunk1_9_1_2Type extends BaseChunkType {
if (!usedSections.get(i)) continue; // Section not set
ChunkSection1_9_1_2 section = new ChunkSection1_9_1_2();
sections[i] = section;
// WIP.
// section.readBlocks(input);
// section.readBlockLight(input);
// section.readSkyLight(input);
/*
BlockStorage blocks = new BlockStorage(in);
NibbleArray3d blocklight = new NibbleArray3d(in, 2048); (1024 bytes)
NibbleArray3d skylight = hasSkylight ? new NibbleArray3d(in, 2048) : null; (1024 bytes)
chunks[index] = new Chunk(blocks, blocklight, skylight);
*/
section.readBlocks(input);
section.readBlockLight(input);
if(clientWorld.getEnvironment() == World.Environment.NORMAL) {
section.readSkyLight(input);
}
}
byte[] biomeData = groundUp ? new byte[256] : null;
if (groundUp)
if (groundUp) {
input.readBytes(biomeData);
}
return new Chunk1_9_1_2(chunkX, chunkZ, groundUp, primaryBitmask, sections, biomeData, new ArrayList<CompoundTag>());
}
@Override
public void write(ByteBuf output, Chunk input) throws Exception {
if (!(input instanceof Chunk1_9_1_2))
throw new Exception("Tried to send the wrong chunk type from 1.9.3-4 chunk: " + input.getClass());
Chunk1_9_1_2 chunk = (Chunk1_9_1_2) input;
Chunk chunk = input;
output.writeInt(chunk.getX());
output.writeInt(chunk.getZ());
@ -71,7 +71,7 @@ public class Chunk1_9_1_2Type extends BaseChunkType {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < 16; i++) {
ChunkSection1_9_1_2 section = chunk.getSections()[i];
ChunkSection section = chunk.getSections()[i];
if (section == null) continue; // Section not set
section.writeBlocks(buf);
section.writeBlockLight(buf);
@ -89,7 +89,5 @@ public class Chunk1_9_1_2Type extends BaseChunkType {
if (chunk.isBiomeData()) {
output.writeBytes(chunk.getBiomeData());
}
// Type.NBT_ARRAY.write(output, tags.toArray(new CompoundTag[0])); Written by the handler
}
}

View File

@ -3,12 +3,13 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray;
import us.myles.ViaVersion.api.type.Type;
import java.util.List;
public class ChunkSection1_9_1_2 {
public class ChunkSection1_9_1_2 implements ChunkSection {
/**
* Size (dimensions) of blocks in a chunks section.
*/
@ -45,9 +46,9 @@ public class ChunkSection1_9_1_2 {
setBlock(index(x, y, z), type, data);
}
public int getBlockId(int x, int y, int z){
public int getBlockId(int x, int y, int z) {
int index = blocks[index(x, y, z)];
return palette.indexOf(index) >> 4;
return palette.get(index) >> 4;
}
/**
@ -88,7 +89,103 @@ public class ChunkSection1_9_1_2 {
}
private int index(int x, int y, int z) {
return z << 8 | y << 4 | x;
return y << 8 | z << 4 | x;
}
/**
* Read blocks from input stream.
* This reads all the block related data:
* <ul>
* <li>Block length/palette type</li>
* <li>Palette</li>
* <li>Block hashes/palette reference</li>
* </ul>
*
* @param input The buffer to read from.
* @throws Exception
*/
public void readBlocks(ByteBuf input) throws Exception {
palette.clear();
// Reaad bits per block
int bitsPerBlock = input.readUnsignedByte();
long maxEntryValue = (1L << bitsPerBlock) - 1;
if (bitsPerBlock == 0) {
bitsPerBlock = 13;
}
if (bitsPerBlock < 4) {
bitsPerBlock = 4;
}
if (bitsPerBlock > 8) {
bitsPerBlock = 13;
}
int paletteLength = Type.VAR_INT.read(input);
if (bitsPerBlock != 13) {
// Read palette
for (int i = 0; i < paletteLength; i++) {
if (bitsPerBlock != 13) {
palette.add(Type.VAR_INT.read(input));
} else {
Type.VAR_INT.read(input);
}
}
}
// Read blocks
Long[] blockData = Type.LONG_ARRAY.read(input);
if (blockData.length > 0) {
for (int i = 0; i < blocks.length; i++) {
int bitIndex = i * bitsPerBlock;
int startIndex = bitIndex / 64;
int endIndex = ((i + 1) * bitsPerBlock - 1) / 64;
int startBitSubIndex = bitIndex % 64;
int val;
if (startIndex == endIndex) {
val = (int) (blockData[startIndex] >>> startBitSubIndex & maxEntryValue);
} else {
int endBitSubIndex = 64 - startBitSubIndex;
val = (int) ((blockData[startIndex] >>> startBitSubIndex | blockData[endIndex] << endBitSubIndex) & maxEntryValue);
}
if (bitsPerBlock == 13) {
int type = val >> 4;
int data = val & 0xF;
setBlock(i, type, data);
} else {
blocks[i] = val;
}
}
}
}
/**
* Read block light from buffer.
*
* @param input The buffer to read from
*/
public void readBlockLight(ByteBuf input) {
byte[] handle = new byte[LIGHT_LENGTH];
input.readBytes(handle);
blockLight.setHandle(handle);
}
/**
* Read sky light from buffer.
* Note: Only sent in overworld!
*
* @param input The buffer to read from
*/
public void readSkyLight(ByteBuf input) {
byte[] handle = new byte[LIGHT_LENGTH];
input.readBytes(handle);
if (skyLight != null) {
skyLight.setHandle(handle);
return;
}
this.skyLight = new NibbleArray(handle);
}
/**

View File

@ -6,15 +6,14 @@ import org.spacehq.opennbt.tag.builtin.StringTag;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks.Chunk1_9to1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks.ChunkSection1_9to1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.types.ChunkType;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
import java.util.ArrayList;
import java.util.List;
@ -74,35 +73,65 @@ public class Protocol1_9_3TO1_9_1_2 extends Protocol {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
ChunkType type = new ChunkType(clientChunks);
if (wrapper.isReadable(type, 0)) {
Chunk1_9to1_8 chunk = (Chunk1_9to1_8) wrapper.read(type);
Chunk1_9_1_2Type type = new Chunk1_9_1_2Type(clientWorld);
Chunk chunk = wrapper.passthrough(type);
List<CompoundTag> tags = new ArrayList<>();
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection1_9to1_8 section = chunk.getSections()[i];
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
for (int x = 0; x < 16; x++)
for (int y = 0; y < 16; y++)
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
int block = section.getBlockId(x, y, z);
if (FakeTileEntity.hasBlock(block)) {
// NOT SURE WHY Y AND Z WORK THIS WAY, TODO: WORK OUT WHY THIS IS OR FIX W/E BROKE IT
tags.add(FakeTileEntity.getFromBlock(x + (chunk.getX() << 4), z + (i << 4), y + (chunk.getZ() << 4), block));
tags.add(FakeTileEntity.getFromBlock(x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block));
}
}
}
}
}
wrapper.write(type, chunk);
wrapper.write(Type.NBT_ARRAY, tags.toArray(new CompoundTag[0]));
} else {
wrapper.passthroughAll();
wrapper.write(Type.VAR_INT, 0);
}
});
}
});
// Join (save dimension id)
registerOutgoing(State.PLAY, 0x23, 0x23, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Entity ID
map(Type.UNSIGNED_BYTE); // 1 - Gamemode
map(Type.INT); // 2 - Dimension
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 1);
clientChunks.setEnvironment(dimensionId);
}
});
}
});
// Respawn (save dimension id)
registerOutgoing(State.PLAY, 0x33, 0x33, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Dimension ID
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
int dimensionId = wrapper.get(Type.INT, 0);
clientWorld.setEnvironment(dimensionId);
}
});
}
@ -110,7 +139,7 @@ public class Protocol1_9_3TO1_9_1_2 extends Protocol {
}
@Override
public void init(UserConnection userConnection) {
public void init(UserConnection user) {
user.put(new ClientWorld(user));
}
}

View File

@ -0,0 +1,32 @@
package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage;
import lombok.Getter;
import org.bukkit.World;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
@Getter
public class ClientWorld extends StoredObject {
private World.Environment environment;
public ClientWorld(UserConnection user) {
super(user);
}
public void setEnvironment(int environmentId) {
this.environment = getEnvFromId(environmentId);
}
private World.Environment getEnvFromId(int id) {
switch(id) {
case -1:
return World.Environment.NETHER;
case 0:
return World.Environment.NORMAL;
case 1:
return World.Environment.THE_END;
default:
throw new IllegalArgumentException("Invalid environment id:" + id);
}
}
}

View File

@ -36,4 +36,14 @@ public class Chunk1_9to1_8 implements Chunk {
public boolean hasBiomeData() {
return biomeData != null && groundUp;
}
@Override
public boolean isBiomeData() {
return biomeData != null;
}
@Override
public int getBitmask() {
return primaryBitmask;
}
}

View File

@ -3,12 +3,13 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks;
import com.google.common.collect.Lists;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray;
import us.myles.ViaVersion.api.type.Type;
import java.util.List;
public class ChunkSection1_9to1_8 {
public class ChunkSection1_9to1_8 implements ChunkSection {
/**
* Size (dimensions) of blocks in a chunks section.
*/
@ -88,7 +89,7 @@ public class ChunkSection1_9to1_8 {
}
private int index(int x, int y, int z) {
return z << 8 | y << 4 | x;
return y << 8 | z << 4 | x;
}
/**

View File

@ -3,7 +3,10 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.storage;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldType;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;