Files
sweetgum-server/src/main/java/net/packet/ByteBufOutPacket.java
P0nk 503740542d Fix issue with packet bytes reuse
Big oversight that caused broadcasted (packet sent to multiple clients)
packets to only be correct for the first receiver, for example when
3 players were in the same map.
2021-11-17 22:00:21 +01:00

94 lines
2.1 KiB
Java

package net.packet;
import constants.string.CharsetConstants;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import net.jcip.annotations.NotThreadSafe;
import net.opcodes.SendOpcode;
import java.awt.*;
@NotThreadSafe
public class ByteBufOutPacket implements OutPacket {
private final ByteBuf byteBuf;
public ByteBufOutPacket() {
this.byteBuf = Unpooled.buffer();
}
public ByteBufOutPacket(SendOpcode op) {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeShortLE((short) op.getValue());
this.byteBuf = byteBuf;
}
public ByteBufOutPacket(SendOpcode op, int initialCapacity) {
ByteBuf byteBuf = Unpooled.buffer(initialCapacity);
byteBuf.writeShortLE((short) op.getValue());
this.byteBuf = byteBuf;
}
@Override
public byte[] getBytes() {
return ByteBufUtil.getBytes(byteBuf);
}
@Override
public void writeByte(byte value) {
byteBuf.writeByte(value);
}
@Override
public void writeByte(int value) {
writeByte((byte) value);
}
@Override
public void writeBytes(byte[] value) {
byteBuf.writeBytes(value);
}
@Override
public void writeShort(int value) {
byteBuf.writeShortLE(value);
}
@Override
public void writeInt(int value) {
byteBuf.writeIntLE(value);
}
@Override
public void writeLong(long value) {
byteBuf.writeLongLE(value);
}
@Override
public void writeBool(boolean value) {
byteBuf.writeByte(value ? 1 : 0);
}
@Override
public void writeString(String value) {
writeShort((short) value.length());
writeBytes(value.getBytes(CharsetConstants.CHARSET));
}
@Override
public void writeFixedString(String value) {
writeBytes(value.getBytes(CharsetConstants.CHARSET));
}
@Override
public void writePos(Point value) {
writeShort((short) value.getX());
writeShort((short) value.getY());
}
@Override
public void skip(int numberOfBytes) {
writeBytes(new byte[numberOfBytes]);
}
}