diff --git a/pom.xml b/pom.xml index 095cdc63..205d761b 100644 --- a/pom.xml +++ b/pom.xml @@ -88,9 +88,9 @@ compile - com.github.GeyserMC + com.github.steveice10 packetlib - 2.1 + 3.0-SNAPSHOT compile diff --git a/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java b/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java index d5c31e21..217960d5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java +++ b/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java @@ -2,17 +2,20 @@ package com.github.steveice10.mc.protocol; import com.github.steveice10.mc.auth.data.GameProfile; import com.github.steveice10.mc.protocol.codec.MinecraftCodec; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.codec.PacketCodec; import com.github.steveice10.mc.protocol.codec.PacketStateCodec; import com.github.steveice10.mc.protocol.data.ProtocolState; import com.github.steveice10.packetlib.Server; import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.codec.PacketCodecHelper; +import com.github.steveice10.packetlib.codec.PacketDefinition; import com.github.steveice10.packetlib.crypt.AESEncryption; import com.github.steveice10.packetlib.crypt.PacketEncryption; -import com.github.steveice10.packetlib.io.NetInput; import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.PacketHeader; import com.github.steveice10.packetlib.packet.PacketProtocol; +import io.netty.buffer.ByteBuf; import lombok.Getter; import lombok.NonNull; import lombok.Setter; @@ -131,6 +134,11 @@ public class MinecraftProtocol extends PacketProtocol { return MinecraftConstants.PACKET_HEADER; } + @Override + public PacketCodecHelper createHelper() { + return this.codec.getHelperFactory().get(); + } + @Override public void newClientSession(Session session) { session.setFlag(MinecraftConstants.PROFILE_KEY, this.profile); @@ -175,8 +183,8 @@ public class MinecraftProtocol extends PacketProtocol { } @Override - public Packet createClientboundPacket(int id, NetInput in) throws IOException { - return this.stateCodec.createClientboundPacket(id, in); + public Packet createClientboundPacket(int id, ByteBuf buf, PacketCodecHelper codecHelper) throws IOException { + return this.stateCodec.createClientboundPacket(id, buf, codecHelper); } @Override @@ -195,8 +203,8 @@ public class MinecraftProtocol extends PacketProtocol { } @Override - public Packet createServerboundPacket(int id, NetInput in) throws IOException { - return this.stateCodec.createServerboundPacket(id, in); + public Packet createServerboundPacket(int id, ByteBuf buf, PacketCodecHelper codecHelper) throws IOException { + return this.stateCodec.createServerboundPacket(id, buf, codecHelper); } @Override @@ -213,4 +221,14 @@ public class MinecraftProtocol extends PacketProtocol { public Class getServerboundClass(int id) { return this.stateCodec.getServerboundClass(id); } + + @Override + public PacketDefinition getServerboundDefinition(int id) { + return this.stateCodec.getServerboundDefinition(id); + } + + @Override + public PacketDefinition getClientboundDefinition(int id) { + return this.stateCodec.getClientboundDefinition(id); + } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodec.java b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodec.java index d3d3d994..5a3a117c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodec.java +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodec.java @@ -1,6 +1,8 @@ package com.github.steveice10.mc.protocol.codec; import com.github.steveice10.mc.protocol.data.ProtocolState; +import com.github.steveice10.mc.protocol.data.game.level.event.LevelEvent; +import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound; import com.github.steveice10.mc.protocol.packet.handshake.serverbound.ClientIntentionPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundAwardStatsPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundBossEventPacket; @@ -169,11 +171,30 @@ import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPo import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket; import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket; import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundStatusRequestPacket; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; + +import java.util.HashMap; +import java.util.Map; public class MinecraftCodec { + private static final Int2ObjectMap LEVEL_EVENTS = new Int2ObjectOpenHashMap<>(); + private static final Map SOUND_NAMES = new HashMap<>(); + + static { + for (LevelEvent levelEvent : LevelEvent.values()) { + LEVEL_EVENTS.put(levelEvent.getId(), levelEvent); + } + + for (BuiltinSound sound : BuiltinSound.values()) { + SOUND_NAMES.put(sound.getName(), sound); + } + } + public static final PacketCodec CODEC = PacketCodec.builder() - .protocolVersion((1 << 30) | 87) - .minecraftVersion("1.19-pre3") + .protocolVersion((1 << 30) | 91) + .helper(() -> new MinecraftCodecHelper(LEVEL_EVENTS, SOUND_NAMES)) + .minecraftVersion("1.19-rc2") .state(ProtocolState.HANDSHAKE, PacketStateCodec.builder() .registerServerboundPacket(0x00, ClientIntentionPacket.class, ClientIntentionPacket::new) ) diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java new file mode 100644 index 00000000..f66506a8 --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftCodecHelper.java @@ -0,0 +1,694 @@ +package com.github.steveice10.mc.protocol.codec; + +import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; +import com.github.steveice10.mc.protocol.data.MagicValues; +import com.github.steveice10.mc.protocol.data.game.Identifier; +import com.github.steveice10.mc.protocol.data.game.MessageType; +import com.github.steveice10.mc.protocol.data.game.chunk.BitStorage; +import com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection; +import com.github.steveice10.mc.protocol.data.game.chunk.DataPalette; +import com.github.steveice10.mc.protocol.data.game.chunk.NibbleArray3d; +import com.github.steveice10.mc.protocol.data.game.chunk.palette.*; +import com.github.steveice10.mc.protocol.data.game.entity.Effect; +import com.github.steveice10.mc.protocol.data.game.entity.EntityEvent; +import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierOperation; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.*; +import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; +import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage; +import com.github.steveice10.mc.protocol.data.game.entity.type.PaintingType; +import com.github.steveice10.mc.protocol.data.game.level.LightUpdateData; +import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType; +import com.github.steveice10.mc.protocol.data.game.level.event.LevelEvent; +import com.github.steveice10.mc.protocol.data.game.level.particle.*; +import com.github.steveice10.mc.protocol.data.game.level.particle.positionsource.BlockPositionSource; +import com.github.steveice10.mc.protocol.data.game.level.particle.positionsource.EntityPositionSource; +import com.github.steveice10.mc.protocol.data.game.level.particle.positionsource.PositionSource; +import com.github.steveice10.mc.protocol.data.game.level.particle.positionsource.PositionSourceType; +import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound; +import com.github.steveice10.mc.protocol.data.game.level.sound.SoundCategory; +import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient; +import com.github.steveice10.mc.protocol.data.game.statistic.StatisticCategory; +import com.github.steveice10.opennbt.NBTIO; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.Tag; +import com.github.steveice10.packetlib.codec.BasePacketCodecHelper; +import com.nukkitx.math.vector.Vector3f; +import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import lombok.RequiredArgsConstructor; +import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.function.ObjIntConsumer; +import java.util.function.ToIntFunction; + +@RequiredArgsConstructor +public class MinecraftCodecHelper extends BasePacketCodecHelper { + private static final int POSITION_X_SIZE = 38; + private static final int POSITION_Y_SIZE = 12; + private static final int POSITION_Z_SIZE = 38; + private static final int POSITION_Y_SHIFT = 0xFFF; + private static final int POSITION_WRITE_SHIFT = 0x3FFFFFF; + + private final Int2ObjectMap levelEvents; + private final Map soundNames; + + public UUID readUUID(ByteBuf buf) { + return new UUID(buf.readLong(), buf.readLong()); + } + + public void writeUUID(ByteBuf buf, UUID uuid) { + buf.writeLong(uuid.getMostSignificantBits()); + buf.writeLong(uuid.getLeastSignificantBits()); + } + + public byte[] readByteArray(ByteBuf buf) { + return this.readByteArray(buf, this::readVarInt); + } + + public byte[] readByteArray(ByteBuf buf, ToIntFunction reader) { + int length = reader.applyAsInt(buf); + byte[] bytes = new byte[length]; + buf.readBytes(bytes); + return bytes; + } + + public void writeByteArray(ByteBuf buf, byte[] bytes) { + this.writeByteArray(buf, bytes, this::writeVarInt); + } + + public void writeByteArray(ByteBuf buf, byte[] bytes, ObjIntConsumer writer) { + writer.accept(buf, bytes.length); + buf.writeBytes(bytes); + } + + public long[] readLongArray(ByteBuf buf) { + return this.readLongArray(buf, this::readVarInt); + } + + public long[] readLongArray(ByteBuf buf, ToIntFunction reader) { + int length = reader.applyAsInt(buf); + if (length < 0) { + throw new IllegalArgumentException("Array cannot have length less than 0."); + } + + long[] l = new long[length]; + for (int index = 0; index < length; index++) { + l[index] = buf.readLong(); + } + + return l; + } + + public void writeLongArray(ByteBuf buf, long[] l) { + this.writeLongArray(buf, l, this::writeVarInt); + } + + public void writeLongArray(ByteBuf buf, long[] l, ObjIntConsumer writer) { + writer.accept(buf, l.length); + for (long value : l) { + buf.writeLong(value); + } + } + + public CompoundTag readTag(ByteBuf buf) throws IOException { + return readTag(buf, CompoundTag.class); + } + + @Nullable + public T readTag(ByteBuf buf, Class expected) throws IOException { + Tag tag = NBTIO.readTag(new InputStream() { + @Override + public int read() { + return buf.readUnsignedByte(); + } + }); + + if (tag == null) { + return null; + } + + if (tag.getClass() != expected) { + throw new IllegalArgumentException("Expected tag of type " + expected.getName() + " but got " + tag.getClass().getName()); + } + + return expected.cast(tag); + } + + public CompoundTag readTagLE(ByteBuf buf) throws IOException { + return readTagLE(buf, CompoundTag.class); + } + + @Nullable + public T readTagLE(ByteBuf buf, Class expected) throws IOException { + Tag tag = NBTIO.readTag(new InputStream() { + @Override + public int read() { + return buf.readUnsignedByte(); + } + }, true); + + if (tag == null) { + return null; + } + + if (tag.getClass() != expected) { + throw new IllegalArgumentException("Expected tag of type " + expected.getName() + " but got " + tag.getClass().getName()); + } + + return expected.cast(tag); + } + + public void writeTag(ByteBuf buf, T tag) throws IOException { + NBTIO.writeTag(new OutputStream() { + @Override + public void write(int b) throws IOException { + buf.writeByte(b); + } + }, tag); + } + + public void writeTagLE(ByteBuf buf, T tag) throws IOException { + NBTIO.writeTag(new OutputStream() { + @Override + public void write(int b) throws IOException { + buf.writeByte(b); + } + }, tag, true); + } + + public ItemStack readItemStack(ByteBuf buf) throws IOException { + boolean present = buf.readBoolean(); + if (!present) { + return null; + } + + int item = this.readVarInt(buf); + return new ItemStack(item, buf.readByte(), this.readTag(buf)); + } + + public void writeItemStack(ByteBuf buf, ItemStack item) throws IOException { + buf.writeBoolean(item != null); + if (item != null) { + this.writeVarInt(buf, item.getId()); + buf.writeByte(item.getAmount()); + this.writeTag(buf, item.getNbt()); + } + } + + public Vector3i readPosition(ByteBuf buf) { + long val = buf.readLong(); + + int x = (int) (val >> POSITION_X_SIZE); + int y = (int) (val << 52 >> 52); + int z = (int) (val << 26 >> POSITION_Z_SIZE); + + return Vector3i.from(x, y, z); + } + + public void writePosition(ByteBuf buf, Vector3i pos) { + long x = pos.getX() & POSITION_WRITE_SHIFT; + long y = pos.getY() & POSITION_Y_SHIFT; + long z = pos.getZ() & POSITION_WRITE_SHIFT; + + buf.writeLong(x << POSITION_X_SIZE | z << POSITION_Y_SIZE | y); + } + + public Vector3f readRotation(ByteBuf buf) { + float x = buf.readFloat(); + float y = buf.readFloat(); + float z = buf.readFloat(); + + return Vector3f.from(x, y, z); + } + + public void writeRotation(ByteBuf buf, Vector3f rot) { + buf.writeFloat(rot.getX()); + buf.writeFloat(rot.getY()); + buf.writeFloat(rot.getZ()); + } + + public Direction readDirection(ByteBuf buf) { + return Direction.from(this.readVarInt(buf)); + } + + public void writeDirection(ByteBuf buf, Direction dir) { + this.writeEnum(buf, dir); + } + + public Pose readPose(ByteBuf buf) { + return Pose.from(this.readVarInt(buf)); + } + + public void writePose(ByteBuf buf, Pose pose) { + this.writeEnum(buf, pose); + } + + public PaintingType readPaintingType(ByteBuf buf) { + return PaintingType.from(this.readVarInt(buf)); + } + + public void writePaintingType(ByteBuf buf, PaintingType type) { + this.writeEnum(buf, type); + } + + private void writeEnum(ByteBuf buf, Enum e){ + this.writeVarInt(buf, e.ordinal()); + } + + public Component readComponent(ByteBuf buf) { + return DefaultComponentSerializer.get().deserialize(this.readString(buf)); + } + + public void writeComponent(ByteBuf buf, Component component) { + this.writeString(buf, DefaultComponentSerializer.get().serialize(component)); + } + + public EntityMetadata[] readEntityMetadata(ByteBuf buf) throws IOException { + List> ret = new ArrayList<>(); + int id; + while ((id = buf.readUnsignedByte()) != 255) { + ret.add(this.readMetadata(buf, id)); + } + + return ret.toArray(new EntityMetadata[0]); + } + + public void writeEntityMetadata(ByteBuf buf, EntityMetadata[] metadata) throws IOException { + for (EntityMetadata meta : metadata) { + this.writeMetadata(buf, meta); + } + + buf.writeByte(255); + } + + public EntityMetadata readMetadata(ByteBuf buf, int id) throws IOException { + MetadataType type = this.readMetadataType(buf); + return type.readMetadata(this, buf, id); + } + + public void writeMetadata(ByteBuf buf, EntityMetadata metadata) throws IOException { + buf.writeByte(metadata.getId()); + this.writeMetadataType(buf, metadata.getType()); + metadata.write(this, buf); + } + + public MetadataType readMetadataType(ByteBuf buf) { + int id = this.readVarInt(buf); + if (id >= MetadataType.size()) { + throw new IllegalArgumentException("Received id " + id + " for MetadataType when the maximum was " + MetadataType.size() + "!"); + } + + return MetadataType.from(id); + } + + public void writeMetadataType(ByteBuf buf, MetadataType type) { + this.writeVarInt(buf, type.getId()); + } + + public GlobalPos readGlobalPos(ByteBuf buf) { + String dimension = Identifier.formalize(this.readString(buf)); + Vector3i pos = this.readPosition(buf); + return new GlobalPos(dimension, pos); + } + + public void writeGlobalPos(ByteBuf buf, GlobalPos pos) { + this.writeString(buf, pos.getDimension()); + this.writePosition(buf, pos.getPosition()); + } + + public ParticleType readParticleType(ByteBuf buf) { + return ParticleType.from(this.readVarInt(buf)); + } + + public void writeParticleType(ByteBuf buf, ParticleType type) { + this.writeEnum(buf, type); + } + + public Particle readParticle(ByteBuf buf) throws IOException { + ParticleType particleType = this.readParticleType(buf); + return new Particle(particleType, this.readParticleData(buf, particleType)); + } + + public void writeParticle(ByteBuf buf, Particle particle) throws IOException { + this.writeEnum(buf, particle.getType()); + this.writeParticleData(buf, particle.getType(), particle.getData()); + } + + public ParticleData readParticleData(ByteBuf buf, ParticleType type) throws IOException { + switch (type) { + case BLOCK: + case BLOCK_MARKER: + return new BlockParticleData(this.readVarInt(buf)); + case DUST: + float red = buf.readFloat(); + float green = buf.readFloat(); + float blue = buf.readFloat(); + float scale = buf.readFloat(); + return new DustParticleData(red, green, blue, scale); + case DUST_COLOR_TRANSITION: + red = buf.readFloat(); + green = buf.readFloat(); + blue = buf.readFloat(); + scale = buf.readFloat(); + float newRed = buf.readFloat(); + float newGreen = buf.readFloat(); + float newBlue = buf.readFloat(); + return new DustColorTransitionParticleData(red, green, blue, scale, newRed, newGreen, newBlue); + case FALLING_DUST: + return new FallingDustParticleData(this.readVarInt(buf)); + case ITEM: + return new ItemParticleData(this.readItemStack(buf)); + case SCULK_CHARGE: + return new SculkChargeParticleData(buf.readFloat()); + case SHRIEK: + return new ShriekParticleData(this.readVarInt(buf)); + case VIBRATION: + return new VibrationParticleData(this.readPositionSource(buf), this.readVarInt(buf)); + default: + return null; + } + } + + public void writeParticleData(ByteBuf buf, ParticleType type, ParticleData data) throws IOException { + switch (type) { + case BLOCK: + case BLOCK_MARKER: + this.writeVarInt(buf, ((BlockParticleData) data).getBlockState()); + break; + case DUST: + buf.writeFloat(((DustParticleData) data).getRed()); + buf.writeFloat(((DustParticleData) data).getGreen()); + buf.writeFloat(((DustParticleData) data).getBlue()); + buf.writeFloat(((DustParticleData) data).getScale()); + break; + case DUST_COLOR_TRANSITION: + buf.writeFloat(((DustParticleData) data).getRed()); + buf.writeFloat(((DustParticleData) data).getGreen()); + buf.writeFloat(((DustParticleData) data).getBlue()); + buf.writeFloat(((DustParticleData) data).getScale()); + buf.writeFloat(((DustColorTransitionParticleData) data).getNewRed()); + buf.writeFloat(((DustColorTransitionParticleData) data).getNewGreen()); + buf.writeFloat(((DustColorTransitionParticleData) data).getNewBlue()); + break; + case FALLING_DUST: + this.writeVarInt(buf, ((FallingDustParticleData) data).getBlockState()); + break; + case ITEM: + this.writeItemStack(buf, ((ItemParticleData) data).getItemStack()); + break; + case SCULK_CHARGE: + buf.writeFloat(((SculkChargeParticleData) data).getRoll()); + break; + case SHRIEK: + this.writeVarInt(buf, ((ShriekParticleData) data).getDelay()); + break; + case VIBRATION: + this.writePositionSource(buf, ((VibrationParticleData) data).getPositionSource()); + this.writeVarInt(buf, ((VibrationParticleData) data).getArrivalTicks()); + break; + } + } + + public PositionSource readPositionSource(ByteBuf buf) { + PositionSourceType type = this.readPositionSourceType(buf); + switch (type) { + case BLOCK: + return new BlockPositionSource(this.readPosition(buf)); + case ENTITY: + return new EntityPositionSource(this.readVarInt(buf), buf.readFloat()); + default: + throw new IllegalStateException("Unknown position source type!"); + } + } + + public void writePositionSource(ByteBuf buf, PositionSource positionSource) { + this.writePositionSourceType(buf, positionSource.getType()); + if (positionSource instanceof BlockPositionSource) { + this.writePosition(buf, ((BlockPositionSource) positionSource).getPosition()); + } else if (positionSource instanceof EntityPositionSource) { + this.writeVarInt(buf, ((EntityPositionSource) positionSource).getEntityId()); + buf.writeFloat(((EntityPositionSource) positionSource).getYOffset()); + } + + throw new IllegalStateException("Unknown position source type!"); + } + + public PositionSourceType readPositionSourceType(ByteBuf buf) { + return MagicValues.key(PositionSourceType.class, Identifier.formalize(this.readString(buf))); + } + + public void writePositionSourceType(ByteBuf buf, PositionSourceType type) { + this.writeString(buf, MagicValues.value(String.class, type)); + } + + public VillagerData readVillagerData(ByteBuf buf) { + return new VillagerData(this.readVarInt(buf), this.readVarInt(buf), this.readVarInt(buf)); + } + + public void writeVillagerData(ByteBuf buf, VillagerData villagerData) { + this.writeVarInt(buf, villagerData.getType()); + this.writeVarInt(buf, villagerData.getProfession()); + this.writeVarInt(buf, villagerData.getLevel()); + } + + public ModifierOperation readModifierOperation(ByteBuf buf) { + return ModifierOperation.from(buf.readByte()); + } + + public void writeModifierOperation(ByteBuf buf, ModifierOperation operation) { + buf.writeByte(operation.ordinal()); + } + + public Effect readEffect(ByteBuf buf) { + return Effect.from(this.readVarInt(buf) - 1); + } + + public void writeEffect(ByteBuf buf, Effect effect) { + this.writeVarInt(buf, effect.ordinal() + 1); + } + + public BlockBreakStage readBlockBreakStage(ByteBuf buf) { + int stage = buf.readUnsignedByte(); + if (stage >= 0 && stage < 10) { + return BlockBreakStage.STAGES[stage]; + } else { + return BlockBreakStage.RESET; + } + } + + public void writeBlockBreakStage(ByteBuf buf, BlockBreakStage stage) { + if (stage == BlockBreakStage.RESET) { + buf.writeByte(255); + } else { + buf.writeByte(stage.ordinal()); + } + } + + public BlockEntityType readBlockEntityType(ByteBuf buf) { + return BlockEntityType.from(this.readVarInt(buf)); + } + + public void writeBlockEntityType(ByteBuf buf, BlockEntityType type) { + this.writeEnum(buf, type); + } + + public LightUpdateData readLightUpdateData(ByteBuf buf) { + boolean trustEdges = buf.readBoolean(); + + BitSet skyYMask = BitSet.valueOf(this.readLongArray(buf)); + BitSet blockYMask = BitSet.valueOf(this.readLongArray(buf)); + BitSet emptySkyYMask = BitSet.valueOf(this.readLongArray(buf)); + BitSet emptyBlockYMask = BitSet.valueOf(this.readLongArray(buf)); + + int skyUpdateSize = this.readVarInt(buf); + List skyUpdates = new ArrayList<>(skyUpdateSize); + for (int i = 0; i < skyUpdateSize; i++) { + skyUpdates.add(this.readByteArray(buf)); + } + + int blockUpdateSize = this.readVarInt(buf); + List blockUpdates = new ArrayList<>(blockUpdateSize); + for (int i = 0; i < blockUpdateSize; i++) { + blockUpdates.add(this.readByteArray(buf)); + } + + return new LightUpdateData(skyYMask, blockYMask, emptySkyYMask, emptyBlockYMask, skyUpdates, blockUpdates, trustEdges); + } + + public void writeLightUpdateData(ByteBuf buf, LightUpdateData data) { + buf.writeBoolean(data.isTrustEdges()); + + writeBitSet(buf, data.getSkyYMask()); + writeBitSet(buf, data.getBlockYMask()); + writeBitSet(buf, data.getEmptySkyYMask()); + writeBitSet(buf, data.getEmptyBlockYMask()); + + this.writeVarInt(buf, data.getSkyUpdates().size()); + for (byte[] array : data.getSkyUpdates()) { + this.writeByteArray(buf, array); + } + + this.writeVarInt(buf, data.getBlockUpdates().size()); + for (byte[] array : data.getBlockUpdates()) { + this.writeByteArray(buf, array); + } + } + + private void writeBitSet(ByteBuf buf, BitSet bitSet) { + long[] array = bitSet.toLongArray(); + this.writeLongArray(buf, array); + } + + public LevelEvent readLevelEvent(ByteBuf buf) { + return this.levelEvents.get(this.readVarInt(buf)); + } + + public void writeLevelEvent(ByteBuf buf, LevelEvent event) { + this.writeVarInt(buf, event.getId()); + } + + public StatisticCategory readStatisticCategory(ByteBuf buf) { + return StatisticCategory.from(this.readVarInt(buf)); + } + + public void writeStatisticCategory(ByteBuf buf, StatisticCategory category) { + this.writeEnum(buf, category); + } + + public MessageType readMessageType(ByteBuf buf) { + return MessageType.from(this.readVarInt(buf)); + } + + public void writeMessageType(ByteBuf buf, MessageType type) { + this.writeEnum(buf, type); + } + + public SoundCategory readSoundCategory(ByteBuf buf) { + return SoundCategory.from(this.readVarInt(buf)); + } + + public void writeSoundCategory(ByteBuf buf, SoundCategory category) { + this.writeEnum(buf, category); + } + + public BuiltinSound readBuiltinSound(ByteBuf buf) { + return BuiltinSound.from(this.readVarInt(buf)); + } + + public void writeBuiltinSound(ByteBuf buf, BuiltinSound sound) { + this.writeEnum(buf, sound); + } + + @Nullable + public BuiltinSound getBuiltinSound(String name) { + return this.soundNames.get(name); + } + + public EntityEvent readEntityEvent(ByteBuf buf) { + return EntityEvent.from(buf.readByte()); + } + + public void writeEntityEvent(ByteBuf buf, EntityEvent event) { + buf.writeByte(event.ordinal()); + } + + public Ingredient readRecipeIngredient(ByteBuf buf) throws IOException { + ItemStack[] options = new ItemStack[this.readVarInt(buf)]; + for (int i = 0; i < options.length; i++) { + options[i] = this.readItemStack(buf); + } + + return new Ingredient(options); + } + + public void writeRecipeIngredient(ByteBuf buf, Ingredient ingredient) throws IOException { + this.writeVarInt(buf, ingredient.getOptions().length); + for (ItemStack option : ingredient.getOptions()) { + this.writeItemStack(buf, option); + } + } + + public DataPalette readDataPalette(ByteBuf buf, PaletteType paletteType, int globalPaletteBits) throws IOException { + int bitsPerEntry = buf.readByte(); + Palette palette = this.readPalette(buf, paletteType, bitsPerEntry); + BitStorage storage; + if (!(palette instanceof SingletonPalette)) { + storage = new BitStorage(bitsPerEntry, paletteType.getStorageSize(), this.readLongArray(buf)); + } else { + this.readVarInt(buf); + storage = null; + } + + return new DataPalette(palette, storage, paletteType, globalPaletteBits); + } + + public void writeDataPalette(ByteBuf buf, DataPalette palette) { + if (palette.getPalette() instanceof SingletonPalette) { + buf.writeByte(0); // Bits per entry + this.writeVarInt(buf, palette.getPalette().idToState(0)); + this.writeVarInt(buf, 0); // Data length + return; + } + + buf.writeByte(palette.getStorage().getBitsPerEntry()); + + if (!(palette.getPalette() instanceof GlobalPalette)) { + int paletteLength = palette.getPalette().size(); + this.writeVarInt(buf, paletteLength); + for (int i = 0; i < paletteLength; i++) { + this.writeVarInt(buf, palette.getPalette().idToState(i)); + } + } + + long[] data = palette.getStorage().getData(); + this.writeLongArray(buf, data); + } + + private Palette readPalette(ByteBuf buf, PaletteType paletteType, int bitsPerEntry) throws IOException { + if (bitsPerEntry > paletteType.getMaxBitsPerEntry()) { + return new GlobalPalette(); + } + + if (bitsPerEntry == 0) { + return new SingletonPalette(this.readVarInt(buf)); + } + + if (bitsPerEntry <= paletteType.getMinBitsPerEntry()) { + return new ListPalette(bitsPerEntry, buf, this); + } else { + return new MapPalette(bitsPerEntry, buf, this); + } + } + + public ChunkSection readChunkSection(ByteBuf buf, int globalBiomePaletteBits) throws IOException { + int blockCount = buf.readShort(); + + DataPalette chunkPalette = this.readDataPalette(buf, PaletteType.CHUNK, DataPalette.GLOBAL_PALETTE_BITS_PER_ENTRY); + DataPalette biomePalette = this.readDataPalette(buf, PaletteType.BIOME, globalBiomePaletteBits); + return new ChunkSection(blockCount, chunkPalette, biomePalette); + } + + public void writeChunkSection(ByteBuf buf, ChunkSection section) { + buf.writeShort(section.getBlockCount()); + this.writeDataPalette(buf, section.getChunkData()); + this.writeDataPalette(buf, section.getBiomeData()); + } + + public NibbleArray3d readNibbleArray(ByteBuf buf, int size) { + return new NibbleArray3d(this.readByteArray(buf, ignored -> size)); + } + + public void writeNibbleArray(ByteBuf buf, NibbleArray3d nibbleArray) { + buf.writeBytes(nibbleArray.getData()); + } +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftPacket.java b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftPacket.java new file mode 100644 index 00000000..9fbea889 --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftPacket.java @@ -0,0 +1,11 @@ +package com.github.steveice10.mc.protocol.codec; + +import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; + +import java.io.IOException; + +public interface MinecraftPacket extends Packet { + + void serialize(ByteBuf buf, MinecraftCodecHelper helper) throws IOException; +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftPacketSerializer.java b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftPacketSerializer.java new file mode 100644 index 00000000..1026e411 --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/MinecraftPacketSerializer.java @@ -0,0 +1,23 @@ +package com.github.steveice10.mc.protocol.codec; + +import com.github.steveice10.packetlib.codec.PacketDefinition; +import com.github.steveice10.packetlib.codec.PacketSerializer; +import io.netty.buffer.ByteBuf; +import lombok.RequiredArgsConstructor; + +import java.io.IOException; + +@RequiredArgsConstructor +public class MinecraftPacketSerializer implements PacketSerializer { + private final PacketFactory factory; + + @Override + public void serialize(ByteBuf buf, MinecraftCodecHelper helper, T packet) throws IOException { + packet.serialize(buf, helper); + } + + @Override + public T deserialize(ByteBuf buf, MinecraftCodecHelper helper, PacketDefinition definition) throws IOException { + return this.factory.construct(buf, helper); + } +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/PacketCodec.java b/src/main/java/com/github/steveice10/mc/protocol/codec/PacketCodec.java index deadec4d..4bb454b3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/codec/PacketCodec.java +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/PacketCodec.java @@ -5,10 +5,9 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; -import javax.annotation.concurrent.Immutable; import java.util.EnumMap; +import java.util.function.Supplier; -@Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class PacketCodec { @@ -20,6 +19,9 @@ public class PacketCodec { private final EnumMap stateProtocols; + @Getter + private final Supplier helperFactory; + public PacketStateCodec getCodec(ProtocolState protocolState) { return this.stateProtocols.get(protocolState); } @@ -34,6 +36,7 @@ public class PacketCodec { builder.protocolVersion = this.protocolVersion; builder.stateProtocols = this.stateProtocols; builder.minecraftVersion = this.minecraftVersion; + builder.helperFactory = this.helperFactory; return builder; } @@ -42,6 +45,7 @@ public class PacketCodec { private int protocolVersion = -1; private String minecraftVersion = null; private EnumMap stateProtocols = new EnumMap<>(ProtocolState.class); + private Supplier helperFactory; public Builder protocolVersion(int protocolVersion) { this.protocolVersion = protocolVersion; @@ -58,8 +62,13 @@ public class PacketCodec { return this; } + public Builder helper(Supplier helperFactory) { + this.helperFactory = helperFactory; + return this; + } + public PacketCodec build() { - return new PacketCodec(this.protocolVersion, this.minecraftVersion, this.stateProtocols); + return new PacketCodec(this.protocolVersion, this.minecraftVersion, this.stateProtocols, this.helperFactory); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/PacketFactory.java b/src/main/java/com/github/steveice10/mc/protocol/codec/PacketFactory.java new file mode 100644 index 00000000..209a0dc3 --- /dev/null +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/PacketFactory.java @@ -0,0 +1,26 @@ +package com.github.steveice10.mc.protocol.codec; + +import com.github.steveice10.packetlib.codec.PacketCodecHelper; +import com.github.steveice10.packetlib.codec.PacketSerializer; +import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; + +import java.io.IOException; + +/** + * Factory for constructing {@link Packet}s. + * + * @param the packet type + */ +@FunctionalInterface +public interface PacketFactory { + + /** + * Constructs a new {@link Packet}. + * + * @param buf the input buffer + * @param codecHelper the codec helper + * @return a new packet from the input + */ + T construct(ByteBuf buf, H codecHelper) throws IOException; +} diff --git a/src/main/java/com/github/steveice10/mc/protocol/codec/PacketStateCodec.java b/src/main/java/com/github/steveice10/mc/protocol/codec/PacketStateCodec.java index b6698d43..2b76d941 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/codec/PacketStateCodec.java +++ b/src/main/java/com/github/steveice10/mc/protocol/codec/PacketStateCodec.java @@ -3,9 +3,8 @@ package com.github.steveice10.mc.protocol.codec; import com.github.steveice10.mc.protocol.MinecraftConstants; import com.github.steveice10.packetlib.Server; import com.github.steveice10.packetlib.Session; -import com.github.steveice10.packetlib.packet.Packet; -import com.github.steveice10.packetlib.packet.PacketDefinition; -import com.github.steveice10.packetlib.packet.PacketFactory; +import com.github.steveice10.packetlib.codec.PacketCodecHelper; +import com.github.steveice10.packetlib.codec.PacketDefinition; import com.github.steveice10.packetlib.packet.PacketHeader; import com.github.steveice10.packetlib.packet.PacketProtocol; @@ -28,6 +27,11 @@ public class PacketStateCodec extends PacketProtocol { return MinecraftConstants.PACKET_HEADER; } + @Override + public PacketCodecHelper createHelper() { + throw new UnsupportedOperationException("Not supported!"); + } + @Override public void newClientSession(Session session) { throw new UnsupportedOperationException("Not supported!"); @@ -39,26 +43,26 @@ public class PacketStateCodec extends PacketProtocol { } public static class Builder { - private final Map> clientboundPackets = new HashMap<>(); - private final Map> serverboundPackets = new HashMap<>(); + private final Map> clientboundPackets = new HashMap<>(); + private final Map> serverboundPackets = new HashMap<>(); - public Builder registerClientboundPacket(int id, Class packetClass, PacketFactory factory) { - this.clientboundPackets.put(id, new PacketDefinition<>(id, packetClass, factory)); + public Builder registerClientboundPacket(int id, Class packetClass, PacketFactory factory) { + this.clientboundPackets.put(id, new PacketDefinition<>(id, packetClass, new MinecraftPacketSerializer<>(factory))); return this; } - public Builder registerServerboundPacket(int id, Class packetClass, PacketFactory factory) { - this.serverboundPackets.put(id, new PacketDefinition<>(id, packetClass, factory)); + public Builder registerServerboundPacket(int id, Class packetClass, PacketFactory factory) { + this.serverboundPackets.put(id, new PacketDefinition<>(id, packetClass, new MinecraftPacketSerializer<>(factory))); return this; } public PacketStateCodec build() { PacketStateCodec codec = new PacketStateCodec(); - for (Map.Entry> entry : this.clientboundPackets.entrySet()) { + for (Map.Entry> entry : this.clientboundPackets.entrySet()) { codec.registerClientbound(entry.getValue()); } - for (Map.Entry> entry : this.serverboundPackets.entrySet()) { + for (Map.Entry> entry : this.serverboundPackets.entrySet()) { codec.registerServerbound(entry.getValue()); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java index ddd58e80..be1275a5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/MessageType.java @@ -10,5 +10,9 @@ public enum MessageType { EMOTE_COMMAND, TELLRAW_COMMAND; - public static final MessageType[] VALUES = values(); + private static final MessageType[] VALUES = values(); + + public static MessageType from(int id) { + return VALUES[id]; + } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/NBT.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/NBT.java deleted file mode 100644 index bb11c128..00000000 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/NBT.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.github.steveice10.mc.protocol.data.game; - -import com.github.steveice10.opennbt.NBTIO; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class NBT { - private NBT() { - } - - public static CompoundTag read(NetInput in) throws IOException { - return (CompoundTag) NBTIO.readTag(new InputStream() { - @Override - public int read() throws IOException { - return in.readUnsignedByte(); - } - }); - } - - public static void write(NetOutput out, CompoundTag tag) throws IOException { - NBTIO.writeTag(new OutputStream() { - @Override - public void write(int b) throws IOException { - out.writeByte(b); - } - }, tag); - } -} diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/ChunkSection.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/ChunkSection.java index 1bafe4cc..1d7e3e4d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/ChunkSection.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/ChunkSection.java @@ -1,12 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.chunk; -import com.github.steveice10.mc.protocol.data.game.chunk.palette.PaletteType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import lombok.*; -import java.io.IOException; - @Data @Setter(AccessLevel.NONE) @AllArgsConstructor @@ -24,20 +19,6 @@ public class ChunkSection { this(0, DataPalette.createForChunk(), DataPalette.createForBiome(4)); } - public static ChunkSection read(NetInput in, int globalBiomePaletteBits) throws IOException { - int blockCount = in.readShort(); - - DataPalette chunkPalette = DataPalette.read(in, PaletteType.CHUNK, DataPalette.GLOBAL_PALETTE_BITS_PER_ENTRY); - DataPalette biomePalette = DataPalette.read(in, PaletteType.BIOME, globalBiomePaletteBits); - return new ChunkSection(blockCount, chunkPalette, biomePalette); - } - - public static void write(NetOutput out, ChunkSection section, int globalBiomePaletteBits) throws IOException { - out.writeShort(section.blockCount); - DataPalette.write(out, section.chunkData); - DataPalette.write(out, section.biomeData); - } - public int getBlock(int x, int y, int z) { return this.chunkData.get(x, y, z); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/DataPalette.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/DataPalette.java index 871e5b1b..f9e000eb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/DataPalette.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/DataPalette.java @@ -1,12 +1,8 @@ package com.github.steveice10.mc.protocol.data.game.chunk; import com.github.steveice10.mc.protocol.data.game.chunk.palette.*; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import lombok.*; -import java.io.IOException; - @Getter @Setter @AllArgsConstructor @@ -37,44 +33,6 @@ public class DataPalette { new BitStorage(paletteType.getMinBitsPerEntry(), paletteType.getStorageSize()), paletteType, globalPaletteBits); } - public static DataPalette read(NetInput in, PaletteType paletteType, int globalPaletteBits) throws IOException { - int bitsPerEntry = in.readByte(); - Palette palette = readPalette(paletteType, bitsPerEntry, in); - BitStorage storage; - if (!(palette instanceof SingletonPalette)) { - int length = in.readVarInt(); - storage = new BitStorage(bitsPerEntry, paletteType.getStorageSize(), in.readLongs(length)); - } else { - in.readVarInt(); - storage = null; - } - - return new DataPalette(palette, storage, paletteType, globalPaletteBits); - } - - public static void write(NetOutput out, DataPalette palette) throws IOException { - if (palette.palette instanceof SingletonPalette) { - out.writeByte(0); // Bits per entry - out.writeVarInt(palette.palette.idToState(0)); - out.writeVarInt(0); // Data length - return; - } - - out.writeByte(palette.storage.getBitsPerEntry()); - - if (!(palette.palette instanceof GlobalPalette)) { - int paletteLength = palette.palette.size(); - out.writeVarInt(paletteLength); - for (int i = 0; i < paletteLength; i++) { - out.writeVarInt(palette.palette.idToState(i)); - } - } - - long[] data = palette.storage.getData(); - out.writeVarInt(data.length); - out.writeLongs(data); - } - public int get(int x, int y, int z) { if (storage != null) { int id = this.storage.get(index(x, y, z)); @@ -106,20 +64,6 @@ public class DataPalette { } } - private static Palette readPalette(PaletteType paletteType, int bitsPerEntry, NetInput in) throws IOException { - if (bitsPerEntry > paletteType.getMaxBitsPerEntry()) { - return new GlobalPalette(); - } - if (bitsPerEntry == 0) { - return new SingletonPalette(in); - } - if (bitsPerEntry <= paletteType.getMinBitsPerEntry()) { - return new ListPalette(bitsPerEntry, in); - } else { - return new MapPalette(bitsPerEntry, in); - } - } - private int sanitizeBitsPerEntry(int bitsPerEntry) { if (bitsPerEntry <= this.paletteType.getMaxBitsPerEntry()) { return Math.max(this.paletteType.getMinBitsPerEntry(), bitsPerEntry); diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/NibbleArray3d.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/NibbleArray3d.java index 44937529..13f8405f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/NibbleArray3d.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/NibbleArray3d.java @@ -1,13 +1,9 @@ package com.github.steveice10.mc.protocol.data.game.chunk; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; -import java.io.IOException; - @Data @AllArgsConstructor public class NibbleArray3d { @@ -17,14 +13,6 @@ public class NibbleArray3d { this(new byte[size >> 1]); } - public NibbleArray3d(@NonNull NetInput in, int size) throws IOException { - this(in.readBytes(size)); - } - - public void write(@NonNull NetOutput out) throws IOException { - out.writeBytes(this.data); - } - public int get(int x, int y, int z) { int key = y << 8 | z << 4 | x; int index = key >> 1; diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/ListPalette.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/ListPalette.java index 9e622205..16f30dad 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/ListPalette.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/ListPalette.java @@ -1,13 +1,16 @@ package com.github.steveice10.mc.protocol.data.game.chunk.palette; -import com.github.steveice10.packetlib.io.NetInput; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; +import lombok.Getter; import java.io.IOException; /** * A palette backed by a List. */ +@Getter @EqualsAndHashCode public class ListPalette implements Palette { private final int maxId; @@ -21,13 +24,14 @@ public class ListPalette implements Palette { this.data = new int[this.maxId + 1]; } - public ListPalette(int bitsPerEntry, NetInput in) throws IOException { + public ListPalette(int bitsPerEntry, ByteBuf in, MinecraftCodecHelper helper) throws IOException { this(bitsPerEntry); - int paletteLength = in.readVarInt(); + int paletteLength = helper.readVarInt(in); for (int i = 0; i < paletteLength; i++) { - this.data[i] = in.readVarInt(); + this.data[i] = helper.readVarInt(in); } + this.nextId = paletteLength; } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/MapPalette.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/MapPalette.java index 2d09a3b2..cdc4c374 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/MapPalette.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/MapPalette.java @@ -1,6 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.chunk.palette; -import com.github.steveice10.packetlib.io.NetInput; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import io.netty.buffer.ByteBuf; import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectMap; import lombok.EqualsAndHashCode; @@ -24,12 +25,12 @@ public class MapPalette implements Palette { this.idToState = new int[this.maxId + 1]; } - public MapPalette(int bitsPerEntry, NetInput in) throws IOException { + public MapPalette(int bitsPerEntry, ByteBuf in, MinecraftCodecHelper helper) throws IOException { this(bitsPerEntry); - int paletteLength = in.readVarInt(); + int paletteLength = helper.readVarInt(in); for (int i = 0; i < paletteLength; i++) { - int state = in.readVarInt(); + int state = helper.readVarInt(in); this.idToState[i] = state; this.stateToId.putIfAbsent(state, i); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/SingletonPalette.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/SingletonPalette.java index d435cc70..6743ea51 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/SingletonPalette.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/palette/SingletonPalette.java @@ -1,10 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.chunk.palette; -import com.github.steveice10.packetlib.io.NetInput; import lombok.EqualsAndHashCode; -import java.io.IOException; - /** * A palette containing one state. */ @@ -16,10 +13,6 @@ public class SingletonPalette implements Palette { this.state = state; } - public SingletonPalette(NetInput in) throws IOException { - this.state = in.readVarInt(); - } - @Override public int size() { return 1; diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/Effect.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/Effect.java index 3f474f46..a084773b 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/Effect.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/Effect.java @@ -37,11 +37,7 @@ public enum Effect { public static final Effect[] VALUES = values(); - public static Effect fromNetworkId(int id) { - return VALUES[id - 1]; - } - - public static int toNetworkId(Effect effect) { - return effect.ordinal() + 1; + public static Effect from(int id) { + return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/EntityEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/EntityEvent.java index dd9fe0e5..24323a83 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/EntityEvent.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/EntityEvent.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.entity; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum EntityEvent { TIPPED_ARROW_EMIT_PARTICLES, RABBIT_JUMP_OR_MINECART_SPAWNER_DELAY_RESET, @@ -71,11 +67,7 @@ public enum EntityEvent { private static final EntityEvent[] VALUES = values(); - public static EntityEvent read(NetInput in) throws IOException { - return VALUES[in.readByte()]; - } - - public static EntityEvent fromId(int id) { + public static EntityEvent from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierOperation.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierOperation.java index ad85adb7..63c3d02d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierOperation.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierOperation.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.entity.attribute; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum ModifierOperation { ADD, ADD_MULTIPLIED, @@ -11,7 +7,7 @@ public enum ModifierOperation { private static final ModifierOperation[] VALUES = values(); - public static ModifierOperation read(NetInput in) throws IOException { - return VALUES[in.readByte()]; + public static ModifierOperation from(int id) { + return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/BooleanMetadataType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/BooleanMetadataType.java index a277d361..47f40636 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/BooleanMetadataType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/BooleanMetadataType.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.BooleanEntityMetadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import java.io.IOException; @@ -20,32 +20,32 @@ public class BooleanMetadataType extends MetadataType { } @Override - public EntityMetadata readMetadata(NetInput input, int id) throws IOException { + public EntityMetadata readMetadata(MinecraftCodecHelper helper, ByteBuf input, int id) throws IOException { return this.primitiveFactory.createPrimitive(id, this, this.primitiveReader.readPrimitive(input)); } - public void writeMetadataPrimitive(NetOutput output, boolean value) throws IOException { + public void writeMetadataPrimitive(ByteBuf output, boolean value) throws IOException { this.primitiveWriter.writePrimitive(output, value); } @FunctionalInterface - public interface BooleanReader extends Reader { - boolean readPrimitive(NetInput input) throws IOException; + public interface BooleanReader extends BasicReader { + boolean readPrimitive(ByteBuf input) throws IOException; @Deprecated @Override - default Boolean read(NetInput input) throws IOException { + default Boolean read(ByteBuf input) throws IOException { return this.readPrimitive(input); } } @FunctionalInterface - public interface BooleanWriter extends Writer { - void writePrimitive(NetOutput output, boolean value) throws IOException; + public interface BooleanWriter extends BasicWriter { + void writePrimitive(ByteBuf output, boolean value) throws IOException; @Deprecated @Override - default void write(NetOutput output, Boolean value) throws IOException { + default void write(ByteBuf output, Boolean value) throws IOException { this.writePrimitive(output, value); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ByteMetadataType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ByteMetadataType.java index 4cda6a36..5e4139cd 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ByteMetadataType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ByteMetadataType.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.ByteEntityMetadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import java.io.IOException; @@ -20,32 +20,32 @@ public class ByteMetadataType extends MetadataType { } @Override - public EntityMetadata readMetadata(NetInput input, int id) throws IOException { + public EntityMetadata readMetadata(MinecraftCodecHelper helper, ByteBuf input, int id) throws IOException { return this.primitiveFactory.createPrimitive(id, this, this.primitiveReader.readPrimitive(input)); } - public void writeMetadataPrimitive(NetOutput output, byte value) throws IOException { + public void writeMetadataPrimitive(ByteBuf output, byte value) throws IOException { this.primitiveWriter.writePrimitive(output, value); } @FunctionalInterface - public interface ByteReader extends Reader { - byte readPrimitive(NetInput input) throws IOException; + public interface ByteReader extends BasicReader { + byte readPrimitive(ByteBuf input) throws IOException; @Deprecated @Override - default Byte read(NetInput input) throws IOException { + default Byte read(ByteBuf input) throws IOException { return this.readPrimitive(input); } } @FunctionalInterface - public interface ByteWriter extends Writer { - void writePrimitive(NetOutput output, byte value) throws IOException; + public interface ByteWriter extends BasicWriter { + void writePrimitive(ByteBuf output, byte value) throws IOException; @Deprecated @Override - default void write(NetOutput output, Byte value) throws IOException { + default void write(ByteBuf output, Byte value) throws IOException { this.writePrimitive(output, value); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java index cd23294f..7758c5f0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java @@ -1,14 +1,14 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.ObjectEntityMetadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import java.io.IOException; -import java.util.*; +import java.util.Objects; @Data @AllArgsConstructor @@ -21,33 +21,12 @@ public abstract class EntityMetadata> { */ public abstract V getValue(); - public static EntityMetadata[] read(NetInput in) throws IOException { - List> ret = new ArrayList<>(); - int id; - while ((id = in.readUnsignedByte()) != 255) { - MetadataType type = MetadataType.read(in); - ret.add(type.readMetadata(in, id)); - } - - return ret.toArray(new EntityMetadata[0]); - } - /** * Overridden for primitive classes. This write method still checks for these primitives in the event * they are manually created using {@link ObjectEntityMetadata}. */ - protected void write(NetOutput out) throws IOException { - this.type.writeMetadata(out, this.getValue()); - } - - public static void write(NetOutput out, EntityMetadata[] metadata) throws IOException { - for (EntityMetadata meta : metadata) { - out.writeByte(meta.getId()); - MetadataType.write(out, meta.getType()); - meta.write(out); - } - - out.writeByte(255); + public void write(MinecraftCodecHelper helper, ByteBuf out) throws IOException { + this.type.writeMetadata(helper, out, this.getValue()); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/FloatMetadataType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/FloatMetadataType.java index c2699629..684ccd06 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/FloatMetadataType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/FloatMetadataType.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.FloatEntityMetadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import java.io.IOException; @@ -20,32 +20,32 @@ public class FloatMetadataType extends MetadataType { } @Override - public EntityMetadata readMetadata(NetInput input, int id) throws IOException { + public EntityMetadata readMetadata(MinecraftCodecHelper helper, ByteBuf input, int id) throws IOException { return this.primitiveFactory.createPrimitive(id, this, this.primitiveReader.readPrimitive(input)); } - public void writeMetadataPrimitive(NetOutput output, float value) throws IOException { + public void writeMetadataPrimitive(ByteBuf output, float value) throws IOException { this.primitiveWriter.writePrimitive(output, value); } @FunctionalInterface - public interface FloatReader extends Reader { - float readPrimitive(NetInput input) throws IOException; + public interface FloatReader extends BasicReader { + float readPrimitive(ByteBuf input) throws IOException; @Deprecated @Override - default Float read(NetInput input) throws IOException { + default Float read(ByteBuf input) throws IOException { return this.readPrimitive(input); } } @FunctionalInterface - public interface FloatWriter extends Writer { - void writePrimitive(NetOutput output, float value) throws IOException; + public interface FloatWriter extends BasicWriter { + void writePrimitive(ByteBuf output, float value) throws IOException; @Deprecated @Override - default void write(NetOutput output, Float value) throws IOException { + default void write(ByteBuf output, Float value) throws IOException { this.writePrimitive(output, value); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/GlobalPos.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/GlobalPos.java index 6511c1e6..32a8fa7f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/GlobalPos.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/GlobalPos.java @@ -1,12 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; -import com.github.steveice10.mc.protocol.data.game.Identifier; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import com.nukkitx.math.vector.Vector3i; -import java.io.IOException; - public class GlobalPos { private final String dimension; private final Vector3i position; @@ -35,15 +30,4 @@ public class GlobalPos { public int getZ() { return position.getZ(); } - - public static GlobalPos read(NetInput in) throws IOException { - String dimension = Identifier.formalize(in.readString()); - Vector3i pos = Position.read(in); - return new GlobalPos(dimension, pos); - } - - public static void write(NetOutput out, GlobalPos pos) throws IOException { - out.writeString(pos.getDimension()); - Position.write(out, pos.getPosition()); - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/IntMetadataType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/IntMetadataType.java index ab69cc13..da909e51 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/IntMetadataType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/IntMetadataType.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.IntEntityMetadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import java.io.IOException; @@ -20,33 +20,33 @@ public class IntMetadataType extends MetadataType { } @Override - public EntityMetadata readMetadata(NetInput input, int id) throws IOException { - return this.primitiveFactory.createPrimitive(id, this, this.primitiveReader.readPrimitive(input)); + public EntityMetadata readMetadata(MinecraftCodecHelper helper, ByteBuf input, int id) throws IOException { + return this.primitiveFactory.createPrimitive(id, this, this.primitiveReader.readPrimitive(helper, input)); } - public void writeMetadataPrimitive(NetOutput output, int value) throws IOException { - this.primitiveWriter.writePrimitive(output, value); + public void writeMetadataPrimitive(MinecraftCodecHelper helper, ByteBuf output, int value) throws IOException { + this.primitiveWriter.writePrimitive(helper, output, value); } @FunctionalInterface - public interface IntReader extends Reader { - int readPrimitive(NetInput input) throws IOException; + public interface IntReader extends HelperReader { + int readPrimitive(MinecraftCodecHelper helper, ByteBuf input) throws IOException; @Deprecated @Override - default Integer read(NetInput input) throws IOException { - return this.readPrimitive(input); + default Integer read(MinecraftCodecHelper helper, ByteBuf input) throws IOException { + return this.readPrimitive(helper, input); } } @FunctionalInterface - public interface IntWriter extends Writer { - void writePrimitive(NetOutput output, int value) throws IOException; + public interface IntWriter extends HelperWriter { + void writePrimitive(MinecraftCodecHelper helper, ByteBuf output, int value) throws IOException; @Deprecated @Override - default void write(NetOutput output, Integer value) throws IOException { - this.writePrimitive(output, value); + default void write(MinecraftCodecHelper helper, ByteBuf output, Integer value) throws IOException { + this.writePrimitive(helper, output, value); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ItemStack.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ItemStack.java index 9d11e08c..948420b0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ItemStack.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/ItemStack.java @@ -1,14 +1,9 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; -import com.github.steveice10.mc.protocol.data.game.NBT; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import lombok.AllArgsConstructor; import lombok.Data; -import java.io.IOException; - @Data @AllArgsConstructor public class ItemStack { @@ -23,23 +18,4 @@ public class ItemStack { public ItemStack(int id, int amount) { this(id, amount, null); } - - public static ItemStack read(NetInput in) throws IOException { - boolean present = in.readBoolean(); - if (!present) { - return null; - } - - int item = in.readVarInt(); - return new ItemStack(item, in.readByte(), NBT.read(in)); - } - - public static void write(NetOutput out, ItemStack item) throws IOException { - out.writeBoolean(item != null); - if (item != null) { - out.writeVarInt(item.getId()); - out.writeByte(item.getAmount()); - NBT.write(out, item.getNbt()); - } - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/MetadataType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/MetadataType.java index 09a3f154..c3b20aec 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/MetadataType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/MetadataType.java @@ -1,19 +1,15 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; -import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.mc.protocol.data.game.NBT; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.BooleanEntityMetadata; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.ByteEntityMetadata; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.FloatEntityMetadata; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.IntEntityMetadata; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.ObjectEntityMetadata; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.*; import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; import com.github.steveice10.mc.protocol.data.game.entity.type.PaintingType; import com.github.steveice10.mc.protocol.data.game.level.particle.Particle; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; +import lombok.Getter; import net.kyori.adventure.text.Component; import java.io.IOException; @@ -22,43 +18,40 @@ import java.util.List; import java.util.Optional; import java.util.UUID; +@Getter public class MetadataType { private static final List> VALUES = new ArrayList<>(); - public static final ByteMetadataType BYTE = new ByteMetadataType(NetInput::readByte, NetOutput::writeByte, ByteEntityMetadata::new); - public static final IntMetadataType INT = new IntMetadataType(NetInput::readVarInt, NetOutput::writeVarInt, IntEntityMetadata::new); - public static final FloatMetadataType FLOAT = new FloatMetadataType(NetInput::readFloat, NetOutput::writeFloat, FloatEntityMetadata::new); - public static final MetadataType STRING = new MetadataType<>(NetInput::readString, NetOutput::writeString, ObjectEntityMetadata::new); - public static final MetadataType CHAT = new MetadataType<>(in -> DefaultComponentSerializer.get().deserialize(in.readString()), - (out, value) -> out.writeString(DefaultComponentSerializer.get().serialize(value)), - ObjectEntityMetadata::new); - public static final MetadataType> OPTIONAL_CHAT = new MetadataType<>(optionalReader(in -> DefaultComponentSerializer.get().deserialize(in.readString())), - optionalWriter((out, value) -> out.writeString(DefaultComponentSerializer.get().serialize(value))), - ObjectEntityMetadata::new); - public static final MetadataType ITEM = new MetadataType<>(ItemStack::read, ItemStack::write, ObjectEntityMetadata::new); - public static final BooleanMetadataType BOOLEAN = new BooleanMetadataType(NetInput::readBoolean, NetOutput::writeBoolean, BooleanEntityMetadata::new); - public static final MetadataType ROTATION = new MetadataType<>(Rotation::read, Rotation::write, ObjectEntityMetadata::new); - public static final MetadataType POSITION = new MetadataType<>(Position::read, Position::write, ObjectEntityMetadata::new); - public static final MetadataType> OPTIONAL_POSITION = new MetadataType<>(optionalReader(Position::read), optionalWriter(Position::write), ObjectEntityMetadata::new); - public static final MetadataType DIRECTION = new MetadataType<>(in -> in.readEnum(Direction.VALUES), NetOutput::writeEnum, ObjectEntityMetadata::new); - public static final MetadataType> OPTIONAL_UUID = new MetadataType<>(optionalReader(NetInput::readUUID), optionalWriter(NetOutput::writeUUID), ObjectEntityMetadata::new); - public static final IntMetadataType BLOCK_STATE = new IntMetadataType(NetInput::readVarInt, NetOutput::writeVarInt, IntEntityMetadata::new); - public static final MetadataType NBT_TAG = new MetadataType<>(NBT::read, NBT::write, ObjectEntityMetadata::new); - public static final MetadataType PARTICLE = new MetadataType<>(Particle::read, Particle::write, ObjectEntityMetadata::new); - public static final MetadataType VILLAGER_DATA = new MetadataType<>(VillagerData::read, VillagerData::write, ObjectEntityMetadata::new); + public static final ByteMetadataType BYTE = new ByteMetadataType(ByteBuf::readByte, ByteBuf::writeByte, ByteEntityMetadata::new); + public static final IntMetadataType INT = new IntMetadataType(MinecraftCodecHelper::readVarInt, MinecraftCodecHelper::writeVarInt, IntEntityMetadata::new); + public static final FloatMetadataType FLOAT = new FloatMetadataType(ByteBuf::readFloat, ByteBuf::writeFloat, FloatEntityMetadata::new); + public static final MetadataType STRING = new MetadataType<>(MinecraftCodecHelper::readString, MinecraftCodecHelper::writeString, ObjectEntityMetadata::new); + public static final MetadataType CHAT = new MetadataType<>(MinecraftCodecHelper::readComponent, MinecraftCodecHelper::writeComponent, ObjectEntityMetadata::new); + public static final MetadataType> OPTIONAL_CHAT = new MetadataType<>(optionalReader(MinecraftCodecHelper::readComponent), optionalWriter(MinecraftCodecHelper::writeComponent), ObjectEntityMetadata::new); + public static final MetadataType ITEM = new MetadataType<>(MinecraftCodecHelper::readItemStack, MinecraftCodecHelper::writeItemStack, ObjectEntityMetadata::new); + public static final BooleanMetadataType BOOLEAN = new BooleanMetadataType(ByteBuf::readBoolean, ByteBuf::writeBoolean, BooleanEntityMetadata::new); + public static final MetadataType ROTATION = new MetadataType<>(MinecraftCodecHelper::readRotation, MinecraftCodecHelper::writeRotation, ObjectEntityMetadata::new); + public static final MetadataType POSITION = new MetadataType<>(MinecraftCodecHelper::readPosition, MinecraftCodecHelper::writePosition, ObjectEntityMetadata::new); + public static final MetadataType> OPTIONAL_POSITION = new MetadataType<>(optionalReader(MinecraftCodecHelper::readPosition), optionalWriter(MinecraftCodecHelper::writePosition), ObjectEntityMetadata::new); + public static final MetadataType DIRECTION = new MetadataType<>(MinecraftCodecHelper::readDirection, MinecraftCodecHelper::writeDirection, ObjectEntityMetadata::new); + public static final MetadataType> OPTIONAL_UUID = new MetadataType<>(optionalReader(MinecraftCodecHelper::readUUID), optionalWriter(MinecraftCodecHelper::writeUUID), ObjectEntityMetadata::new); + public static final IntMetadataType BLOCK_STATE = new IntMetadataType(MinecraftCodecHelper::readVarInt, MinecraftCodecHelper::writeVarInt, IntEntityMetadata::new); + public static final MetadataType NBT_TAG = new MetadataType<>(MinecraftCodecHelper::readTag, MinecraftCodecHelper::writeTag, ObjectEntityMetadata::new); + public static final MetadataType PARTICLE = new MetadataType<>(MinecraftCodecHelper::readParticle, MinecraftCodecHelper::writeParticle, ObjectEntityMetadata::new); + public static final MetadataType VILLAGER_DATA = new MetadataType<>(MinecraftCodecHelper::readVillagerData, MinecraftCodecHelper::writeVillagerData, ObjectEntityMetadata::new); public static final OptionalIntMetadataType OPTIONAL_VARINT = new OptionalIntMetadataType(ObjectEntityMetadata::new); - public static final MetadataType POSE = new MetadataType<>(in -> in.readEnum(Pose.VALUES), NetOutput::writeEnum, ObjectEntityMetadata::new); - public static final IntMetadataType CAT_VARIANT = new IntMetadataType(NetInput::readVarInt, NetOutput::writeVarInt, IntEntityMetadata::new); - public static final IntMetadataType FROG_VARIANT = new IntMetadataType(NetInput::readVarInt, NetOutput::writeVarInt, IntEntityMetadata::new); - public static final MetadataType> OPTIONAL_GLOBAL_POS = new MetadataType<>(optionalReader(GlobalPos::read), optionalWriter(GlobalPos::write), ObjectEntityMetadata::new); - public static final MetadataType PAINTING_VARIANT = new MetadataType<>(PaintingType::read, NetOutput::writeEnum, ObjectEntityMetadata::new); + public static final MetadataType POSE = new MetadataType<>(MinecraftCodecHelper::readPose, MinecraftCodecHelper::writePose, ObjectEntityMetadata::new); + public static final IntMetadataType CAT_VARIANT = new IntMetadataType(MinecraftCodecHelper::readVarInt, MinecraftCodecHelper::writeVarInt, IntEntityMetadata::new); + public static final IntMetadataType FROG_VARIANT = new IntMetadataType(MinecraftCodecHelper::readVarInt, MinecraftCodecHelper::writeVarInt, IntEntityMetadata::new); + public static final MetadataType> OPTIONAL_GLOBAL_POS = new MetadataType<>(optionalReader(MinecraftCodecHelper::readGlobalPos), optionalWriter(MinecraftCodecHelper::writeGlobalPos), ObjectEntityMetadata::new); + public static final MetadataType PAINTING_VARIANT = new MetadataType<>(MinecraftCodecHelper::readPaintingType, MinecraftCodecHelper::writePaintingType, ObjectEntityMetadata::new); protected final int id; protected final Reader reader; protected final Writer writer; protected final EntityMetadataFactory metadataFactory; - protected MetadataType(Reader reader, Writer writer, EntityMetadataFactory metadataFactory) { + protected MetadataType(BasicReader reader, BasicWriter writer, EntityMetadataFactory metadataFactory) { this.id = VALUES.size(); this.reader = reader; this.writer = writer; @@ -67,22 +60,69 @@ public class MetadataType { VALUES.add(this); } - public EntityMetadata> readMetadata(NetInput input, int id) throws IOException { - return this.metadataFactory.create(id, this, this.reader.read(input)); + protected MetadataType(HelperReader reader, HelperWriter writer, EntityMetadataFactory metadataFactory) { + this.id = VALUES.size(); + this.reader = reader; + this.writer = writer; + this.metadataFactory = metadataFactory; + + VALUES.add(this); } - public void writeMetadata(NetOutput output, T value) throws IOException { - this.writer.write(output, value); + public EntityMetadata> readMetadata(MinecraftCodecHelper helper, ByteBuf input, int id) throws IOException { + return this.metadataFactory.create(id, this, this.reader.read(helper, input)); + } + + public void writeMetadata(MinecraftCodecHelper helper, ByteBuf output, T value) throws IOException { + this.writer.write(helper, output, value); } - @FunctionalInterface public interface Reader { - V read(NetInput input) throws IOException; + V read(ByteBuf input) throws IOException; + + V read(MinecraftCodecHelper helper, ByteBuf input) throws IOException; + } + + public interface Writer { + void write(ByteBuf output, V value) throws IOException; + + void write(MinecraftCodecHelper helper, ByteBuf output, V value) throws IOException; } @FunctionalInterface - public interface Writer { - void write(NetOutput output, V value) throws IOException; + public interface BasicReader extends Reader { + V read(ByteBuf input) throws IOException; + + default V read(MinecraftCodecHelper helper, ByteBuf input) throws IOException { + return this.read(input); + } + } + + @FunctionalInterface + public interface BasicWriter extends Writer { + void write(ByteBuf output, V value) throws IOException; + + default void write(MinecraftCodecHelper helper, ByteBuf output, V value) throws IOException { + this.write(output, value); + } + } + + @FunctionalInterface + public interface HelperReader extends Reader { + default V read(ByteBuf input) throws IOException { + throw new UnsupportedOperationException("This reader needs a codec helper!"); + } + + V read(MinecraftCodecHelper helper, ByteBuf input) throws IOException; + } + + @FunctionalInterface + public interface HelperWriter extends Writer { + default void write(ByteBuf output, V value) throws IOException { + throw new UnsupportedOperationException("This writer needs a codec helper!"); + } + + void write(MinecraftCodecHelper helper, ByteBuf output, V value) throws IOException; } @FunctionalInterface @@ -90,7 +130,7 @@ public class MetadataType { EntityMetadata> create(int id, MetadataType type, V value); } - private static Reader> optionalReader(Reader reader) { + private static BasicReader> optionalReader(BasicReader reader) { return input -> { if (!input.readBoolean()) { return Optional.empty(); @@ -100,7 +140,17 @@ public class MetadataType { }; } - private static Writer> optionalWriter(Writer writer) { + private static HelperReader> optionalReader(HelperReader reader) { + return (helper, input) -> { + if (!input.readBoolean()) { + return Optional.empty(); + } + + return Optional.of(reader.read(helper, input)); + }; + } + + private static BasicWriter> optionalWriter(BasicWriter writer) { return (ouput, value) -> { ouput.writeBoolean(value.isPresent()); if (value.isPresent()) { @@ -109,16 +159,29 @@ public class MetadataType { }; } - public static MetadataType read(NetInput in) throws IOException { - int id = in.readVarInt(); + private static HelperWriter> optionalWriter(HelperWriter writer) { + return (helper, ouput, value) -> { + ouput.writeBoolean(value.isPresent()); + if (value.isPresent()) { + writer.write(helper, ouput, value.get()); + } + }; + } + + public static MetadataType read(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + int id = helper.readVarInt(in); if (id >= VALUES.size()) { throw new IllegalArgumentException("Received id " + id + " for MetadataType when the maximum was " + VALUES.size() + "!"); } return VALUES.get(id); } - - public static void write(NetOutput out, MetadataType type) throws IOException { - out.writeVarInt(type.id); + + public static MetadataType from(int id) { + return VALUES.get(id); + } + + public static int size() { + return VALUES.size(); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/OptionalIntMetadataType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/OptionalIntMetadataType.java index c1fb3178..1408baea 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/OptionalIntMetadataType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/OptionalIntMetadataType.java @@ -1,7 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import io.netty.buffer.ByteBuf; import java.io.IOException; import java.util.OptionalInt; @@ -11,12 +11,12 @@ public class OptionalIntMetadataType extends MetadataType { super(OptionalIntReader.INSTANCE, OptionalIntWriter.INSTANCE, metadataFactory); } - public static class OptionalIntReader implements Reader { + public static class OptionalIntReader implements HelperReader { protected static final OptionalIntReader INSTANCE = new OptionalIntReader(); @Override - public OptionalInt read(NetInput input) throws IOException { - int value = input.readVarInt(); + public OptionalInt read(MinecraftCodecHelper helper, ByteBuf input) throws IOException { + int value = helper.readVarInt(input); if (value == 0) { return OptionalInt.empty(); } @@ -25,15 +25,15 @@ public class OptionalIntMetadataType extends MetadataType { } } - public static class OptionalIntWriter implements Writer { + public static class OptionalIntWriter implements HelperWriter { protected static final OptionalIntWriter INSTANCE = new OptionalIntWriter(); @Override - public void write(NetOutput output, OptionalInt value) throws IOException { + public void write(MinecraftCodecHelper helper, ByteBuf output, OptionalInt value) throws IOException { if (value.isPresent()) { - output.writeVarInt(value.getAsInt() + 1); + helper.writeVarInt(output, value.getAsInt() + 1); } else { - output.writeVarInt(0); + helper.writeVarInt(output, 0); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Pose.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Pose.java index b37db747..ff2c168c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Pose.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Pose.java @@ -16,5 +16,9 @@ public enum Pose { EMERGING, DIGGING; - public static final Pose[] VALUES = values(); + private static final Pose[] VALUES = values(); + + public static Pose from(int id) { + return VALUES[id]; + } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Position.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Position.java deleted file mode 100644 index 565b9f90..00000000 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Position.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.github.steveice10.mc.protocol.data.game.entity.metadata; - -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.nukkitx.math.vector.Vector3i; - -import java.io.IOException; - -public final class Position { - private static final int POSITION_X_SIZE = 38; - private static final int POSITION_Y_SIZE = 12; - private static final int POSITION_Z_SIZE = 38; - private static final int POSITION_Y_SHIFT = 0xFFF; - private static final int POSITION_WRITE_SHIFT = 0x3FFFFFF; - - public static Vector3i read(NetInput in) throws IOException { - long val = in.readLong(); - - int x = (int) (val >> POSITION_X_SIZE); - int y = (int) (val << 52 >> 52); - int z = (int) (val << 26 >> POSITION_Z_SIZE); - - return Vector3i.from(x, y, z); - } - - public static void write(NetOutput out, Vector3i pos) throws IOException { - long x = pos.getX() & POSITION_WRITE_SHIFT; - long y = pos.getY() & POSITION_Y_SHIFT; - long z = pos.getZ() & POSITION_WRITE_SHIFT; - - out.writeLong(x << POSITION_X_SIZE | z << POSITION_Y_SIZE | y); - } - - private Position() { - } -} diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Rotation.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Rotation.java deleted file mode 100644 index 71c1ca94..00000000 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/Rotation.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.github.steveice10.mc.protocol.data.game.entity.metadata; - -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import lombok.AllArgsConstructor; -import lombok.Data; - -import java.io.IOException; - -@Data -@AllArgsConstructor -public class Rotation { - private final float pitch; - private final float yaw; - private final float roll; - - public static Rotation read(NetInput in) throws IOException { - return new Rotation(in.readFloat(), in.readFloat(), in.readFloat()); - } - - public static void write(NetOutput out, Rotation rot) throws IOException { - out.writeFloat(rot.getPitch()); - out.writeFloat(rot.getYaw()); - out.writeFloat(rot.getRoll()); - } -} diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/VillagerData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/VillagerData.java index e44d727c..50360f86 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/VillagerData.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/VillagerData.java @@ -1,26 +1,12 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import lombok.AllArgsConstructor; import lombok.Data; -import java.io.IOException; - @Data @AllArgsConstructor public class VillagerData { private final int type; private final int profession; private final int level; - - public static VillagerData read(NetInput in) throws IOException { - return new VillagerData(in.readVarInt(), in.readVarInt(), in.readVarInt()); - } - - public static void write(NetOutput out, VillagerData villagerData) throws IOException { - out.writeVarInt(villagerData.getType()); - out.writeVarInt(villagerData.getProfession()); - out.writeVarInt(villagerData.getLevel()); - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/BooleanEntityMetadata.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/BooleanEntityMetadata.java index 23681536..711403bf 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/BooleanEntityMetadata.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/BooleanEntityMetadata.java @@ -1,8 +1,9 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata.type; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.BooleanMetadataType; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import lombok.NonNull; import java.io.IOException; @@ -26,7 +27,7 @@ public class BooleanEntityMetadata extends EntityMetadata { } @Override - protected void write(NetOutput out) throws IOException { + public void write(MinecraftCodecHelper helper, ByteBuf out) throws IOException { this.type.writeMetadataPrimitive(out, this.value); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/FloatEntityMetadata.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/FloatEntityMetadata.java index 01f34f20..b29aefcb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/FloatEntityMetadata.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/type/FloatEntityMetadata.java @@ -1,8 +1,9 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata.type; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.FloatMetadataType; -import com.github.steveice10.packetlib.io.NetOutput; +import io.netty.buffer.ByteBuf; import lombok.NonNull; import java.io.IOException; @@ -26,7 +27,7 @@ public class FloatEntityMetadata extends EntityMetadata } @Override - protected void write(NetOutput out) throws IOException { - this.type.writeMetadataPrimitive(out, value); + public void write(MinecraftCodecHelper helper, ByteBuf out) throws IOException { + this.type.writeMetadataPrimitive(helper, out, value); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/object/Direction.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/object/Direction.java index 1ba4bb72..8f7c94a5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/object/Direction.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/object/Direction.java @@ -23,4 +23,8 @@ public enum Direction implements ObjectData { public static Direction getByHorizontalIndex(int index) { return HORIZONTAL_VALUES[index % HORIZONTAL_VALUES.length]; } + + public static Direction from(int id) { + return VALUES[id]; + } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/player/BlockBreakStage.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/player/BlockBreakStage.java index dad7b222..73661885 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/player/BlockBreakStage.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/player/BlockBreakStage.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.entity.player; -import com.github.steveice10.packetlib.io.NetOutput; - -import java.io.IOException; - public enum BlockBreakStage { STAGE_1, STAGE_2, @@ -27,12 +23,4 @@ public enum BlockBreakStage { STAGES = new BlockBreakStage[allValues.length - 1]; System.arraycopy(allValues, 0, STAGES, 0, 10); } - - public void write(NetOutput out) throws IOException { - if (this == RESET) { - out.writeByte(255); - } else { - out.writeByte(ordinal()); - } - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/EntityType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/EntityType.java index b3d51439..7b4e7121 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/EntityType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/EntityType.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.entity.type; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum EntityType { ALLAY, AREA_EFFECT_CLOUD, @@ -126,11 +122,7 @@ public enum EntityType { private static final EntityType[] VALUES = values(); - public static EntityType read(NetInput in) throws IOException { - return in.readEnum(VALUES); - } - - public static EntityType fromId(int id) { + public static EntityType from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/PaintingType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/PaintingType.java index 6ee5a9b7..e35d99e4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/PaintingType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/type/PaintingType.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.entity.type; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum PaintingType { KEBAB, AZTEC, @@ -38,11 +34,7 @@ public enum PaintingType { private static final PaintingType[] VALUES = values(); - public static PaintingType read(NetInput in) throws IOException { - return in.readEnum(VALUES); - } - - public static PaintingType fromId(int id) { + public static PaintingType from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/LightUpdateData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/LightUpdateData.java index 8c6d7ad5..707db20b 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/LightUpdateData.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/LightUpdateData.java @@ -1,7 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -22,61 +22,56 @@ public class LightUpdateData { private final @NonNull List blockUpdates; private final boolean trustEdges; - public static LightUpdateData read(NetInput in) throws IOException { - return new LightUpdateData(in); + public static LightUpdateData read(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + return new LightUpdateData(in, helper); } - private LightUpdateData(NetInput in) throws IOException { + private LightUpdateData(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.trustEdges = in.readBoolean(); - this.skyYMask = BitSet.valueOf(in.readLongs(in.readVarInt())); - this.blockYMask = BitSet.valueOf(in.readLongs(in.readVarInt())); - this.emptySkyYMask = BitSet.valueOf(in.readLongs(in.readVarInt())); - this.emptyBlockYMask = BitSet.valueOf(in.readLongs(in.readVarInt())); + this.skyYMask = BitSet.valueOf(helper.readLongArray(in)); + this.blockYMask = BitSet.valueOf(helper.readLongArray(in)); + this.emptySkyYMask = BitSet.valueOf(helper.readLongArray(in)); + this.emptyBlockYMask = BitSet.valueOf(helper.readLongArray(in)); - int skyUpdateSize = in.readVarInt(); + int skyUpdateSize = helper.readVarInt(in); skyUpdates = new ArrayList<>(skyUpdateSize); for (int i = 0; i < skyUpdateSize; i++) { - skyUpdates.add(in.readBytes(in.readVarInt())); + skyUpdates.add(helper.readByteArray(in)); } - int blockUpdateSize = in.readVarInt(); + int blockUpdateSize = helper.readVarInt(in); blockUpdates = new ArrayList<>(blockUpdateSize); for (int i = 0; i < blockUpdateSize; i++) { - blockUpdates.add(in.readBytes(in.readVarInt())); + blockUpdates.add(helper.readByteArray(in)); } } - public static void write(NetOutput out, LightUpdateData data) throws IOException { - data.write(out); + public static void write(ByteBuf out, MinecraftCodecHelper helper, LightUpdateData data) throws IOException { + data.write(out, helper); } - private void write(NetOutput out) throws IOException { + private void write(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.trustEdges); - writeBitSet(out, this.skyYMask); - writeBitSet(out, this.blockYMask); - writeBitSet(out, this.emptySkyYMask); - writeBitSet(out, this.emptyBlockYMask); + writeBitSet(out, helper, this.skyYMask); + writeBitSet(out, helper, this.blockYMask); + writeBitSet(out, helper, this.emptySkyYMask); + writeBitSet(out, helper, this.emptyBlockYMask); - out.writeVarInt(this.skyUpdates.size()); + helper.writeVarInt(out, this.skyUpdates.size()); for (byte[] array : this.skyUpdates) { - out.writeVarInt(array.length); - out.writeBytes(array); + helper.writeByteArray(out, array); } - out.writeVarInt(this.blockUpdates.size()); + helper.writeVarInt(out, this.blockUpdates.size()); for (byte[] array : this.blockUpdates) { - out.writeVarInt(array.length); - out.writeBytes(array); + helper.writeByteArray(out, array); } } - private void writeBitSet(NetOutput out, BitSet bitSet) throws IOException { + private void writeBitSet(ByteBuf out, MinecraftCodecHelper helper, BitSet bitSet) throws IOException { long[] array = bitSet.toLongArray(); - out.writeVarInt(array.length); - for (long content : array) { - out.writeLong(content); - } + helper.writeLongArray(out, array); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityInfo.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityInfo.java index d1481502..a05186e4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityInfo.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityInfo.java @@ -3,8 +3,7 @@ package com.github.steveice10.mc.protocol.data.game.level.block; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import lombok.AllArgsConstructor; import lombok.Data; - -import javax.annotation.Nullable; +import org.jetbrains.annotations.Nullable; @Data @AllArgsConstructor diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityType.java index 1ebbb4bc..bd020c32 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/block/BlockEntityType.java @@ -1,10 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.level.block; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; - -import java.io.IOException; - public enum BlockEntityType { FURNACE, CHEST, @@ -45,20 +40,11 @@ public enum BlockEntityType { private static final BlockEntityType[] VALUES = values(); - public static BlockEntityType read(NetInput in) throws IOException { - int id = in.readVarInt(); + public static BlockEntityType from(int id) { if (id >= 0 && id < VALUES.length) { return VALUES[id]; } else { return null; } } - - public static void write(NetOutput out, BlockEntityType type) throws IOException { - if (type == null) { - out.writeVarInt(-1); - } else { - out.writeVarInt(type.ordinal()); - } - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/event/LevelEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/event/LevelEvent.java index f501d3b1..f60e211a 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/event/LevelEvent.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/event/LevelEvent.java @@ -1,13 +1,8 @@ package com.github.steveice10.mc.protocol.data.game.level.event; -import com.github.steveice10.packetlib.io.NetInput; import lombok.AllArgsConstructor; import lombok.Getter; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - @AllArgsConstructor public enum LevelEvent { BLOCK_DISPENSER_DISPENSE(1000), @@ -89,22 +84,4 @@ public enum LevelEvent { @Getter private final int id; - - private static final LevelEvent[] VALUES = values(); - private static final Map LEVEL_EVENT_MAP = new HashMap<>(); - - public static LevelEvent read(NetInput in) throws IOException { - int levelEventId = in.readInt(); - LevelEvent levelEvent = LEVEL_EVENT_MAP.get(levelEventId); - if (levelEvent == null) { - throw new IllegalStateException("Unknown level event with id: " + levelEventId); - } - return levelEvent; - } - - static { - for (LevelEvent levelEvent : VALUES) { - LEVEL_EVENT_MAP.put(levelEvent.id, levelEvent); - } - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/Particle.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/Particle.java index f42f20d7..22f8675f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/Particle.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/Particle.java @@ -1,26 +1,12 @@ package com.github.steveice10.mc.protocol.data.game.level.particle; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; -import java.io.IOException; - @Data @AllArgsConstructor public class Particle { private final @NonNull ParticleType type; private final ParticleData data; - - public static Particle read(NetInput in) throws IOException { - ParticleType particleType = ParticleType.read(in); - return new Particle(particleType, ParticleData.read(in, particleType)); - } - - public static void write(NetOutput out, Particle particle) throws IOException { - out.writeEnum(particle.getType()); - ParticleData.write(out, particle.getType(), particle.getData()); - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleData.java index 51855fd3..a0519d41 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleData.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleData.java @@ -1,85 +1,4 @@ package com.github.steveice10.mc.protocol.data.game.level.particle; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.mc.protocol.data.game.level.particle.positionsource.PositionSource; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; - -import java.io.IOException; - public interface ParticleData { - static ParticleData read(NetInput in, ParticleType type) throws IOException { - switch (type) { - case BLOCK: - case BLOCK_MARKER: - return new BlockParticleData(in.readVarInt()); - case DUST: - float red = in.readFloat(); - float green = in.readFloat(); - float blue = in.readFloat(); - float scale = in.readFloat(); - return new DustParticleData(red, green, blue, scale); - case DUST_COLOR_TRANSITION: - red = in.readFloat(); - green = in.readFloat(); - blue = in.readFloat(); - scale = in.readFloat(); - float newRed = in.readFloat(); - float newGreen = in.readFloat(); - float newBlue = in.readFloat(); - return new DustColorTransitionParticleData(red, green, blue, scale, newRed, newGreen, newBlue); - case FALLING_DUST: - return new FallingDustParticleData(in.readVarInt()); - case ITEM: - return new ItemParticleData(ItemStack.read(in)); - case SCULK_CHARGE: - return new SculkChargeParticleData(in.readFloat()); - case SHRIEK: - return new ShriekParticleData(in.readVarInt()); - case VIBRATION: - return new VibrationParticleData(PositionSource.read(in), in.readVarInt()); - default: - return null; - } - } - - static void write(NetOutput out, ParticleType type, ParticleData data) throws IOException { - switch (type) { - case BLOCK: - case BLOCK_MARKER: - out.writeVarInt(((BlockParticleData) data).getBlockState()); - break; - case DUST: - out.writeFloat(((DustParticleData) data).getRed()); - out.writeFloat(((DustParticleData) data).getGreen()); - out.writeFloat(((DustParticleData) data).getBlue()); - out.writeFloat(((DustParticleData) data).getScale()); - break; - case DUST_COLOR_TRANSITION: - out.writeFloat(((DustParticleData) data).getRed()); - out.writeFloat(((DustParticleData) data).getGreen()); - out.writeFloat(((DustParticleData) data).getBlue()); - out.writeFloat(((DustParticleData) data).getScale()); - out.writeFloat(((DustColorTransitionParticleData) data).getNewRed()); - out.writeFloat(((DustColorTransitionParticleData) data).getNewGreen()); - out.writeFloat(((DustColorTransitionParticleData) data).getNewBlue()); - break; - case FALLING_DUST: - out.writeVarInt(((FallingDustParticleData) data).getBlockState()); - break; - case ITEM: - ItemStack.write(out, ((ItemParticleData) data).getItemStack()); - break; - case SCULK_CHARGE: - out.writeFloat(((SculkChargeParticleData) data).getRoll()); - break; - case SHRIEK: - out.writeVarInt(((ShriekParticleData) data).getDelay()); - break; - case VIBRATION: - PositionSource.write(out, ((VibrationParticleData) data).getPositionSource()); - out.writeVarInt(((VibrationParticleData) data).getArrivalTicks()); - break; - } - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleType.java index 6f0e3f2e..91a768b7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/ParticleType.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.level.particle; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum ParticleType { AMBIENT_ENTITY_EFFECT, ANGRY_VILLAGER, @@ -101,11 +97,7 @@ public enum ParticleType { private static final ParticleType[] VALUES = values(); - public static ParticleType read(NetInput in) throws IOException { - return in.readEnum(VALUES); - } - - public static ParticleType fromId(int id) { + public static ParticleType from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSource.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSource.java index a53e727c..e3119a73 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSource.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSource.java @@ -1,35 +1,6 @@ package com.github.steveice10.mc.protocol.data.game.level.particle.positionsource; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; - -import java.io.IOException; - public interface PositionSource { - static PositionSource read(NetInput in) throws IOException { - PositionSourceType type = PositionSourceType.read(in); - switch (type) { - case BLOCK: - return new BlockPositionSource(Position.read(in)); - case ENTITY: - return new EntityPositionSource(in.readVarInt(), in.readFloat()); - default: - throw new IllegalStateException("Unknown position source type!"); - } - } - - static void write(NetOutput out, PositionSource positionSource) throws IOException { - positionSource.getType().write(out); - if (positionSource instanceof BlockPositionSource) { - Position.write(out, ((BlockPositionSource) positionSource).getPosition()); - } else if (positionSource instanceof EntityPositionSource) { - out.writeVarInt(((EntityPositionSource) positionSource).getEntityId()); - out.writeFloat(((EntityPositionSource) positionSource).getYOffset()); - } - throw new IllegalStateException("Unknown position source type!"); - } - PositionSourceType getType(); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSourceType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSourceType.java index e042f9d9..921a961a 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSourceType.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/particle/positionsource/PositionSourceType.java @@ -1,21 +1,6 @@ package com.github.steveice10.mc.protocol.data.game.level.particle.positionsource; -import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.Identifier; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; - -import java.io.IOException; - public enum PositionSourceType { BLOCK, ENTITY; - - public static PositionSourceType read(NetInput in) throws IOException { - return MagicValues.key(PositionSourceType.class, Identifier.formalize(in.readString())); - } - - public void write(NetOutput out) throws IOException { - out.writeString(MagicValues.value(String.class, this)); - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/BuiltinSound.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/BuiltinSound.java index ba84ffea..11adadd5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/BuiltinSound.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/BuiltinSound.java @@ -1334,18 +1334,12 @@ public enum BuiltinSound implements Sound { private final @NonNull String name; - /** - * For grabbing when the CustomSoundPacket is sent. - */ - public static final Map NAME_TO_SOUND = new HashMap<>(); /** * For grabbing when the SoundPacket is sent. */ - public static final BuiltinSound[] VALUES = values(); + private static final BuiltinSound[] VALUES = values(); - static { - for (BuiltinSound sound : VALUES) { - NAME_TO_SOUND.put(sound.name, sound); - } + public static BuiltinSound from(int id) { + return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/SoundCategory.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/SoundCategory.java index a821dbd0..1ef9e736 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/SoundCategory.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/level/sound/SoundCategory.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.level.sound; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum SoundCategory { MASTER, MUSIC, @@ -18,11 +14,8 @@ public enum SoundCategory { private static final SoundCategory[] VALUES = values(); - public static SoundCategory read(NetInput in) throws IOException { - return in.readEnum(VALUES); - } - public static SoundCategory fromId(int id) { + public static SoundCategory from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/CustomStatistic.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/CustomStatistic.java index cc30f1b2..c9773f29 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/CustomStatistic.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/CustomStatistic.java @@ -1,10 +1,7 @@ package com.github.steveice10.mc.protocol.data.game.statistic; -import com.github.steveice10.packetlib.io.NetInput; import lombok.Getter; -import java.io.IOException; - /** * "Custom" statistics in Minecraft that don't belong to any * specific category. @@ -99,11 +96,7 @@ public enum CustomStatistic implements Statistic { this.format = format; } - public static CustomStatistic read(NetInput in) throws IOException { - return in.readEnum(VALUES); - } - - public static CustomStatistic fromId(int id) { + public static CustomStatistic from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/StatisticCategory.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/StatisticCategory.java index 80e34e4c..eb1e3cbb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/StatisticCategory.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/statistic/StatisticCategory.java @@ -1,9 +1,5 @@ package com.github.steveice10.mc.protocol.data.game.statistic; -import com.github.steveice10.packetlib.io.NetInput; - -import java.io.IOException; - public enum StatisticCategory { BREAK_BLOCK, CRAFT_ITEM, @@ -17,11 +13,7 @@ public enum StatisticCategory { private static final StatisticCategory[] VALUES = values(); - public static StatisticCategory read(NetInput in) throws IOException { - return in.readEnum(VALUES); - } - - public static StatisticCategory fromId(int id) { + public static StatisticCategory from(int id) { return VALUES[id]; } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacket.java index a3423ea3..bcdfe2b5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.handshake.serverbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.handshake.HandshakeIntent; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,25 +15,25 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientIntentionPacket implements Packet { +public class ClientIntentionPacket implements MinecraftPacket { private final int protocolVersion; private final @NonNull String hostname; private final int port; private final @NonNull HandshakeIntent intent; - public ClientIntentionPacket(NetInput in) throws IOException { - this.protocolVersion = in.readVarInt(); - this.hostname = in.readString(); + public ClientIntentionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.protocolVersion = helper.readVarInt(in); + this.hostname = helper.readString(in); this.port = in.readUnsignedShort(); - this.intent = MagicValues.key(HandshakeIntent.class, in.readVarInt()); + this.intent = MagicValues.key(HandshakeIntent.class, helper.readVarInt(in)); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.protocolVersion); - out.writeString(this.hostname); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.protocolVersion); + helper.writeString(out, this.hostname); out.writeShort(this.port); - out.writeVarInt(MagicValues.value(Integer.class, this.intent)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.intent)); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java index 5e509524..da53cae4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundAwardStatsPacket.java @@ -1,20 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType; -import com.github.steveice10.mc.protocol.data.game.statistic.BreakBlockStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.BreakItemStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.CraftItemStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.CustomStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.DropItemStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.KillEntityStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.KilledByEntityStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.PickupItemStatistic; -import com.github.steveice10.mc.protocol.data.game.statistic.Statistic; -import com.github.steveice10.mc.protocol.data.game.statistic.StatisticCategory; -import com.github.steveice10.mc.protocol.data.game.statistic.UseItemStatistic; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.data.game.statistic.*; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -27,14 +17,14 @@ import java.util.Map; @Data @With @AllArgsConstructor -public class ClientboundAwardStatsPacket implements Packet { +public class ClientboundAwardStatsPacket implements MinecraftPacket { private final @NonNull Map statistics = new HashMap<>(); //TODO Fastutil - public ClientboundAwardStatsPacket(NetInput in) throws IOException { - int length = in.readVarInt(); + public ClientboundAwardStatsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + int length = helper.readVarInt(in); for (int index = 0; index < length; index++) { - StatisticCategory category = StatisticCategory.read(in); - int statisticId = in.readVarInt(); + StatisticCategory category = helper.readStatisticCategory(in); + int statisticId = helper.readVarInt(in); Statistic statistic; switch (category) { case BREAK_BLOCK: @@ -56,24 +46,24 @@ public class ClientboundAwardStatsPacket implements Packet { statistic = new DropItemStatistic(statisticId); break; case KILL_ENTITY: - statistic = new KillEntityStatistic(EntityType.fromId(statisticId)); + statistic = new KillEntityStatistic(EntityType.from(statisticId)); break; case KILLED_BY_ENTITY: - statistic = new KilledByEntityStatistic(EntityType.fromId(statisticId)); + statistic = new KilledByEntityStatistic(EntityType.from(statisticId)); break; case CUSTOM: - statistic = CustomStatistic.fromId(statisticId); + statistic = CustomStatistic.from(statisticId); break; default: throw new IllegalStateException(); } - this.statistics.put(statistic, in.readVarInt()); + this.statistics.put(statistic, helper.readVarInt(in)); } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.statistics.size()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.statistics.size()); for (Map.Entry entry : statistics.entrySet()) { Statistic statistic = entry.getKey(); @@ -109,9 +99,9 @@ public class ClientboundAwardStatsPacket implements Packet { } else { throw new IllegalStateException(); } - out.writeVarInt(category.ordinal()); - out.writeVarInt(statisticId); - out.writeVarInt(entry.getValue()); + helper.writeStatisticCategory(out, category); + helper.writeVarInt(out, statisticId); + helper.writeVarInt(out, entry.getValue()); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundBossEventPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundBossEventPacket.java index 78b552f0..7c599654 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundBossEventPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundBossEventPacket.java @@ -1,13 +1,13 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.BossBarAction; import com.github.steveice10.mc.protocol.data.game.BossBarColor; import com.github.steveice10.mc.protocol.data.game.BossBarDivision; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.*; import net.kyori.adventure.text.Component; @@ -17,7 +17,7 @@ import java.util.UUID; @Data @With @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class ClientboundBossEventPacket implements Packet { +public class ClientboundBossEventPacket implements MinecraftPacket { private final @NonNull UUID uuid; private final @NonNull BossBarAction action; @@ -66,12 +66,12 @@ public class ClientboundBossEventPacket implements Packet { this.showFog = showFog; } - public ClientboundBossEventPacket(NetInput in) throws IOException { - this.uuid = in.readUUID(); - this.action = MagicValues.key(BossBarAction.class, in.readVarInt()); + public ClientboundBossEventPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.uuid = helper.readUUID(in); + this.action = MagicValues.key(BossBarAction.class, helper.readVarInt(in)); if (this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_TITLE) { - this.title = DefaultComponentSerializer.get().deserialize(in.readString()); + this.title = helper.readComponent(in); } else { this.title = null; } @@ -83,8 +83,8 @@ public class ClientboundBossEventPacket implements Packet { } if (this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_STYLE) { - this.color = MagicValues.key(BossBarColor.class, in.readVarInt()); - this.division = MagicValues.key(BossBarDivision.class, in.readVarInt()); + this.color = MagicValues.key(BossBarColor.class, helper.readVarInt(in)); + this.division = MagicValues.key(BossBarDivision.class, helper.readVarInt(in)); } else { this.color = null; this.division = null; @@ -103,12 +103,12 @@ public class ClientboundBossEventPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeUUID(this.uuid); - out.writeVarInt(MagicValues.value(Integer.class, this.action)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeUUID(out, this.uuid); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); if (this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_TITLE) { - out.writeString(DefaultComponentSerializer.get().serialize(this.title)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.title)); } if (this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_HEALTH) { @@ -116,8 +116,8 @@ public class ClientboundBossEventPacket implements Packet { } if (this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_STYLE) { - out.writeVarInt(MagicValues.value(Integer.class, this.color)); - out.writeVarInt(MagicValues.value(Integer.class, this.division)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.color)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.division)); } if (this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_FLAGS) { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChangeDifficultyPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChangeDifficultyPacket.java index 95c4c2a0..c4963526 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChangeDifficultyPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChangeDifficultyPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.setting.Difficulty; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,17 +15,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundChangeDifficultyPacket implements Packet { +public class ClientboundChangeDifficultyPacket implements MinecraftPacket { private final @NonNull Difficulty difficulty; private final boolean difficultyLocked; - public ClientboundChangeDifficultyPacket(NetInput in) throws IOException { + public ClientboundChangeDifficultyPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.difficulty = MagicValues.key(Difficulty.class, in.readUnsignedByte()); this.difficultyLocked = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(MagicValues.value(Integer.class, this.difficulty)); out.writeBoolean(this.difficultyLocked); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChatPreviewPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChatPreviewPacket.java index 7e22b369..368aac8e 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChatPreviewPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundChatPreviewPacket.java @@ -1,38 +1,38 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundChatPreviewPacket implements Packet { +public class ClientboundChatPreviewPacket implements MinecraftPacket { private final int queryId; private final @Nullable Component preview; - public ClientboundChatPreviewPacket(NetInput in) throws IOException { + public ClientboundChatPreviewPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.queryId = in.readInt(); if (in.readBoolean()) { - this.preview = DefaultComponentSerializer.get().deserialize(in.readString()); + this.preview = helper.readComponent(in); } else { this.preview = null; } } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.queryId); if (this.preview != null) { - out.writeString(DefaultComponentSerializer.get().serialize(this.preview)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.preview)); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandSuggestionsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandSuggestionsPacket.java index ab639d19..f453c549 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandSuggestionsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandSuggestionsPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.Data; import lombok.NonNull; import lombok.With; @@ -14,7 +14,7 @@ import java.util.Arrays; @Data @With -public class ClientboundCommandSuggestionsPacket implements Packet { +public class ClientboundCommandSuggestionsPacket implements MinecraftPacket { private final int transactionId; private final int start; private final int length; @@ -33,32 +33,32 @@ public class ClientboundCommandSuggestionsPacket implements Packet { this.tooltips = Arrays.copyOf(tooltips, tooltips.length); } - public ClientboundCommandSuggestionsPacket(NetInput in) throws IOException { - this.transactionId = in.readVarInt(); - this.start = in.readVarInt(); - this.length = in.readVarInt(); - this.matches = new String[in.readVarInt()]; + public ClientboundCommandSuggestionsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.transactionId = helper.readVarInt(in); + this.start = helper.readVarInt(in); + this.length = helper.readVarInt(in); + this.matches = new String[helper.readVarInt(in)]; this.tooltips = new Component[this.matches.length]; for (int index = 0; index < this.matches.length; index++) { - this.matches[index] = in.readString(); + this.matches[index] = helper.readString(in); if (in.readBoolean()) { - this.tooltips[index] = DefaultComponentSerializer.get().deserialize(in.readString()); + this.tooltips[index] = helper.readComponent(in); } } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.transactionId); - out.writeVarInt(this.start); - out.writeVarInt(this.length); - out.writeVarInt(this.matches.length); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.transactionId); + helper.writeVarInt(out, this.start); + helper.writeVarInt(out, this.length); + helper.writeVarInt(out, this.matches.length); for (int index = 0; index < this.matches.length; index++) { - out.writeString(this.matches[index]); + helper.writeString(out, this.matches[index]); Component tooltip = this.tooltips[index]; if (tooltip != null) { out.writeBoolean(true); - out.writeString(DefaultComponentSerializer.get().serialize(tooltip)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(tooltip)); } else { out.writeBoolean(false); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java index 24cc4077..c44ea29f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCommandsPacket.java @@ -1,24 +1,15 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.Identifier; import com.github.steveice10.mc.protocol.data.game.command.CommandNode; import com.github.steveice10.mc.protocol.data.game.command.CommandParser; import com.github.steveice10.mc.protocol.data.game.command.CommandType; import com.github.steveice10.mc.protocol.data.game.command.SuggestionType; -import com.github.steveice10.mc.protocol.data.game.command.properties.CommandProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.DoubleProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.EntityProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.FloatProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.IntegerProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.LongProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.RangeProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.ScoreHolderProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.StringProperties; -import com.github.steveice10.mc.protocol.data.game.command.properties.ResourceProperties; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.data.game.command.properties.*; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -29,7 +20,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundCommandsPacket implements Packet { +public class ClientboundCommandsPacket implements MinecraftPacket { private static final int FLAG_TYPE_MASK = 0x03; private static final int FLAG_EXECUTABLE = 0x04; private static final int FLAG_REDIRECT = 0x08; @@ -44,32 +35,32 @@ public class ClientboundCommandsPacket implements Packet { private final @NonNull CommandNode[] nodes; private final int firstNodeIndex; - public ClientboundCommandsPacket(NetInput in) throws IOException { - this.nodes = new CommandNode[in.readVarInt()]; + public ClientboundCommandsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.nodes = new CommandNode[helper.readVarInt(in)]; for (int i = 0; i < this.nodes.length; i++) { byte flags = in.readByte(); CommandType type = MagicValues.key(CommandType.class, flags & FLAG_TYPE_MASK); boolean executable = (flags & FLAG_EXECUTABLE) != 0; - int[] children = new int[in.readVarInt()]; + int[] children = new int[helper.readVarInt(in)]; for (int j = 0; j < children.length; j++) { - children[j] = in.readVarInt(); + children[j] = helper.readVarInt(in); } int redirectIndex = -1; if ((flags & FLAG_REDIRECT) != 0) { - redirectIndex = in.readVarInt(); + redirectIndex = helper.readVarInt(in); } String name = null; if (type == CommandType.LITERAL || type == CommandType.ARGUMENT) { - name = in.readString(); + name = helper.readString(in); } CommandParser parser = null; CommandProperties properties = null; if (type == CommandType.ARGUMENT) { - parser = MagicValues.key(CommandParser.class, in.readVarInt()); + parser = MagicValues.key(CommandParser.class, helper.readVarInt(in)); switch (parser) { case DOUBLE: { byte numberFlags = in.readByte(); @@ -132,7 +123,7 @@ public class ClientboundCommandsPacket implements Packet { break; } case STRING: - properties = MagicValues.key(StringProperties.class, in.readVarInt()); + properties = MagicValues.key(StringProperties.class, helper.readVarInt(in)); break; case ENTITY: { byte entityFlags = in.readByte(); @@ -145,7 +136,7 @@ public class ClientboundCommandsPacket implements Packet { break; case RESOURCE: case RESOURCE_OR_TAG: - properties = new ResourceProperties(in.readString()); + properties = new ResourceProperties(helper.readString(in)); break; default: break; @@ -154,18 +145,18 @@ public class ClientboundCommandsPacket implements Packet { SuggestionType suggestionType = null; if ((flags & FLAG_SUGGESTION_TYPE) != 0) { - suggestionType = MagicValues.key(SuggestionType.class, Identifier.formalize(in.readString())); + suggestionType = MagicValues.key(SuggestionType.class, Identifier.formalize(helper.readString(in))); } this.nodes[i] = new CommandNode(type, executable, children, redirectIndex, name, parser, properties, suggestionType); } - this.firstNodeIndex = in.readVarInt(); + this.firstNodeIndex = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.nodes.length); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.nodes.length); for (CommandNode node : this.nodes) { int flags = MagicValues.value(Integer.class, node.getType()) & FLAG_TYPE_MASK; if (node.isExecutable()) { @@ -182,21 +173,21 @@ public class ClientboundCommandsPacket implements Packet { out.writeByte(flags); - out.writeVarInt(node.getChildIndices().length); + helper.writeVarInt(out, node.getChildIndices().length); for (int childIndex : node.getChildIndices()) { - out.writeVarInt(childIndex); + helper.writeVarInt(out, childIndex); } if (node.getRedirectIndex() != -1) { - out.writeVarInt(node.getRedirectIndex()); + helper.writeVarInt(out, node.getRedirectIndex()); } if (node.getType() == CommandType.LITERAL || node.getType() == CommandType.ARGUMENT) { - out.writeString(node.getName()); + helper.writeString(out, node.getName()); } if (node.getType() == CommandType.ARGUMENT) { - out.writeVarInt(MagicValues.value(Integer.class, node.getParser())); + helper.writeVarInt(out, MagicValues.value(Integer.class, node.getParser())); switch (node.getParser()) { case DOUBLE: { DoubleProperties properties = (DoubleProperties) node.getProperties(); @@ -291,7 +282,7 @@ public class ClientboundCommandsPacket implements Packet { break; } case STRING: - out.writeVarInt(MagicValues.value(Integer.class, node.getProperties())); + helper.writeVarInt(out, MagicValues.value(Integer.class, node.getProperties())); break; case ENTITY: { EntityProperties properties = (EntityProperties) node.getProperties(); @@ -312,7 +303,7 @@ public class ClientboundCommandsPacket implements Packet { break; case RESOURCE: case RESOURCE_OR_TAG: - out.writeString(((ResourceProperties) node.getProperties()).getRegistryKey()); + helper.writeString(out, ((ResourceProperties) node.getProperties()).getRegistryKey()); break; default: break; @@ -320,10 +311,10 @@ public class ClientboundCommandsPacket implements Packet { } if (node.getSuggestionType() != null) { - out.writeString(MagicValues.value(String.class, node.getSuggestionType())); + helper.writeString(out, MagicValues.value(String.class, node.getSuggestionType())); } } - out.writeVarInt(this.firstNodeIndex); + helper.writeVarInt(out, this.firstNodeIndex); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCooldownPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCooldownPacket.java index ffaa8d6e..1f0e61a0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCooldownPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCooldownPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundCooldownPacket implements Packet { +public class ClientboundCooldownPacket implements MinecraftPacket { private final int itemId; private final int cooldownTicks; - public ClientboundCooldownPacket(NetInput in) throws IOException { - this.itemId = in.readVarInt(); - this.cooldownTicks = in.readVarInt(); + public ClientboundCooldownPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.itemId = helper.readVarInt(in); + this.cooldownTicks = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.itemId); - out.writeVarInt(this.cooldownTicks); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.itemId); + helper.writeVarInt(out, this.cooldownTicks); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCustomPayloadPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCustomPayloadPacket.java index 23d2ceee..0df956b8 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCustomPayloadPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundCustomPayloadPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,18 +13,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundCustomPayloadPacket implements Packet { +public class ClientboundCustomPayloadPacket implements MinecraftPacket { private final @NonNull String channel; private final @NonNull byte[] data; - public ClientboundCustomPayloadPacket(NetInput in) throws IOException { - this.channel = in.readString(); - this.data = in.readBytes(in.available()); + public ClientboundCustomPayloadPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.channel = helper.readString(in); + this.data = helper.readByteArray(in, ByteBuf::readableBytes); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.channel); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.channel); out.writeBytes(this.data); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundDisconnectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundDisconnectPacket.java index d53b49a2..c0366df5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundDisconnectPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundDisconnectPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,20 +15,20 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundDisconnectPacket implements Packet { +public class ClientboundDisconnectPacket implements MinecraftPacket { private final @NonNull Component reason; public ClientboundDisconnectPacket(@NonNull String reason) { this(DefaultComponentSerializer.get().deserialize(reason)); } - public ClientboundDisconnectPacket(NetInput in) throws IOException { - this.reason = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundDisconnectPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.reason = helper.readComponent(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serialize(this.reason)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeComponent(out, this.reason); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundKeepAlivePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundKeepAlivePacket.java index 2780a061..a31fffb0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundKeepAlivePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundKeepAlivePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundKeepAlivePacket implements Packet { +public class ClientboundKeepAlivePacket implements MinecraftPacket { private final long pingId; - public ClientboundKeepAlivePacket(NetInput in) throws IOException { + public ClientboundKeepAlivePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.pingId = in.readLong(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeLong(this.pingId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundLoginPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundLoginPacket.java index e9835a7c..2f531a2f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundLoginPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundLoginPacket.java @@ -1,25 +1,24 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.NBT; import com.github.steveice10.mc.protocol.data.game.entity.metadata.GlobalPos; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundLoginPacket implements Packet { +public class ClientboundLoginPacket implements MinecraftPacket { private static final int GAMEMODE_MASK = 0x07; private final int entityId; @@ -41,38 +40,38 @@ public class ClientboundLoginPacket implements Packet { private final boolean flat; private final @Nullable GlobalPos lastDeathPos; - public ClientboundLoginPacket(NetInput in) throws IOException { + public ClientboundLoginPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.entityId = in.readInt(); this.hardcore = in.readBoolean(); int gameMode = in.readUnsignedByte(); this.gameMode = MagicValues.key(GameMode.class, gameMode & GAMEMODE_MASK); this.previousGamemode = MagicValues.key(GameMode.class, in.readUnsignedByte()); - this.worldCount = in.readVarInt(); + this.worldCount = helper.readVarInt(in); this.worldNames = new String[this.worldCount]; for (int i = 0; i < this.worldCount; i++) { - this.worldNames[i] = in.readString(); + this.worldNames[i] = helper.readString(in); } - this.dimensionCodec = NBT.read(in); - this.dimension = in.readString(); - this.worldName = in.readString(); + this.dimensionCodec = helper.readTag(in); + this.dimension = helper.readString(in); + this.worldName = helper.readString(in); this.hashedSeed = in.readLong(); - this.maxPlayers = in.readVarInt(); - this.viewDistance = in.readVarInt(); - this.simulationDistance = in.readVarInt(); + this.maxPlayers = helper.readVarInt(in); + this.viewDistance = helper.readVarInt(in); + this.simulationDistance = helper.readVarInt(in); this.reducedDebugInfo = in.readBoolean(); this.enableRespawnScreen = in.readBoolean(); this.debug = in.readBoolean(); this.flat = in.readBoolean(); if (in.readBoolean()) { - this.lastDeathPos = GlobalPos.read(in); + this.lastDeathPos = helper.readGlobalPos(in); } else { this.lastDeathPos = null; } } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.entityId); out.writeBoolean(this.hardcore); @@ -80,24 +79,24 @@ public class ClientboundLoginPacket implements Packet { out.writeByte(gameMode); out.writeByte(MagicValues.value(Integer.class, this.previousGamemode)); - out.writeVarInt(this.worldCount); + helper.writeVarInt(out, this.worldCount); for (String worldName : this.worldNames) { - out.writeString(worldName); + helper.writeString(out, worldName); } - NBT.write(out, this.dimensionCodec); - out.writeString(this.dimension); - out.writeString(this.worldName); + helper.writeTag(out, this.dimensionCodec); + helper.writeString(out, this.dimension); + helper.writeString(out, this.worldName); out.writeLong(this.hashedSeed); - out.writeVarInt(this.maxPlayers); - out.writeVarInt(this.viewDistance); - out.writeVarInt(this.simulationDistance); + helper.writeVarInt(out, this.maxPlayers); + helper.writeVarInt(out, this.viewDistance); + helper.writeVarInt(out, this.simulationDistance); out.writeBoolean(this.reducedDebugInfo); out.writeBoolean(this.enableRespawnScreen); out.writeBoolean(this.debug); out.writeBoolean(this.flat); out.writeBoolean(this.lastDeathPos != null); if (this.lastDeathPos != null) { - GlobalPos.write(out, this.lastDeathPos); + helper.writeGlobalPos(out, this.lastDeathPos); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPingPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPingPacket.java index 61e8addf..d8c60e03 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPingPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPingPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPingPacket implements Packet { +public class ClientboundPingPacket implements MinecraftPacket { private final int id; - public ClientboundPingPacket(NetInput in) throws IOException { + public ClientboundPingPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.id = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.id); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java index 55dae97f..460fd815 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerChatPacket.java @@ -1,24 +1,24 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.game.MessageType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.ToString; import lombok.With; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; import java.util.UUID; @Data @With @AllArgsConstructor -public class ClientboundPlayerChatPacket implements Packet { +public class ClientboundPlayerChatPacket implements MinecraftPacket { private final Component signedContent; private final @Nullable Component unsignedContent; private final MessageType type; @@ -30,37 +30,37 @@ public class ClientboundPlayerChatPacket implements Packet { private final byte[] signature; private final long timeStamp; - public ClientboundPlayerChatPacket(NetInput in) throws IOException { - this.signedContent = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundPlayerChatPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.signedContent = helper.readComponent(in); if (in.readBoolean()) { - this.unsignedContent = DefaultComponentSerializer.get().deserialize(in.readString()); + this.unsignedContent = helper.readComponent(in); } else { this.unsignedContent = null; } - this.type = in.readEnum(MessageType.VALUES); - this.senderUUID = in.readUUID(); - this.senderName = DefaultComponentSerializer.get().deserialize(in.readString()); + this.type = helper.readMessageType(in); + this.senderUUID = helper.readUUID(in); + this.senderName = helper.readComponent(in); if (in.readBoolean()) { - this.senderTeamName = DefaultComponentSerializer.get().deserialize(in.readString()); + this.senderTeamName = helper.readComponent(in); } else { this.senderTeamName = null; } this.timeStamp = in.readLong(); this.salt = in.readLong(); - this.signature = in.readBytes(in.readVarInt()); + this.signature = helper.readByteArray(in); } @Override - public void write(NetOutput out) throws IOException { - DefaultComponentSerializer.get().serialize(this.signedContent); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeComponent(out, this.signedContent); if (this.unsignedContent != null) { - DefaultComponentSerializer.get().serialize(this.unsignedContent); + helper.writeComponent(out, this.unsignedContent); } - out.writeEnum(this.type); - out.writeUUID(this.senderUUID); + helper.writeMessageType(out, this.type); + helper.writeUUID(out, this.senderUUID); DefaultComponentSerializer.get().serialize(this.senderName); if (this.senderTeamName != null) { DefaultComponentSerializer.get().serialize(this.senderTeamName); @@ -68,7 +68,7 @@ public class ClientboundPlayerChatPacket implements Packet { out.writeLong(this.timeStamp); out.writeLong(this.salt); - out.writeVarInt(this.signature.length); + helper.writeVarInt(out, this.signature.length); out.writeBytes(this.signature); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoPacket.java index 7af63660..9442c4ef 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundPlayerInfoPacket.java @@ -1,14 +1,14 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; import com.github.steveice10.mc.auth.data.GameProfile; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.PlayerListEntry; import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -27,18 +27,18 @@ import java.util.UUID; @Data @With @AllArgsConstructor -public class ClientboundPlayerInfoPacket implements Packet { +public class ClientboundPlayerInfoPacket implements MinecraftPacket { private final @NonNull PlayerListEntryAction action; private final @NonNull PlayerListEntry[] entries; - public ClientboundPlayerInfoPacket(NetInput in) throws IOException { - this.action = MagicValues.key(PlayerListEntryAction.class, in.readVarInt()); - this.entries = new PlayerListEntry[in.readVarInt()]; + public ClientboundPlayerInfoPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.action = MagicValues.key(PlayerListEntryAction.class, helper.readVarInt(in)); + this.entries = new PlayerListEntry[helper.readVarInt(in)]; for (int count = 0; count < this.entries.length; count++) { - UUID uuid = in.readUUID(); + UUID uuid = helper.readUUID(in); GameProfile profile; if (this.action == PlayerListEntryAction.ADD_PLAYER) { - profile = new GameProfile(uuid, in.readString()); + profile = new GameProfile(uuid, helper.readString(in)); } else { profile = new GameProfile(uuid, null); } @@ -46,14 +46,14 @@ public class ClientboundPlayerInfoPacket implements Packet { PlayerListEntry entry = null; switch (this.action) { case ADD_PLAYER: { - int properties = in.readVarInt(); + int properties = helper.readVarInt(in); List propertyList = new ArrayList<>(); for (int index = 0; index < properties; index++) { - String propertyName = in.readString(); - String value = in.readString(); + String propertyName = helper.readString(in); + String value = helper.readString(in); String signature = null; if (in.readBoolean()) { - signature = in.readString(); + signature = helper.readString(in); } propertyList.add(new GameProfile.Property(propertyName, value, signature)); @@ -61,12 +61,12 @@ public class ClientboundPlayerInfoPacket implements Packet { profile.setProperties(propertyList); - int rawGameMode = in.readVarInt(); + int rawGameMode = helper.readVarInt(in); GameMode gameMode = MagicValues.key(GameMode.class, Math.max(rawGameMode, 0)); - int ping = in.readVarInt(); + int ping = helper.readVarInt(in); Component displayName = null; if (in.readBoolean()) { - displayName = DefaultComponentSerializer.get().deserialize(in.readString()); + displayName = helper.readComponent(in); } long expiresAt; @@ -74,8 +74,8 @@ public class ClientboundPlayerInfoPacket implements Packet { byte[] keySignature = null; if (in.readBoolean()) { expiresAt = in.readLong(); - byte[] keyBytes = in.readBytes(in.readVarInt()); - keySignature = in.readBytes(in.readVarInt()); + byte[] keyBytes = helper.readByteArray(in); + keySignature = helper.readByteArray(in); try { publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)); @@ -90,14 +90,14 @@ public class ClientboundPlayerInfoPacket implements Packet { break; } case UPDATE_GAMEMODE: { - int rawGameMode = in.readVarInt(); + int rawGameMode = helper.readVarInt(in); GameMode mode = MagicValues.key(GameMode.class, Math.max(rawGameMode, 0)); entry = new PlayerListEntry(profile, mode); break; } case UPDATE_LATENCY: { - int ping = in.readVarInt(); + int ping = helper.readVarInt(in); entry = new PlayerListEntry(profile, ping); break; @@ -105,7 +105,7 @@ public class ClientboundPlayerInfoPacket implements Packet { case UPDATE_DISPLAY_NAME: { Component displayName = null; if (in.readBoolean()) { - displayName = DefaultComponentSerializer.get().deserialize(in.readString()); + displayName = helper.readComponent(in); } entry = new PlayerListEntry(profile, displayName); @@ -121,51 +121,51 @@ public class ClientboundPlayerInfoPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.action)); - out.writeVarInt(this.entries.length); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); + helper.writeVarInt(out, this.entries.length); for (PlayerListEntry entry : this.entries) { - out.writeUUID(entry.getProfile().getId()); + helper.writeUUID(out, entry.getProfile().getId()); switch (this.action) { case ADD_PLAYER: - out.writeString(entry.getProfile().getName()); - out.writeVarInt(entry.getProfile().getProperties().size()); + helper.writeString(out, entry.getProfile().getName()); + helper.writeVarInt(out, entry.getProfile().getProperties().size()); for (GameProfile.Property property : entry.getProfile().getProperties()) { - out.writeString(property.getName()); - out.writeString(property.getValue()); + helper.writeString(out, property.getName()); + helper.writeString(out, property.getValue()); out.writeBoolean(property.hasSignature()); if (property.hasSignature()) { - out.writeString(property.getSignature()); + helper.writeString(out, property.getSignature()); } } - out.writeVarInt(MagicValues.value(Integer.class, entry.getGameMode())); - out.writeVarInt(entry.getPing()); + helper.writeVarInt(out, MagicValues.value(Integer.class, entry.getGameMode())); + helper.writeVarInt(out, entry.getPing()); out.writeBoolean(entry.getDisplayName() != null); if (entry.getDisplayName() != null) { - out.writeString(DefaultComponentSerializer.get().serialize(entry.getDisplayName())); + helper.writeString(out, DefaultComponentSerializer.get().serialize(entry.getDisplayName())); } if (entry.getPublicKey() != null) { out.writeLong(entry.getExpiresAt()); byte[] encoded = entry.getPublicKey().getEncoded(); - out.writeVarInt(encoded.length); + helper.writeVarInt(out, encoded.length); out.writeBytes(encoded); - out.writeVarInt(entry.getKeySignature().length); + helper.writeVarInt(out, entry.getKeySignature().length); out.writeBytes(entry.getKeySignature()); } break; case UPDATE_GAMEMODE: - out.writeVarInt(MagicValues.value(Integer.class, entry.getGameMode())); + helper.writeVarInt(out, MagicValues.value(Integer.class, entry.getGameMode())); break; case UPDATE_LATENCY: - out.writeVarInt(entry.getPing()); + helper.writeVarInt(out, entry.getPing()); break; case UPDATE_DISPLAY_NAME: out.writeBoolean(entry.getDisplayName() != null); if (entry.getDisplayName() != null) { - out.writeString(DefaultComponentSerializer.get().serialize(entry.getDisplayName())); + helper.writeString(out, DefaultComponentSerializer.get().serialize(entry.getDisplayName())); } break; diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRecipePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRecipePacket.java index 26f25200..4088d3f0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRecipePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRecipePacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.UnlockRecipesAction; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.*; import java.io.IOException; @@ -13,7 +13,7 @@ import java.util.Arrays; @Data @With @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class ClientboundRecipePacket implements Packet { +public class ClientboundRecipePacket implements MinecraftPacket { private final @NonNull UnlockRecipesAction action; private final @NonNull String[] recipes; @@ -72,8 +72,8 @@ public class ClientboundRecipePacket implements Packet { this.alreadyKnownRecipes = Arrays.copyOf(alreadyKnownRecipes, alreadyKnownRecipes.length); } - public ClientboundRecipePacket(NetInput in) throws IOException { - this.action = MagicValues.key(UnlockRecipesAction.class, in.readVarInt()); + public ClientboundRecipePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.action = MagicValues.key(UnlockRecipesAction.class, helper.readVarInt(in)); this.openCraftingBook = in.readBoolean(); this.activateCraftingFiltering = in.readBoolean(); @@ -85,23 +85,23 @@ public class ClientboundRecipePacket implements Packet { this.activateSmokingFiltering = in.readBoolean(); if (this.action == UnlockRecipesAction.INIT) { - this.alreadyKnownRecipes = new String[in.readVarInt()]; + this.alreadyKnownRecipes = new String[helper.readVarInt(in)]; for (int i = 0; i < this.alreadyKnownRecipes.length; i++) { - this.alreadyKnownRecipes[i] = in.readString(); + this.alreadyKnownRecipes[i] = helper.readString(in); } } else { this.alreadyKnownRecipes = null; } - this.recipes = new String[in.readVarInt()]; + this.recipes = new String[helper.readVarInt(in)]; for (int i = 0; i < this.recipes.length; i++) { - this.recipes[i] = in.readString(); + this.recipes[i] = helper.readString(in); } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.action)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); out.writeBoolean(this.openCraftingBook); out.writeBoolean(this.activateCraftingFiltering); @@ -113,15 +113,15 @@ public class ClientboundRecipePacket implements Packet { out.writeBoolean(this.activateSmokingFiltering); if (this.action == UnlockRecipesAction.INIT) { - out.writeVarInt(this.alreadyKnownRecipes.length); + helper.writeVarInt(out, this.alreadyKnownRecipes.length); for (String recipeId : this.alreadyKnownRecipes) { - out.writeString(recipeId); + helper.writeString(out, recipeId); } } - out.writeVarInt(this.recipes.length); + helper.writeVarInt(out, this.recipes.length); for (String recipeId : this.recipes) { - out.writeString(recipeId); + helper.writeString(out, recipeId); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundResourcePackPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundResourcePackPacket.java index 604a7c40..5f59073f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundResourcePackPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundResourcePackPacket.java @@ -1,46 +1,45 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundResourcePackPacket implements Packet { +public class ClientboundResourcePackPacket implements MinecraftPacket { private final @NonNull String url; private final @NonNull String hash; private final boolean required; private final @Nullable Component prompt; - public ClientboundResourcePackPacket(NetInput in) throws IOException { - this.url = in.readString(); - this.hash = in.readString(); + public ClientboundResourcePackPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.url = helper.readString(in); + this.hash = helper.readString(in); this.required = in.readBoolean(); if (in.readBoolean()) { - this.prompt = DefaultComponentSerializer.get().deserialize(in.readString()); + this.prompt = helper.readComponent(in); } else { this.prompt = null; } } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.url); - out.writeString(this.hash); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.url); + helper.writeString(out, this.hash); out.writeBoolean(this.required); out.writeBoolean(this.prompt != null); if (this.prompt != null) { - out.writeString(DefaultComponentSerializer.get().serialize(this.prompt)); + helper.writeComponent(out, this.prompt); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRespawnPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRespawnPacket.java index 1018adc6..ee8c5738 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRespawnPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundRespawnPacket.java @@ -1,26 +1,23 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.NBT; import com.github.steveice10.mc.protocol.data.game.entity.metadata.GlobalPos; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; -import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundRespawnPacket implements Packet { +public class ClientboundRespawnPacket implements MinecraftPacket { private final @NonNull String dimension; private final @NonNull String worldName; private final long hashedSeed; @@ -31,9 +28,9 @@ public class ClientboundRespawnPacket implements Packet { private final boolean copyMetadata; private final @Nullable GlobalPos lastDeathPos; - public ClientboundRespawnPacket(NetInput in) throws IOException { - this.dimension = in.readString(); - this.worldName = in.readString(); + public ClientboundRespawnPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.dimension = helper.readString(in); + this.worldName = helper.readString(in); this.hashedSeed = in.readLong(); this.gamemode = MagicValues.key(GameMode.class, in.readUnsignedByte()); this.previousGamemode = MagicValues.key(GameMode.class, in.readUnsignedByte()); @@ -41,16 +38,16 @@ public class ClientboundRespawnPacket implements Packet { this.flat = in.readBoolean(); this.copyMetadata = in.readBoolean(); if (in.readBoolean()) { - this.lastDeathPos = GlobalPos.read(in); + this.lastDeathPos = helper.readGlobalPos(in); } else { this.lastDeathPos = null; } } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.dimension); - out.writeString(this.worldName); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.dimension); + helper.writeString(out, this.worldName); out.writeLong(this.hashedSeed); out.writeByte(MagicValues.value(Integer.class, this.gamemode)); out.writeByte(MagicValues.value(Integer.class, this.previousGamemode)); @@ -59,7 +56,7 @@ public class ClientboundRespawnPacket implements Packet { out.writeBoolean(this.copyMetadata); out.writeBoolean(this.lastDeathPos != null); if (this.lastDeathPos != null) { - GlobalPos.write(out, this.lastDeathPos); + helper.writeGlobalPos(out, this.lastDeathPos); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSelectAdvancementsTabPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSelectAdvancementsTabPacket.java index d06256ab..0a65cd68 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSelectAdvancementsTabPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSelectAdvancementsTabPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,22 +12,22 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSelectAdvancementsTabPacket implements Packet { +public class ClientboundSelectAdvancementsTabPacket implements MinecraftPacket { private final String tabId; - public ClientboundSelectAdvancementsTabPacket(NetInput in) throws IOException { + public ClientboundSelectAdvancementsTabPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { if (in.readBoolean()) { - this.tabId = in.readString(); + this.tabId = helper.readString(in); } else { this.tabId = null; } } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { if (this.tabId != null) { out.writeBoolean(true); - out.writeString(this.tabId); + helper.writeString(out, this.tabId); } else { out.writeBoolean(false); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundServerDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundServerDataPacket.java index 579a0d85..fc2d8611 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundServerDataPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundServerDataPacket.java @@ -1,34 +1,34 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundServerDataPacket implements Packet { +public class ClientboundServerDataPacket implements MinecraftPacket { private final @Nullable Component motd; private final @Nullable String iconBase64; private final boolean previewsChat; - public ClientboundServerDataPacket(NetInput in) throws IOException { + public ClientboundServerDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { if (in.readBoolean()) { - this.motd = DefaultComponentSerializer.get().deserialize(in.readString()); + this.motd = helper.readComponent(in); } else { this.motd = null; } if (in.readBoolean()) { - this.iconBase64 = in.readString(); + this.iconBase64 = helper.readString(in); } else { this.iconBase64 = null; } @@ -37,15 +37,15 @@ public class ClientboundServerDataPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.motd != null); if (this.motd != null) { - out.writeString(DefaultComponentSerializer.get().serialize(this.motd)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.motd)); } out.writeBoolean(this.iconBase64 != null); if (this.iconBase64 != null) { - out.writeString(this.iconBase64); + helper.writeString(out, this.iconBase64); } out.writeBoolean(this.previewsChat); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetCameraPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetCameraPacket.java index ff172f4a..9c88a751 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetCameraPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetCameraPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetCameraPacket implements Packet { +public class ClientboundSetCameraPacket implements MinecraftPacket { private final int cameraEntityId; - public ClientboundSetCameraPacket(NetInput in) throws IOException { - this.cameraEntityId = in.readVarInt(); + public ClientboundSetCameraPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.cameraEntityId = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.cameraEntityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.cameraEntityId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetDisplayChatPreviewPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetDisplayChatPreviewPacket.java index 37597b13..dab9da01 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetDisplayChatPreviewPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSetDisplayChatPreviewPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetDisplayChatPreviewPacket implements Packet { +public class ClientboundSetDisplayChatPreviewPacket implements MinecraftPacket { private final boolean enabled; - public ClientboundSetDisplayChatPreviewPacket(NetInput in) throws IOException { + public ClientboundSetDisplayChatPreviewPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.enabled = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.enabled); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSoundEntityPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSoundEntityPacket.java index f31205e5..5a959024 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSoundEntityPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSoundEntityPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound; import com.github.steveice10.mc.protocol.data.game.level.sound.SoundCategory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,7 +15,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSoundEntityPacket implements Packet { +public class ClientboundSoundEntityPacket implements MinecraftPacket { private final @NonNull BuiltinSound sound; private final @NonNull SoundCategory category; private final int entityId; @@ -23,20 +23,20 @@ public class ClientboundSoundEntityPacket implements Packet { private final float pitch; private final long seed; - public ClientboundSoundEntityPacket(NetInput in) throws IOException { - this.sound = BuiltinSound.VALUES[in.readVarInt()]; - this.category = SoundCategory.read(in); - this.entityId = in.readVarInt(); + public ClientboundSoundEntityPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.sound = helper.readBuiltinSound(in); + this.category = helper.readSoundCategory(in); + this.entityId = helper.readVarInt(in); this.volume = in.readFloat(); this.pitch = in.readFloat(); this.seed = in.readLong(); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.sound.ordinal()); - out.writeVarInt(this.category.ordinal()); - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeBuiltinSound(out, this.sound); + helper.writeSoundCategory(out, this.category); + helper.writeVarInt(out, this.entityId); out.writeFloat(this.volume); out.writeFloat(this.pitch); out.writeLong(this.seed); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundStopSoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundStopSoundPacket.java index 04161c71..2260404e 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundStopSoundPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundStopSoundPacket.java @@ -1,40 +1,40 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound; import com.github.steveice10.mc.protocol.data.game.level.sound.CustomSound; import com.github.steveice10.mc.protocol.data.game.level.sound.Sound; import com.github.steveice10.mc.protocol.data.game.level.sound.SoundCategory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundStopSoundPacket implements Packet { +public class ClientboundStopSoundPacket implements MinecraftPacket { private static final int FLAG_CATEGORY = 0x01; private static final int FLAG_SOUND = 0x02; private final @Nullable SoundCategory category; private final @Nullable Sound sound; - public ClientboundStopSoundPacket(NetInput in) throws IOException { + public ClientboundStopSoundPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { int flags = in.readByte(); if ((flags & FLAG_CATEGORY) != 0) { - this.category = SoundCategory.read(in); + this.category = helper.readSoundCategory(in); } else { this.category = null; } if ((flags & FLAG_SOUND) != 0) { - String value = in.readString(); - Sound sound = BuiltinSound.NAME_TO_SOUND.get(value); + String value = helper.readString(in); + Sound sound = helper.getBuiltinSound(value); if (sound != null) { this.sound = sound; } else { @@ -46,7 +46,7 @@ public class ClientboundStopSoundPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { int flags = 0; if (this.category != null) { flags |= FLAG_CATEGORY; @@ -69,7 +69,7 @@ public class ClientboundStopSoundPacket implements Packet { value = ((BuiltinSound) this.sound).getName(); } - out.writeString(value); + helper.writeString(out, value); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java index da34672a..76b45201 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundSystemChatPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.game.MessageType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -15,18 +15,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSystemChatPacket implements Packet { +public class ClientboundSystemChatPacket implements MinecraftPacket { private final Component content; private final MessageType type; - public ClientboundSystemChatPacket(NetInput in) throws IOException { - this.content = DefaultComponentSerializer.get().deserialize(in.readString()); - this.type = in.readEnum(MessageType.VALUES); + public ClientboundSystemChatPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.content = helper.readComponent(in); + this.type = helper.readMessageType(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serialize(this.content)); - out.writeEnum(this.type); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.content)); + helper.writeMessageType(out, this.type); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundTabListPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundTabListPacket.java index de8f3b25..464989f6 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundTabListPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundTabListPacket.java @@ -1,9 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; -import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundTabListPacket implements Packet { +public class ClientboundTabListPacket implements MinecraftPacket { private final @NonNull Component header; private final @NonNull Component footer; - public ClientboundTabListPacket(NetInput in) throws IOException { - this.header = DefaultComponentSerializer.get().deserialize(in.readString()); - this.footer = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundTabListPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.header = helper.readComponent(in); + this.footer = helper.readComponent(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serialize(this.header)); - out.writeString(DefaultComponentSerializer.get().serialize(this.footer)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeComponent(out, this.header); + helper.writeComponent(out, this.footer); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateAdvancementsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateAdvancementsPacket.java index 553bfe05..de0f59c1 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateAdvancementsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateAdvancementsPacket.java @@ -1,14 +1,14 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement.DisplayData; import com.github.steveice10.mc.protocol.data.game.advancement.Advancement.DisplayData.FrameType; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -24,7 +24,7 @@ import java.util.Map; @Data @With @AllArgsConstructor -public class ClientboundUpdateAdvancementsPacket implements Packet { +public class ClientboundUpdateAdvancementsPacket implements MinecraftPacket { private static final int FLAG_HAS_BACKGROUND_TEXTURE = 0x01; private static final int FLAG_SHOW_TOAST = 0x02; private static final int FLAG_HIDDEN = 0x04; @@ -47,26 +47,26 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { return progress.get(criterionId); } - public ClientboundUpdateAdvancementsPacket(NetInput in) throws IOException { + public ClientboundUpdateAdvancementsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.reset = in.readBoolean(); - this.advancements = new Advancement[in.readVarInt()]; + this.advancements = new Advancement[helper.readVarInt(in)]; for (int i = 0; i < this.advancements.length; i++) { - String id = in.readString(); - String parentId = in.readBoolean() ? in.readString() : null; + String id = helper.readString(in); + String parentId = in.readBoolean() ? helper.readString(in) : null; DisplayData displayData = null; if (in.readBoolean()) { - Component title = DefaultComponentSerializer.get().deserialize(in.readString()); - Component description = DefaultComponentSerializer.get().deserialize(in.readString()); - ItemStack icon = ItemStack.read(in); - FrameType frameType = MagicValues.key(FrameType.class, in.readVarInt()); + Component title = helper.readComponent(in); + Component description = helper.readComponent(in); + ItemStack icon = helper.readItemStack(in); + FrameType frameType = MagicValues.key(FrameType.class, helper.readVarInt(in)); int flags = in.readInt(); boolean hasBackgroundTexture = (flags & FLAG_HAS_BACKGROUND_TEXTURE) != 0; boolean showToast = (flags & FLAG_SHOW_TOAST) != 0; boolean hidden = (flags & FLAG_HIDDEN) != 0; - String backgroundTexture = hasBackgroundTexture ? in.readString() : null; + String backgroundTexture = hasBackgroundTexture ? helper.readString(in) : null; float posX = in.readFloat(); float posY = in.readFloat(); @@ -74,18 +74,18 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { } List criteria = new ArrayList<>(); - int criteriaCount = in.readVarInt(); + int criteriaCount = helper.readVarInt(in); for (int j = 0; j < criteriaCount; j++) { - criteria.add(in.readString()); + criteria.add(helper.readString(in)); } List> requirements = new ArrayList<>(); - int requirementCount = in.readVarInt(); + int requirementCount = helper.readVarInt(in); for (int j = 0; j < requirementCount; j++) { List requirement = new ArrayList<>(); - int componentCount = in.readVarInt(); + int componentCount = helper.readVarInt(in); for (int k = 0; k < componentCount; k++) { - requirement.add(in.readString()); + requirement.add(helper.readString(in)); } requirements.add(requirement); @@ -94,20 +94,20 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { this.advancements[i] = new Advancement(id, criteria, requirements, parentId, displayData); } - this.removedAdvancements = new String[in.readVarInt()]; + this.removedAdvancements = new String[helper.readVarInt(in)]; for (int i = 0; i < this.removedAdvancements.length; i++) { - this.removedAdvancements[i] = in.readString(); + this.removedAdvancements[i] = helper.readString(in); } this.progress = new HashMap<>(); - int progressCount = in.readVarInt(); + int progressCount = helper.readVarInt(in); for (int i = 0; i < progressCount; i++) { - String advancementId = in.readString(); + String advancementId = helper.readString(in); Map advancementProgress = new HashMap<>(); - int criterionCount = in.readVarInt(); + int criterionCount = helper.readVarInt(in); for (int j = 0; j < criterionCount; j++) { - String criterionId = in.readString(); + String criterionId = helper.readString(in); long achievedDate = in.readBoolean() ? in.readLong() : -1; advancementProgress.put(criterionId, achievedDate); } @@ -117,15 +117,15 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.reset); - out.writeVarInt(this.advancements.length); + helper.writeVarInt(out, this.advancements.length); for (Advancement advancement : this.advancements) { - out.writeString(advancement.getId()); + helper.writeString(out, advancement.getId()); if (advancement.getParentId() != null) { out.writeBoolean(true); - out.writeString(advancement.getParentId()); + helper.writeString(out, advancement.getParentId()); } else { out.writeBoolean(false); } @@ -133,10 +133,10 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { DisplayData displayData = advancement.getDisplayData(); if (displayData != null) { out.writeBoolean(true); - out.writeString(DefaultComponentSerializer.get().serialize(displayData.getTitle())); - out.writeString(DefaultComponentSerializer.get().serialize(displayData.getDescription())); - ItemStack.write(out, displayData.getIcon()); - out.writeVarInt(MagicValues.value(Integer.class, displayData.getFrameType())); + helper.writeString(out, DefaultComponentSerializer.get().serialize(displayData.getTitle())); + helper.writeString(out, DefaultComponentSerializer.get().serialize(displayData.getDescription())); + helper.writeItemStack(out, displayData.getIcon()); + helper.writeVarInt(out, MagicValues.value(Integer.class, displayData.getFrameType())); String backgroundTexture = displayData.getBackgroundTexture(); int flags = 0; @@ -155,7 +155,7 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { out.writeInt(flags); if (backgroundTexture != null) { - out.writeString(backgroundTexture); + helper.writeString(out, backgroundTexture); } out.writeFloat(displayData.getPosX()); @@ -164,32 +164,32 @@ public class ClientboundUpdateAdvancementsPacket implements Packet { out.writeBoolean(false); } - out.writeVarInt(advancement.getCriteria().size()); + helper.writeVarInt(out, advancement.getCriteria().size()); for (String criterion : advancement.getCriteria()) { - out.writeString(criterion); + helper.writeString(out, criterion); } - out.writeVarInt(advancement.getRequirements().size()); + helper.writeVarInt(out, advancement.getRequirements().size()); for (List requirement : advancement.getRequirements()) { - out.writeVarInt(requirement.size()); + helper.writeVarInt(out, requirement.size()); for (String criterion : requirement) { - out.writeString(criterion); + helper.writeString(out, criterion); } } } - out.writeVarInt(this.removedAdvancements.length); + helper.writeVarInt(out, this.removedAdvancements.length); for (String id : this.removedAdvancements) { - out.writeString(id); + helper.writeString(out, id); } - out.writeVarInt(this.progress.size()); + helper.writeVarInt(out, this.progress.size()); for (Map.Entry> advancement : this.progress.entrySet()) { - out.writeString(advancement.getKey()); + helper.writeString(out, advancement.getKey()); Map advancementProgress = advancement.getValue(); - out.writeVarInt(advancementProgress.size()); + helper.writeVarInt(out, advancementProgress.size()); for (Map.Entry criterion : advancementProgress.entrySet()) { - out.writeString(criterion.getKey()); + helper.writeString(out, criterion.getKey()); if (criterion.getValue() != -1) { out.writeBoolean(true); out.writeLong(criterion.getValue()); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateRecipesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateRecipesPacket.java index 2c5356bb..b22dedb7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateRecipesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateRecipesPacket.java @@ -1,20 +1,15 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.Identifier; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient; import com.github.steveice10.mc.protocol.data.game.recipe.Recipe; import com.github.steveice10.mc.protocol.data.game.recipe.RecipeType; -import com.github.steveice10.mc.protocol.data.game.recipe.data.CookedRecipeData; -import com.github.steveice10.mc.protocol.data.game.recipe.data.RecipeData; -import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapedRecipeData; -import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapelessRecipeData; -import com.github.steveice10.mc.protocol.data.game.recipe.data.SmithingRecipeData; -import com.github.steveice10.mc.protocol.data.game.recipe.data.StoneCuttingRecipeData; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.data.game.recipe.data.*; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -25,38 +20,38 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundUpdateRecipesPacket implements Packet { +public class ClientboundUpdateRecipesPacket implements MinecraftPacket { private final @NonNull Recipe[] recipes; - public ClientboundUpdateRecipesPacket(NetInput in) throws IOException { - this.recipes = new Recipe[in.readVarInt()]; + public ClientboundUpdateRecipesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.recipes = new Recipe[helper.readVarInt(in)]; for (int i = 0; i < this.recipes.length; i++) { - RecipeType type = MagicValues.key(RecipeType.class, Identifier.formalize(in.readString())); - String identifier = in.readString(); + RecipeType type = MagicValues.key(RecipeType.class, Identifier.formalize(helper.readString(in))); + String identifier = helper.readString(in); RecipeData data = null; switch (type) { case CRAFTING_SHAPELESS: { - String group = in.readString(); - Ingredient[] ingredients = new Ingredient[in.readVarInt()]; + String group = helper.readString(in); + Ingredient[] ingredients = new Ingredient[helper.readVarInt(in)]; for (int j = 0; j < ingredients.length; j++) { - ingredients[j] = this.readIngredient(in); + ingredients[j] = helper.readRecipeIngredient(in); } - ItemStack result = ItemStack.read(in); + ItemStack result = helper.readItemStack(in); data = new ShapelessRecipeData(group, ingredients, result); break; } case CRAFTING_SHAPED: { - int width = in.readVarInt(); - int height = in.readVarInt(); - String group = in.readString(); + int width = helper.readVarInt(in); + int height = helper.readVarInt(in); + String group = helper.readString(in); Ingredient[] ingredients = new Ingredient[width * height]; for (int j = 0; j < ingredients.length; j++) { - ingredients[j] = this.readIngredient(in); + ingredients[j] = helper.readRecipeIngredient(in); } - ItemStack result = ItemStack.read(in); + ItemStack result = helper.readItemStack(in); data = new ShapedRecipeData(width, height, group, ingredients, result); break; @@ -65,27 +60,27 @@ public class ClientboundUpdateRecipesPacket implements Packet { case BLASTING: case SMOKING: case CAMPFIRE_COOKING: { - String group = in.readString(); - Ingredient ingredient = this.readIngredient(in); - ItemStack result = ItemStack.read(in); + String group = helper.readString(in); + Ingredient ingredient = helper.readRecipeIngredient(in); + ItemStack result = helper.readItemStack(in); float experience = in.readFloat(); - int cookingTime = in.readVarInt(); + int cookingTime = helper.readVarInt(in); data = new CookedRecipeData(group, ingredient, result, experience, cookingTime); break; } case STONECUTTING: { - String group = in.readString(); - Ingredient ingredient = this.readIngredient(in); - ItemStack result = ItemStack.read(in); + String group = helper.readString(in); + Ingredient ingredient = helper.readRecipeIngredient(in); + ItemStack result = helper.readItemStack(in); data = new StoneCuttingRecipeData(group, ingredient, result); break; } case SMITHING: { - Ingredient base = this.readIngredient(in); - Ingredient addition = this.readIngredient(in); - ItemStack result = ItemStack.read(in); + Ingredient base = helper.readRecipeIngredient(in); + Ingredient addition = helper.readRecipeIngredient(in); + ItemStack result = helper.readItemStack(in); data = new SmithingRecipeData(base, addition, result); break; @@ -98,32 +93,23 @@ public class ClientboundUpdateRecipesPacket implements Packet { } } - private Ingredient readIngredient(NetInput in) throws IOException { - ItemStack[] options = new ItemStack[in.readVarInt()]; - for (int i = 0; i < options.length; i++) { - options[i] = ItemStack.read(in); - } - - return new Ingredient(options); - } - @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.recipes.length); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.recipes.length); for (Recipe recipe : this.recipes) { - out.writeString(MagicValues.value(String.class, recipe.getType())); - out.writeString(recipe.getIdentifier()); + helper.writeString(out, MagicValues.value(String.class, recipe.getType())); + helper.writeString(out, recipe.getIdentifier()); switch (recipe.getType()) { case CRAFTING_SHAPELESS: { ShapelessRecipeData data = (ShapelessRecipeData) recipe.getData(); - out.writeString(data.getGroup()); - out.writeVarInt(data.getIngredients().length); + helper.writeString(out, data.getGroup()); + helper.writeVarInt(out, data.getIngredients().length); for (Ingredient ingredient : data.getIngredients()) { - this.writeIngredient(out, ingredient); + helper.writeRecipeIngredient(out, ingredient); } - ItemStack.write(out, data.getResult()); + helper.writeItemStack(out, data.getResult()); break; } case CRAFTING_SHAPED: { @@ -132,14 +118,14 @@ public class ClientboundUpdateRecipesPacket implements Packet { throw new IllegalStateException("Shaped recipe must have ingredient count equal to width * height."); } - out.writeVarInt(data.getWidth()); - out.writeVarInt(data.getHeight()); - out.writeString(data.getGroup()); + helper.writeVarInt(out, data.getWidth()); + helper.writeVarInt(out, data.getHeight()); + helper.writeString(out, data.getGroup()); for (Ingredient ingredient : data.getIngredients()) { - this.writeIngredient(out, ingredient); + helper.writeRecipeIngredient(out, ingredient); } - ItemStack.write(out, data.getResult()); + helper.writeItemStack(out, data.getResult()); break; } case SMELTING: @@ -148,27 +134,27 @@ public class ClientboundUpdateRecipesPacket implements Packet { case CAMPFIRE_COOKING: { CookedRecipeData data = (CookedRecipeData) recipe.getData(); - out.writeString(data.getGroup()); - this.writeIngredient(out, data.getIngredient()); - ItemStack.write(out, data.getResult()); + helper.writeString(out, data.getGroup()); + helper.writeRecipeIngredient(out, data.getIngredient()); + helper.writeItemStack(out, data.getResult()); out.writeFloat(data.getExperience()); - out.writeVarInt(data.getCookingTime()); + helper.writeVarInt(out, data.getCookingTime()); break; } case STONECUTTING: { StoneCuttingRecipeData data = (StoneCuttingRecipeData) recipe.getData(); - out.writeString(data.getGroup()); - this.writeIngredient(out, data.getIngredient()); - ItemStack.write(out, data.getResult()); + helper.writeString(out, data.getGroup()); + helper.writeRecipeIngredient(out, data.getIngredient()); + helper.writeItemStack(out, data.getResult()); break; } case SMITHING: { SmithingRecipeData data = (SmithingRecipeData) recipe.getData(); - this.writeIngredient(out, data.getBase()); - this.writeIngredient(out, data.getAddition()); - ItemStack.write(out, data.getResult()); + helper.writeRecipeIngredient(out, data.getBase()); + helper.writeRecipeIngredient(out, data.getAddition()); + helper.writeItemStack(out, data.getResult()); break; } default: @@ -176,11 +162,4 @@ public class ClientboundUpdateRecipesPacket implements Packet { } } } - - private void writeIngredient(NetOutput out, Ingredient ingredient) throws IOException { - out.writeVarInt(ingredient.getOptions().length); - for (ItemStack option : ingredient.getOptions()) { - ItemStack.write(out, option); - } - } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateTagsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateTagsPacket.java index 5a2a00e9..80b28af3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateTagsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/ClientboundUpdateTagsPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.Identifier; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -16,21 +16,21 @@ import java.util.Map; @Data @With @AllArgsConstructor -public class ClientboundUpdateTagsPacket implements Packet { +public class ClientboundUpdateTagsPacket implements MinecraftPacket { private final @NonNull Map> tags = new HashMap<>(); - public ClientboundUpdateTagsPacket(NetInput in) throws IOException { - int totalTagCount = in.readVarInt(); + public ClientboundUpdateTagsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + int totalTagCount = helper.readVarInt(in); for (int i = 0; i < totalTagCount; i++) { Map tag = new HashMap<>(); - String tagName = Identifier.formalize(in.readString()); - int tagsCount = in.readVarInt(); + String tagName = Identifier.formalize(helper.readString(in)); + int tagsCount = helper.readVarInt(in); for (int j = 0; j < tagsCount; j++) { - String name = in.readString(); - int entriesCount = in.readVarInt(); + String name = helper.readString(in); + int entriesCount = helper.readVarInt(in); int[] entries = new int[entriesCount]; for (int index = 0; index < entriesCount; index++) { - entries[index] = in.readVarInt(); + entries[index] = helper.readVarInt(in); } tag.put(name, entries); @@ -40,16 +40,16 @@ public class ClientboundUpdateTagsPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(tags.size()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, tags.size()); for (Map.Entry> tagSet : tags.entrySet()) { - out.writeString(tagSet.getKey()); - out.writeVarInt(tagSet.getValue().size()); + helper.writeString(out, tagSet.getKey()); + helper.writeVarInt(out, tagSet.getValue().size()); for (Map.Entry tag : tagSet.getValue().entrySet()) { - out.writeString(tag.getKey()); - out.writeVarInt(tag.getValue().length); + helper.writeString(out, tag.getKey()); + helper.writeVarInt(out, tag.getValue().length); for (int id : tag.getValue()) { - out.writeVarInt(id); + helper.writeVarInt(out, id); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundAnimatePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundAnimatePacket.java index bd7ed179..b05c131f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundAnimatePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundAnimatePacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.Animation; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +15,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundAnimatePacket implements Packet { +public class ClientboundAnimatePacket implements MinecraftPacket { private final int entityId; private final @NonNull Animation animation; - public ClientboundAnimatePacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundAnimatePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.animation = MagicValues.key(Animation.class, in.readUnsignedByte()); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeByte(MagicValues.value(Integer.class, this.animation)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundEntityEventPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundEntityEventPacket.java index 699fa89f..02782f8c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundEntityEventPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundEntityEventPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.EntityEvent; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundEntityEventPacket implements Packet { +public class ClientboundEntityEventPacket implements MinecraftPacket { private final int entityId; - private final @NonNull EntityEvent status; + private final @NonNull EntityEvent event; - public ClientboundEntityEventPacket(NetInput in) throws IOException { + public ClientboundEntityEventPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.entityId = in.readInt(); - this.status = EntityEvent.read(in); + this.event = helper.readEntityEvent(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.entityId); - out.writeByte(this.status.ordinal()); + helper.writeEntityEvent(out, this.event); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosPacket.java index 1186b5fe..222dfb01 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundMoveEntityPosPacket implements Packet { +public class ClientboundMoveEntityPosPacket implements MinecraftPacket { private final int entityId; private final double moveX; private final double moveY; private final double moveZ; private final boolean onGround; - public ClientboundMoveEntityPosPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundMoveEntityPosPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.moveX = in.readShort() / 4096D; this.moveY = in.readShort() / 4096D; this.moveZ = in.readShort() / 4096D; @@ -28,8 +28,8 @@ public class ClientboundMoveEntityPosPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeShort((int) (this.moveX * 4096)); out.writeShort((int) (this.moveY * 4096)); out.writeShort((int) (this.moveZ * 4096)); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosRotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosRotPacket.java index dfbdf111..38ca6238 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosRotPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityPosRotPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundMoveEntityPosRotPacket implements Packet { +public class ClientboundMoveEntityPosRotPacket implements MinecraftPacket { private final int entityId; private final double moveX; private final double moveY; @@ -21,8 +21,8 @@ public class ClientboundMoveEntityPosRotPacket implements Packet { private final float pitch; private final boolean onGround; - public ClientboundMoveEntityPosRotPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundMoveEntityPosRotPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.moveX = in.readShort() / 4096D; this.moveY = in.readShort() / 4096D; this.moveZ = in.readShort() / 4096D; @@ -32,8 +32,8 @@ public class ClientboundMoveEntityPosRotPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeShort((int) (this.moveX * 4096)); out.writeShort((int) (this.moveY * 4096)); out.writeShort((int) (this.moveZ * 4096)); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityRotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityRotPacket.java index 563f42fe..b59a7997 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityRotPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveEntityRotPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,22 +12,22 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundMoveEntityRotPacket implements Packet { +public class ClientboundMoveEntityRotPacket implements MinecraftPacket { private final int entityId; private final float yaw; private final float pitch; private final boolean onGround; - public ClientboundMoveEntityRotPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundMoveEntityRotPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.yaw = in.readByte() * 360 / 256f; this.pitch = in.readByte() * 360 / 256f; this.onGround = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeByte((byte) (this.yaw * 256 / 360)); out.writeByte((byte) (this.pitch * 256 / 360)); out.writeBoolean(this.onGround); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveVehiclePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveVehiclePacket.java index 968763a3..052b3aca 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveVehiclePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundMoveVehiclePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,14 +12,14 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundMoveVehiclePacket implements Packet { +public class ClientboundMoveVehiclePacket implements MinecraftPacket { private final double x; private final double y; private final double z; private final float yaw; private final float pitch; - public ClientboundMoveVehiclePacket(NetInput in) throws IOException { + public ClientboundMoveVehiclePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -28,7 +28,7 @@ public class ClientboundMoveVehiclePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveEntitiesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveEntitiesPacket.java index 9a3e1d92..b75e38f9 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveEntitiesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveEntitiesPacket.java @@ -1,33 +1,33 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; +import org.jetbrains.annotations.NotNull; -import javax.annotation.Nonnull; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundRemoveEntitiesPacket implements Packet { - private final @Nonnull int[] entityIds; +public class ClientboundRemoveEntitiesPacket implements MinecraftPacket { + private final @NotNull int[] entityIds; - public ClientboundRemoveEntitiesPacket(NetInput in) throws IOException { - this.entityIds = new int[in.readVarInt()]; + public ClientboundRemoveEntitiesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityIds = new int[helper.readVarInt(in)]; for (int i = 0; i < this.entityIds.length; i++) { - this.entityIds[i] = in.readVarInt(); + this.entityIds[i] = helper.readVarInt(in); } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityIds.length); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityIds.length); for (int entityId : this.entityIds) { - out.writeVarInt(entityId); + helper.writeVarInt(out, entityId); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveMobEffectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveMobEffectPacket.java index 53e617a1..96010b48 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveMobEffectPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRemoveMobEffectPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.Effect; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundRemoveMobEffectPacket implements Packet { +public class ClientboundRemoveMobEffectPacket implements MinecraftPacket { private final int entityId; private final @NonNull Effect effect; - public ClientboundRemoveMobEffectPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.effect = Effect.fromNetworkId(in.readVarInt()); + public ClientboundRemoveMobEffectPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.effect = helper.readEffect(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeVarInt(Effect.toNetworkId(this.effect)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeEffect(out, this.effect); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRotateHeadPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRotateHeadPacket.java index 7cbb6820..ddc4d0ac 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRotateHeadPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundRotateHeadPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundRotateHeadPacket implements Packet { +public class ClientboundRotateHeadPacket implements MinecraftPacket { private final int entityId; private final float headYaw; - public ClientboundRotateHeadPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundRotateHeadPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.headYaw = in.readByte() * 360 / 256f; } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeByte((byte) (this.headYaw * 256 / 360)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityDataPacket.java index cd335986..3e045d48 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityDataPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityDataPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetEntityDataPacket implements Packet { +public class ClientboundSetEntityDataPacket implements MinecraftPacket { private final int entityId; private final @NonNull EntityMetadata[] metadata; - public ClientboundSetEntityDataPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.metadata = EntityMetadata.read(in); + public ClientboundSetEntityDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.metadata = helper.readEntityMetadata(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - EntityMetadata.write(out, this.metadata); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeEntityMetadata(out, this.metadata); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityLinkPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityLinkPacket.java index c059c306..8081ec0d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityLinkPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityLinkPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,17 +12,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetEntityLinkPacket implements Packet { +public class ClientboundSetEntityLinkPacket implements MinecraftPacket { private final int entityId; private final int attachedToId; - public ClientboundSetEntityLinkPacket(NetInput in) throws IOException { + public ClientboundSetEntityLinkPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.entityId = in.readInt(); this.attachedToId = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.entityId); out.writeInt(this.attachedToId); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityMotionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityMotionPacket.java index 05de8813..a599b8dc 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityMotionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEntityMotionPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,22 +12,22 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetEntityMotionPacket implements Packet { +public class ClientboundSetEntityMotionPacket implements MinecraftPacket { private final int entityId; private final double motionX; private final double motionY; private final double motionZ; - public ClientboundSetEntityMotionPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundSetEntityMotionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.motionX = in.readShort() / 8000D; this.motionY = in.readShort() / 8000D; this.motionZ = in.readShort() / 8000D; } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeShort((int) (this.motionX * 8000)); out.writeShort((int) (this.motionY * 8000)); out.writeShort((int) (this.motionZ * 8000)); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEquipmentPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEquipmentPacket.java index c3b2994c..a93f69f2 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEquipmentPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetEquipmentPacket.java @@ -1,12 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.EquipmentSlot; import com.github.steveice10.mc.protocol.data.game.entity.metadata.Equipment; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -19,18 +19,18 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ClientboundSetEquipmentPacket implements Packet { +public class ClientboundSetEquipmentPacket implements MinecraftPacket { private final int entityId; private final @NonNull Equipment[] equipment; - public ClientboundSetEquipmentPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundSetEquipmentPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); boolean hasNextEntry = true; List list = new ArrayList<>(); while (hasNextEntry) { int rawSlot = in.readByte(); EquipmentSlot slot = MagicValues.key(EquipmentSlot.class, ((byte) rawSlot) & 127); - ItemStack item = ItemStack.read(in); + ItemStack item = helper.readItemStack(in); list.add(new Equipment(slot, item)); hasNextEntry = (rawSlot & 128) == 128; } @@ -38,15 +38,15 @@ public class ClientboundSetEquipmentPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); for (int i = 0; i < this.equipment.length; i++) { int rawSlot = MagicValues.value(Integer.class, this.equipment[i].getSlot()); if (i != equipment.length - 1) { rawSlot = rawSlot | 128; } out.writeByte(rawSlot); - ItemStack.write(out, this.equipment[i].getItem()); + helper.writeItemStack(out, this.equipment[i].getItem()); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetPassengersPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetPassengersPacket.java index a57bb719..eaf0fe47 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetPassengersPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundSetPassengersPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,24 +13,24 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetPassengersPacket implements Packet { +public class ClientboundSetPassengersPacket implements MinecraftPacket { private final int entityId; private final @NonNull int[] passengerIds; - public ClientboundSetPassengersPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.passengerIds = new int[in.readVarInt()]; + public ClientboundSetPassengersPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.passengerIds = new int[helper.readVarInt(in)]; for (int index = 0; index < this.passengerIds.length; index++) { - this.passengerIds[index] = in.readVarInt(); + this.passengerIds[index] = helper.readVarInt(in); } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeVarInt(this.passengerIds.length); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeVarInt(out, this.passengerIds.length); for (int entityId : this.passengerIds) { - out.writeVarInt(entityId); + helper.writeVarInt(out, entityId); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTakeItemEntityPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTakeItemEntityPacket.java index c58e6400..d90523ff 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTakeItemEntityPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTakeItemEntityPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,21 +12,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundTakeItemEntityPacket implements Packet { +public class ClientboundTakeItemEntityPacket implements MinecraftPacket { private final int collectedEntityId; private final int collectorEntityId; private final int itemCount; - public ClientboundTakeItemEntityPacket(NetInput in) throws IOException { - this.collectedEntityId = in.readVarInt(); - this.collectorEntityId = in.readVarInt(); - this.itemCount = in.readVarInt(); + public ClientboundTakeItemEntityPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.collectedEntityId = helper.readVarInt(in); + this.collectorEntityId = helper.readVarInt(in); + this.itemCount = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.collectedEntityId); - out.writeVarInt(this.collectorEntityId); - out.writeVarInt(this.itemCount); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.collectedEntityId); + helper.writeVarInt(out, this.collectorEntityId); + helper.writeVarInt(out, this.itemCount); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTeleportEntityPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTeleportEntityPacket.java index 2fc9bfa4..a12d1779 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTeleportEntityPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundTeleportEntityPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundTeleportEntityPacket implements Packet { +public class ClientboundTeleportEntityPacket implements MinecraftPacket { private final int entityId; private final double x; private final double y; @@ -21,8 +21,8 @@ public class ClientboundTeleportEntityPacket implements Packet { private final float pitch; private final boolean onGround; - public ClientboundTeleportEntityPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundTeleportEntityPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -32,8 +32,8 @@ public class ClientboundTeleportEntityPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateAttributesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateAttributesPacket.java index b371a58e..73f9f40b 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateAttributesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateAttributesPacket.java @@ -1,13 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.Identifier; import com.github.steveice10.mc.protocol.data.game.entity.attribute.Attribute; import com.github.steveice10.mc.protocol.data.game.entity.attribute.AttributeModifier; import com.github.steveice10.mc.protocol.data.game.entity.attribute.AttributeType; -import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierOperation; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -20,21 +19,21 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ClientboundUpdateAttributesPacket implements Packet { +public class ClientboundUpdateAttributesPacket implements MinecraftPacket { private final int entityId; private final @NonNull List attributes; - public ClientboundUpdateAttributesPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundUpdateAttributesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.attributes = new ArrayList<>(); - int length = in.readVarInt(); + int length = helper.readVarInt(in); for (int index = 0; index < length; index++) { - String key = in.readString(); + String key = helper.readString(in); double value = in.readDouble(); List modifiers = new ArrayList<>(); - int len = in.readVarInt(); + int len = helper.readVarInt(in); for (int ind = 0; ind < len; ind++) { - modifiers.add(new AttributeModifier(in.readUUID(), in.readDouble(), ModifierOperation.read(in))); + modifiers.add(new AttributeModifier(helper.readUUID(in), in.readDouble(), helper.readModifierOperation(in))); } AttributeType type = AttributeType.Builtin.BUILTIN.computeIfAbsent(Identifier.formalize(key), AttributeType.Custom::new); @@ -43,17 +42,17 @@ public class ClientboundUpdateAttributesPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeVarInt(this.attributes.size()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeVarInt(out, this.attributes.size()); for (Attribute attribute : this.attributes) { - out.writeString(attribute.getType().getIdentifier()); + helper.writeString(out, attribute.getType().getIdentifier()); out.writeDouble(attribute.getValue()); - out.writeVarInt(attribute.getModifiers().size()); + helper.writeVarInt(out, attribute.getModifiers().size()); for (AttributeModifier modifier : attribute.getModifiers()) { - out.writeUUID(modifier.getUuid()); + helper.writeUUID(out, modifier.getUuid()); out.writeDouble(modifier.getAmount()); - out.writeByte(modifier.getOperation().ordinal()); + helper.writeModifierOperation(out, modifier.getOperation()); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java index 111bdbc2..8c576fc1 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/ClientboundUpdateMobEffectPacket.java @@ -1,23 +1,22 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity; -import com.github.steveice10.mc.protocol.data.game.NBT; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.Effect; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundUpdateMobEffectPacket implements Packet { +public class ClientboundUpdateMobEffectPacket implements MinecraftPacket { private static final int FLAG_AMBIENT = 0x01; private static final int FLAG_SHOW_PARTICLES = 0x02; @@ -29,28 +28,28 @@ public class ClientboundUpdateMobEffectPacket implements Packet { private final boolean showParticles; private final @Nullable CompoundTag factorData; - public ClientboundUpdateMobEffectPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.effect = Effect.fromNetworkId(in.readVarInt()); + public ClientboundUpdateMobEffectPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.effect = helper.readEffect(in); this.amplifier = in.readByte(); - this.duration = in.readVarInt(); + this.duration = helper.readVarInt(in); int flags = in.readByte(); this.ambient = (flags & FLAG_AMBIENT) != 0; this.showParticles = (flags & FLAG_SHOW_PARTICLES) != 0; if (in.readBoolean()) { - this.factorData = NBT.read(in); + this.factorData = helper.readTag(in); } else { this.factorData = null; } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeVarInt(Effect.toNetworkId(this.effect)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeEffect(out, this.effect); out.writeByte(this.amplifier); - out.writeVarInt(this.duration); + helper.writeVarInt(out, this.duration); int flags = 0; if (this.ambient) { @@ -64,7 +63,7 @@ public class ClientboundUpdateMobEffectPacket implements Packet { out.writeByte(flags); out.writeBoolean(this.factorData != null); if (this.factorData != null) { - NBT.write(out, this.factorData); + helper.writeTag(out, this.factorData); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundBlockChangedAckPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundBlockChangedAckPacket.java index 35b6a66d..5859a5cf 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundBlockChangedAckPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundBlockChangedAckPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundBlockChangedAckPacket implements Packet { +public class ClientboundBlockChangedAckPacket implements MinecraftPacket { private final int sequence; - public ClientboundBlockChangedAckPacket(NetInput in) throws IOException { - this.sequence = in.readVarInt(); + public ClientboundBlockChangedAckPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.sequence = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.sequence); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.sequence); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerAbilitiesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerAbilitiesPacket.java index 87b6c1e4..9471aa87 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerAbilitiesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerAbilitiesPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPlayerAbilitiesPacket implements Packet { +public class ClientboundPlayerAbilitiesPacket implements MinecraftPacket { private static final int FLAG_INVINCIBLE = 0x01; private static final int FLAG_FLYING = 0x02; private static final int FLAG_CAN_FLY = 0x04; @@ -25,7 +25,7 @@ public class ClientboundPlayerAbilitiesPacket implements Packet { private final float flySpeed; private final float walkSpeed; - public ClientboundPlayerAbilitiesPacket(NetInput in) throws IOException { + public ClientboundPlayerAbilitiesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { byte flags = in.readByte(); this.invincible = (flags & FLAG_INVINCIBLE) > 0; this.canFly = (flags & FLAG_CAN_FLY) > 0; @@ -37,7 +37,7 @@ public class ClientboundPlayerAbilitiesPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { int flags = 0; if (this.invincible) { flags |= FLAG_INVINCIBLE; diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEndPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEndPacket.java index a47fedf2..1c675ca4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEndPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEndPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPlayerCombatEndPacket implements Packet { +public class ClientboundPlayerCombatEndPacket implements MinecraftPacket { private final int killerId; private final int duration; - public ClientboundPlayerCombatEndPacket(NetInput in) throws IOException { - this.duration = in.readVarInt(); + public ClientboundPlayerCombatEndPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.duration = helper.readVarInt(in); this.killerId = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.duration); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.duration); out.writeInt(this.killerId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEnterPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEnterPacket.java index 69d80f8f..35c9730a 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEnterPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatEnterPacket.java @@ -1,23 +1,25 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; -import lombok.*; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.With; import java.io.IOException; @Data @With @NoArgsConstructor -public class ClientboundPlayerCombatEnterPacket implements Packet { +public class ClientboundPlayerCombatEnterPacket implements MinecraftPacket { - public ClientboundPlayerCombatEnterPacket(NetInput in) { + public ClientboundPlayerCombatEnterPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { // no-op } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { // no-op } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatKillPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatKillPacket.java index e29d4f7b..6b8379f9 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatKillPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerCombatKillPacket.java @@ -1,9 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -14,21 +13,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPlayerCombatKillPacket implements Packet { +public class ClientboundPlayerCombatKillPacket implements MinecraftPacket { private final int playerId; private final int killerId; private final Component message; - public ClientboundPlayerCombatKillPacket(NetInput in) throws IOException { - this.playerId = in.readVarInt(); + public ClientboundPlayerCombatKillPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.playerId = helper.readVarInt(in); this.killerId = in.readInt(); - this.message = DefaultComponentSerializer.get().deserialize(in.readString()); + this.message = helper.readComponent(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.playerId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.playerId); out.writeInt(this.killerId); - out.writeString(DefaultComponentSerializer.get().serialize(this.message)); + helper.writeComponent(out, this.message); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerLookAtPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerLookAtPacket.java index 2fef1db4..45b78858 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerLookAtPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerLookAtPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.RotationOrigin; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,7 +15,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPlayerLookAtPacket implements Packet { +public class ClientboundPlayerLookAtPacket implements MinecraftPacket { private final @NonNull RotationOrigin origin; private final double x; private final double y; @@ -28,15 +28,15 @@ public class ClientboundPlayerLookAtPacket implements Packet { this(origin, x, y, z, 0, null); } - public ClientboundPlayerLookAtPacket(NetInput in) throws IOException { - this.origin = MagicValues.key(RotationOrigin.class, in.readVarInt()); + public ClientboundPlayerLookAtPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.origin = MagicValues.key(RotationOrigin.class, helper.readVarInt(in)); this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); if (in.readBoolean()) { - this.targetEntityId = in.readVarInt(); - this.targetEntityOrigin = MagicValues.key(RotationOrigin.class, in.readVarInt()); + this.targetEntityId = helper.readVarInt(in); + this.targetEntityOrigin = MagicValues.key(RotationOrigin.class, helper.readVarInt(in)); } else { this.targetEntityId = 0; this.targetEntityOrigin = null; @@ -44,16 +44,16 @@ public class ClientboundPlayerLookAtPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.origin)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.origin)); out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); if (this.targetEntityOrigin != null) { out.writeBoolean(true); - out.writeVarInt(this.targetEntityId); - out.writeVarInt(MagicValues.value(Integer.class, this.targetEntityOrigin)); + helper.writeVarInt(out, this.targetEntityId); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.targetEntityOrigin)); } else { out.writeBoolean(false); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerPositionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerPositionPacket.java index 23959e3d..17ffa863 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerPositionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundPlayerPositionPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.PositionElement; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -18,7 +18,7 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ClientboundPlayerPositionPacket implements Packet { +public class ClientboundPlayerPositionPacket implements MinecraftPacket { private final double x; private final double y; private final double z; @@ -32,7 +32,7 @@ public class ClientboundPlayerPositionPacket implements Packet { this(x, y, z, yaw, pitch, teleportId, dismountVehicle, Arrays.asList(relative != null ? relative : new PositionElement[0])); } - public ClientboundPlayerPositionPacket(NetInput in) throws IOException { + public ClientboundPlayerPositionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -48,12 +48,12 @@ public class ClientboundPlayerPositionPacket implements Packet { } } - this.teleportId = in.readVarInt(); + this.teleportId = helper.readVarInt(in); this.dismountVehicle = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); @@ -67,7 +67,7 @@ public class ClientboundPlayerPositionPacket implements Packet { out.writeByte(flags); - out.writeVarInt(this.teleportId); + helper.writeVarInt(out, this.teleportId); out.writeBoolean(this.dismountVehicle); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetCarriedItemPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetCarriedItemPacket.java index a714c287..a2499039 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetCarriedItemPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetCarriedItemPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetCarriedItemPacket implements Packet { +public class ClientboundSetCarriedItemPacket implements MinecraftPacket { private final int slot; - public ClientboundSetCarriedItemPacket(NetInput in) throws IOException { + public ClientboundSetCarriedItemPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.slot = in.readByte(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.slot); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetExperiencePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetExperiencePacket.java index 0e84c0b7..30b918eb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetExperiencePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetExperiencePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,21 +12,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetExperiencePacket implements Packet { +public class ClientboundSetExperiencePacket implements MinecraftPacket { private final float experience; private final int level; private final int totalExperience; - public ClientboundSetExperiencePacket(NetInput in) throws IOException { + public ClientboundSetExperiencePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.experience = in.readFloat(); - this.level = in.readVarInt(); - this.totalExperience = in.readVarInt(); + this.level = helper.readVarInt(in); + this.totalExperience = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeFloat(this.experience); - out.writeVarInt(this.level); - out.writeVarInt(this.totalExperience); + helper.writeVarInt(out, this.level); + helper.writeVarInt(out, this.totalExperience); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetHealthPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetHealthPacket.java index f77b0b6d..d3913595 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetHealthPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/player/ClientboundSetHealthPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,21 +12,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetHealthPacket implements Packet { +public class ClientboundSetHealthPacket implements MinecraftPacket { private final float health; private final int food; private final float saturation; - public ClientboundSetHealthPacket(NetInput in) throws IOException { + public ClientboundSetHealthPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.health = in.readFloat(); - this.food = in.readVarInt(); + this.food = helper.readVarInt(in); this.saturation = in.readFloat(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeFloat(this.health); - out.writeVarInt(this.food); + helper.writeVarInt(out, this.food); out.writeFloat(this.saturation); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddEntityPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddEntityPacket.java index 271b94fd..f0a91bde 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddEntityPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddEntityPacket.java @@ -1,17 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.spawn; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; -import com.github.steveice10.mc.protocol.data.game.entity.object.FallingBlockData; -import com.github.steveice10.mc.protocol.data.game.entity.object.GenericObjectData; -import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType; -import com.github.steveice10.mc.protocol.data.game.entity.object.ObjectData; -import com.github.steveice10.mc.protocol.data.game.entity.object.ProjectileData; -import com.github.steveice10.mc.protocol.data.game.entity.object.SplashPotionData; +import com.github.steveice10.mc.protocol.data.game.entity.object.*; import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -23,7 +17,7 @@ import java.util.UUID; @Data @With @AllArgsConstructor -public class ClientboundAddEntityPacket implements Packet { +public class ClientboundAddEntityPacket implements MinecraftPacket { private static final GenericObjectData EMPTY_DATA = new GenericObjectData(0); private final int entityId; @@ -56,10 +50,10 @@ public class ClientboundAddEntityPacket implements Packet { this(entityId, uuid, type, EMPTY_DATA, x, y, z, yaw, headYaw, pitch, motionX, motionY, motionZ); } - public ClientboundAddEntityPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.uuid = in.readUUID(); - this.type = EntityType.read(in); + public ClientboundAddEntityPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.uuid = helper.readUUID(in); + this.type = EntityType.from(helper.readVarInt(in)); this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -67,7 +61,7 @@ public class ClientboundAddEntityPacket implements Packet { this.yaw = in.readByte() * 360 / 256f; this.headYaw = in.readByte() * 360 / 256f; - int data = in.readVarInt(); + int data = helper.readVarInt(in); if (this.type == EntityType.MINECART) { this.data = MagicValues.key(MinecartType.class, data); } else if (this.type == EntityType.ITEM_FRAME || this.type == EntityType.GLOW_ITEM_FRAME || this.type == EntityType.PAINTING) { @@ -93,10 +87,10 @@ public class ClientboundAddEntityPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeUUID(this.uuid); - out.writeVarInt(this.type.ordinal()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeUUID(out, this.uuid); + helper.writeVarInt(out, this.type.ordinal()); out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); @@ -119,7 +113,7 @@ public class ClientboundAddEntityPacket implements Packet { data = ((GenericObjectData) this.data).getValue(); } - out.writeVarInt(data); + helper.writeVarInt(out, data); out.writeShort((int) (this.motionX * 8000)); out.writeShort((int) (this.motionY * 8000)); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddExperienceOrbPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddExperienceOrbPacket.java index 286fa6d7..9b39894c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddExperienceOrbPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddExperienceOrbPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.spawn; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundAddExperienceOrbPacket implements Packet { +public class ClientboundAddExperienceOrbPacket implements MinecraftPacket { private final int entityId; private final double x; private final double y; private final double z; private final int exp; - public ClientboundAddExperienceOrbPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); + public ClientboundAddExperienceOrbPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -28,8 +28,8 @@ public class ClientboundAddExperienceOrbPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddPlayerPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddPlayerPacket.java index 56fdbd9d..012f64a4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddPlayerPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/entity/spawn/ClientboundAddPlayerPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.spawn; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,7 +14,7 @@ import java.util.UUID; @Data @With @AllArgsConstructor -public class ClientboundAddPlayerPacket implements Packet { +public class ClientboundAddPlayerPacket implements MinecraftPacket { private final int entityId; private final @NonNull UUID uuid; private final double x; @@ -23,9 +23,9 @@ public class ClientboundAddPlayerPacket implements Packet { private final float yaw; private final float pitch; - public ClientboundAddPlayerPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.uuid = in.readUUID(); + public ClientboundAddPlayerPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.uuid = helper.readUUID(in); this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -34,9 +34,9 @@ public class ClientboundAddPlayerPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeUUID(this.uuid); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeUUID(out, this.uuid); out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerClosePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerClosePacket.java index 3e57b24c..bc32f27c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerClosePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerClosePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundContainerClosePacket implements Packet { +public class ClientboundContainerClosePacket implements MinecraftPacket { private final int containerId; - public ClientboundContainerClosePacket(NetInput in) throws IOException { + public ClientboundContainerClosePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readUnsignedByte(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetContentPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetContentPacket.java index 4f86813d..3020579c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetContentPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetContentPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,30 +14,30 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundContainerSetContentPacket implements Packet { +public class ClientboundContainerSetContentPacket implements MinecraftPacket { private final int containerId; private final int stateId; private final @NonNull ItemStack[] items; private final ItemStack carriedItem; - public ClientboundContainerSetContentPacket(NetInput in) throws IOException { + public ClientboundContainerSetContentPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readUnsignedByte(); - this.stateId = in.readVarInt(); - this.items = new ItemStack[in.readVarInt()]; + this.stateId = helper.readVarInt(in); + this.items = new ItemStack[helper.readVarInt(in)]; for (int index = 0; index < this.items.length; index++) { - this.items[index] = ItemStack.read(in); + this.items[index] = helper.readItemStack(in); } - this.carriedItem = ItemStack.read(in); + this.carriedItem = helper.readItemStack(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); - out.writeVarInt(this.stateId); - out.writeVarInt(this.items.length); + helper.writeVarInt(out, this.stateId); + helper.writeVarInt(out, this.items.length); for (ItemStack item : this.items) { - ItemStack.write(out, item); + helper.writeItemStack(out, item); } - ItemStack.write(out, this.carriedItem); + helper.writeItemStack(out, this.carriedItem); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetDataPacket.java index e4f1c9a0..57b7c0ed 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetDataPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetDataPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.inventory.property.ContainerProperty; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -14,7 +14,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundContainerSetDataPacket implements Packet { +public class ClientboundContainerSetDataPacket implements MinecraftPacket { private final int containerId; private final int rawProperty; private final int value; @@ -27,14 +27,14 @@ public class ClientboundContainerSetDataPacket implements Packet { return MagicValues.key(type, this.value); } - public ClientboundContainerSetDataPacket(NetInput in) throws IOException { + public ClientboundContainerSetDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readUnsignedByte(); this.rawProperty = in.readShort(); this.value = in.readShort(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); out.writeShort(this.rawProperty); out.writeShort(this.value); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetSlotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetSlotPacket.java index 4d577e64..9c2aeb84 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetSlotPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundContainerSetSlotPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -13,24 +13,24 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundContainerSetSlotPacket implements Packet { +public class ClientboundContainerSetSlotPacket implements MinecraftPacket { private final int containerId; private final int stateId; private final int slot; private final ItemStack item; - public ClientboundContainerSetSlotPacket(NetInput in) throws IOException { + public ClientboundContainerSetSlotPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readUnsignedByte(); - this.stateId = in.readVarInt(); + this.stateId = helper.readVarInt(in); this.slot = in.readShort(); - this.item = ItemStack.read(in); + this.item = helper.readItemStack(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); - out.writeVarInt(this.stateId); + helper.writeVarInt(out, this.stateId); out.writeShort(this.slot); - ItemStack.write(out, this.item); + helper.writeItemStack(out, this.item); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundHorseScreenOpenPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundHorseScreenOpenPacket.java index c9855b22..fab955fd 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundHorseScreenOpenPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundHorseScreenOpenPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,21 +12,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundHorseScreenOpenPacket implements Packet { +public class ClientboundHorseScreenOpenPacket implements MinecraftPacket { private final int containerId; private final int numberOfSlots; private final int entityId; - public ClientboundHorseScreenOpenPacket(NetInput in) throws IOException { + public ClientboundHorseScreenOpenPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readByte(); - this.numberOfSlots = in.readVarInt(); + this.numberOfSlots = helper.readVarInt(in); this.entityId = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); - out.writeVarInt(this.numberOfSlots); + helper.writeVarInt(out, this.numberOfSlots); out.writeInt(this.entityId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundMerchantOffersPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundMerchantOffersPacket.java index 4b6e3cd9..946ed577 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundMerchantOffersPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundMerchantOffersPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.inventory.VillagerTrade; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,7 +15,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundMerchantOffersPacket implements Packet { +public class ClientboundMerchantOffersPacket implements MinecraftPacket { private final int containerId; private final @NonNull VillagerTrade[] trades; private final int villagerLevel; @@ -23,18 +23,18 @@ public class ClientboundMerchantOffersPacket implements Packet { private final boolean regularVillager; private final boolean canRestock; - public ClientboundMerchantOffersPacket(NetInput in) throws IOException { - this.containerId = in.readVarInt(); + public ClientboundMerchantOffersPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.containerId = helper.readVarInt(in); byte size = in.readByte(); this.trades = new VillagerTrade[size]; for (int i = 0; i < trades.length; i++) { - ItemStack firstInput = ItemStack.read(in); - ItemStack output = ItemStack.read(in); + ItemStack firstInput = helper.readItemStack(in); + ItemStack output = helper.readItemStack(in); ItemStack secondInput = null; if (in.readBoolean()) { - secondInput = ItemStack.read(in); + secondInput = helper.readItemStack(in); } boolean tradeDisabled = in.readBoolean(); @@ -48,27 +48,27 @@ public class ClientboundMerchantOffersPacket implements Packet { this.trades[i] = new VillagerTrade(firstInput, secondInput, output, tradeDisabled, numUses, maxUses, xp, specialPrice, priceMultiplier, demand); } - this.villagerLevel = in.readVarInt(); - this.experience = in.readVarInt(); + this.villagerLevel = helper.readVarInt(in); + this.experience = helper.readVarInt(in); this.regularVillager = in.readBoolean(); this.canRestock = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.containerId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.containerId); out.writeByte(this.trades.length); for (int i = 0; i < this.trades.length; i++) { VillagerTrade trade = this.trades[i]; - ItemStack.write(out, trade.getFirstInput()); - ItemStack.write(out, trade.getOutput()); + helper.writeItemStack(out, trade.getFirstInput()); + helper.writeItemStack(out, trade.getOutput()); boolean hasSecondItem = trade.getSecondInput() != null; out.writeBoolean(hasSecondItem); if (hasSecondItem) { - ItemStack.write(out, trade.getSecondInput()); + helper.writeItemStack(out, trade.getSecondInput()); } out.writeBoolean(trade.isTradeDisabled()); @@ -80,8 +80,8 @@ public class ClientboundMerchantOffersPacket implements Packet { out.writeInt(trade.getDemand()); } - out.writeVarInt(this.villagerLevel); - out.writeVarInt(this.experience); + helper.writeVarInt(out, this.villagerLevel); + helper.writeVarInt(out, this.experience); out.writeBoolean(this.regularVillager); out.writeBoolean(this.canRestock); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenBookPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenBookPacket.java index 32db655d..79cc7306 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenBookPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenBookPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,15 +15,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundOpenBookPacket implements Packet { +public class ClientboundOpenBookPacket implements MinecraftPacket { private final @NonNull Hand hand; - public ClientboundOpenBookPacket(NetInput in) throws IOException { - this.hand = MagicValues.key(Hand.class, in.readVarInt()); + public ClientboundOpenBookPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.hand = MagicValues.key(Hand.class, helper.readVarInt(in)); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, hand)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, hand)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenScreenPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenScreenPacket.java index 867ce627..20025062 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenScreenPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundOpenScreenPacket.java @@ -1,11 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -17,15 +17,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundOpenScreenPacket implements Packet { +public class ClientboundOpenScreenPacket implements MinecraftPacket { private final int containerId; private final @NonNull ContainerType type; private final @NonNull Component title; - public ClientboundOpenScreenPacket(NetInput in) throws IOException { - this.containerId = in.readVarInt(); - this.type = MagicValues.key(ContainerType.class, in.readVarInt()); - this.title = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundOpenScreenPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.containerId = helper.readVarInt(in); + this.type = MagicValues.key(ContainerType.class, helper.readVarInt(in)); + this.title = helper.readComponent(in); } @Deprecated @@ -36,10 +36,10 @@ public class ClientboundOpenScreenPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.containerId); - out.writeVarInt(MagicValues.value(Integer.class, this.type)); - out.writeString(DefaultComponentSerializer.get().serialize(this.title)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.containerId); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.type)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.title)); } @Deprecated diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundPlaceGhostRecipePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundPlaceGhostRecipePacket.java index 62699f04..f949cc2d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundPlaceGhostRecipePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/inventory/ClientboundPlaceGhostRecipePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,18 +13,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPlaceGhostRecipePacket implements Packet { +public class ClientboundPlaceGhostRecipePacket implements MinecraftPacket { private final int containerId; private final @NonNull String recipeId; - public ClientboundPlaceGhostRecipePacket(NetInput in) throws IOException { + public ClientboundPlaceGhostRecipePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readByte(); - this.recipeId = in.readString(); + this.recipeId = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); - out.writeString(this.recipeId); + helper.writeString(out, this.recipeId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockDestructionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockDestructionPacket.java index 900c53f8..6a465871 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockDestructionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockDestructionPacket.java @@ -1,11 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -16,26 +15,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundBlockDestructionPacket implements Packet { +public class ClientboundBlockDestructionPacket implements MinecraftPacket { private final int breakerEntityId; private final @NonNull Vector3i position; private final @NonNull BlockBreakStage stage; - public ClientboundBlockDestructionPacket(NetInput in) throws IOException { - this.breakerEntityId = in.readVarInt(); - this.position = Position.read(in); - int stage = in.readUnsignedByte(); - if (stage >= 0 && stage < 10) { - this.stage = BlockBreakStage.STAGES[stage]; - } else { - this.stage = BlockBreakStage.RESET; - } + public ClientboundBlockDestructionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.breakerEntityId = helper.readVarInt(in); + this.position = helper.readPosition(in); + this.stage = helper.readBlockBreakStage(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.breakerEntityId); - Position.write(out, this.position); - this.stage.write(out); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.breakerEntityId); + helper.writePosition(out, this.position); + helper.writeBlockBreakStage(out, this.stage); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java index c00ccff7..77136bd1 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEntityDataPacket.java @@ -1,39 +1,37 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.NBT; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundBlockEntityDataPacket implements Packet { +public class ClientboundBlockEntityDataPacket implements MinecraftPacket { private final @NonNull Vector3i position; private final BlockEntityType type; private final @Nullable CompoundTag nbt; - public ClientboundBlockEntityDataPacket(NetInput in) throws IOException { - this.position = Position.read(in); - this.type = BlockEntityType.read(in); - this.nbt = NBT.read(in); + public ClientboundBlockEntityDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); + this.type = helper.readBlockEntityType(in); + this.nbt = helper.readTag(in); } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); - BlockEntityType.write(out, this.type); - NBT.write(out, this.nbt); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); + helper.writeBlockEntityType(out, this.type); + helper.writeTag(out, this.nbt); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java index 90a3b23c..a6b8f614 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockEventPacket.java @@ -1,25 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.mc.protocol.data.game.level.block.value.BlockValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.BlockValueType; -import com.github.steveice10.mc.protocol.data.game.level.block.value.ChestValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.ChestValueType; -import com.github.steveice10.mc.protocol.data.game.level.block.value.EndGatewayValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.EndGatewayValueType; -import com.github.steveice10.mc.protocol.data.game.level.block.value.GenericBlockValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.GenericBlockValueType; -import com.github.steveice10.mc.protocol.data.game.level.block.value.MobSpawnerValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.MobSpawnerValueType; -import com.github.steveice10.mc.protocol.data.game.level.block.value.NoteBlockValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.NoteBlockValueType; -import com.github.steveice10.mc.protocol.data.game.level.block.value.PistonValue; -import com.github.steveice10.mc.protocol.data.game.level.block.value.PistonValueType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.data.game.level.block.value.*; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -30,7 +16,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundBlockEventPacket implements Packet { +public class ClientboundBlockEventPacket implements MinecraftPacket { private static final int NOTE_BLOCK = 89; private static final int STICKY_PISTON = 108; private static final int PISTON = 115; @@ -47,12 +33,13 @@ public class ClientboundBlockEventPacket implements Packet { private final @NonNull BlockValue value; private final int blockId; - public ClientboundBlockEventPacket(NetInput in) throws IOException { - this.position = Position.read(in); + public ClientboundBlockEventPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); int type = in.readUnsignedByte(); int value = in.readUnsignedByte(); - this.blockId = in.readVarInt() & 0xFFF; + this.blockId = helper.readVarInt(in) & 0xFFF; + // TODO: Handle this in MinecraftCodecHelper if (this.blockId == NOTE_BLOCK) { this.type = MagicValues.key(NoteBlockValueType.class, type); this.value = new NoteBlockValue(value); @@ -76,8 +63,9 @@ public class ClientboundBlockEventPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { int val = 0; + // TODO: Handle this in MinecraftCodecHelper if (this.type instanceof NoteBlockValueType) { val = ((NoteBlockValue) this.value).getPitch(); } else if (this.type instanceof PistonValueType) { @@ -88,9 +76,9 @@ public class ClientboundBlockEventPacket implements Packet { val = ((GenericBlockValue) this.value).getValue(); } - Position.write(out, this.position); + helper.writePosition(out, this.position); out.writeByte(MagicValues.value(Integer.class, this.type)); out.writeByte(val); - out.writeVarInt(this.blockId & 4095); + helper.writeVarInt(out, this.blockId & 4095); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacket.java index ae62cf64..602f802c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.block.BlockChangeEntry; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,16 +14,16 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundBlockUpdatePacket implements Packet { +public class ClientboundBlockUpdatePacket implements MinecraftPacket { private final @NonNull BlockChangeEntry entry; - public ClientboundBlockUpdatePacket(NetInput in) throws IOException { - this.entry = new BlockChangeEntry(Position.read(in), in.readVarInt()); + public ClientboundBlockUpdatePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entry = new BlockChangeEntry(helper.readPosition(in), helper.readVarInt(in)); } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.entry.getPosition()); - out.writeVarInt(this.entry.getBlock()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.entry.getPosition()); + helper.writeVarInt(out, this.entry.getBlock()); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundCustomSoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundCustomSoundPacket.java index df36e163..617ab67b 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundCustomSoundPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundCustomSoundPacket.java @@ -1,12 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound; import com.github.steveice10.mc.protocol.data.game.level.sound.CustomSound; import com.github.steveice10.mc.protocol.data.game.level.sound.Sound; import com.github.steveice10.mc.protocol.data.game.level.sound.SoundCategory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -17,7 +17,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundCustomSoundPacket implements Packet { +public class ClientboundCustomSoundPacket implements MinecraftPacket { private final @NonNull Sound sound; private final @NonNull SoundCategory category; private final double x; @@ -27,16 +27,16 @@ public class ClientboundCustomSoundPacket implements Packet { private final float pitch; private final long seed; - public ClientboundCustomSoundPacket(NetInput in) throws IOException { - String value = in.readString(); - Sound sound = BuiltinSound.NAME_TO_SOUND.get(value); + public ClientboundCustomSoundPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + String value = helper.readString(in); + Sound sound = helper.getBuiltinSound(value); if (sound != null) { this.sound = sound; } else { this.sound = new CustomSound(value); } - this.category = SoundCategory.read(in); + this.category = helper.readSoundCategory(in); this.x = in.readInt() / 8D; this.y = in.readInt() / 8D; this.z = in.readInt() / 8D; @@ -46,7 +46,7 @@ public class ClientboundCustomSoundPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { String value = ""; if (this.sound instanceof CustomSound) { value = ((CustomSound) this.sound).getName(); @@ -54,8 +54,8 @@ public class ClientboundCustomSoundPacket implements Packet { value = ((BuiltinSound) this.sound).getName(); } - out.writeString(value); - out.writeVarInt(this.category.ordinal()); + helper.writeString(out, value); + helper.writeSoundCategory(out, this.category); out.writeInt((int) (this.x * 8)); out.writeInt((int) (this.y * 8)); out.writeInt((int) (this.z * 8)); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundExplodePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundExplodePacket.java index 3568f45b..40e46ab6 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundExplodePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundExplodePacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -16,7 +16,7 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ClientboundExplodePacket implements Packet { +public class ClientboundExplodePacket implements MinecraftPacket { private final float x; private final float y; private final float z; @@ -26,13 +26,13 @@ public class ClientboundExplodePacket implements Packet { private final float pushY; private final float pushZ; - public ClientboundExplodePacket(NetInput in) throws IOException { + public ClientboundExplodePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readFloat(); this.y = in.readFloat(); this.z = in.readFloat(); this.radius = in.readFloat(); this.exploded = new ArrayList<>(); - int length = in.readVarInt(); + int length = helper.readVarInt(in); for (int count = 0; count < length; count++) { this.exploded.add(Vector3i.from(in.readByte(), in.readByte(), in.readByte())); } @@ -43,12 +43,12 @@ public class ClientboundExplodePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeFloat(this.x); out.writeFloat(this.y); out.writeFloat(this.z); out.writeFloat(this.radius); - out.writeVarInt(this.exploded.size()); + helper.writeVarInt(out, this.exploded.size()); for (Vector3i record : this.exploded) { out.writeByte(record.getX()); out.writeByte(record.getY()); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundForgetLevelChunkPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundForgetLevelChunkPacket.java index 6181de12..cd54bc1d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundForgetLevelChunkPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundForgetLevelChunkPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,17 +12,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundForgetLevelChunkPacket implements Packet { +public class ClientboundForgetLevelChunkPacket implements MinecraftPacket { private final int x; private final int z; - public ClientboundForgetLevelChunkPacket(NetInput in) throws IOException { + public ClientboundForgetLevelChunkPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readInt(); this.z = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.x); out.writeInt(this.z); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundGameEventPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundGameEventPacket.java index 320cdb33..ba3364eb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundGameEventPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundGameEventPacket.java @@ -1,17 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; -import com.github.steveice10.mc.protocol.data.game.level.notify.DemoMessageValue; -import com.github.steveice10.mc.protocol.data.game.level.notify.EnterCreditsValue; -import com.github.steveice10.mc.protocol.data.game.level.notify.GameEvent; -import com.github.steveice10.mc.protocol.data.game.level.notify.GameEventValue; -import com.github.steveice10.mc.protocol.data.game.level.notify.RainStrengthValue; -import com.github.steveice10.mc.protocol.data.game.level.notify.RespawnScreenValue; -import com.github.steveice10.mc.protocol.data.game.level.notify.ThunderStrengthValue; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.data.game.level.notify.*; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -22,13 +16,14 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundGameEventPacket implements Packet { +public class ClientboundGameEventPacket implements MinecraftPacket { private final @NonNull GameEvent notification; private final GameEventValue value; - public ClientboundGameEventPacket(NetInput in) throws IOException { + public ClientboundGameEventPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.notification = MagicValues.key(GameEvent.class, in.readUnsignedByte()); float value = in.readFloat(); + // TODO: Handle this in MinecraftCodecHelper if (this.notification == GameEvent.CHANGE_GAMEMODE) { this.value = MagicValues.key(GameMode.class, ((int) value == -1) ? 255 : (int) value); // https://bugs.mojang.com/browse/MC-189885 - since we read as a float this bug doesn't apply here } else if (this.notification == GameEvent.DEMO_MESSAGE) { @@ -47,9 +42,10 @@ public class ClientboundGameEventPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(MagicValues.value(Integer.class, this.notification)); float value = 0; + // TODO: Handle this in MinecraftCodecHelper if (this.value instanceof GameMode && this.value == GameMode.UNKNOWN) { value = -1; } else if (this.value instanceof Enum) { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java index f741da1c..c1c808e5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelChunkWithLightPacket.java @@ -1,13 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.NBT; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.LightUpdateData; import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityInfo; import com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -18,7 +17,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundLevelChunkWithLightPacket implements Packet { +public class ClientboundLevelChunkWithLightPacket implements MinecraftPacket { private final int x; private final int z; private final @NonNull byte[] chunkData; @@ -26,42 +25,42 @@ public class ClientboundLevelChunkWithLightPacket implements Packet { private final @NonNull BlockEntityInfo[] blockEntities; private final @NonNull LightUpdateData lightData; - public ClientboundLevelChunkWithLightPacket(NetInput in) throws IOException { + public ClientboundLevelChunkWithLightPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readInt(); this.z = in.readInt(); - this.heightMaps = NBT.read(in); - this.chunkData = in.readBytes(in.readVarInt()); + this.heightMaps = helper.readTag(in); + this.chunkData = helper.readByteArray(in); - this.blockEntities = new BlockEntityInfo[in.readVarInt()]; + this.blockEntities = new BlockEntityInfo[helper.readVarInt(in)]; for (int i = 0; i < this.blockEntities.length; i++) { byte xz = in.readByte(); int blockEntityX = (xz >> 4) & 15; int blockEntityZ = xz & 15; int blockEntityY = in.readShort(); - BlockEntityType type = BlockEntityType.read(in); - CompoundTag tag = NBT.read(in); + BlockEntityType type = helper.readBlockEntityType(in); + CompoundTag tag = helper.readTag(in); this.blockEntities[i] = new BlockEntityInfo(blockEntityX, blockEntityY, blockEntityZ, type, tag); } - this.lightData = LightUpdateData.read(in); + this.lightData = helper.readLightUpdateData(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.x); out.writeInt(this.z); - NBT.write(out, this.heightMaps); - out.writeVarInt(this.chunkData.length); + helper.writeTag(out, this.heightMaps); + helper.writeVarInt(out, this.chunkData.length); out.writeBytes(this.chunkData); - out.writeVarInt(this.blockEntities.length); + helper.writeVarInt(out, this.blockEntities.length); for (BlockEntityInfo blockEntity : this.blockEntities) { out.writeByte(((blockEntity.getX() & 15) << 4) | blockEntity.getZ() & 15); out.writeShort(blockEntity.getY()); - BlockEntityType.write(out, blockEntity.getType()); - NBT.write(out, blockEntity.getNbt()); + helper.writeBlockEntityType(out, blockEntity.getType()); + helper.writeTag(out, blockEntity.getNbt()); } - LightUpdateData.write(out, this.lightData); + helper.writeLightUpdateData(out, this.lightData); } } \ No newline at end of file diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelEventPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelEventPacket.java index 0fc9a985..cf353aa0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelEventPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelEventPacket.java @@ -1,12 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.level.event.*; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -17,7 +16,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundLevelEventPacket implements Packet { +public class ClientboundLevelEventPacket implements MinecraftPacket { private final @NonNull LevelEvent event; private final @NonNull Vector3i position; private final LevelEventData data; @@ -27,9 +26,9 @@ public class ClientboundLevelEventPacket implements Packet { this(event, position, data, false); } - public ClientboundLevelEventPacket(NetInput in) throws IOException { - this.event = LevelEvent.read(in); - this.position = Position.read(in); + public ClientboundLevelEventPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.event = helper.readLevelEvent(in); + this.position = helper.readPosition(in); int value = in.readInt(); switch (this.event) { case RECORD: @@ -67,9 +66,9 @@ public class ClientboundLevelEventPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeInt(this.event.getId()); - Position.write(out, this.position); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeLevelEvent(out, this.event); + helper.writePosition(out, this.position); int value = 0; if (this.data instanceof RecordEventData) { value = ((RecordEventData) this.data).getRecordId(); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelParticlesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelParticlesPacket.java index 60566b1b..afb6af59 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelParticlesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLevelParticlesPacket.java @@ -1,11 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.particle.Particle; import com.github.steveice10.mc.protocol.data.game.level.particle.ParticleData; import com.github.steveice10.mc.protocol.data.game.level.particle.ParticleType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -16,7 +16,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundLevelParticlesPacket implements Packet { +public class ClientboundLevelParticlesPacket implements MinecraftPacket { private final @NonNull Particle particle; private final boolean longDistance; private final double x; @@ -28,8 +28,8 @@ public class ClientboundLevelParticlesPacket implements Packet { private final float velocityOffset; private final int amount; - public ClientboundLevelParticlesPacket(NetInput in) throws IOException { - ParticleType type = ParticleType.read(in); + public ClientboundLevelParticlesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + ParticleType type = helper.readParticleType(in); this.longDistance = in.readBoolean(); this.x = in.readDouble(); this.y = in.readDouble(); @@ -39,12 +39,12 @@ public class ClientboundLevelParticlesPacket implements Packet { this.offsetZ = in.readFloat(); this.velocityOffset = in.readFloat(); this.amount = in.readInt(); - this.particle = new Particle(type, ParticleData.read(in, type)); + this.particle = new Particle(type, helper.readParticleData(in, type)); } @Override - public void write(NetOutput out) throws IOException { - out.writeInt(particle.getType().ordinal()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeParticleType(out, this.particle.getType()); out.writeBoolean(this.longDistance); out.writeDouble(this.x); out.writeDouble(this.y); @@ -54,6 +54,6 @@ public class ClientboundLevelParticlesPacket implements Packet { out.writeFloat(this.offsetZ); out.writeFloat(this.velocityOffset); out.writeInt(this.amount); - ParticleData.write(out, this.particle.getType(), this.particle.getData()); + helper.writeParticleData(out, this.particle.getType(), this.particle.getData()); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLightUpdatePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLightUpdatePacket.java index 82973de0..1770ad70 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLightUpdatePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundLightUpdatePacket.java @@ -1,15 +1,15 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.LightUpdateData; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.NotNull; -import javax.annotation.Nonnull; import java.io.IOException; import java.util.BitSet; import java.util.List; @@ -17,10 +17,10 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ClientboundLightUpdatePacket implements Packet { +public class ClientboundLightUpdatePacket implements MinecraftPacket { private final int x; private final int z; - private final @Nonnull LightUpdateData lightData; + private final @NotNull LightUpdateData lightData; public ClientboundLightUpdatePacket(int x, int z, @NonNull BitSet skyYMask, @NonNull BitSet blockYMask, @NonNull BitSet emptySkyYMask, @NonNull BitSet emptyBlockYMask, @@ -40,16 +40,16 @@ public class ClientboundLightUpdatePacket implements Packet { this.lightData = new LightUpdateData(skyYMask, blockYMask, emptySkyYMask, emptyBlockYMask, skyUpdates, blockUpdates, trustEdges); } - public ClientboundLightUpdatePacket(NetInput in) throws IOException { - this.x = in.readVarInt(); - this.z = in.readVarInt(); - this.lightData = LightUpdateData.read(in); + public ClientboundLightUpdatePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.x = helper.readVarInt(in); + this.z = helper.readVarInt(in); + this.lightData = helper.readLightUpdateData(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.x); - out.writeVarInt(this.z); - LightUpdateData.write(out, this.lightData); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.x); + helper.writeVarInt(out, this.z); + helper.writeLightUpdateData(out, this.lightData); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundMapItemDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundMapItemDataPacket.java index c81e3e25..00c4fdd7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundMapItemDataPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundMapItemDataPacket.java @@ -1,13 +1,13 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.level.map.MapData; import com.github.steveice10.mc.protocol.data.game.level.map.MapIcon; import com.github.steveice10.mc.protocol.data.game.level.map.MapIconType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -19,7 +19,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundMapItemDataPacket implements Packet { +public class ClientboundMapItemDataPacket implements MinecraftPacket { private final int mapId; private final byte scale; private final boolean locked; @@ -31,21 +31,21 @@ public class ClientboundMapItemDataPacket implements Packet { this(mapId, scale, locked, icons, null); } - public ClientboundMapItemDataPacket(NetInput in) throws IOException { - this.mapId = in.readVarInt(); + public ClientboundMapItemDataPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.mapId = helper.readVarInt(in); this.scale = in.readByte(); this.locked = in.readBoolean(); boolean hasIcons = in.readBoolean(); - this.icons = new MapIcon[hasIcons ? in.readVarInt() : 0]; + this.icons = new MapIcon[hasIcons ? helper.readVarInt(in) : 0]; if (hasIcons) { for (int index = 0; index < this.icons.length; index++) { - int type = in.readVarInt(); + int type = helper.readVarInt(in); int x = in.readUnsignedByte(); int z = in.readUnsignedByte(); int rotation = in.readUnsignedByte(); Component displayName = null; if (in.readBoolean()) { - displayName = DefaultComponentSerializer.get().deserialize(in.readString()); + displayName = helper.readComponent(in); } this.icons[index] = new MapIcon(x, z, MagicValues.key(MapIconType.class, type), rotation, displayName); @@ -57,7 +57,7 @@ public class ClientboundMapItemDataPacket implements Packet { int rows = in.readUnsignedByte(); int x = in.readUnsignedByte(); int y = in.readUnsignedByte(); - byte[] data = in.readBytes(in.readVarInt()); + byte[] data = helper.readByteArray(in); this.data = new MapData(columns, rows, x, y, data); } else { @@ -66,22 +66,22 @@ public class ClientboundMapItemDataPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.mapId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.mapId); out.writeByte(this.scale); out.writeBoolean(this.locked); if (this.icons.length != 0) { out.writeBoolean(true); - out.writeVarInt(this.icons.length); + helper.writeVarInt(out, this.icons.length); for (MapIcon icon : this.icons) { int type = MagicValues.value(Integer.class, icon.getIconType()); - out.writeVarInt(type); + helper.writeVarInt(out, type); out.writeByte(icon.getCenterX()); out.writeByte(icon.getCenterZ()); out.writeByte(icon.getIconRotation()); if (icon.getDisplayName() != null) { out.writeBoolean(true); - out.writeString(DefaultComponentSerializer.get().serialize(icon.getDisplayName())); + helper.writeString(out, DefaultComponentSerializer.get().serialize(icon.getDisplayName())); } else { out.writeBoolean(false); } @@ -95,7 +95,7 @@ public class ClientboundMapItemDataPacket implements Packet { out.writeByte(this.data.getRows()); out.writeByte(this.data.getX()); out.writeByte(this.data.getY()); - out.writeVarInt(this.data.getData().length); + helper.writeVarInt(out, this.data.getData().length); out.writeBytes(this.data.getData()); } else { out.writeByte(0); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundOpenSignEditorPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundOpenSignEditorPacket.java index d71b92c8..57813fcc 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundOpenSignEditorPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundOpenSignEditorPacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,15 +14,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundOpenSignEditorPacket implements Packet { +public class ClientboundOpenSignEditorPacket implements MinecraftPacket { private final @NonNull Vector3i position; - public ClientboundOpenSignEditorPacket(NetInput in) throws IOException { - this.position = Position.read(in); + public ClientboundOpenSignEditorPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSectionBlocksUpdatePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSectionBlocksUpdatePacket.java index 591efdce..89ba5c30 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSectionBlocksUpdatePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSectionBlocksUpdatePacket.java @@ -1,11 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.block.BlockChangeEntry; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.Data; import lombok.NonNull; import lombok.With; @@ -14,7 +13,7 @@ import java.io.IOException; @Data @With -public class ClientboundSectionBlocksUpdatePacket implements Packet { +public class ClientboundSectionBlocksUpdatePacket implements MinecraftPacket { private final int chunkX; private final int chunkY; private final int chunkZ; @@ -36,15 +35,15 @@ public class ClientboundSectionBlocksUpdatePacket implements Packet { this.entries = entries; } - public ClientboundSectionBlocksUpdatePacket(NetInput in) throws IOException { + public ClientboundSectionBlocksUpdatePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { long chunkPosition = in.readLong(); this.chunkX = (int) (chunkPosition >> 42); this.chunkY = (int) (chunkPosition << 44 >> 44); this.chunkZ = (int) (chunkPosition << 22 >> 42); this.ignoreOldLight = in.readBoolean(); - this.entries = new BlockChangeEntry[in.readVarInt()]; + this.entries = new BlockChangeEntry[helper.readVarInt(in)]; for (int index = 0; index < this.entries.length; index++) { - long blockData = in.readVarLong(); + long blockData = helper.readVarLong(in); short position = (short) (blockData & 0xFFFL); int x = (this.chunkX << 4) + (position >>> 8 & 0xF); int y = (this.chunkY << 4) + (position & 0xF); @@ -54,16 +53,16 @@ public class ClientboundSectionBlocksUpdatePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { long chunkPosition = 0; chunkPosition |= (this.chunkX & 0x3FFFFFL) << 42; chunkPosition |= (this.chunkZ & 0x3FFFFFL) << 20; out.writeLong(chunkPosition | (this.chunkY & 0xFFFFFL)); out.writeBoolean(this.ignoreOldLight); - out.writeVarInt(this.entries.length); + helper.writeVarInt(out, this.entries.length); for (BlockChangeEntry entry : this.entries) { short position = (short) ((entry.getPosition().getX() - (this.chunkX << 4)) << 8 | (entry.getPosition().getZ() - (this.chunkZ << 4)) << 4 | (entry.getPosition().getY() - (this.chunkY << 4))); - out.writeVarLong((long) entry.getBlock() << 12 | position); + helper.writeVarLong(out, (long) entry.getBlock() << 12 | position); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheCenterPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheCenterPacket.java index 5fff1862..0304fde2 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheCenterPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheCenterPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetChunkCacheCenterPacket implements Packet { +public class ClientboundSetChunkCacheCenterPacket implements MinecraftPacket { private final int chunkX; private final int chunkZ; - public ClientboundSetChunkCacheCenterPacket(NetInput in) throws IOException { - this.chunkX = in.readVarInt(); - this.chunkZ = in.readVarInt(); + public ClientboundSetChunkCacheCenterPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.chunkX = helper.readVarInt(in); + this.chunkZ = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.chunkX); - out.writeVarInt(this.chunkZ); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.chunkX); + helper.writeVarInt(out, this.chunkZ); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheRadiusPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheRadiusPacket.java index c0d8b7f1..a24dcbea 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheRadiusPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetChunkCacheRadiusPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetChunkCacheRadiusPacket implements Packet { +public class ClientboundSetChunkCacheRadiusPacket implements MinecraftPacket { private final int viewDistance; - public ClientboundSetChunkCacheRadiusPacket(NetInput in) throws IOException { - this.viewDistance = in.readVarInt(); + public ClientboundSetChunkCacheRadiusPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.viewDistance = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.viewDistance); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.viewDistance); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetDefaultSpawnPositionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetDefaultSpawnPositionPacket.java index 925ad518..ce1c5af4 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetDefaultSpawnPositionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetDefaultSpawnPositionPacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetDefaultSpawnPositionPacket implements Packet { +public class ClientboundSetDefaultSpawnPositionPacket implements MinecraftPacket { private final @NonNull Vector3i position; private final float angle; - public ClientboundSetDefaultSpawnPositionPacket(NetInput in) throws IOException { - this.position = Position.read(in); + public ClientboundSetDefaultSpawnPositionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); this.angle = in.readFloat(); } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); out.writeFloat(this.angle); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetSimulationDistancePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetSimulationDistancePacket.java index 856b02b0..4d25a227 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetSimulationDistancePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetSimulationDistancePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetSimulationDistancePacket implements Packet { +public class ClientboundSetSimulationDistancePacket implements MinecraftPacket { private final int simulationDistance; - public ClientboundSetSimulationDistancePacket(NetInput in) throws IOException { - this.simulationDistance = in.readVarInt(); + public ClientboundSetSimulationDistancePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.simulationDistance = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.simulationDistance); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.simulationDistance); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetTimePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetTimePacket.java index 6185292e..394a07f0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetTimePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetTimePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,17 +12,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetTimePacket implements Packet { +public class ClientboundSetTimePacket implements MinecraftPacket { private final long worldAge; private final long time; - public ClientboundSetTimePacket(NetInput in) throws IOException { + public ClientboundSetTimePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.worldAge = in.readLong(); this.time = in.readLong(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeLong(this.worldAge); out.writeLong(this.time); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSoundPacket.java index 66923f1f..4de918b7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSoundPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSoundPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.level.sound.BuiltinSound; import com.github.steveice10.mc.protocol.data.game.level.sound.SoundCategory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,7 +15,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSoundPacket implements Packet { +public class ClientboundSoundPacket implements MinecraftPacket { private final @NonNull BuiltinSound sound; private final @NonNull SoundCategory category; private final double x; @@ -25,9 +25,9 @@ public class ClientboundSoundPacket implements Packet { private final float pitch; private final long seed; - public ClientboundSoundPacket(NetInput in) throws IOException { - this.sound = BuiltinSound.VALUES[in.readVarInt()]; - this.category = SoundCategory.read(in); + public ClientboundSoundPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.sound = helper.readBuiltinSound(in); + this.category = helper.readSoundCategory(in); this.x = in.readInt() / 8D; this.y = in.readInt() / 8D; this.z = in.readInt() / 8D; @@ -37,9 +37,9 @@ public class ClientboundSoundPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.sound.ordinal()); - out.writeVarInt(this.category.ordinal()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeBuiltinSound(out, this.sound); + helper.writeSoundCategory(out, this.category); out.writeInt((int) (this.x * 8)); out.writeInt((int) (this.y * 8)); out.writeInt((int) (this.z * 8)); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java index 76d53982..9908dcfa 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundTagQueryPacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.NBT; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundTagQueryPacket implements Packet { +public class ClientboundTagQueryPacket implements MinecraftPacket { private final int transactionId; private final @NonNull CompoundTag nbt; - public ClientboundTagQueryPacket(NetInput in) throws IOException { - this.transactionId = in.readVarInt(); - this.nbt = NBT.read(in); + public ClientboundTagQueryPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.transactionId = helper.readVarInt(in); + this.nbt = helper.readTag(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.transactionId); - NBT.write(out, this.nbt); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.transactionId); + helper.writeTag(out, this.nbt); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundInitializeBorderPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundInitializeBorderPacket.java index cea52612..33280b78 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundInitializeBorderPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundInitializeBorderPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.border; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundInitializeBorderPacket implements Packet { +public class ClientboundInitializeBorderPacket implements MinecraftPacket { private final double newCenterX; private final double newCenterZ; private final double oldSize; @@ -22,26 +22,26 @@ public class ClientboundInitializeBorderPacket implements Packet { private final int warningBlocks; private final int warningTime; - public ClientboundInitializeBorderPacket(NetInput in) throws IOException { + public ClientboundInitializeBorderPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.newCenterX = in.readDouble(); this.newCenterZ = in.readDouble(); this.oldSize = in.readDouble(); this.newSize = in.readDouble(); - this.lerpTime = in.readVarLong(); - this.newAbsoluteMaxSize = in.readVarInt(); - this.warningBlocks = in.readVarInt(); - this.warningTime = in.readVarInt(); + this.lerpTime = helper.readVarLong(in); + this.newAbsoluteMaxSize = helper.readVarInt(in); + this.warningBlocks = helper.readVarInt(in); + this.warningTime = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.newCenterX); out.writeDouble(this.newCenterZ); out.writeDouble(this.oldSize); out.writeDouble(this.newSize); - out.writeVarLong(this.lerpTime); - out.writeVarInt(this.newAbsoluteMaxSize); - out.writeVarInt(this.warningBlocks); - out.writeVarInt(this.warningTime); + helper.writeVarLong(out, this.lerpTime); + helper.writeVarInt(out, this.newAbsoluteMaxSize); + helper.writeVarInt(out, this.warningBlocks); + helper.writeVarInt(out, this.warningTime); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderCenterPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderCenterPacket.java index 24e0fded..a0dc2831 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderCenterPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderCenterPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.border; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,17 +12,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetBorderCenterPacket implements Packet { +public class ClientboundSetBorderCenterPacket implements MinecraftPacket { private final double newCenterX; private final double newCenterZ; - public ClientboundSetBorderCenterPacket(NetInput in) throws IOException { + public ClientboundSetBorderCenterPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.newCenterX = in.readDouble(); this.newCenterZ = in.readDouble(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.newCenterX); out.writeDouble(this.newCenterZ); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderLerpSizePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderLerpSizePacket.java index 8010e91f..34903edd 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderLerpSizePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderLerpSizePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.border; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,21 +12,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetBorderLerpSizePacket implements Packet { +public class ClientboundSetBorderLerpSizePacket implements MinecraftPacket { private final double oldSize; private final double newSize; private final long lerpTime; - public ClientboundSetBorderLerpSizePacket(NetInput in) throws IOException { + public ClientboundSetBorderLerpSizePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.oldSize = in.readDouble(); this.newSize = in.readDouble(); - this.lerpTime = in.readVarLong(); + this.lerpTime = helper.readVarLong(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.oldSize); out.writeDouble(this.newSize); - out.writeVarLong(this.lerpTime); + helper.writeVarLong(out, this.lerpTime); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderSizePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderSizePacket.java index 97197fe2..d275f015 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderSizePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderSizePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.border; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetBorderSizePacket implements Packet { +public class ClientboundSetBorderSizePacket implements MinecraftPacket { private final double size; - public ClientboundSetBorderSizePacket(NetInput in) throws IOException { + public ClientboundSetBorderSizePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.size = in.readDouble(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.size); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDelayPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDelayPacket.java index 8d5e3095..fb871eff 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDelayPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDelayPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.border; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetBorderWarningDelayPacket implements Packet { +public class ClientboundSetBorderWarningDelayPacket implements MinecraftPacket { private final int warningDelay; - public ClientboundSetBorderWarningDelayPacket(NetInput in) throws IOException { - this.warningDelay = in.readVarInt(); + public ClientboundSetBorderWarningDelayPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.warningDelay = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.warningDelay); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.warningDelay); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDistancePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDistancePacket.java index f30ce9ab..24767f33 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDistancePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/border/ClientboundSetBorderWarningDistancePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.border; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetBorderWarningDistancePacket implements Packet { +public class ClientboundSetBorderWarningDistancePacket implements MinecraftPacket { private final int warningBlocks; - public ClientboundSetBorderWarningDistancePacket(NetInput in) throws IOException { - this.warningBlocks = in.readVarInt(); + public ClientboundSetBorderWarningDistancePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.warningBlocks = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.warningBlocks); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.warningBlocks); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetDisplayObjectivePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetDisplayObjectivePacket.java index 64376ae0..636ff1a9 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetDisplayObjectivePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetDisplayObjectivePacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.scoreboard; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreboardPosition; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +15,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetDisplayObjectivePacket implements Packet { +public class ClientboundSetDisplayObjectivePacket implements MinecraftPacket { private final @NonNull ScoreboardPosition position; private final @NonNull String name; - public ClientboundSetDisplayObjectivePacket(NetInput in) throws IOException { + public ClientboundSetDisplayObjectivePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.position = MagicValues.key(ScoreboardPosition.class, in.readByte()); - this.name = in.readString(); + this.name = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(MagicValues.value(Integer.class, this.position)); - out.writeString(this.name); + helper.writeString(out, this.name); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetObjectivePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetObjectivePacket.java index 59738446..301183c3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetObjectivePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetObjectivePacket.java @@ -1,12 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.scoreboard; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.scoreboard.ObjectiveAction; import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.Data; import lombok.NonNull; import lombok.With; @@ -16,7 +16,7 @@ import java.io.IOException; @Data @With -public class ClientboundSetObjectivePacket implements Packet { +public class ClientboundSetObjectivePacket implements MinecraftPacket { private final @NonNull String name; private final @NonNull ObjectiveAction action; @@ -51,12 +51,12 @@ public class ClientboundSetObjectivePacket implements Packet { this.type = type; } - public ClientboundSetObjectivePacket(NetInput in) throws IOException { - this.name = in.readString(); + public ClientboundSetObjectivePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.name = helper.readString(in); this.action = MagicValues.key(ObjectiveAction.class, in.readByte()); if (this.action == ObjectiveAction.ADD || this.action == ObjectiveAction.UPDATE) { - this.displayName = DefaultComponentSerializer.get().deserialize(in.readString()); - this.type = MagicValues.key(ScoreType.class, in.readVarInt()); + this.displayName = helper.readComponent(in); + this.type = MagicValues.key(ScoreType.class, helper.readVarInt(in)); } else { this.displayName = null; this.type = null; @@ -64,12 +64,12 @@ public class ClientboundSetObjectivePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.name); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.name); out.writeByte(MagicValues.value(Integer.class, this.action)); if (this.action == ObjectiveAction.ADD || this.action == ObjectiveAction.UPDATE) { - out.writeString(DefaultComponentSerializer.get().serialize(this.displayName)); - out.writeVarInt(MagicValues.value(Integer.class, this.type)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.displayName)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.type)); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetPlayerTeamPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetPlayerTeamPacket.java index 9907f287..7ad44ca6 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetPlayerTeamPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetPlayerTeamPacket.java @@ -1,14 +1,14 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.scoreboard; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.scoreboard.CollisionRule; import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility; import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamAction; import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.*; import net.kyori.adventure.text.Component; @@ -18,7 +18,7 @@ import java.util.Arrays; @Data @With @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class ClientboundSetPlayerTeamPacket implements Packet { +public class ClientboundSetPlayerTeamPacket implements MinecraftPacket { private final @NonNull String teamName; private final @NonNull TeamAction action; @@ -105,21 +105,21 @@ public class ClientboundSetPlayerTeamPacket implements Packet { this.players = Arrays.copyOf(players, players.length); } - public ClientboundSetPlayerTeamPacket(NetInput in) throws IOException { - this.teamName = in.readString(); + public ClientboundSetPlayerTeamPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.teamName = helper.readString(in); this.action = MagicValues.key(TeamAction.class, in.readByte()); if (this.action == TeamAction.CREATE || this.action == TeamAction.UPDATE) { - this.displayName = DefaultComponentSerializer.get().deserialize(in.readString()); + this.displayName = helper.readComponent(in); byte flags = in.readByte(); this.friendlyFire = (flags & 0x1) != 0; this.seeFriendlyInvisibles = (flags & 0x2) != 0; - this.nameTagVisibility = MagicValues.key(NameTagVisibility.class, in.readString()); - this.collisionRule = MagicValues.key(CollisionRule.class, in.readString()); + this.nameTagVisibility = MagicValues.key(NameTagVisibility.class, helper.readString(in)); + this.collisionRule = MagicValues.key(CollisionRule.class, helper.readString(in)); - this.color = TeamColor.VALUES[in.readVarInt()]; + this.color = TeamColor.VALUES[helper.readVarInt(in)]; - this.prefix = DefaultComponentSerializer.get().deserialize(in.readString()); - this.suffix = DefaultComponentSerializer.get().deserialize(in.readString()); + this.prefix = helper.readComponent(in); + this.suffix = helper.readComponent(in); } else { this.displayName = null; this.prefix = null; @@ -132,9 +132,9 @@ public class ClientboundSetPlayerTeamPacket implements Packet { } if (this.action == TeamAction.CREATE || this.action == TeamAction.ADD_PLAYER || this.action == TeamAction.REMOVE_PLAYER) { - this.players = new String[in.readVarInt()]; + this.players = new String[helper.readVarInt(in)]; for (int index = 0; index < this.players.length; index++) { - this.players[index] = in.readString(); + this.players[index] = helper.readString(in); } } else { this.players = null; @@ -142,24 +142,24 @@ public class ClientboundSetPlayerTeamPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.teamName); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.teamName); out.writeByte(MagicValues.value(Integer.class, this.action)); if (this.action == TeamAction.CREATE || this.action == TeamAction.UPDATE) { - out.writeString(DefaultComponentSerializer.get().serialize(this.displayName)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.displayName)); out.writeByte((this.friendlyFire ? 0x1 : 0x0) | (this.seeFriendlyInvisibles ? 0x2 : 0x0)); - out.writeString(MagicValues.value(String.class, this.nameTagVisibility)); - out.writeString(MagicValues.value(String.class, this.collisionRule)); - out.writeVarInt(this.color.ordinal()); - out.writeString(DefaultComponentSerializer.get().serialize(this.prefix)); - out.writeString(DefaultComponentSerializer.get().serialize(this.suffix)); + helper.writeString(out, MagicValues.value(String.class, this.nameTagVisibility)); + helper.writeString(out, MagicValues.value(String.class, this.collisionRule)); + helper.writeVarInt(out, this.color.ordinal()); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.prefix)); + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.suffix)); } if (this.action == TeamAction.CREATE || this.action == TeamAction.ADD_PLAYER || this.action == TeamAction.REMOVE_PLAYER) { - out.writeVarInt(this.players.length); + helper.writeVarInt(out, this.players.length); for (String player : this.players) { if (player != null) { - out.writeString(player); + helper.writeString(out, player); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetScorePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetScorePacket.java index ed8caa3e..beb3b0f1 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetScorePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/scoreboard/ClientboundSetScorePacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.scoreboard; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreboardAction; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.*; import java.io.IOException; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class ClientboundSetScorePacket implements Packet { +public class ClientboundSetScorePacket implements MinecraftPacket { private final @NonNull String entry; private final @NonNull ScoreboardAction action; private final @NonNull String objective; @@ -39,24 +39,24 @@ public class ClientboundSetScorePacket implements Packet { this.value = value; } - public ClientboundSetScorePacket(NetInput in) throws IOException { - this.entry = in.readString(); - this.action = MagicValues.key(ScoreboardAction.class, in.readVarInt()); - this.objective = in.readString(); + public ClientboundSetScorePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entry = helper.readString(in); + this.action = MagicValues.key(ScoreboardAction.class, helper.readVarInt(in)); + this.objective = helper.readString(in); if (this.action == ScoreboardAction.ADD_OR_UPDATE) { - this.value = in.readVarInt(); + this.value = helper.readVarInt(in); } else { this.value = 0; } } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.entry); - out.writeVarInt(MagicValues.value(Integer.class, this.action)); - out.writeString(this.objective); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.entry); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); + helper.writeString(out, this.objective); if (this.action == ScoreboardAction.ADD_OR_UPDATE) { - out.writeVarInt(this.value); + helper.writeVarInt(out, this.value); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundClearTitlesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundClearTitlesPacket.java index 3df192e5..b20402c8 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundClearTitlesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundClearTitlesPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.title; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundClearTitlesPacket implements Packet { +public class ClientboundClearTitlesPacket implements MinecraftPacket { private final boolean resetTimes; - public ClientboundClearTitlesPacket(NetInput in) throws IOException { + public ClientboundClearTitlesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.resetTimes = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.resetTimes); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetActionBarTextPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetActionBarTextPacket.java index 9f92bcc9..845fa2b6 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetActionBarTextPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetActionBarTextPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.title; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -14,15 +14,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetActionBarTextPacket implements Packet { +public class ClientboundSetActionBarTextPacket implements MinecraftPacket { private final Component text; - public ClientboundSetActionBarTextPacket(NetInput in) throws IOException { - this.text = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundSetActionBarTextPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.text = helper.readComponent(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serialize(this.text)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.text)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetSubtitleTextPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetSubtitleTextPacket.java index fc5c2cc4..fe6ebd2d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetSubtitleTextPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetSubtitleTextPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.title; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -14,15 +14,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetSubtitleTextPacket implements Packet { +public class ClientboundSetSubtitleTextPacket implements MinecraftPacket { private final Component text; - public ClientboundSetSubtitleTextPacket(NetInput in) throws IOException { - this.text = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundSetSubtitleTextPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.text = helper.readComponent(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serialize(this.text)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.text)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitleTextPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitleTextPacket.java index e2ba3190..6ffbe57c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitleTextPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitleTextPacket.java @@ -1,29 +1,29 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.title; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetTitleTextPacket implements Packet { +public class ClientboundSetTitleTextPacket implements MinecraftPacket { private final @Nullable Component text; - public ClientboundSetTitleTextPacket(NetInput in) throws IOException { - this.text = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundSetTitleTextPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.text = helper.readComponent(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serializeOr(this.text, "null")); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, DefaultComponentSerializer.get().serializeOr(this.text, "null")); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitlesAnimationPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitlesAnimationPacket.java index eda7dc91..ea8fa5ee 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitlesAnimationPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/title/ClientboundSetTitlesAnimationPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.title; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,19 +12,19 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundSetTitlesAnimationPacket implements Packet { +public class ClientboundSetTitlesAnimationPacket implements MinecraftPacket { private final int fadeIn; private final int stay; private final int fadeOut; - public ClientboundSetTitlesAnimationPacket(NetInput in) throws IOException { + public ClientboundSetTitlesAnimationPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.fadeIn = in.readInt(); this.stay = in.readInt(); this.fadeOut = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.fadeIn); out.writeInt(this.stay); out.writeInt(this.fadeOut); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChangeDifficultyPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChangeDifficultyPacket.java index 26570248..304177be 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChangeDifficultyPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChangeDifficultyPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.setting.Difficulty; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,15 +15,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundChangeDifficultyPacket implements Packet { +public class ServerboundChangeDifficultyPacket implements MinecraftPacket { private final @NonNull Difficulty difficulty; - public ServerboundChangeDifficultyPacket(NetInput in) throws IOException { + public ServerboundChangeDifficultyPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.difficulty = MagicValues.key(Difficulty.class, in.readByte()); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(MagicValues.value(Byte.class, this.difficulty)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatCommandPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatCommandPacket.java index 7a245aec..23bdad43 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatCommandPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatCommandPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -14,22 +14,22 @@ import java.util.Map; @Data @With @AllArgsConstructor -public class ServerboundChatCommandPacket implements Packet { +public class ServerboundChatCommandPacket implements MinecraftPacket { private final String command; private final long timeStamp; private final long salt; private final Map signatures; private final boolean signedPreview; - public ServerboundChatCommandPacket(NetInput in) throws IOException { - this.command = in.readString(); + public ServerboundChatCommandPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.command = helper.readString(in); this.timeStamp = in.readLong(); this.salt = in.readLong(); this.signatures = new HashMap<>(); - int signatureCount = in.readVarInt(); + int signatureCount = helper.readVarInt(in); for (int i = 0; i < signatureCount; i++) { - String signatureId = in.readString(); - byte[] signature = in.readBytes(in.readVarInt()); + String signatureId = helper.readString(in); + byte[] signature = helper.readByteArray(in); signatures.put(signatureId, signature); } @@ -37,14 +37,14 @@ public class ServerboundChatCommandPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.command); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.command); out.writeLong(this.timeStamp); out.writeLong(this.salt); - out.writeVarInt(this.signatures.size()); + helper.writeVarInt(out, this.signatures.size()); for (Map.Entry signature : this.signatures.entrySet()) { - out.writeString(signature.getKey()); - out.writeVarInt(signature.getValue().length); + helper.writeString(out, signature.getKey()); + helper.writeVarInt(out, signature.getValue().length); out.writeBytes(signature.getValue()); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java index 8089d153..36171568 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,27 +13,27 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundChatPacket implements Packet { +public class ServerboundChatPacket implements MinecraftPacket { private final @NonNull String message; private final long timeStamp; private final long salt; private final byte[] signature; private final boolean signedPreview; - public ServerboundChatPacket(NetInput in) throws IOException { - this.message = in.readString(); + public ServerboundChatPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.message = helper.readString(in); this.timeStamp = in.readLong(); this.salt = in.readLong(); - this.signature = in.readBytes(in.readVarInt()); + this.signature = helper.readByteArray(in); this.signedPreview = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.message); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.message); out.writeLong(this.timeStamp); out.writeLong(this.salt); - out.writeVarInt(this.signature.length); + helper.writeVarInt(out, this.signature.length); out.writeBytes(this.signature); out.writeBoolean(this.signedPreview); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPreviewPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPreviewPacket.java index 85c2fabc..e30976d2 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPreviewPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundChatPreviewPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundChatPreviewPacket implements Packet { +public class ServerboundChatPreviewPacket implements MinecraftPacket { private final int queryId; private final String query; - public ServerboundChatPreviewPacket(NetInput in) throws IOException { + public ServerboundChatPreviewPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.queryId = in.readInt(); - this.query = in.readString(); + this.query = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.queryId); - out.writeString(this.query); + helper.writeString(out, this.query); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientCommandPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientCommandPacket.java index bf52fcb5..b13c88b5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientCommandPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientCommandPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.ClientCommand; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,15 +15,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundClientCommandPacket implements Packet { +public class ServerboundClientCommandPacket implements MinecraftPacket { private final @NonNull ClientCommand request; - public ServerboundClientCommandPacket(NetInput in) throws IOException { - this.request = MagicValues.key(ClientCommand.class, in.readVarInt()); + public ServerboundClientCommandPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.request = MagicValues.key(ClientCommand.class, helper.readVarInt(in)); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.request)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.request)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientInformationPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientInformationPacket.java index 9469c692..e1a3f800 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientInformationPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundClientInformationPacket.java @@ -1,12 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.HandPreference; import com.github.steveice10.mc.protocol.data.game.setting.ChatVisibility; import com.github.steveice10.mc.protocol.data.game.setting.SkinPart; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -19,7 +19,7 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ServerboundClientInformationPacket implements Packet { +public class ServerboundClientInformationPacket implements MinecraftPacket { private final @NonNull String locale; private final int renderDistance; private final @NonNull ChatVisibility chatVisibility; @@ -32,10 +32,10 @@ public class ServerboundClientInformationPacket implements Packet { */ private final boolean allowsListing; - public ServerboundClientInformationPacket(NetInput in) throws IOException { - this.locale = in.readString(); + public ServerboundClientInformationPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.locale = helper.readString(in); this.renderDistance = in.readByte(); - this.chatVisibility = MagicValues.key(ChatVisibility.class, in.readVarInt()); + this.chatVisibility = MagicValues.key(ChatVisibility.class, helper.readVarInt(in)); this.useChatColors = in.readBoolean(); this.visibleParts = new ArrayList<>(); @@ -47,16 +47,16 @@ public class ServerboundClientInformationPacket implements Packet { } } - this.mainHand = MagicValues.key(HandPreference.class, in.readVarInt()); + this.mainHand = MagicValues.key(HandPreference.class, helper.readVarInt(in)); this.textFilteringEnabled = in.readBoolean(); this.allowsListing = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.locale); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.locale); out.writeByte(this.renderDistance); - out.writeVarInt(MagicValues.value(Integer.class, this.chatVisibility)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.chatVisibility)); out.writeBoolean(this.useChatColors); int flags = 0; @@ -66,7 +66,7 @@ public class ServerboundClientInformationPacket implements Packet { out.writeByte(flags); - out.writeVarInt(MagicValues.value(Integer.class, this.mainHand)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.mainHand)); out.writeBoolean(this.textFilteringEnabled); out.writeBoolean(allowsListing); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCommandSuggestionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCommandSuggestionPacket.java index 1ef34d99..e3c59fdd 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCommandSuggestionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCommandSuggestionPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,18 +13,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundCommandSuggestionPacket implements Packet { +public class ServerboundCommandSuggestionPacket implements MinecraftPacket { private final int transactionId; private final @NonNull String text; - public ServerboundCommandSuggestionPacket(NetInput in) throws IOException { - this.transactionId = in.readVarInt(); - this.text = in.readString(); + public ServerboundCommandSuggestionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.transactionId = helper.readVarInt(in); + this.text = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.transactionId); - out.writeString(this.text); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.transactionId); + helper.writeString(out, this.text); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCustomPayloadPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCustomPayloadPacket.java index ae3ce6a2..e7c7fe21 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCustomPayloadPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundCustomPayloadPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,18 +13,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundCustomPayloadPacket implements Packet { +public class ServerboundCustomPayloadPacket implements MinecraftPacket { private final @NonNull String channel; private final @NonNull byte data[]; - public ServerboundCustomPayloadPacket(NetInput in) throws IOException { - this.channel = in.readString(); - this.data = in.readBytes(in.available()); + public ServerboundCustomPayloadPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.channel = helper.readString(in); + this.data = helper.readByteArray(in, ByteBuf::readableBytes); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.channel); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.channel); out.writeBytes(this.data); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundKeepAlivePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundKeepAlivePacket.java index ce7b5cb0..d4259cb9 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundKeepAlivePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundKeepAlivePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundKeepAlivePacket implements Packet { +public class ServerboundKeepAlivePacket implements MinecraftPacket { private final long pingId; - public ServerboundKeepAlivePacket(NetInput in) throws IOException { + public ServerboundKeepAlivePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.pingId = in.readLong(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeLong(this.pingId); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundLockDifficultyPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundLockDifficultyPacket.java index f51b4c95..8e3617b7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundLockDifficultyPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundLockDifficultyPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundLockDifficultyPacket implements Packet { +public class ServerboundLockDifficultyPacket implements MinecraftPacket { private final boolean locked; - public ServerboundLockDifficultyPacket(NetInput in) throws IOException { + public ServerboundLockDifficultyPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.locked = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.locked); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundPongPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundPongPacket.java index 4563dc97..8ca9cb87 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundPongPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundPongPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPongPacket implements Packet { +public class ServerboundPongPacket implements MinecraftPacket { private final int id; - public ServerboundPongPacket(NetInput in) throws IOException { + public ServerboundPongPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.id = in.readInt(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeInt(this.id); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundResourcePackPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundResourcePackPacket.java index 19f71214..0162facc 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundResourcePackPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/ServerboundResourcePackPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.ResourcePackStatus; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,15 +15,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundResourcePackPacket implements Packet { +public class ServerboundResourcePackPacket implements MinecraftPacket { private final @NonNull ResourcePackStatus status; - public ServerboundResourcePackPacket(NetInput in) throws IOException { - this.status = MagicValues.key(ResourcePackStatus.class, in.readVarInt()); + public ServerboundResourcePackPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.status = MagicValues.key(ResourcePackStatus.class, helper.readVarInt(in)); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.status)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.status)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerButtonClickPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerButtonClickPacket.java index f2a1e1c8..ddf7ab48 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerButtonClickPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerButtonClickPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,17 +12,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundContainerButtonClickPacket implements Packet { +public class ServerboundContainerButtonClickPacket implements MinecraftPacket { private final int containerId; private final int buttonId; - public ServerboundContainerButtonClickPacket(NetInput in) throws IOException { + public ServerboundContainerButtonClickPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readByte(); this.buttonId = in.readByte(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); out.writeByte(this.buttonId); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClickPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClickPacket.java index 66b87af3..7f68500b 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClickPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClickPacket.java @@ -1,5 +1,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.inventory.ClickItemAction; @@ -11,10 +13,9 @@ import com.github.steveice10.mc.protocol.data.game.inventory.FillStackAction; import com.github.steveice10.mc.protocol.data.game.inventory.MoveToHotbarAction; import com.github.steveice10.mc.protocol.data.game.inventory.ShiftClickItemAction; import com.github.steveice10.mc.protocol.data.game.inventory.SpreadItemAction; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; -import io.netty.util.collection.IntObjectHashMap; +import io.netty.buffer.ByteBuf; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import lombok.Data; import lombok.NonNull; import lombok.With; @@ -25,7 +26,7 @@ import java.util.Map; @Data @With -public class ServerboundContainerClickPacket implements Packet { +public class ServerboundContainerClickPacket implements MinecraftPacket { public static final int CLICK_OUTSIDE_NOT_HOLDING_SLOT = -999; private final int containerId; @@ -34,9 +35,13 @@ public class ServerboundContainerClickPacket implements Packet { private final ContainerActionType action; private final ContainerAction param; private final ItemStack carriedItem; - private final @NonNull Map changedSlots; + private final @NonNull Int2ObjectMap changedSlots; public ServerboundContainerClickPacket(int containerId, int stateId, int slot, ContainerActionType action, ContainerAction param, ItemStack carriedItem, @NotNull Map changedSlots) { + this(containerId, stateId, slot, action, param, carriedItem, new Int2ObjectOpenHashMap<>(changedSlots)); + } + + public ServerboundContainerClickPacket(int containerId, int stateId, int slot, ContainerActionType action, ContainerAction param, ItemStack carriedItem, @NotNull Int2ObjectMap changedSlots) { if ((param == DropItemAction.LEFT_CLICK_OUTSIDE_NOT_HOLDING || param == DropItemAction.RIGHT_CLICK_OUTSIDE_NOT_HOLDING) && slot != -CLICK_OUTSIDE_NOT_HOLDING_SLOT) { throw new IllegalArgumentException("Slot must be " + CLICK_OUTSIDE_NOT_HOLDING_SLOT @@ -52,9 +57,9 @@ public class ServerboundContainerClickPacket implements Packet { this.changedSlots = changedSlots; } - public ServerboundContainerClickPacket(NetInput in) throws IOException { + public ServerboundContainerClickPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readByte(); - this.stateId = in.readVarInt(); + this.stateId = helper.readVarInt(in); this.slot = in.readShort(); byte param = in.readByte(); this.action = MagicValues.key(ContainerActionType.class, in.readByte()); @@ -76,21 +81,21 @@ public class ServerboundContainerClickPacket implements Packet { throw new IllegalStateException(); } - int changedItemsSize = in.readVarInt(); - this.changedSlots = new IntObjectHashMap<>(changedItemsSize); + int changedItemsSize = helper.readVarInt(in); + this.changedSlots = new Int2ObjectOpenHashMap<>(changedItemsSize); for (int i = 0; i < changedItemsSize; i++) { int key = in.readShort(); - ItemStack value = ItemStack.read(in); + ItemStack value = helper.readItemStack(in); this.changedSlots.put(key, value); } - this.carriedItem = ItemStack.read(in); + this.carriedItem = helper.readItemStack(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); - out.writeVarInt(this.stateId); + helper.writeVarInt(out, this.stateId); out.writeShort(this.slot); int param = MagicValues.value(Integer.class, this.param); @@ -101,12 +106,12 @@ public class ServerboundContainerClickPacket implements Packet { out.writeByte(param); out.writeByte(MagicValues.value(Integer.class, this.action)); - out.writeVarInt(this.changedSlots.size()); - for (Map.Entry pair : this.changedSlots.entrySet()) { - out.writeShort(pair.getKey()); - ItemStack.write(out, pair.getValue()); + helper.writeVarInt(out, this.changedSlots.size()); + for (Int2ObjectMap.Entry pair : this.changedSlots.int2ObjectEntrySet()) { + out.writeShort(pair.getIntKey()); + helper.writeItemStack(out, pair.getValue()); } - ItemStack.write(out, this.carriedItem); + helper.writeItemStack(out, this.carriedItem); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClosePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClosePacket.java index 8914bfe7..4d1b4015 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClosePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundContainerClosePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundContainerClosePacket implements Packet { +public class ServerboundContainerClosePacket implements MinecraftPacket { private final int containerId; - public ServerboundContainerClosePacket(NetInput in) throws IOException { + public ServerboundContainerClosePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readByte(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundEditBookPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundEditBookPacket.java index 910a9f87..3818c9a6 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundEditBookPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundEditBookPacket.java @@ -1,13 +1,13 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -15,35 +15,35 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ServerboundEditBookPacket implements Packet { +public class ServerboundEditBookPacket implements MinecraftPacket { private final int slot; private final List pages; private final @Nullable String title; - public ServerboundEditBookPacket(NetInput in) throws IOException { - this.slot = in.readVarInt(); + public ServerboundEditBookPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.slot = helper.readVarInt(in); this.pages = new ArrayList<>(); - int pagesSize = in.readVarInt(); + int pagesSize = helper.readVarInt(in); for (int i = 0; i < pagesSize; i++) { - this.pages.add(in.readString()); + this.pages.add(helper.readString(in)); } if (in.readBoolean()) { - this.title = in.readString(); + this.title = helper.readString(in); } else { this.title = null; } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(slot); - out.writeVarInt(this.pages.size()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, slot); + helper.writeVarInt(out, this.pages.size()); for (String page : this.pages) { - out.writeString(page); + helper.writeString(out, page); } out.writeBoolean(this.title != null); if (this.title != null) { - out.writeString(title); + helper.writeString(out, title); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPickItemPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPickItemPacket.java index f03033fd..d7c196e3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPickItemPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPickItemPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPickItemPacket implements Packet { +public class ServerboundPickItemPacket implements MinecraftPacket { private final int slot; - public ServerboundPickItemPacket(NetInput in) throws IOException { - this.slot = in.readVarInt(); + public ServerboundPickItemPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.slot = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.slot); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.slot); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPlaceRecipePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPlaceRecipePacket.java index f5f6a348..37e0c023 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPlaceRecipePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundPlaceRecipePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,21 +13,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPlaceRecipePacket implements Packet { +public class ServerboundPlaceRecipePacket implements MinecraftPacket { private final int containerId; private final @NonNull String recipeId; private final boolean makeAll; - public ServerboundPlaceRecipePacket(NetInput in) throws IOException { + public ServerboundPlaceRecipePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.containerId = in.readByte(); - this.recipeId = in.readString(); + this.recipeId = helper.readString(in); this.makeAll = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeByte(this.containerId); - out.writeString(this.recipeId); + helper.writeString(out, this.recipeId); out.writeBoolean(this.makeAll); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookChangeSettingsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookChangeSettingsPacket.java index 0a698973..5f8da17c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookChangeSettingsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookChangeSettingsPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.inventory.CraftingBookStateType; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,20 +15,20 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundRecipeBookChangeSettingsPacket implements Packet { +public class ServerboundRecipeBookChangeSettingsPacket implements MinecraftPacket { private final @NonNull CraftingBookStateType type; private final boolean bookOpen; private final boolean filterActive; - public ServerboundRecipeBookChangeSettingsPacket(NetInput in) throws IOException { - this.type = MagicValues.key(CraftingBookStateType.class, in.readVarInt()); + public ServerboundRecipeBookChangeSettingsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.type = MagicValues.key(CraftingBookStateType.class, helper.readVarInt(in)); this.bookOpen = in.readBoolean(); this.filterActive = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.type)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.type)); out.writeBoolean(this.bookOpen); out.writeBoolean(this.filterActive); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookSeenRecipePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookSeenRecipePacket.java index c0fbf7a3..350b8bc8 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookSeenRecipePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRecipeBookSeenRecipePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,15 +13,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundRecipeBookSeenRecipePacket implements Packet { +public class ServerboundRecipeBookSeenRecipePacket implements MinecraftPacket { private final @NonNull String recipeId; - public ServerboundRecipeBookSeenRecipePacket(NetInput in) throws IOException { - this.recipeId = in.readString(); + public ServerboundRecipeBookSeenRecipePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.recipeId = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.recipeId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.recipeId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRenameItemPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRenameItemPacket.java index 917ecc05..75a2f05d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRenameItemPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundRenameItemPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,15 +13,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundRenameItemPacket implements Packet { +public class ServerboundRenameItemPacket implements MinecraftPacket { private final @NonNull String name; - public ServerboundRenameItemPacket(NetInput in) throws IOException { - this.name = in.readString(); + public ServerboundRenameItemPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.name = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.name); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.name); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSeenAdvancementsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSeenAdvancementsPacket.java index e2d180a1..ba2462cb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSeenAdvancementsPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSeenAdvancementsPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.inventory.AdvancementTabAction; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; @@ -14,7 +14,7 @@ import java.io.IOException; @ToString @EqualsAndHashCode -public class ServerboundSeenAdvancementsPacket implements Packet { +public class ServerboundSeenAdvancementsPacket implements MinecraftPacket { @Getter private final @NonNull AdvancementTabAction action; private final String tabId; @@ -41,14 +41,14 @@ public class ServerboundSeenAdvancementsPacket implements Packet { return this.tabId; } - public ServerboundSeenAdvancementsPacket(NetInput in) throws IOException { - this.action = MagicValues.key(AdvancementTabAction.class, in.readVarInt()); + public ServerboundSeenAdvancementsPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.action = MagicValues.key(AdvancementTabAction.class, helper.readVarInt(in)); switch (this.action) { case CLOSED_SCREEN: this.tabId = null; break; case OPENED_TAB: - this.tabId = in.readString(); + this.tabId = helper.readString(in); break; default: throw new IOException("Unknown advancement tab action: " + this.action); @@ -56,13 +56,13 @@ public class ServerboundSeenAdvancementsPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.action)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); switch (this.action) { case CLOSED_SCREEN: break; case OPENED_TAB: - out.writeString(this.tabId); + helper.writeString(out, this.tabId); break; default: throw new IOException("Unknown advancement tab action: " + this.action); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSelectTradePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSelectTradePacket.java index 07dec815..cb914923 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSelectTradePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSelectTradePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSelectTradePacket implements Packet { +public class ServerboundSelectTradePacket implements MinecraftPacket { private final int slot; - public ServerboundSelectTradePacket(NetInput in) throws IOException { - this.slot = in.readVarInt(); + public ServerboundSelectTradePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.slot = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.slot); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.slot); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetBeaconPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetBeaconPacket.java index 02a7c91c..e1891acc 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetBeaconPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetBeaconPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -13,34 +13,34 @@ import java.util.OptionalInt; @Data @With @AllArgsConstructor -public class ServerboundSetBeaconPacket implements Packet { +public class ServerboundSetBeaconPacket implements MinecraftPacket { private final OptionalInt primaryEffect; private final OptionalInt secondaryEffect; - public ServerboundSetBeaconPacket(NetInput in) throws IOException { + public ServerboundSetBeaconPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { if (in.readBoolean()) { - this.primaryEffect = OptionalInt.of(in.readVarInt()); + this.primaryEffect = OptionalInt.of(helper.readVarInt(in)); } else { this.primaryEffect = OptionalInt.empty(); } if (in.readBoolean()) { - this.secondaryEffect = OptionalInt.of(in.readVarInt()); + this.secondaryEffect = OptionalInt.of(helper.readVarInt(in)); } else { this.secondaryEffect = OptionalInt.empty(); } } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.primaryEffect.isPresent()); if (this.primaryEffect.isPresent()) { - out.writeVarInt(this.primaryEffect.getAsInt()); + helper.writeVarInt(out, this.primaryEffect.getAsInt()); } out.writeBoolean(this.secondaryEffect.isPresent()); if (this.secondaryEffect.isPresent()) { - out.writeVarInt(this.secondaryEffect.getAsInt()); + helper.writeVarInt(out, this.secondaryEffect.getAsInt()); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandBlockPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandBlockPacket.java index a0b93e57..1a9cef72 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandBlockPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandBlockPacket.java @@ -1,12 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.level.block.CommandBlockMode; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -17,7 +16,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSetCommandBlockPacket implements Packet { +public class ServerboundSetCommandBlockPacket implements MinecraftPacket { private static final int FLAG_TRACK_OUTPUT = 0x01; private static final int FLAG_CONDITIONAL = 0x02; private static final int FLAG_AUTOMATIC = 0x04; @@ -29,10 +28,10 @@ public class ServerboundSetCommandBlockPacket implements Packet { private final boolean conditional; private final boolean automatic; - public ServerboundSetCommandBlockPacket(NetInput in) throws IOException { - this.position = Position.read(in); - this.command = in.readString(); - this.mode = MagicValues.key(CommandBlockMode.class, in.readVarInt()); + public ServerboundSetCommandBlockPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); + this.command = helper.readString(in); + this.mode = MagicValues.key(CommandBlockMode.class, helper.readVarInt(in)); int flags = in.readUnsignedByte(); this.doesTrackOutput = (flags & FLAG_TRACK_OUTPUT) != 0; @@ -41,10 +40,10 @@ public class ServerboundSetCommandBlockPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); - out.writeString(this.command); - out.writeVarInt(MagicValues.value(Integer.class, this.mode)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); + helper.writeString(out, this.command); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.mode)); int flags = 0; if (this.doesTrackOutput) { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandMinecartPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandMinecartPacket.java index db574cd9..74782dd0 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandMinecartPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCommandMinecartPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,21 +13,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSetCommandMinecartPacket implements Packet { +public class ServerboundSetCommandMinecartPacket implements MinecraftPacket { private final int entityId; private final @NonNull String command; private final boolean doesTrackOutput; - public ServerboundSetCommandMinecartPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.command = in.readString(); + public ServerboundSetCommandMinecartPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.command = helper.readString(in); this.doesTrackOutput = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeString(this.command); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeString(out, this.command); out.writeBoolean(this.doesTrackOutput); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCreativeModeSlotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCreativeModeSlotPacket.java index a3e69c86..024179f3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCreativeModeSlotPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetCreativeModeSlotPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSetCreativeModeSlotPacket implements Packet { +public class ServerboundSetCreativeModeSlotPacket implements MinecraftPacket { private final int slot; private final @NonNull ItemStack clickedItem; - public ServerboundSetCreativeModeSlotPacket(NetInput in) throws IOException { + public ServerboundSetCreativeModeSlotPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.slot = in.readShort(); - this.clickedItem = ItemStack.read(in); + this.clickedItem = helper.readItemStack(in); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeShort(this.slot); - ItemStack.write(out, this.clickedItem); + helper.writeItemStack(out, this.clickedItem); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetJigsawBlockPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetJigsawBlockPacket.java index 27f529ae..f1c7526c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetJigsawBlockPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetJigsawBlockPacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,7 +14,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSetJigsawBlockPacket implements Packet { +public class ServerboundSetJigsawBlockPacket implements MinecraftPacket { private final @NonNull Vector3i position; private final @NonNull String name; private final @NonNull String target; @@ -23,22 +22,22 @@ public class ServerboundSetJigsawBlockPacket implements Packet { private final @NonNull String finalState; private final @NonNull String jointType; - public ServerboundSetJigsawBlockPacket(NetInput in) throws IOException { - this.position = Position.read(in); - this.name = in.readString(); - this.target = in.readString(); - this.pool = in.readString(); - this.finalState = in.readString(); - this.jointType = in.readString(); + public ServerboundSetJigsawBlockPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); + this.name = helper.readString(in); + this.target = helper.readString(in); + this.pool = helper.readString(in); + this.finalState = helper.readString(in); + this.jointType = helper.readString(in); } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); - out.writeString(this.name); - out.writeString(this.target); - out.writeString(this.pool); - out.writeString(this.finalState); - out.writeString(this.jointType); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); + helper.writeString(out, this.name); + helper.writeString(out, this.target); + helper.writeString(out, this.pool); + helper.writeString(out, this.finalState); + helper.writeString(out, this.jointType); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetStructureBlockPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetStructureBlockPacket.java index ac54f9f9..b9bf4daf 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetStructureBlockPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/inventory/ServerboundSetStructureBlockPacket.java @@ -1,15 +1,14 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockAction; import com.github.steveice10.mc.protocol.data.game.inventory.UpdateStructureBlockMode; import com.github.steveice10.mc.protocol.data.game.level.block.StructureMirror; import com.github.steveice10.mc.protocol.data.game.level.block.StructureRotation; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -20,7 +19,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSetStructureBlockPacket implements Packet { +public class ServerboundSetStructureBlockPacket implements MinecraftPacket { private static final int FLAG_IGNORE_ENTITIES = 0x01; private static final int FLAG_SHOW_AIR = 0x02; private static final int FLAG_SHOW_BOUNDING_BOX = 0x04; @@ -40,18 +39,18 @@ public class ServerboundSetStructureBlockPacket implements Packet { private final boolean showAir; private final boolean showBoundingBox; - public ServerboundSetStructureBlockPacket(NetInput in) throws IOException { - this.position = Position.read(in); - this.action = MagicValues.key(UpdateStructureBlockAction.class, in.readVarInt()); - this.mode = MagicValues.key(UpdateStructureBlockMode.class, in.readVarInt()); - this.name = in.readString(); + public ServerboundSetStructureBlockPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); + this.action = MagicValues.key(UpdateStructureBlockAction.class, helper.readVarInt(in)); + this.mode = MagicValues.key(UpdateStructureBlockMode.class, helper.readVarInt(in)); + this.name = helper.readString(in); this.offset = Vector3i.from(in.readByte(), in.readByte(), in.readByte()); this.size = Vector3i.from(in.readUnsignedByte(), in.readUnsignedByte(), in.readUnsignedByte()); - this.mirror = MagicValues.key(StructureMirror.class, in.readVarInt()); - this.rotation = MagicValues.key(StructureRotation.class, in.readVarInt()); - this.metadata = in.readString(); + this.mirror = MagicValues.key(StructureMirror.class, helper.readVarInt(in)); + this.rotation = MagicValues.key(StructureRotation.class, helper.readVarInt(in)); + this.metadata = helper.readString(in); this.integrity = in.readFloat(); - this.seed = in.readVarLong(); + this.seed = helper.readVarLong(in); int flags = in.readUnsignedByte(); this.ignoreEntities = (flags & FLAG_IGNORE_ENTITIES) != 0; @@ -60,22 +59,22 @@ public class ServerboundSetStructureBlockPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); - out.writeVarInt(MagicValues.value(Integer.class, this.action)); - out.writeVarInt(MagicValues.value(Integer.class, this.mode)); - out.writeString(this.name); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.mode)); + helper.writeString(out, this.name); out.writeByte(this.offset.getX()); out.writeByte(this.offset.getY()); out.writeByte(this.offset.getZ()); out.writeByte(this.size.getX()); out.writeByte(this.size.getY()); out.writeByte(this.size.getZ()); - out.writeVarInt(MagicValues.value(Integer.class, this.mirror)); - out.writeVarInt(MagicValues.value(Integer.class, this.rotation)); - out.writeString(this.metadata); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.mirror)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.rotation)); + helper.writeString(out, this.metadata); out.writeFloat(this.integrity); - out.writeVarLong(this.seed); + helper.writeVarLong(out, this.seed); int flags = 0; if (this.ignoreEntities) { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundAcceptTeleportationPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundAcceptTeleportationPacket.java index edff0632..4daf051e 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundAcceptTeleportationPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundAcceptTeleportationPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundAcceptTeleportationPacket implements Packet { +public class ServerboundAcceptTeleportationPacket implements MinecraftPacket { private final int id; - public ServerboundAcceptTeleportationPacket(NetInput in) throws IOException { - this.id = in.readVarInt(); + public ServerboundAcceptTeleportationPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.id = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.id); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.id); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundBlockEntityTagQuery.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundBlockEntityTagQuery.java index 92caddec..3f3efde7 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundBlockEntityTagQuery.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundBlockEntityTagQuery.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +14,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundBlockEntityTagQuery implements Packet { +public class ServerboundBlockEntityTagQuery implements MinecraftPacket { private final int transactionId; private final @NonNull Vector3i position; - public ServerboundBlockEntityTagQuery(NetInput in) throws IOException { - this.transactionId = in.readVarInt(); - this.position = Position.read(in); + public ServerboundBlockEntityTagQuery(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.transactionId = helper.readVarInt(in); + this.position = helper.readPosition(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.transactionId); - Position.write(out, this.position); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.transactionId); + helper.writePosition(out, this.position); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundEntityTagQuery.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundEntityTagQuery.java index b3ae23c4..e1a938cf 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundEntityTagQuery.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundEntityTagQuery.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundEntityTagQuery implements Packet { +public class ServerboundEntityTagQuery implements MinecraftPacket { private final int transactionId; private final int entityId; - public ServerboundEntityTagQuery(NetInput in) throws IOException { - this.transactionId = in.readVarInt(); - this.entityId = in.readVarInt(); + public ServerboundEntityTagQuery(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.transactionId = helper.readVarInt(in); + this.entityId = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.transactionId); - out.writeVarInt(this.entityId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.transactionId); + helper.writeVarInt(out, this.entityId); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundJigsawGeneratePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundJigsawGeneratePacket.java index 996325c6..a5482388 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundJigsawGeneratePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundJigsawGeneratePacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,21 +14,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundJigsawGeneratePacket implements Packet { +public class ServerboundJigsawGeneratePacket implements MinecraftPacket { private final @NonNull Vector3i position; private final int levels; private final boolean keepJigsaws; - public ServerboundJigsawGeneratePacket(NetInput in) throws IOException { - this.position = Position.read(in); - this.levels = in.readVarInt(); + public ServerboundJigsawGeneratePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); + this.levels = helper.readVarInt(in); this.keepJigsaws = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); - out.writeVarInt(this.levels); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); + helper.writeVarInt(out, this.levels); out.writeBoolean(this.keepJigsaws); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundMoveVehiclePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundMoveVehiclePacket.java index d507afed..7cbc98f5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundMoveVehiclePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundMoveVehiclePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,14 +12,14 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundMoveVehiclePacket implements Packet { +public class ServerboundMoveVehiclePacket implements MinecraftPacket { private final double x; private final double y; private final double z; private final float yaw; private final float pitch; - public ServerboundMoveVehiclePacket(NetInput in) throws IOException { + public ServerboundMoveVehiclePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -28,7 +28,7 @@ public class ServerboundMoveVehiclePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPaddleBoatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPaddleBoatPacket.java index 6e6d61f7..47d8a96e 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPaddleBoatPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPaddleBoatPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,17 +12,17 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPaddleBoatPacket implements Packet { +public class ServerboundPaddleBoatPacket implements MinecraftPacket { private final boolean rightPaddleTurning; private final boolean leftPaddleTurning; - public ServerboundPaddleBoatPacket(NetInput in) throws IOException { + public ServerboundPaddleBoatPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.rightPaddleTurning = in.readBoolean(); this.leftPaddleTurning = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.rightPaddleTurning); out.writeBoolean(this.leftPaddleTurning); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPlayerInputPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPlayerInputPacket.java index 5e0ca40c..2e89ff5f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPlayerInputPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundPlayerInputPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPlayerInputPacket implements Packet { +public class ServerboundPlayerInputPacket implements MinecraftPacket { private static final int FLAG_JUMP = 0x01; private static final int FLAG_DISMOUNT = 0x02; @@ -21,7 +21,7 @@ public class ServerboundPlayerInputPacket implements Packet { private final boolean jump; private final boolean dismount; - public ServerboundPlayerInputPacket(NetInput in) throws IOException { + public ServerboundPlayerInputPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.sideways = in.readFloat(); this.forward = in.readFloat(); @@ -31,7 +31,7 @@ public class ServerboundPlayerInputPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeFloat(this.sideways); out.writeFloat(this.forward); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundSignUpdatePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundSignUpdatePacket.java index f5ec0463..f9949c5a 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundSignUpdatePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundSignUpdatePacket.java @@ -1,10 +1,9 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.Data; import lombok.NonNull; import lombok.With; @@ -14,7 +13,7 @@ import java.util.Arrays; @Data @With -public class ServerboundSignUpdatePacket implements Packet { +public class ServerboundSignUpdatePacket implements MinecraftPacket { private final @NonNull Vector3i position; private final @NonNull String[] lines; @@ -27,19 +26,19 @@ public class ServerboundSignUpdatePacket implements Packet { this.lines = Arrays.copyOf(lines, lines.length); } - public ServerboundSignUpdatePacket(NetInput in) throws IOException { - this.position = Position.read(in); + public ServerboundSignUpdatePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.position = helper.readPosition(in); this.lines = new String[4]; for (int count = 0; count < this.lines.length; count++) { - this.lines[count] = in.readString(); + this.lines[count] = helper.readString(in); } } @Override - public void write(NetOutput out) throws IOException { - Position.write(out, this.position); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writePosition(out, this.position); for (String line : this.lines) { - out.writeString(line); + helper.writeString(out, line); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundTeleportToEntityPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundTeleportToEntityPacket.java index 453a0808..221b0d54 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundTeleportToEntityPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/level/ServerboundTeleportToEntityPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.level; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -14,15 +14,15 @@ import java.util.UUID; @Data @With @AllArgsConstructor -public class ServerboundTeleportToEntityPacket implements Packet { +public class ServerboundTeleportToEntityPacket implements MinecraftPacket { private final @NonNull UUID target; - public ServerboundTeleportToEntityPacket(NetInput in) throws IOException { - this.target = in.readUUID(); + public ServerboundTeleportToEntityPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.target = helper.readUUID(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeUUID(this.target); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeUUID(out, this.target); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundInteractPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundInteractPacket.java index 9f27a21c..bcacea78 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundInteractPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundInteractPacket.java @@ -1,11 +1,11 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -16,7 +16,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundInteractPacket implements Packet { +public class ServerboundInteractPacket implements MinecraftPacket { private final int entityId; private final @NonNull InteractAction action; @@ -34,9 +34,9 @@ public class ServerboundInteractPacket implements Packet { this(entityId, action, 0, 0, 0, hand, isSneaking); } - public ServerboundInteractPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.action = MagicValues.key(InteractAction.class, in.readVarInt()); + public ServerboundInteractPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.action = MagicValues.key(InteractAction.class, helper.readVarInt(in)); if (this.action == InteractAction.INTERACT_AT) { this.targetX = in.readFloat(); this.targetY = in.readFloat(); @@ -48,7 +48,7 @@ public class ServerboundInteractPacket implements Packet { } if (this.action == InteractAction.INTERACT || this.action == InteractAction.INTERACT_AT) { - this.hand = MagicValues.key(Hand.class, in.readVarInt()); + this.hand = MagicValues.key(Hand.class, helper.readVarInt(in)); } else { this.hand = null; } @@ -56,9 +56,9 @@ public class ServerboundInteractPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeVarInt(MagicValues.value(Integer.class, this.action)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); if (this.action == InteractAction.INTERACT_AT) { out.writeFloat(this.targetX); out.writeFloat(this.targetY); @@ -66,7 +66,7 @@ public class ServerboundInteractPacket implements Packet { } if (this.action == InteractAction.INTERACT || this.action == InteractAction.INTERACT_AT) { - out.writeVarInt(MagicValues.value(Integer.class, this.hand)); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.hand)); } out.writeBoolean(this.isSneaking); } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosPacket.java index f074a599..4137143d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,13 +12,13 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundMovePlayerPosPacket implements Packet { +public class ServerboundMovePlayerPosPacket implements MinecraftPacket { private final boolean onGround; private final double x; private final double y; private final double z; - public ServerboundMovePlayerPosPacket(NetInput in) throws IOException { + public ServerboundMovePlayerPosPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -26,7 +26,7 @@ public class ServerboundMovePlayerPosPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosRotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosRotPacket.java index f88c0fba..773b4673 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosRotPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerPosRotPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundMovePlayerPosRotPacket implements Packet { +public class ServerboundMovePlayerPosRotPacket implements MinecraftPacket { private final boolean onGround; private final double x; private final double y; @@ -20,7 +20,7 @@ public class ServerboundMovePlayerPosRotPacket implements Packet { private final float yaw; private final float pitch; - public ServerboundMovePlayerPosRotPacket(NetInput in) throws IOException { + public ServerboundMovePlayerPosRotPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.x = in.readDouble(); this.y = in.readDouble(); this.z = in.readDouble(); @@ -30,7 +30,7 @@ public class ServerboundMovePlayerPosRotPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeDouble(this.x); out.writeDouble(this.y); out.writeDouble(this.z); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerRotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerRotPacket.java index 0b37426b..19414987 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerRotPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerRotPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,19 +12,19 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundMovePlayerRotPacket implements Packet { +public class ServerboundMovePlayerRotPacket implements MinecraftPacket { private final boolean onGround; private final float yaw; private final float pitch; - public ServerboundMovePlayerRotPacket(NetInput in) throws IOException { + public ServerboundMovePlayerRotPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.yaw = in.readFloat(); this.pitch = in.readFloat(); this.onGround = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeFloat(this.yaw); out.writeFloat(this.pitch); out.writeBoolean(this.onGround); diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerStatusOnlyPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerStatusOnlyPacket.java index 33f67157..997f5a51 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerStatusOnlyPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundMovePlayerStatusOnlyPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundMovePlayerStatusOnlyPacket implements Packet { +public class ServerboundMovePlayerStatusOnlyPacket implements MinecraftPacket { private final boolean onGround; - public ServerboundMovePlayerStatusOnlyPacket(NetInput in) throws IOException { + public ServerboundMovePlayerStatusOnlyPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.onGround = in.readBoolean(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeBoolean(this.onGround); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerAbilitiesPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerAbilitiesPacket.java index 92fc2747..c6744214 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerAbilitiesPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerAbilitiesPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,18 +12,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPlayerAbilitiesPacket implements Packet { +public class ServerboundPlayerAbilitiesPacket implements MinecraftPacket { private static final int FLAG_FLYING = 0x02; private final boolean flying; - public ServerboundPlayerAbilitiesPacket(NetInput in) throws IOException { + public ServerboundPlayerAbilitiesPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { byte flags = in.readByte(); this.flying = (flags & FLAG_FLYING) > 0; } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { int flags = 0; if (this.flying) { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerActionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerActionPacket.java index bd4b073d..38229a2c 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerActionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerActionPacket.java @@ -1,13 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -18,24 +17,24 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPlayerActionPacket implements Packet { +public class ServerboundPlayerActionPacket implements MinecraftPacket { private final @NonNull PlayerAction action; private final @NonNull Vector3i position; private final @NonNull Direction face; private final int sequence; - public ServerboundPlayerActionPacket(NetInput in) throws IOException { - this.action = MagicValues.key(PlayerAction.class, in.readVarInt()); - this.position = Position.read(in); + public ServerboundPlayerActionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.action = MagicValues.key(PlayerAction.class, helper.readVarInt(in)); + this.position = helper.readPosition(in); this.face = Direction.VALUES[in.readUnsignedByte()]; - this.sequence = in.readVarInt(); + this.sequence = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.action)); - Position.write(out, this.position); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.action)); + helper.writePosition(out, this.position); out.writeByte(this.face.ordinal()); - out.writeVarInt(this.sequence); + helper.writeVarInt(out, this.sequence); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerCommandPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerCommandPacket.java index 84cf0fec..146873bb 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerCommandPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundPlayerCommandPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,7 +15,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPlayerCommandPacket implements Packet { +public class ServerboundPlayerCommandPacket implements MinecraftPacket { private final int entityId; private final @NonNull PlayerState state; private final int jumpBoost; @@ -24,16 +24,16 @@ public class ServerboundPlayerCommandPacket implements Packet { this(entityId, state, 0); } - public ServerboundPlayerCommandPacket(NetInput in) throws IOException { - this.entityId = in.readVarInt(); - this.state = MagicValues.key(PlayerState.class, in.readVarInt()); - this.jumpBoost = in.readVarInt(); + public ServerboundPlayerCommandPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.entityId = helper.readVarInt(in); + this.state = MagicValues.key(PlayerState.class, helper.readVarInt(in)); + this.jumpBoost = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.entityId); - out.writeVarInt(MagicValues.value(Integer.class, this.state)); - out.writeVarInt(this.jumpBoost); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.entityId); + helper.writeVarInt(out, MagicValues.value(Integer.class, this.state)); + helper.writeVarInt(out, this.jumpBoost); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSetCarriedItemPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSetCarriedItemPacket.java index 9b251c3b..302ec208 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSetCarriedItemPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSetCarriedItemPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSetCarriedItemPacket implements Packet { +public class ServerboundSetCarriedItemPacket implements MinecraftPacket { private final int slot; - public ServerboundSetCarriedItemPacket(NetInput in) throws IOException { + public ServerboundSetCarriedItemPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.slot = in.readShort(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeShort(this.slot); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSwingPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSwingPacket.java index cc882e2c..fb54cea9 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSwingPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundSwingPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,15 +15,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundSwingPacket implements Packet { +public class ServerboundSwingPacket implements MinecraftPacket { private final @NonNull Hand hand; - public ServerboundSwingPacket(NetInput in) throws IOException { - this.hand = MagicValues.key(Hand.class, in.readVarInt()); + public ServerboundSwingPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.hand = MagicValues.key(Hand.class, helper.readVarInt(in)); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.hand)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.hand)); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemOnPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemOnPacket.java index 5e0a8384..b5110a28 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemOnPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemOnPacket.java @@ -1,13 +1,12 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.nukkitx.math.vector.Vector3i; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -18,7 +17,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundUseItemOnPacket implements Packet { +public class ServerboundUseItemOnPacket implements MinecraftPacket { private final @NonNull Vector3i position; private final @NonNull Direction face; private final @NonNull Hand hand; @@ -28,26 +27,26 @@ public class ServerboundUseItemOnPacket implements Packet { private final boolean insideBlock; private final int sequence; - public ServerboundUseItemOnPacket(NetInput in) throws IOException { - this.hand = MagicValues.key(Hand.class, in.readVarInt()); - this.position = Position.read(in); - this.face = in.readEnum(Direction.VALUES); + public ServerboundUseItemOnPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.hand = MagicValues.key(Hand.class, helper.readVarInt(in)); + this.position = helper.readPosition(in); + this.face = helper.readDirection(in); this.cursorX = in.readFloat(); this.cursorY = in.readFloat(); this.cursorZ = in.readFloat(); this.insideBlock = in.readBoolean(); - this.sequence = in.readVarInt(); + this.sequence = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.hand)); - Position.write(out, this.position); - out.writeEnum(this.face); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.hand)); + helper.writePosition(out, this.position); + helper.writeDirection(out, this.face); out.writeFloat(this.cursorX); out.writeFloat(this.cursorY); out.writeFloat(this.cursorZ); out.writeBoolean(this.insideBlock); - out.writeVarInt(this.sequence); + helper.writeVarInt(out, this.sequence); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemPacket.java index cf58244c..be9a36e2 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/serverbound/player/ServerboundUseItemPacket.java @@ -1,10 +1,10 @@ package com.github.steveice10.mc.protocol.packet.ingame.serverbound.player; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.MagicValues; import com.github.steveice10.mc.protocol.data.game.entity.player.Hand; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,18 +15,18 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundUseItemPacket implements Packet { +public class ServerboundUseItemPacket implements MinecraftPacket { private final @NonNull Hand hand; private final int sequence; - public ServerboundUseItemPacket(NetInput in) throws IOException { - this.hand = MagicValues.key(Hand.class, in.readVarInt()); - this.sequence = in.readVarInt(); + public ServerboundUseItemPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.hand = MagicValues.key(Hand.class, helper.readVarInt(in)); + this.sequence = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(MagicValues.value(Integer.class, this.hand)); - out.writeVarInt(this.sequence); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, MagicValues.value(Integer.class, this.hand)); + helper.writeVarInt(out, this.sequence); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundCustomQueryPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundCustomQueryPacket.java index c6020265..6a9c4c84 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundCustomQueryPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundCustomQueryPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.login.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -13,21 +13,21 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundCustomQueryPacket implements Packet { +public class ClientboundCustomQueryPacket implements MinecraftPacket { private final int messageId; private final @NonNull String channel; private final @NonNull byte[] data; - public ClientboundCustomQueryPacket(NetInput in) throws IOException { - this.messageId = in.readVarInt(); - this.channel = in.readString(); - this.data = in.readBytes(in.available()); + public ClientboundCustomQueryPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.messageId = helper.readVarInt(in); + this.channel = helper.readString(in); + this.data = helper.readByteArray(in, ByteBuf::readableBytes); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.messageId); - out.writeString(this.channel); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.messageId); + helper.writeString(out, this.channel); out.writeBytes(this.data); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java index b6133ac6..976fd6e3 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.login.clientbound; import com.github.steveice10.mc.auth.data.GameProfile; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -16,19 +16,19 @@ import java.util.List; @Data @With @AllArgsConstructor -public class ClientboundGameProfilePacket implements Packet { +public class ClientboundGameProfilePacket implements MinecraftPacket { private final @NonNull GameProfile profile; - public ClientboundGameProfilePacket(NetInput in) throws IOException { - GameProfile profile = new GameProfile(in.readUUID(), in.readString()); - int properties = in.readVarInt(); + public ClientboundGameProfilePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + GameProfile profile = new GameProfile(helper.readUUID(in), helper.readString(in)); + int properties = helper.readVarInt(in); List propertyList = new ArrayList<>(); for (int index = 0; index < properties; index++) { - String propertyName = in.readString(); - String value = in.readString(); + String propertyName = helper.readString(in); + String value = helper.readString(in); String signature = null; if (in.readBoolean()) { - signature = in.readString(); + signature = helper.readString(in); } propertyList.add(new GameProfile.Property(propertyName, value, signature)); @@ -39,16 +39,16 @@ public class ClientboundGameProfilePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeUUID(this.profile.getId()); - out.writeString(this.profile.getName()); - out.writeVarInt(this.profile.getProperties().size()); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeUUID(out, this.profile.getId()); + helper.writeString(out, this.profile.getName()); + helper.writeVarInt(out, this.profile.getProperties().size()); for (GameProfile.Property property : this.profile.getProperties()) { - out.writeString(property.getName()); - out.writeString(property.getValue()); + helper.writeString(out, property.getName()); + helper.writeString(out, property.getValue()); out.writeBoolean(property.hasSignature()); if (property.hasSignature()) { - out.writeString(property.getSignature()); + helper.writeString(out, property.getSignature()); } } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundHelloPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundHelloPacket.java index 32f2ba01..c4e90774 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundHelloPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundHelloPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.login.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -17,15 +17,15 @@ import java.security.spec.X509EncodedKeySpec; @Data @With @AllArgsConstructor -public class ClientboundHelloPacket implements Packet { +public class ClientboundHelloPacket implements MinecraftPacket { private final @NonNull String serverId; private final @NonNull PublicKey publicKey; private final @NonNull byte[] verifyToken; - public ClientboundHelloPacket(NetInput in) throws IOException { - this.serverId = in.readString(); - byte[] publicKey = in.readBytes(in.readVarInt()); - this.verifyToken = in.readBytes(in.readVarInt()); + public ClientboundHelloPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.serverId = helper.readString(in); + byte[] publicKey = helper.readByteArray(in); + this.verifyToken = helper.readByteArray(in); try { this.publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKey)); @@ -35,13 +35,11 @@ public class ClientboundHelloPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.serverId); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.serverId); byte[] encoded = this.publicKey.getEncoded(); - out.writeVarInt(encoded.length); - out.writeBytes(encoded); - out.writeVarInt(this.verifyToken.length); - out.writeBytes(this.verifyToken); + helper.writeByteArray(out, encoded); + helper.writeByteArray(out, this.verifyToken); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java index 704ddcc9..d783623d 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.login.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,16 +12,16 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundLoginCompressionPacket implements Packet { +public class ClientboundLoginCompressionPacket implements MinecraftPacket { private final int threshold; - public ClientboundLoginCompressionPacket(NetInput in) throws IOException { - this.threshold = in.readVarInt(); + public ClientboundLoginCompressionPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.threshold = helper.readVarInt(in); } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.threshold); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.threshold); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java index 1e10b4c3..0ae0e704 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.login.clientbound; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -15,20 +15,20 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundLoginDisconnectPacket implements Packet { +public class ClientboundLoginDisconnectPacket implements MinecraftPacket { private final @NonNull Component reason; public ClientboundLoginDisconnectPacket(String text) { this(DefaultComponentSerializer.get().deserialize(text)); } - public ClientboundLoginDisconnectPacket(NetInput in) throws IOException { - this.reason = DefaultComponentSerializer.get().deserialize(in.readString()); + public ClientboundLoginDisconnectPacket(ByteBuf in, MinecraftCodecHelper codecHelper) throws IOException { + this.reason = DefaultComponentSerializer.get().deserialize(codecHelper.readString(in)); } @Override - public void write(NetOutput out) throws IOException { - out.writeString(DefaultComponentSerializer.get().serialize(this.reason)); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, DefaultComponentSerializer.get().serialize(this.reason)); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundCustomQueryPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundCustomQueryPacket.java index 1ceab913..ba1625ce 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundCustomQueryPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundCustomQueryPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.login.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,7 +12,7 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundCustomQueryPacket implements Packet { +public class ServerboundCustomQueryPacket implements MinecraftPacket { private final int messageId; private final byte[] data; @@ -20,19 +20,19 @@ public class ServerboundCustomQueryPacket implements Packet { this(messageId, null); } - public ServerboundCustomQueryPacket(NetInput in) throws IOException { - this.messageId = in.readVarInt(); + public ServerboundCustomQueryPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.messageId = helper.readVarInt(in); if (in.readBoolean()) { - this.data = in.readBytes(in.available()); + this.data = helper.readByteArray(in, ByteBuf::readableBytes); } else { this.data = null; } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.messageId); - if (data != null) { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeVarInt(out, this.messageId); + if (this.data != null) { out.writeBoolean(true); out.writeBytes(this.data); } else { diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundHelloPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundHelloPacket.java index 6df291fc..d902a2ec 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundHelloPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundHelloPacket.java @@ -1,14 +1,14 @@ package com.github.steveice10.mc.protocol.packet.login.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; import lombok.With; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyFactory; @@ -18,18 +18,18 @@ import java.security.spec.X509EncodedKeySpec; @Data @With @AllArgsConstructor -public class ServerboundHelloPacket implements Packet { +public class ServerboundHelloPacket implements MinecraftPacket { private final @NonNull String username; private final @Nullable Long expiresAt; private final @Nullable PublicKey publicKey; private final @Nullable byte[] keySignature; - public ServerboundHelloPacket(NetInput in) throws IOException { - this.username = in.readString(); + public ServerboundHelloPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.username = helper.readString(in); if (in.readBoolean()) { this.expiresAt = in.readLong(); - byte[] publicKey = in.readBytes(in.readVarInt()); - this.keySignature = in.readBytes(in.readVarInt()); + byte[] publicKey = helper.readByteArray(in); + this.keySignature = helper.readByteArray(in); try { this.publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKey)); @@ -44,16 +44,14 @@ public class ServerboundHelloPacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { - out.writeString(this.username); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeString(out, this.username); out.writeBoolean(this.publicKey != null); if (this.publicKey != null) { out.writeLong(this.expiresAt); byte[] encoded = this.publicKey.getEncoded(); - out.writeVarInt(encoded.length); - out.writeBytes(encoded); - out.writeVarInt(this.keySignature.length); - out.writeBytes(this.keySignature); + helper.writeByteArray(out, encoded); + helper.writeByteArray(out, this.keySignature); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundKeyPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundKeyPacket.java index e07c86c0..6e1523de 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundKeyPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/serverbound/ServerboundKeyPacket.java @@ -1,13 +1,13 @@ package com.github.steveice10.mc.protocol.packet.login.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.NonNull; import lombok.ToString; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; @@ -19,7 +19,7 @@ import java.security.PublicKey; @ToString @EqualsAndHashCode -public class ServerboundKeyPacket implements Packet { +public class ServerboundKeyPacket implements MinecraftPacket { private final @NonNull byte[] sharedKey; private final @Nullable byte[] verifyToken; private final @Nullable Long salt; @@ -47,31 +47,28 @@ public class ServerboundKeyPacket implements Packet { return runEncryption(Cipher.DECRYPT_MODE, privateKey, this.verifyToken); } - public ServerboundKeyPacket(NetInput in) throws IOException { - this.sharedKey = in.readBytes(in.readVarInt()); + public ServerboundKeyPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + this.sharedKey = helper.readByteArray(in); if (in.readBoolean()) { - this.verifyToken = in.readBytes(in.readVarInt()); + this.verifyToken = helper.readByteArray(in); this.salt = null; this.signature = null; } else { this.salt = in.readLong(); - this.signature = in.readBytes(in.readVarInt()); + this.signature = helper.readByteArray(in); this.verifyToken = null; } } @Override - public void write(NetOutput out) throws IOException { - out.writeVarInt(this.sharedKey.length); - out.writeBytes(this.sharedKey); + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { + helper.writeByteArray(out, this.sharedKey); out.writeBoolean(this.verifyToken != null); if (this.verifyToken != null) { - out.writeVarInt(this.verifyToken.length); - out.writeBytes(this.verifyToken); + helper.writeByteArray(out, this.verifyToken); } else { out.writeLong(this.salt); - out.writeVarInt(this.signature.length); - out.writeBytes(this.signature); + helper.writeByteArray(out, this.signature); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundPongResponsePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundPongResponsePacket.java index 359fa5d3..24d93173 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundPongResponsePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundPongResponsePacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.status.clientbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ClientboundPongResponsePacket implements Packet { +public class ClientboundPongResponsePacket implements MinecraftPacket { private final long pingTime; - public ClientboundPongResponsePacket(NetInput in) throws IOException { + public ClientboundPongResponsePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.pingTime = in.readLong(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeLong(this.pingTime); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java index 092314d7..21dd9e30 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java @@ -2,17 +2,17 @@ package com.github.steveice10.mc.protocol.packet.status.clientbound; import com.github.steveice10.mc.auth.data.GameProfile; import com.github.steveice10.mc.auth.util.Base64; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.status.PlayerInfo; import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo; import com.github.steveice10.mc.protocol.data.status.VersionInfo; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NonNull; @@ -25,11 +25,11 @@ import java.nio.charset.StandardCharsets; @Data @With @AllArgsConstructor -public class ClientboundStatusResponsePacket implements Packet { +public class ClientboundStatusResponsePacket implements MinecraftPacket { private final @NonNull ServerStatusInfo info; - public ClientboundStatusResponsePacket(NetInput in) throws IOException { - JsonObject obj = new Gson().fromJson(in.readString(), JsonObject.class); + public ClientboundStatusResponsePacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { + JsonObject obj = new Gson().fromJson(helper.readString(in), JsonObject.class); JsonObject ver = obj.get("version").getAsJsonObject(); VersionInfo version = new VersionInfo(ver.get("name").getAsString(), ver.get("protocol").getAsInt()); JsonObject plrs = obj.get("players").getAsJsonObject(); @@ -58,7 +58,7 @@ public class ClientboundStatusResponsePacket implements Packet { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { JsonObject obj = new JsonObject(); JsonObject ver = new JsonObject(); ver.addProperty("name", this.info.getVersionInfo().getVersionName()); @@ -86,7 +86,7 @@ public class ClientboundStatusResponsePacket implements Packet { } obj.addProperty("previewsChat", this.info.isPreviewsChat()); - out.writeString(obj.toString()); + helper.writeString(out, obj.toString()); } @Override diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundPingRequestPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundPingRequestPacket.java index cfa4e94e..1da7f0d5 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundPingRequestPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundPingRequestPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.status.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.AllArgsConstructor; import lombok.Data; import lombok.With; @@ -12,15 +12,15 @@ import java.io.IOException; @Data @With @AllArgsConstructor -public class ServerboundPingRequestPacket implements Packet { +public class ServerboundPingRequestPacket implements MinecraftPacket { private final long pingTime; - public ServerboundPingRequestPacket(NetInput in) throws IOException { + public ServerboundPingRequestPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { this.pingTime = in.readLong(); } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { out.writeLong(this.pingTime); } } diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundStatusRequestPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundStatusRequestPacket.java index 9eab426e..958ff9fa 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundStatusRequestPacket.java +++ b/src/main/java/com/github/steveice10/mc/protocol/packet/status/serverbound/ServerboundStatusRequestPacket.java @@ -1,8 +1,8 @@ package com.github.steveice10.mc.protocol.packet.status.serverbound; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.NetOutput; -import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; +import io.netty.buffer.ByteBuf; import lombok.Data; import lombok.NoArgsConstructor; @@ -10,12 +10,12 @@ import java.io.IOException; @Data @NoArgsConstructor -public class ServerboundStatusRequestPacket implements Packet { +public class ServerboundStatusRequestPacket implements MinecraftPacket { - public ServerboundStatusRequestPacket(NetInput in) { + public ServerboundStatusRequestPacket(ByteBuf in, MinecraftCodecHelper helper) throws IOException { } @Override - public void write(NetOutput out) throws IOException { + public void serialize(ByteBuf out, MinecraftCodecHelper helper) throws IOException { } } diff --git a/src/test/java/com/github/steveice10/mc/protocol/data/ChunkTest.java b/src/test/java/com/github/steveice10/mc/protocol/data/ChunkTest.java index a01814d2..b7c97131 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/data/ChunkTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/data/ChunkTest.java @@ -1,19 +1,20 @@ package com.github.steveice10.mc.protocol.data; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection; import com.github.steveice10.mc.protocol.data.game.chunk.DataPalette; import com.github.steveice10.mc.protocol.data.game.chunk.palette.PaletteType; import com.github.steveice10.mc.protocol.data.game.chunk.palette.SingletonPalette; -import com.github.steveice10.packetlib.io.stream.StreamNetInput; -import com.github.steveice10.packetlib.io.stream.StreamNetOutput; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class ChunkTest { @@ -36,14 +37,13 @@ public class ChunkTest { @Test public void testChunkSectionEncoding() throws IOException { + MinecraftCodecHelper helper = new MinecraftCodecHelper(Int2ObjectMaps.emptyMap(), Collections.emptyMap()); for (ChunkSection section : chunkSectionsToTest) { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - StreamNetOutput out = new StreamNetOutput(stream); - ChunkSection.write(out, section, 4); - StreamNetInput in = new StreamNetInput(new ByteArrayInputStream(stream.toByteArray())); + ByteBuf buf = Unpooled.buffer(); + helper.writeChunkSection(buf, section); ChunkSection decoded; try { - decoded = ChunkSection.read(in, 4); + decoded = helper.readChunkSection(buf, 4); } catch (Exception e) { System.out.println(section); e.printStackTrace(); diff --git a/src/test/java/com/github/steveice10/mc/protocol/data/NetworkDataTests.java b/src/test/java/com/github/steveice10/mc/protocol/data/NetworkDataTests.java index e1579494..5446e9f8 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/data/NetworkDataTests.java +++ b/src/test/java/com/github/steveice10/mc/protocol/data/NetworkDataTests.java @@ -1,9 +1,15 @@ package com.github.steveice10.mc.protocol.data; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; import com.github.steveice10.mc.protocol.data.game.entity.Effect; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; import org.junit.Assert; import org.junit.Test; +import java.util.Collections; + /** * Miscellaneous tests for reading and writing classes to/from the network */ @@ -11,9 +17,12 @@ public class NetworkDataTests { @Test public void testEffects() { + MinecraftCodecHelper helper = new MinecraftCodecHelper(Int2ObjectMaps.emptyMap(), Collections.emptyMap()); for (Effect effect : Effect.VALUES) { - int networkId = Effect.toNetworkId(effect); - Assert.assertEquals(effect, Effect.fromNetworkId(networkId)); + ByteBuf buf = Unpooled.buffer(); + helper.writeEffect(buf, effect); + + Assert.assertEquals(effect, helper.readEffect(buf)); } } } diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java index f36388a2..ae118883 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java @@ -1,42 +1,43 @@ package com.github.steveice10.mc.protocol.packet; -import com.github.steveice10.packetlib.io.NetInput; -import com.github.steveice10.packetlib.io.stream.StreamNetInput; -import com.github.steveice10.packetlib.io.stream.StreamNetOutput; +import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.packetlib.packet.Packet; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.lang.reflect.Constructor; +import java.util.Collections; import static org.junit.Assert.assertEquals; public abstract class PacketTest { - private Packet[] packets; + private MinecraftPacket[] packets; - protected void setPackets(Packet... packets) { + protected void setPackets(MinecraftPacket... packets) { this.packets = packets; } @Test public void testPackets() throws Exception { - for (Packet packet : this.packets) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - packet.write(new StreamNetOutput(out)); - byte[] encoded = out.toByteArray(); + MinecraftCodecHelper helper = new MinecraftCodecHelper(Int2ObjectMaps.emptyMap(), Collections.emptyMap()); + for (MinecraftPacket packet : this.packets) { + ByteBuf buf = Unpooled.buffer(); + packet.serialize(buf, helper); - Packet decoded = this.createPacket(packet.getClass(), new StreamNetInput(new ByteArrayInputStream(encoded))); + Packet decoded = this.createPacket(packet.getClass(), helper, buf); assertEquals("Decoded packet does not match original: " + packet + " vs " + decoded, packet, decoded); } } - private Packet createPacket(Class clazz, NetInput in) { + private Packet createPacket(Class clazz, MinecraftCodecHelper helper, ByteBuf in) { try { - Constructor constructor = clazz.getConstructor(NetInput.class); + Constructor constructor = clazz.getConstructor(ByteBuf.class, MinecraftCodecHelper.class); - return constructor.newInstance(in); + return constructor.newInstance(in, helper); } catch (NoSuchMethodError e) { throw new IllegalStateException("Packet \"" + clazz.getName() + "\" does not have a NetInput constructor for instantiation."); } catch (Exception e) { diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacketTest.java index 8808cb7c..daac3b20 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacketTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/packet/handshake/serverbound/ClientIntentionPacketTest.java @@ -1,9 +1,9 @@ package com.github.steveice10.mc.protocol.packet.handshake.serverbound; import com.github.steveice10.mc.protocol.codec.MinecraftCodec; +import com.github.steveice10.mc.protocol.codec.MinecraftPacket; import com.github.steveice10.mc.protocol.data.handshake.HandshakeIntent; import com.github.steveice10.mc.protocol.packet.PacketTest; -import com.github.steveice10.packetlib.packet.Packet; import org.junit.Before; import java.util.ArrayList; @@ -12,11 +12,11 @@ import java.util.List; public class ClientIntentionPacketTest extends PacketTest { @Before public void setup() { - List packets = new ArrayList<>(); + List packets = new ArrayList<>(); for (HandshakeIntent intent : HandshakeIntent.values()) { packets.add(new ClientIntentionPacket(MinecraftCodec.CODEC.getProtocolVersion(), "localhost", 25565, intent)); } - this.setPackets(packets.toArray(new Packet[0])); + this.setPackets(packets.toArray(new MinecraftPacket[0])); } } diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacketTest.java index 51ff49d9..b91444e4 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacketTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundBlockUpdatePacketTest.java @@ -1,6 +1,5 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.level.block.BlockChangeEntry; import com.github.steveice10.mc.protocol.packet.PacketTest; import com.nukkitx.math.vector.Vector3i; diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetEntityDataPacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetEntityDataPacketTest.java index de79e7fe..3426ed21 100644 --- a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetEntityDataPacketTest.java +++ b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/clientbound/level/ClientboundSetEntityDataPacketTest.java @@ -2,7 +2,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.clientbound.level; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.MetadataType; -import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position; import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.*; import com.github.steveice10.mc.protocol.data.game.entity.object.Direction; import com.github.steveice10.mc.protocol.packet.PacketTest;