Merge remote-tracking branch 'origin/feature/1.19-packetlib-refactor' into feature/1.19

This commit is contained in:
RednedEpic 2022-06-05 11:37:39 -05:00
commit 849c540e96
228 changed files with 2923 additions and 2645 deletions

View file

@ -88,9 +88,9 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.GeyserMC</groupId>
<groupId>com.github.steveice10</groupId>
<artifactId>packetlib</artifactId>
<version>2.1</version>
<version>3.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>

View file

@ -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<? extends Packet> 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);
}
}

View file

@ -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<LevelEvent> LEVEL_EVENTS = new Int2ObjectOpenHashMap<>();
private static final Map<String, BuiltinSound> 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)
)

View file

@ -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<LevelEvent> levelEvents;
private final Map<String, BuiltinSound> 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<ByteBuf> 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<ByteBuf> 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<ByteBuf> 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<ByteBuf> 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 extends Tag> T readTag(ByteBuf buf, Class<T> 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 extends Tag> T readTagLE(ByteBuf buf, Class<T> 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 <T extends Tag> 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 <T extends Tag> 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<EntityMetadata<?, ?>> 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<byte[]> skyUpdates = new ArrayList<>(skyUpdateSize);
for (int i = 0; i < skyUpdateSize; i++) {
skyUpdates.add(this.readByteArray(buf));
}
int blockUpdateSize = this.readVarInt(buf);
List<byte[]> 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());
}
}

View file

@ -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;
}

View file

@ -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<T extends MinecraftPacket> implements PacketSerializer<T, MinecraftCodecHelper> {
private final PacketFactory<T, MinecraftCodecHelper> 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<T, MinecraftCodecHelper> definition) throws IOException {
return this.factory.construct(buf, helper);
}
}

View file

@ -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<ProtocolState, PacketStateCodec> stateProtocols;
@Getter
private final Supplier<MinecraftCodecHelper> 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<ProtocolState, PacketStateCodec> stateProtocols = new EnumMap<>(ProtocolState.class);
private Supplier<MinecraftCodecHelper> helperFactory;
public Builder protocolVersion(int protocolVersion) {
this.protocolVersion = protocolVersion;
@ -58,8 +62,13 @@ public class PacketCodec {
return this;
}
public Builder helper(Supplier<MinecraftCodecHelper> 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);
}
}
}

View file

@ -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 <T> the packet type
*/
@FunctionalInterface
public interface PacketFactory<T extends Packet, H extends PacketCodecHelper> {
/**
* 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;
}

View file

@ -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<Integer, PacketDefinition<? extends Packet>> clientboundPackets = new HashMap<>();
private final Map<Integer, PacketDefinition<? extends Packet>> serverboundPackets = new HashMap<>();
private final Map<Integer, PacketDefinition<? extends MinecraftPacket, MinecraftCodecHelper>> clientboundPackets = new HashMap<>();
private final Map<Integer, PacketDefinition<? extends MinecraftPacket, MinecraftCodecHelper>> serverboundPackets = new HashMap<>();
public <T extends Packet> Builder registerClientboundPacket(int id, Class<T> packetClass, PacketFactory<T> factory) {
this.clientboundPackets.put(id, new PacketDefinition<>(id, packetClass, factory));
public <T extends MinecraftPacket> Builder registerClientboundPacket(int id, Class<T> packetClass, PacketFactory<T, MinecraftCodecHelper> factory) {
this.clientboundPackets.put(id, new PacketDefinition<>(id, packetClass, new MinecraftPacketSerializer<>(factory)));
return this;
}
public <T extends Packet> Builder registerServerboundPacket(int id, Class<T> packetClass, PacketFactory<T> factory) {
this.serverboundPackets.put(id, new PacketDefinition<>(id, packetClass, factory));
public <T extends MinecraftPacket> Builder registerServerboundPacket(int id, Class<T> packetClass, PacketFactory<T, MinecraftCodecHelper> factory) {
this.serverboundPackets.put(id, new PacketDefinition<>(id, packetClass, new MinecraftPacketSerializer<>(factory)));
return this;
}
public PacketStateCodec build() {
PacketStateCodec codec = new PacketStateCodec();
for (Map.Entry<Integer, PacketDefinition<? extends Packet>> entry : this.clientboundPackets.entrySet()) {
for (Map.Entry<Integer, PacketDefinition<? extends MinecraftPacket, MinecraftCodecHelper>> entry : this.clientboundPackets.entrySet()) {
codec.registerClientbound(entry.getValue());
}
for (Map.Entry<Integer, PacketDefinition<? extends Packet>> entry : this.serverboundPackets.entrySet()) {
for (Map.Entry<Integer, PacketDefinition<? extends MinecraftPacket, MinecraftCodecHelper>> entry : this.serverboundPackets.entrySet()) {
codec.registerServerbound(entry.getValue());
}

View file

@ -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];
}
}

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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);

View file

@ -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;

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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;

View file

@ -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];
}
}

View file

@ -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];
}
}

View file

@ -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];
}
}

View file

@ -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<Boolean> {
}
@Override
public EntityMetadata<Boolean, BooleanMetadataType> readMetadata(NetInput input, int id) throws IOException {
public EntityMetadata<Boolean, BooleanMetadataType> 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> {
boolean readPrimitive(NetInput input) throws IOException;
public interface BooleanReader extends BasicReader<Boolean> {
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<Boolean> {
void writePrimitive(NetOutput output, boolean value) throws IOException;
public interface BooleanWriter extends BasicWriter<Boolean> {
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);
}
}

View file

@ -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<Byte> {
}
@Override
public EntityMetadata<Byte, ByteMetadataType> readMetadata(NetInput input, int id) throws IOException {
public EntityMetadata<Byte, ByteMetadataType> 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> {
byte readPrimitive(NetInput input) throws IOException;
public interface ByteReader extends BasicReader<Byte> {
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<Byte> {
void writePrimitive(NetOutput output, byte value) throws IOException;
public interface ByteWriter extends BasicWriter<Byte> {
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);
}
}

View file

@ -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<V, T extends MetadataType<V>> {
*/
public abstract V getValue();
public static EntityMetadata<?, ?>[] read(NetInput in) throws IOException {
List<EntityMetadata<?, ?>> 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

View file

@ -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<Float> {
}
@Override
public EntityMetadata<Float, FloatMetadataType> readMetadata(NetInput input, int id) throws IOException {
public EntityMetadata<Float, FloatMetadataType> 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> {
float readPrimitive(NetInput input) throws IOException;
public interface FloatReader extends BasicReader<Float> {
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<Float> {
void writePrimitive(NetOutput output, float value) throws IOException;
public interface FloatWriter extends BasicWriter<Float> {
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);
}
}

View file

@ -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());
}
}

View file

@ -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<Integer> {
}
@Override
public EntityMetadata<Integer, IntMetadataType> readMetadata(NetInput input, int id) throws IOException {
return this.primitiveFactory.createPrimitive(id, this, this.primitiveReader.readPrimitive(input));
public EntityMetadata<Integer, IntMetadataType> 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<Integer> {
int readPrimitive(NetInput input) throws IOException;
public interface IntReader extends HelperReader<Integer> {
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<Integer> {
void writePrimitive(NetOutput output, int value) throws IOException;
public interface IntWriter extends HelperWriter<Integer> {
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);
}
}

View file

@ -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());
}
}
}

View file

@ -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<T> {
private static final List<MetadataType<?>> 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> STRING = new MetadataType<>(NetInput::readString, NetOutput::writeString, ObjectEntityMetadata::new);
public static final MetadataType<Component> CHAT = new MetadataType<>(in -> DefaultComponentSerializer.get().deserialize(in.readString()),
(out, value) -> out.writeString(DefaultComponentSerializer.get().serialize(value)),
ObjectEntityMetadata::new);
public static final MetadataType<Optional<Component>> 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<ItemStack> 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> ROTATION = new MetadataType<>(Rotation::read, Rotation::write, ObjectEntityMetadata::new);
public static final MetadataType<Vector3i> POSITION = new MetadataType<>(Position::read, Position::write, ObjectEntityMetadata::new);
public static final MetadataType<Optional<Vector3i>> OPTIONAL_POSITION = new MetadataType<>(optionalReader(Position::read), optionalWriter(Position::write), ObjectEntityMetadata::new);
public static final MetadataType<Direction> DIRECTION = new MetadataType<>(in -> in.readEnum(Direction.VALUES), NetOutput::writeEnum, ObjectEntityMetadata::new);
public static final MetadataType<Optional<UUID>> 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<CompoundTag> NBT_TAG = new MetadataType<>(NBT::read, NBT::write, ObjectEntityMetadata::new);
public static final MetadataType<Particle> PARTICLE = new MetadataType<>(Particle::read, Particle::write, ObjectEntityMetadata::new);
public static final MetadataType<VillagerData> 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> STRING = new MetadataType<>(MinecraftCodecHelper::readString, MinecraftCodecHelper::writeString, ObjectEntityMetadata::new);
public static final MetadataType<Component> CHAT = new MetadataType<>(MinecraftCodecHelper::readComponent, MinecraftCodecHelper::writeComponent, ObjectEntityMetadata::new);
public static final MetadataType<Optional<Component>> OPTIONAL_CHAT = new MetadataType<>(optionalReader(MinecraftCodecHelper::readComponent), optionalWriter(MinecraftCodecHelper::writeComponent), ObjectEntityMetadata::new);
public static final MetadataType<ItemStack> 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<Vector3f> ROTATION = new MetadataType<>(MinecraftCodecHelper::readRotation, MinecraftCodecHelper::writeRotation, ObjectEntityMetadata::new);
public static final MetadataType<Vector3i> POSITION = new MetadataType<>(MinecraftCodecHelper::readPosition, MinecraftCodecHelper::writePosition, ObjectEntityMetadata::new);
public static final MetadataType<Optional<Vector3i>> OPTIONAL_POSITION = new MetadataType<>(optionalReader(MinecraftCodecHelper::readPosition), optionalWriter(MinecraftCodecHelper::writePosition), ObjectEntityMetadata::new);
public static final MetadataType<Direction> DIRECTION = new MetadataType<>(MinecraftCodecHelper::readDirection, MinecraftCodecHelper::writeDirection, ObjectEntityMetadata::new);
public static final MetadataType<Optional<UUID>> 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<CompoundTag> NBT_TAG = new MetadataType<>(MinecraftCodecHelper::readTag, MinecraftCodecHelper::writeTag, ObjectEntityMetadata::new);
public static final MetadataType<Particle> PARTICLE = new MetadataType<>(MinecraftCodecHelper::readParticle, MinecraftCodecHelper::writeParticle, ObjectEntityMetadata::new);
public static final MetadataType<VillagerData> 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> 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<GlobalPos>> OPTIONAL_GLOBAL_POS = new MetadataType<>(optionalReader(GlobalPos::read), optionalWriter(GlobalPos::write), ObjectEntityMetadata::new);
public static final MetadataType<PaintingType> PAINTING_VARIANT = new MetadataType<>(PaintingType::read, NetOutput::writeEnum, ObjectEntityMetadata::new);
public static final MetadataType<Pose> 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<GlobalPos>> OPTIONAL_GLOBAL_POS = new MetadataType<>(optionalReader(MinecraftCodecHelper::readGlobalPos), optionalWriter(MinecraftCodecHelper::writeGlobalPos), ObjectEntityMetadata::new);
public static final MetadataType<PaintingType> PAINTING_VARIANT = new MetadataType<>(MinecraftCodecHelper::readPaintingType, MinecraftCodecHelper::writePaintingType, ObjectEntityMetadata::new);
protected final int id;
protected final Reader<T> reader;
protected final Writer<T> writer;
protected final EntityMetadataFactory<T> metadataFactory;
protected MetadataType(Reader<T> reader, Writer<T> writer, EntityMetadataFactory<T> metadataFactory) {
protected MetadataType(BasicReader<T> reader, BasicWriter<T> writer, EntityMetadataFactory<T> metadataFactory) {
this.id = VALUES.size();
this.reader = reader;
this.writer = writer;
@ -67,22 +60,69 @@ public class MetadataType<T> {
VALUES.add(this);
}
public EntityMetadata<T, ? extends MetadataType<T>> readMetadata(NetInput input, int id) throws IOException {
return this.metadataFactory.create(id, this, this.reader.read(input));
protected MetadataType(HelperReader<T> reader, HelperWriter<T> writer, EntityMetadataFactory<T> 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<T, ? extends MetadataType<T>> 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> {
V read(NetInput input) throws IOException;
V read(ByteBuf input) throws IOException;
V read(MinecraftCodecHelper helper, ByteBuf input) throws IOException;
}
public interface Writer<V> {
void write(ByteBuf output, V value) throws IOException;
void write(MinecraftCodecHelper helper, ByteBuf output, V value) throws IOException;
}
@FunctionalInterface
public interface Writer<V> {
void write(NetOutput output, V value) throws IOException;
public interface BasicReader<V> extends Reader<V> {
V read(ByteBuf input) throws IOException;
default V read(MinecraftCodecHelper helper, ByteBuf input) throws IOException {
return this.read(input);
}
}
@FunctionalInterface
public interface BasicWriter<V> extends Writer<V> {
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<V> extends Reader<V> {
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<V> extends Writer<V> {
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<T> {
EntityMetadata<V, ? extends MetadataType<V>> create(int id, MetadataType<V> type, V value);
}
private static <T> Reader<Optional<T>> optionalReader(Reader<T> reader) {
private static <T> BasicReader<Optional<T>> optionalReader(BasicReader<T> reader) {
return input -> {
if (!input.readBoolean()) {
return Optional.empty();
@ -100,7 +140,17 @@ public class MetadataType<T> {
};
}
private static <T> Writer<Optional<T>> optionalWriter(Writer<T> writer) {
private static <T> HelperReader<Optional<T>> optionalReader(HelperReader<T> reader) {
return (helper, input) -> {
if (!input.readBoolean()) {
return Optional.empty();
}
return Optional.of(reader.read(helper, input));
};
}
private static <T> BasicWriter<Optional<T>> optionalWriter(BasicWriter<T> writer) {
return (ouput, value) -> {
ouput.writeBoolean(value.isPresent());
if (value.isPresent()) {
@ -109,8 +159,17 @@ public class MetadataType<T> {
};
}
public static MetadataType<?> read(NetInput in) throws IOException {
int id = in.readVarInt();
private static <T> HelperWriter<Optional<T>> optionalWriter(HelperWriter<T> 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() + "!");
}
@ -118,7 +177,11 @@ public class MetadataType<T> {
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();
}
}

View file

@ -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<OptionalInt> {
super(OptionalIntReader.INSTANCE, OptionalIntWriter.INSTANCE, metadataFactory);
}
public static class OptionalIntReader implements Reader<OptionalInt> {
public static class OptionalIntReader implements HelperReader<OptionalInt> {
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<OptionalInt> {
}
}
public static class OptionalIntWriter implements Writer<OptionalInt> {
public static class OptionalIntWriter implements HelperWriter<OptionalInt> {
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);
}
}
}

View file

@ -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];
}
}

View file

@ -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() {
}
}

View file

@ -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());
}
}

View file

@ -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());
}
}

View file

@ -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<Boolean, BooleanMetada
}
@Override
protected void write(NetOutput out) throws IOException {
public void write(MinecraftCodecHelper helper, ByteBuf out) throws IOException {
this.type.writeMetadataPrimitive(out, this.value);
}
}

View file

@ -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.ByteMetadataType;
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 ByteEntityMetadata extends EntityMetadata<Byte, ByteMetadataType> {
}
@Override
protected void write(NetOutput out) throws IOException {
public void write(MinecraftCodecHelper helper, ByteBuf out) throws IOException {
this.type.writeMetadataPrimitive(out, this.value);
}
}

View file

@ -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<Float, FloatMetadataType
}
@Override
protected void write(NetOutput out) throws IOException {
public void write(MinecraftCodecHelper helper, ByteBuf out) throws IOException {
this.type.writeMetadataPrimitive(out, this.value);
}
}

View file

@ -1,9 +1,10 @@
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.IntMetadataType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.MetadataType;
import com.github.steveice10.packetlib.io.NetOutput;
import io.netty.buffer.ByteBuf;
import lombok.NonNull;
import java.io.IOException;
@ -30,7 +31,7 @@ public class IntEntityMetadata extends EntityMetadata<Integer, IntMetadataType>
}
@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);
}
}

View file

@ -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];
}
}

View file

@ -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());
}
}
}

View file

@ -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];
}
}

View file

@ -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];
}
}

View file

@ -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<byte[]> 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);
}
}

View file

@ -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

View file

@ -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());
}
}
}

View file

@ -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<Integer, LevelEvent> 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);
}
}
}

View file

@ -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());
}
}

View file

@ -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;
}
}
}

View file

@ -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];
}
}

View file

@ -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();
}

View file

@ -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));
}
}

View file

@ -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<String, BuiltinSound> 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];
}
}

View file

@ -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];
}
}

View file

@ -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];
}
}

View file

@ -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];
}
}

View file

@ -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

View file

@ -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<Statistic, Integer> 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<Statistic, Integer> 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());
}
}
}

View file

@ -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) {

View file

@ -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);
}

View file

@ -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));
}
}
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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

View file

@ -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);
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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<GameProfile.Property> 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;

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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);
}
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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<String> 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<List<String>> requirements = new ArrayList<>();
int requirementCount = in.readVarInt();
int requirementCount = helper.readVarInt(in);
for (int j = 0; j < requirementCount; j++) {
List<String> 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<String, Long> 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<String> 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<String, Map<String, Long>> advancement : this.progress.entrySet()) {
out.writeString(advancement.getKey());
helper.writeString(out, advancement.getKey());
Map<String, Long> advancementProgress = advancement.getValue();
out.writeVarInt(advancementProgress.size());
helper.writeVarInt(out, advancementProgress.size());
for (Map.Entry<String, Long> criterion : advancementProgress.entrySet()) {
out.writeString(criterion.getKey());
helper.writeString(out, criterion.getKey());
if (criterion.getValue() != -1) {
out.writeBoolean(true);
out.writeLong(criterion.getValue());

View file

@ -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);
}
}
}

View file

@ -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<String, Map<String, int[]>> 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<String, int[]> 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<String, Map<String, int[]>> 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<String, int[]> 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);
}
}
}

View file

@ -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));
}
}

View file

@ -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);
}
}

View file

@ -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));

View file

@ -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));

View file

@ -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);

View file

@ -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);

View file

@ -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);
}
}
}

View file

@ -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);
}
}

View file

@ -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));
}
}

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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));

View file

@ -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<Equipment> 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());
}
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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<Attribute> 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<AttributeModifier> 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());
}
}
}

Some files were not shown because too many files have changed in this diff Show more