Reorganise packages and push initial interface

This commit is contained in:
Myles 2016-06-22 17:35:07 +01:00
parent 1217a9b7e2
commit 7385db02c2
16 changed files with 202 additions and 81 deletions

View File

@ -1,38 +1,4 @@
package us.myles.ViaVersion.api.minecraft.chunks;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@RequiredArgsConstructor
@Getter
@ToString
public class Chunk {
private final int x;
private final int z;
private final boolean groundUp;
private final int primaryBitmask;
private final ChunkSection[] sections;
private final byte[] biomeData;
private boolean unloadPacket = false;
/**
* Chunk unload.
*
* @param x coord
* @param z coord
*/
public Chunk(int x, int z) {
this(x, z, true, 0, new ChunkSection[16], null);
this.unloadPacket = true;
}
/**
* Does this chunk have biome data
*
* @return True if the chunk has biome data
*/
public boolean hasBiomeData() {
return biomeData != null && groundUp;
}
public interface Chunk {
}

View File

@ -70,7 +70,6 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
/* Actual Class */
private final Class<? super T> outputClass;
private final Set<Class<? extends Type>> compatibilities;
private final String typeName;
public Type(Class<? super T> outputClass) {
@ -80,7 +79,6 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
public Type(String typeName, Class<? super T> outputClass) {
this.outputClass = outputClass;
this.typeName = typeName;
this.compatibilities = new HashSet<>();
}
public Class<? extends Type> getBaseClass() {

View File

@ -0,0 +1,19 @@
package us.myles.ViaVersion.api.type.types.minecraft;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.type.Type;
public abstract class BaseChunkType extends Type<Chunk> {
public BaseChunkType() {
super(Chunk.class);
}
public BaseChunkType(String typeName) {
super(typeName, Chunk.class);
}
@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkType.class;
}
}

View File

@ -17,9 +17,9 @@ public class ViaPacketHandler extends MessageToMessageEncoder {
@Override
protected void encode(ChannelHandlerContext ctx, Object o, List list) throws Exception {
// Split chunk bulk packet up in to single chunk packets before it reached the encoder.
// This will prevent issues with several plugins and other protocol handlers due to the chunk being sent twice.
// It also sends the chunk in the right order possible resolving some issues with added chunk/block/entity data.
// Split chunks bulk packet up in to single chunks packets before it reached the encoder.
// This will prevent issues with several plugins and other protocol handlers due to the chunks being sent twice.
// It also sends the chunks in the right order possible resolving some issues with added chunks/block/entity data.
if (!(o instanceof ByteBuf)) {
info.setLastPacket(o);
/* This transformer is more for fixing issues which we find hard at packet level :) */

View File

@ -52,8 +52,6 @@ public class BlockEntity {
Position pos = new Position((long) x, (long) y, (long) z);
//Sorry, the PacketWrapper class wil handle this in the future
//TODO let the packetwrapper class handle it
if (newId != 9) {
updateBlockEntity(pos, (short) newId, tag, connection);
} else {

View File

@ -3,12 +3,13 @@ package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import java.util.List;
@Data
@AllArgsConstructor
public class Chunk1_9_3_4 {
public class Chunk1_9_3_4 implements Chunk {
private int x;
private int z;
private boolean groundUp;

View File

@ -2,18 +2,20 @@ package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4;
import io.netty.buffer.ByteBuf;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.minecraft.BaseChunkType;
import java.util.ArrayList;
import java.util.List;
public class Chunk1_9_3_4Type extends Type<Chunk1_9_3_4> {
public class Chunk1_9_3_4Type extends BaseChunkType {
public Chunk1_9_3_4Type() {
super("1.9.3 Chunk", Chunk1_9_3_4.class);
super("1.9.3 Chunk");
}
@Override
public Chunk1_9_3_4 read(ByteBuf input) throws Exception {
public Chunk read(ByteBuf input) throws Exception {
int chunkX = input.readInt();
int chunkZ = input.readInt();
@ -33,7 +35,11 @@ public class Chunk1_9_3_4Type extends Type<Chunk1_9_3_4> {
}
@Override
public void write(ByteBuf buffer, Chunk1_9_3_4 chunk) throws Exception {
public void write(ByteBuf buffer, Chunk input) throws Exception {
if (!(input instanceof Chunk1_9_3_4))
throw new Exception("Tried to send the wrong chunk type from 1.9.3-4 chunk: " + input.getClass());
Chunk1_9_3_4 chunk = (Chunk1_9_3_4) input;
buffer.writeInt(chunk.getX());
buffer.writeInt(chunk.getZ());

View File

@ -4,6 +4,7 @@ import org.spacehq.opennbt.tag.builtin.CompoundTag;
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.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
@ -11,7 +12,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
public class Protocol1_9_1_2TO1_9_3_4 extends Protocol {
public static Type<Chunk1_9_3_4> CHUNK = new Chunk1_9_3_4Type();
public static Type<Chunk> CHUNK = new Chunk1_9_3_4Type();
@Override
protected void registerPackets() {
@ -56,8 +57,8 @@ public class Protocol1_9_1_2TO1_9_3_4 extends Protocol {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Chunk1_9_3_4 chunk = wrapper.passthrough(CHUNK);
BlockEntity.handle(chunk.getBlockEntities(), wrapper.user());
Chunk chunk = wrapper.passthrough(CHUNK);
BlockEntity.handle(((Chunk1_9_3_4) chunk).getBlockEntities(), wrapper.user());
}
});
}

View File

@ -0,0 +1,20 @@
package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import java.util.List;
@Data
@AllArgsConstructor
public class Chunk1_9_1_2 implements Chunk {
private int x;
private int z;
private boolean groundUp;
private int bitmask;
private byte[] sections;
List<CompoundTag> blockEntities;
}

View File

@ -0,0 +1,54 @@
package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2;
import io.netty.buffer.ByteBuf;
import org.spacehq.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.minecraft.BaseChunkType;
import java.util.ArrayList;
import java.util.List;
public class Chunk1_9_1_2Type extends BaseChunkType {
public Chunk1_9_1_2Type() {
super("1.9.1/2 Chunk");
}
@Override
public Chunk read(ByteBuf input) throws Exception {
int chunkX = input.readInt();
int chunkZ = input.readInt();
boolean groundUp = input.readBoolean();
int primaryBitmask = Type.VAR_INT.read(input);
int size = Type.VAR_INT.read(input);
byte[] sections = new byte[size];
input.readBytes(sections);
int blockEntities = Type.VAR_INT.read(input);
List<CompoundTag> nbtData = new ArrayList<>();
for (int i = 0; i < blockEntities; i++) {
nbtData.add(Type.NBT.read(input));
}
return new Chunk1_9_1_2(chunkX, chunkZ, groundUp, primaryBitmask, sections, nbtData);
}
@Override
public void write(ByteBuf buffer, 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;
buffer.writeInt(chunk.getX());
buffer.writeInt(chunk.getZ());
buffer.writeBoolean(chunk.isGroundUp());
Type.VAR_INT.write(buffer, chunk.getBitmask());
Type.VAR_INT.write(buffer, chunk.getSections().length);
buffer.writeBytes(chunk.getSections());
// no block entities as it's earlier
}
}

View File

@ -60,6 +60,7 @@ public class Protocol1_9_3TO1_9_1_2 extends Protocol {
}
});
// Chunk packet
registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() {
@Override
public void registerMap() {

View File

@ -0,0 +1,39 @@
package us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
@RequiredArgsConstructor
@Getter
@ToString
public class Chunk1_9to1_8 implements Chunk {
private final int x;
private final int z;
private final boolean groundUp;
private final int primaryBitmask;
private final ChunkSection1_9to1_8[] sections;
private final byte[] biomeData;
private boolean unloadPacket = false;
/**
* Chunk unload.
*
* @param x coord
* @param z coord
*/
public Chunk1_9to1_8(int x, int z) {
this(x, z, true, 0, new ChunkSection1_9to1_8[16], null);
this.unloadPacket = true;
}
/**
* Does this chunks have biome data
*
* @return True if the chunks has biome data
*/
public boolean hasBiomeData() {
return biomeData != null && groundUp;
}
}

View File

@ -1,15 +1,16 @@
package us.myles.ViaVersion.api.minecraft.chunks;
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.NibbleArray;
import us.myles.ViaVersion.api.type.Type;
import java.util.List;
public class ChunkSection {
public class ChunkSection1_9to1_8 {
/**
* Size (dimensions) of blocks in a chunk section.
* Size (dimensions) of blocks in a chunks section.
*/
public static final int SIZE = 16 * 16 * 16; // width * depth * height
/**
@ -25,14 +26,14 @@ public class ChunkSection {
private final NibbleArray blockLight;
private NibbleArray skyLight;
public ChunkSection() {
public ChunkSection1_9to1_8() {
this.blocks = new int[SIZE];
this.blockLight = new NibbleArray(SIZE);
palette.add(0); // AIR
}
/**
* Set a block in the chunk
* Set a block in the chunks
*
* @param x Block X
* @param y Block Y
@ -44,8 +45,13 @@ public class ChunkSection {
setBlock(index(x, y, z), type, data);
}
public int getBlockId(int x, int y, int z){
int index = blocks[index(x, y, z)];
return palette.indexOf(index) >> 4;
}
/**
* Set a block in the chunk based on the index
* Set a block in the chunks based on the index
*
* @param idx Index
* @param type The type of the block
@ -154,7 +160,7 @@ public class ChunkSection {
}
/**
* Get expected size of this chunk section.
* Get expected size of this chunks section.
*
* @return Amount of bytes sent by this section
* @throws Exception If it failed to calculate bits properly

View File

@ -6,7 +6,6 @@ import org.spacehq.opennbt.tag.builtin.StringTag;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
@ -16,6 +15,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.ItemRewriter;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks.Chunk1_9to1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.sounds.Effect;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.sounds.SoundEffect;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks;
@ -115,7 +115,7 @@ public class WorldPackets {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
Chunk chunk = wrapper.passthrough(new ChunkType(clientChunks));
Chunk1_9to1_8 chunk = (Chunk1_9to1_8) wrapper.passthrough(new ChunkType(clientChunks));
if (chunk.isUnloadPacket())
wrapper.setId(0x1D);

View File

@ -29,7 +29,7 @@ public class ClientChunks extends StoredObject {
obfuscateRef = Class.forName("org.spigotmc.AntiXray").getMethod("obfuscate", int.class, int.class, int.class, byte[].class, ReflectionUtil.nms("World"));
worldRef = ReflectionUtil.nms("World");
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Failed to initialise chunk reflection", e);
Bukkit.getLogger().log(Level.WARNING, "Failed to initialise chunks reflection", e);
}
}
@ -81,7 +81,7 @@ public class ClientChunks extends StoredObject {
list.add(chunkPacket);
}
} catch (Exception e) {
Bukkit.getLogger().log(Level.WARNING, "Failed to transform chunk bulk", e);
Bukkit.getLogger().log(Level.WARNING, "Failed to transform chunks bulk", e);
}
return list;
}

View File

@ -4,9 +4,11 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bukkit.Bukkit;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection;
import us.myles.ViaVersion.api.type.PartialType;
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.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 java.nio.ByteBuffer;
@ -17,11 +19,11 @@ import java.util.logging.Level;
public class ChunkType extends PartialType<Chunk, ClientChunks> {
/**
* Amount of sections in a chunk.
* Amount of sections in a chunks.
*/
private static final int SECTION_COUNT = 16;
/**
* size of each chunk section (16x16x16).
* size of each chunks section (16x16x16).
*/
private static final int SECTION_SIZE = 16;
/**
@ -37,6 +39,11 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
return ((long) msw << 32) + lsw - -2147483648L;
}
@Override
public Class<? extends Type> getBaseClass() {
return BaseChunkType.class;
}
@Override
public Chunk read(ByteBuf input, ClientChunks param) throws Exception {
int chunkX = input.readInt();
@ -48,7 +55,7 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
// Data to be read
BitSet usedSections = new BitSet(16);
ChunkSection[] sections = new ChunkSection[16];
ChunkSection1_9to1_8[] sections = new ChunkSection1_9to1_8[16];
byte[] biomeData = null;
// Calculate section count from bitmask
@ -59,30 +66,30 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
}
int sectionCount = usedSections.cardinality(); // the amount of sections set
// If the chunk is from a chunk bulk, it is never an unload packet
// If the chunks is from a chunks bulk, it is never an unload packet
// Other wise, if it has no data, it is :)
boolean isBulkPacket = param.getBulkChunks().remove(chunkHash);
if (sectionCount == 0 && groundUp && !isBulkPacket && param.getLoadedChunks().contains(chunkHash)) {
// This is a chunk unload packet
// This is a chunks unload packet
param.getLoadedChunks().remove(chunkHash);
return new Chunk(chunkX, chunkZ);
return new Chunk1_9to1_8(chunkX, chunkZ);
}
int startIndex = input.readerIndex();
param.getLoadedChunks().add(chunkHash); // mark chunk as loaded
param.getLoadedChunks().add(chunkHash); // mark chunks as loaded
// Read blocks
for (int i = 0; i < SECTION_COUNT; i++) {
if (!usedSections.get(i)) continue; // Section not set
ChunkSection section = new ChunkSection();
ChunkSection1_9to1_8 section = new ChunkSection1_9to1_8();
sections[i] = section;
// Read block data and convert to short buffer
byte[] blockData = new byte[ChunkSection.SIZE * 2];
byte[] blockData = new byte[ChunkSection1_9to1_8.SIZE * 2];
input.readBytes(blockData);
ShortBuffer blockBuf = ByteBuffer.wrap(blockData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
for (int j = 0; j < ChunkSection.SIZE; j++) {
for (int j = 0; j < ChunkSection1_9to1_8.SIZE; j++) {
int mask = blockBuf.get();
int type = mask >> 4;
int data = mask & 0xF;
@ -93,20 +100,20 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
// Read block light
for (int i = 0; i < SECTION_COUNT; i++) {
if (!usedSections.get(i)) continue; // Section not set, has no light
byte[] blockLightArray = new byte[ChunkSection.LIGHT_LENGTH];
byte[] blockLightArray = new byte[ChunkSection1_9to1_8.LIGHT_LENGTH];
input.readBytes(blockLightArray);
sections[i].setBlockLight(blockLightArray);
}
// Read sky light
int bytesLeft = dataLength - (input.readerIndex() - startIndex);
if (bytesLeft >= ChunkSection.LIGHT_LENGTH) {
if (bytesLeft >= ChunkSection1_9to1_8.LIGHT_LENGTH) {
for (int i = 0; i < SECTION_COUNT; i++) {
if (!usedSections.get(i)) continue; // Section not set, has no light
byte[] skyLightArray = new byte[ChunkSection.LIGHT_LENGTH];
byte[] skyLightArray = new byte[ChunkSection1_9to1_8.LIGHT_LENGTH];
input.readBytes(skyLightArray);
sections[i].setSkyLight(skyLightArray);
bytesLeft -= ChunkSection.LIGHT_LENGTH;
bytesLeft -= ChunkSection1_9to1_8.LIGHT_LENGTH;
}
}
@ -119,15 +126,18 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
// Check remaining bytes
if (bytesLeft > 0) {
Bukkit.getLogger().log(Level.WARNING, bytesLeft + " Bytes left after reading chunk! (" + groundUp + ")");
Bukkit.getLogger().log(Level.WARNING, bytesLeft + " Bytes left after reading chunks! (" + groundUp + ")");
}
// Return chunk
return new Chunk(chunkX, chunkZ, groundUp, bitmask, sections, biomeData);
// Return chunks
return new Chunk1_9to1_8(chunkX, chunkZ, groundUp, bitmask, sections, biomeData);
}
@Override
public void write(ByteBuf output, ClientChunks param, Chunk chunk) throws Exception {
public void write(ByteBuf output, ClientChunks param, Chunk input) throws Exception {
if (!(input instanceof Chunk1_9to1_8)) throw new Exception("Incompatible chunk, " + input.getClass());
Chunk1_9to1_8 chunk = (Chunk1_9to1_8) input;
// Write primary info
output.writeInt(chunk.getX());
output.writeInt(chunk.getZ());
@ -137,7 +147,7 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < SECTION_COUNT; i++) {
ChunkSection section = chunk.getSections()[i];
ChunkSection1_9to1_8 section = chunk.getSections()[i];
if (section == null) continue; // Section not set
section.writeBlocks(buf);
section.writeBlockLight(buf);
@ -154,4 +164,6 @@ public class ChunkType extends PartialType<Chunk, ClientChunks> {
output.writeBytes(chunk.getBiomeData());
}
}
}