Merge branch 'dev/pool' into dev/world
# Conflicts: # protocol/src/main/java/mc/protocol/packets/play/client/CPlayerPositionAndLookPacket.java # protocol/src/main/java/mc/protocol/packets/play/client/PlayerLookPacket.java # protocol/src/main/java/mc/protocol/packets/play/client/PlayerPositionPacket.java # protocol/src/main/java/mc/protocol/packets/play/server/ChunkDataPacket.java # protocol/src/test/java/mc/protocol/buffer/NetByteBufReadTest.java # protocol/src/test/java/mc/protocol/buffer/NetByteBufWriteTest.java # server/src/main/resources/logback-default.xml
This commit is contained in:
@@ -3,7 +3,6 @@ apply from: rootDir.toPath().resolve('logic.gradle').toFile()
|
||||
dependencies {
|
||||
api project(':utils')
|
||||
|
||||
implementation libs.objpool
|
||||
implementation libs.netty.transport
|
||||
implementation libs.netty.codec
|
||||
implementation libs.json
|
||||
|
||||
@@ -3,13 +3,11 @@ package mc.protocol.buffer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Delegate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.protocol.model.text.Text;
|
||||
import mc.protocol.model.text.TextSerializer;
|
||||
import mc.protocol.pool.Passivable;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
@@ -50,15 +48,10 @@ import java.util.UUID;
|
||||
@RequiredArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@ToString
|
||||
public class NetByteBuf extends ByteBuf implements Passivable {
|
||||
public abstract class NetByteBuf extends ByteBuf {
|
||||
|
||||
@Delegate
|
||||
private ByteBuf byteBuf;
|
||||
|
||||
public NetByteBuf setByteBuf(ByteBuf byteBuf) {
|
||||
this.byteBuf = byteBuf;
|
||||
return this;
|
||||
}
|
||||
protected ByteBuf byteBuf;
|
||||
|
||||
public void writeUnsignedByte(int value) {
|
||||
byteBuf.writeByte((byte)(value & 0xFF));
|
||||
@@ -182,11 +175,6 @@ public class NetByteBuf extends ByteBuf implements Passivable {
|
||||
}
|
||||
//endregion
|
||||
|
||||
@Override
|
||||
public void passivate() {
|
||||
this.byteBuf = null;
|
||||
}
|
||||
|
||||
public static int readVarInt(ByteBuf byteBuf) {
|
||||
int numRead = 0;
|
||||
int result = 0;
|
||||
|
||||
@@ -8,7 +8,6 @@ import mc.protocol.ProtocolAttributes;
|
||||
import mc.protocol.State;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.protocol.pool.ProtocolObjectPool;
|
||||
import org.apache.commons.pool2.ObjectPool;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
@@ -22,11 +21,11 @@ public class ProtocolInboundHandler extends SimpleChannelInboundHandler<ClientSi
|
||||
private final ProtocolHandlersBus protocolHandlersBus;
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, ClientSidePacket packet) throws Exception {
|
||||
protected void channelRead0(ChannelHandlerContext ctx, ClientSidePacket packet) {
|
||||
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
|
||||
protocolHandlersBus.process(state, ctx, packet);
|
||||
|
||||
ProtocolObjectPool.getPacketPool().returnObject(packet);
|
||||
ProtocolObjectPool.packet().returnObject(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,9 +27,9 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
|
||||
State state = Objects.requireNonNull(ctx.channel().attr(ProtocolAttributes.STATE).get());
|
||||
NetByteBuf netByteBuf = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(in);
|
||||
NetByteBuf netByteBuf = ProtocolObjectPool.netByteBuf().borrowObject(in);
|
||||
|
||||
int packetId = netByteBuf.readVarInt();
|
||||
Class<? extends ClientSidePacket> packetClass = state.getClientSidePacketById(packetId);
|
||||
@@ -37,7 +37,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
log.warn("Unknown packet: State {} ; Id 0x{}", state, packetIdAsHexcode(packetId));
|
||||
|
||||
if (readUnknownPackets) {
|
||||
UnknownPacket unknownPacket = ProtocolObjectPool.getPacketPool().borrowObject(UnknownPacket.class);
|
||||
UnknownPacket unknownPacket = ProtocolObjectPool.packet().borrowObject(UnknownPacket.class);
|
||||
unknownPacket.setState(state);
|
||||
unknownPacket.setId(packetId);
|
||||
unknownPacket.setDataSize(netByteBuf.readableBytes());
|
||||
@@ -47,7 +47,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
netByteBuf.skipBytes(netByteBuf.readableBytes());
|
||||
}
|
||||
} else {
|
||||
ClientSidePacket packet = ProtocolObjectPool.getPacketPool().borrowObject(packetClass);
|
||||
ClientSidePacket packet = ProtocolObjectPool.packet().borrowObject(packetClass);
|
||||
packet.readSelf(netByteBuf);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("IN: {}:{}", state, packet);
|
||||
@@ -55,7 +55,7 @@ public class ProtocolDecoder extends ByteToMessageDecoder {
|
||||
out.add(packet);
|
||||
}
|
||||
|
||||
ProtocolObjectPool.getNetByteBufPool().returnObject(netByteBuf);
|
||||
ProtocolObjectPool.netByteBuf().returnObject(netByteBuf);
|
||||
}
|
||||
|
||||
private static String packetIdAsHexcode(int packetId) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package mc.protocol.handler.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -29,15 +28,15 @@ public class ProtocolEncoder extends MessageToByteEncoder<ServerSidePacket> {
|
||||
log.debug("OUT: {}:{}", state, packet);
|
||||
}
|
||||
|
||||
NetByteBuf buffer = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer());
|
||||
NetByteBuf buffer = ProtocolObjectPool.netByteBuf().borrowObject();
|
||||
buffer.writeVarInt(packetId);
|
||||
packet.writeSelf(buffer);
|
||||
|
||||
NetByteBuf netByteBuf = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(out);
|
||||
NetByteBuf netByteBuf = ProtocolObjectPool.netByteBuf().borrowObject(out);
|
||||
netByteBuf.writeVarInt(buffer.readableBytes());
|
||||
netByteBuf.writeBytes(buffer);
|
||||
|
||||
ProtocolObjectPool.getNetByteBufPool().returnObject(netByteBuf);
|
||||
ProtocolObjectPool.getNetByteBufPool().returnObject(buffer);
|
||||
ProtocolObjectPool.netByteBuf().returnObject(netByteBuf);
|
||||
ProtocolObjectPool.netByteBuf().returnObject(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class ProtocolSplitter extends ByteToMessageDecoder {
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
|
||||
NetByteBuf netByteBuf = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(in);
|
||||
NetByteBuf netByteBuf = ProtocolObjectPool.netByteBuf().borrowObject(in);
|
||||
netByteBuf.markReaderIndex();
|
||||
|
||||
do {
|
||||
@@ -38,6 +38,6 @@ public class ProtocolSplitter extends ByteToMessageDecoder {
|
||||
}
|
||||
} while (netByteBuf.readableBytes() > 0);
|
||||
|
||||
ProtocolObjectPool.getNetByteBufPool().returnObject(netByteBuf);
|
||||
ProtocolObjectPool.netByteBuf().returnObject(netByteBuf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mc.protocol.packets;
|
||||
|
||||
import mc.protocol.buffer.NetByteBuf;
|
||||
import mc.protocol.pool.Passivable;
|
||||
import mc.utils.pool.Passivable;
|
||||
|
||||
/**
|
||||
* Пакеты отправляемые клиентом.
|
||||
|
||||
@@ -43,12 +43,12 @@ public class CPlayerPositionAndLookPacket implements ClientSidePacket {
|
||||
double x = netByteBuf.readDouble();
|
||||
double y = netByteBuf.readDouble();
|
||||
double z = netByteBuf.readDouble();
|
||||
this.position = ProtocolObjectPool.getLocationPool().borrowObject();
|
||||
this.position = ProtocolObjectPool.location().borrowObject();
|
||||
position.set(x, y, z);
|
||||
|
||||
float yaw = netByteBuf.readFloat();
|
||||
float pitch = netByteBuf.readFloat();
|
||||
this.look = ProtocolObjectPool.getLookPool().borrowObject();
|
||||
this.look = ProtocolObjectPool.look().borrowObject();
|
||||
this.look.set(yaw, pitch);
|
||||
|
||||
this.onGround = netByteBuf.readBoolean();
|
||||
@@ -57,11 +57,11 @@ public class CPlayerPositionAndLookPacket implements ClientSidePacket {
|
||||
@Override
|
||||
public void passivate() {
|
||||
this.position.set(0, 0, 0);
|
||||
ProtocolObjectPool.getLocationPool().returnObject(this.position);
|
||||
ProtocolObjectPool.location().returnObject(this.position);
|
||||
this.position = null;
|
||||
|
||||
this.look.set(0, 0);
|
||||
ProtocolObjectPool.getLookPool().returnObject(this.look);
|
||||
ProtocolObjectPool.look().returnObject(this.look);
|
||||
this.look = null;
|
||||
|
||||
this.onGround = false;
|
||||
|
||||
@@ -36,7 +36,7 @@ public class PlayerLookPacket implements ClientSidePacket {
|
||||
public void readSelf(NetByteBuf netByteBuf) {
|
||||
float yaw = netByteBuf.readFloat();
|
||||
float pitch = netByteBuf.readFloat();
|
||||
this.look = ProtocolObjectPool.getLookPool().borrowObject();
|
||||
this.look = ProtocolObjectPool.look().borrowObject();
|
||||
this.look.set(yaw, pitch);
|
||||
|
||||
this.onGround = netByteBuf.readBoolean();
|
||||
@@ -45,7 +45,7 @@ public class PlayerLookPacket implements ClientSidePacket {
|
||||
@Override
|
||||
public void passivate() {
|
||||
this.look.set(0, 0);
|
||||
ProtocolObjectPool.getLookPool().returnObject(this.look);
|
||||
ProtocolObjectPool.look().returnObject(this.look);
|
||||
this.look = null;
|
||||
|
||||
this.onGround = false;
|
||||
|
||||
@@ -39,7 +39,7 @@ public class PlayerPositionPacket implements ClientSidePacket {
|
||||
double x = netByteBuf.readDouble();
|
||||
double y = netByteBuf.readDouble();
|
||||
double z = netByteBuf.readDouble();
|
||||
this.position = ProtocolObjectPool.getLocationPool().borrowObject();
|
||||
this.position = ProtocolObjectPool.location().borrowObject();
|
||||
this.position.set(x, y, z);
|
||||
|
||||
this.onGround = netByteBuf.readBoolean();
|
||||
@@ -48,7 +48,7 @@ public class PlayerPositionPacket implements ClientSidePacket {
|
||||
@Override
|
||||
public void passivate() {
|
||||
this.position.set(0, 0, 0);
|
||||
ProtocolObjectPool.getLocationPool().returnObject(this.position);
|
||||
ProtocolObjectPool.location().returnObject(this.position);
|
||||
this.position = null;
|
||||
|
||||
this.onGround = false;
|
||||
|
||||
@@ -93,17 +93,17 @@ public class ChunkDataPacket implements ServerSidePacket {
|
||||
netByteBuf.writeVarInt(0); // Number of block entities
|
||||
// Block entities (NBT's)
|
||||
|
||||
ProtocolObjectPool.getNetByteBufPool().returnObject(data);
|
||||
ProtocolObjectPool.netByteBuf().returnObject(data);
|
||||
}
|
||||
|
||||
private NetByteBuf createDataStructure() {
|
||||
NetByteBuf dataStructure = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer());
|
||||
NetByteBuf dataStructure = ProtocolObjectPool.netByteBuf().borrowObject(Unpooled.buffer());
|
||||
|
||||
for (int h = 0; h < 16; h++) {
|
||||
ChunkSection section = chunk.getSection(h);
|
||||
NetByteBuf data = ChunkSerializeUtil.serializeSection(section);
|
||||
dataStructure.writeBytes(data); // Data
|
||||
ProtocolObjectPool.getNetByteBufPool().returnObject(data);
|
||||
ProtocolObjectPool.netByteBuf().returnObject(data);
|
||||
}
|
||||
|
||||
// <Biomes>
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import lombok.SneakyThrows;
|
||||
import mc.protocol.buffer.NetByteBuf;
|
||||
import mc.utils.pool.ObjectPool;
|
||||
import mc.utils.pool.PassivablePooledObjectFactory;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public class NetByteBufObjectPool implements ObjectPool<NetByteBuf> {
|
||||
|
||||
private final org.apache.commons.pool2.ObjectPool apacheObjectPool;
|
||||
|
||||
NetByteBufObjectPool() {
|
||||
this.apacheObjectPool = new GenericObjectPool<>(new PassivablePooledObjectFactory<>(PooledNetByteBuf.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetByteBuf borrowObject() {
|
||||
return borrowObject(PooledByteBufAllocator.DEFAULT.directBuffer());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public NetByteBuf borrowObject(ByteBuf byteBuf) {
|
||||
PooledNetByteBuf pooledNetByteBuf = (PooledNetByteBuf) apacheObjectPool.borrowObject();
|
||||
pooledNetByteBuf.init(byteBuf);
|
||||
return pooledNetByteBuf;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void returnObject(NetByteBuf netByteBuf) {
|
||||
if (netByteBuf instanceof PooledNetByteBuf) {
|
||||
apacheObjectPool.returnObject(netByteBuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import mc.protocol.buffer.NetByteBuf;
|
||||
import mc.utils.pool.Passivable;
|
||||
|
||||
public class PooledNetByteBuf extends NetByteBuf implements Passivable {
|
||||
|
||||
void init(ByteBuf byteBuf) {
|
||||
this.byteBuf = byteBuf.retain();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void passivate() {
|
||||
this.byteBuf.release();
|
||||
this.byteBuf = null;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,36 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import mc.protocol.buffer.NetByteBuf;
|
||||
import mc.protocol.model.Location;
|
||||
import mc.protocol.model.Look;
|
||||
import mc.protocol.packets.ClientSidePacket;
|
||||
import mc.utils.pool.MultiObjectPool;
|
||||
import mc.utils.pool.ObjectPool;
|
||||
import mc.utils.pool.PassivableMultiObjectPool;
|
||||
import mc.utils.pool.SimpleObjectPool;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public final class ProtocolObjectPool {
|
||||
|
||||
@Getter
|
||||
private static final ObjectPool<Location> locationPool = new SimpleObjectPool<>(Location.class);
|
||||
|
||||
@Getter
|
||||
private static final ObjectPool<Look> lookPool = new SimpleObjectPool<>(Look.class);
|
||||
|
||||
@Getter
|
||||
private static final ObjectPool<NetByteBuf> netByteBufPool = new PassivableObjectPool<>(NetByteBuf.class);
|
||||
|
||||
@Getter
|
||||
private static final NetByteBufObjectPool netByteBufPool = new NetByteBufObjectPool();
|
||||
private static final MultiObjectPool<ClientSidePacket> packetPool = new PassivableMultiObjectPool<>();
|
||||
|
||||
public static ObjectPool<Location> location() {
|
||||
return locationPool;
|
||||
}
|
||||
|
||||
public static ObjectPool<Look> look() {
|
||||
return lookPool;
|
||||
}
|
||||
|
||||
public static NetByteBufObjectPool netByteBuf() {
|
||||
return netByteBufPool;
|
||||
}
|
||||
|
||||
public static MultiObjectPool<ClientSidePacket> packet() {
|
||||
return packetPool;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package mc.protocol.pool;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import mc.protocol.buffer.NetByteBuf;
|
||||
|
||||
public class UnpooledNetByteBuf extends NetByteBuf {
|
||||
|
||||
public UnpooledNetByteBuf(ByteBuf byteBuf) {
|
||||
this.byteBuf = byteBuf;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public final class ChunkSerializeUtil {
|
||||
}
|
||||
}
|
||||
|
||||
NetByteBuf result = ProtocolObjectPool.getNetByteBufPool().borrowObject().setByteBuf(Unpooled.buffer());
|
||||
NetByteBuf result = ProtocolObjectPool.netByteBuf().borrowObject(Unpooled.buffer());
|
||||
result.writeUnsignedByte(BITS_PER_BLOCK); // Bits Per Block
|
||||
result.writeVarInt(0); // Palette, Direct mode
|
||||
result.writeVarInt(blockArray.size()); // Data Array Length
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mc.protocol.buffer;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import mc.protocol.pool.UnpooledNetByteBuf;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -33,7 +34,7 @@ class NetByteBufReadTest {
|
||||
void readBoolean(byte sourceByte, boolean expectedValue) {
|
||||
baos.write(sourceByte);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(expectedValue, netByteBuf.readBoolean());
|
||||
}
|
||||
@@ -44,7 +45,7 @@ class NetByteBufReadTest {
|
||||
random.nextBytes(bytes);
|
||||
baos.write(bytes[0]);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(bytes[0], netByteBuf.readByte());
|
||||
}
|
||||
@@ -54,7 +55,7 @@ class NetByteBufReadTest {
|
||||
void readUnsignedByte(byte sourceByte, int expectedValue) {
|
||||
baos.write(sourceByte);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(expectedValue, netByteBuf.readUnsignedByte());
|
||||
}
|
||||
@@ -64,7 +65,7 @@ class NetByteBufReadTest {
|
||||
int value = Integer.valueOf(random.nextInt()).shortValue();
|
||||
new DataOutputStream(baos).writeShort(value);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(value, netByteBuf.readShort());
|
||||
}
|
||||
@@ -74,7 +75,7 @@ class NetByteBufReadTest {
|
||||
int value = 32768;
|
||||
new DataOutputStream(baos).writeShort(value);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(value, netByteBuf.readUnsignedShort());
|
||||
}
|
||||
@@ -89,7 +90,7 @@ class NetByteBufReadTest {
|
||||
|
||||
baos.write(bytes);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(string, netByteBuf.readString());
|
||||
}
|
||||
@@ -105,7 +106,7 @@ class NetByteBufReadTest {
|
||||
|
||||
baos.write(bytes);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertThrows(NetIOException.class, () -> netByteBuf.readString(length));
|
||||
}
|
||||
@@ -124,7 +125,7 @@ class NetByteBufReadTest {
|
||||
|
||||
baos.write(bytes);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertThrows(NetIOException.class, () -> netByteBuf.readString(-1));
|
||||
}
|
||||
@@ -134,7 +135,7 @@ class NetByteBufReadTest {
|
||||
void readVarInt(byte[] sourceBytes, int expectedValue) throws IOException {
|
||||
baos.write(sourceBytes);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(expectedValue, netByteBuf.readVarInt());
|
||||
}
|
||||
@@ -143,7 +144,7 @@ class NetByteBufReadTest {
|
||||
void readVarInt_tooBig() throws IOException {
|
||||
baos.write(new byte[]{ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x0F });
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(-1, netByteBuf.readVarInt());
|
||||
}
|
||||
@@ -153,7 +154,7 @@ class NetByteBufReadTest {
|
||||
void readVarLong(byte[] sourceBytes, long expectedValue) throws IOException {
|
||||
baos.write(sourceBytes);
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(expectedValue, netByteBuf.readVarLong());
|
||||
}
|
||||
@@ -164,7 +165,7 @@ class NetByteBufReadTest {
|
||||
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
|
||||
(byte) 0xFF, (byte) 0x0F });
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(-1, netByteBuf.readVarLong());
|
||||
}
|
||||
@@ -196,7 +197,7 @@ class NetByteBufReadTest {
|
||||
(byte) (leastSignificantBits & 0xFF)
|
||||
});
|
||||
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(uuid, netByteBuf.readUUID());
|
||||
}
|
||||
@@ -208,7 +209,7 @@ class NetByteBufReadTest {
|
||||
baos.write(bytes);
|
||||
|
||||
byte[] actualBytes = new byte[128];
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
|
||||
assertEquals(bytes.length, netByteBuf.readableBytes());
|
||||
|
||||
@@ -225,7 +226,7 @@ class NetByteBufReadTest {
|
||||
baos.write(bytes);
|
||||
|
||||
byte[] buff = new byte[128];
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(Unpooled.wrappedBuffer(baos.toByteArray()));
|
||||
netByteBuf.readBytes(buff, 3, 11);
|
||||
|
||||
byte[] expectedBytes = new byte[11];
|
||||
|
||||
@@ -2,6 +2,7 @@ package mc.protocol.buffer;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import mc.protocol.pool.UnpooledNetByteBuf;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -30,7 +31,7 @@ class NetByteBufWriteTest {
|
||||
@MethodSource("paramsWriteBoolean")
|
||||
void writeBoolean(boolean sourceValue, byte expectedByte) {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeBoolean(sourceValue);
|
||||
|
||||
@@ -41,7 +42,7 @@ class NetByteBufWriteTest {
|
||||
@MethodSource("paramsWriteByte")
|
||||
void writeByte(byte sourceValue, byte expectedByte) {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeByte(sourceValue);
|
||||
|
||||
@@ -51,7 +52,7 @@ class NetByteBufWriteTest {
|
||||
@Test
|
||||
void writeUnsignedByte() {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeUnsignedByte(129);
|
||||
|
||||
@@ -62,7 +63,7 @@ class NetByteBufWriteTest {
|
||||
@MethodSource("paramsWriteString")
|
||||
void writeString(String string, int exceptedLength) {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeString(string);
|
||||
|
||||
@@ -81,11 +82,11 @@ class NetByteBufWriteTest {
|
||||
String overSizeString = RandomStringUtils.randomAscii(Short.MAX_VALUE + Short.MAX_VALUE);
|
||||
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeString(overSizeString);
|
||||
|
||||
NetByteBuf netByteBuf2 = new NetByteBuf().setByteBuf(byteBuf.copy());
|
||||
NetByteBuf netByteBuf2 = new UnpooledNetByteBuf(byteBuf.copy());
|
||||
String actualString = netByteBuf2.readString();
|
||||
|
||||
String expectedString = overSizeString.substring(0, Short.MAX_VALUE);
|
||||
@@ -97,7 +98,7 @@ class NetByteBufWriteTest {
|
||||
@MethodSource("paramsWriteVarInt")
|
||||
void writeVarInt(int sourceValue, byte[] expectedBytes) {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeVarInt(sourceValue);
|
||||
byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array();
|
||||
@@ -109,7 +110,7 @@ class NetByteBufWriteTest {
|
||||
@MethodSource({ "paramsWriteVarInt", "paramsWriteVarLong" })
|
||||
void writeVarLong(long sourceValue, byte[] expectedBytes) {
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeVarLong(sourceValue);
|
||||
byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array();
|
||||
@@ -122,7 +123,7 @@ class NetByteBufWriteTest {
|
||||
final UUID uuid = UUID.randomUUID();
|
||||
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeUUID(uuid);
|
||||
|
||||
@@ -158,7 +159,7 @@ class NetByteBufWriteTest {
|
||||
random.nextBytes(bytes);
|
||||
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeBytes(bytes);
|
||||
byte[] actualArray = netByteBuf.copy(0, netByteBuf.readableBytes()).array();
|
||||
@@ -172,7 +173,7 @@ class NetByteBufWriteTest {
|
||||
random.nextBytes(bytes);
|
||||
|
||||
ByteBuf byteBuf = Unpooled.buffer();
|
||||
NetByteBuf netByteBuf = new NetByteBuf().setByteBuf(byteBuf);
|
||||
NetByteBuf netByteBuf = new UnpooledNetByteBuf(byteBuf);
|
||||
|
||||
netByteBuf.writeBytes(bytes, 3, 11);
|
||||
|
||||
|
||||
@@ -129,7 +129,9 @@ public class ScenarioLogin implements PacketScenario {
|
||||
private void sendPlayerPositionAndLook(Player player) {
|
||||
var playerPositionAndLookPacket = new SPlayerPositionAndLookPacket();
|
||||
playerPositionAndLookPacket.setPosition(player.getLocation());
|
||||
playerPositionAndLookPacket.setLook(new Look().set(0f, 0f));
|
||||
Look look = new Look();
|
||||
look.set(0f, 0f);
|
||||
playerPositionAndLookPacket.setLook(look);
|
||||
playerPositionAndLookPacket.setTeleportId(random.nextInt());
|
||||
|
||||
player.getCtx().write(playerPositionAndLookPacket);
|
||||
|
||||
@@ -6,7 +6,10 @@ import mc.protocol.model.Location;
|
||||
@UtilityClass
|
||||
public class LocationUtils {
|
||||
|
||||
//TODO заменить другими инструментами
|
||||
public Location toChunkXZ(Location location) {
|
||||
return new Location().set((int) location.getX() >> 4, 0d, (int) location.getZ() >> 4);
|
||||
Location location0 = new Location();
|
||||
location0.set((int) location.getX() >> 4, 0d, (int) location.getZ() >> 4);
|
||||
return location0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,14 +20,18 @@ public class SomeChunkSection implements ChunkSection {
|
||||
public Block getBlock(int x, int y, int z) {
|
||||
if (this.y == 15) {
|
||||
if (y == 0) {
|
||||
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> {
|
||||
Location location1 = new Location();
|
||||
location1.set(x, y, z);
|
||||
return blocks.computeIfAbsent(location1, location -> {
|
||||
SomeBlock block = new SomeBlock();
|
||||
block.setLocation(location);
|
||||
block.setId(3);
|
||||
return block;
|
||||
});
|
||||
} else {
|
||||
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> {
|
||||
Location location1 = new Location();
|
||||
location1.set(x, y, z);
|
||||
return blocks.computeIfAbsent(location1, location -> {
|
||||
SomeBlock block = new SomeBlock();
|
||||
block.setLocation(location);
|
||||
block.setId(0);
|
||||
@@ -35,7 +39,9 @@ public class SomeChunkSection implements ChunkSection {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> {
|
||||
Location location1 = new Location();
|
||||
location1.set(x, y, z);
|
||||
return blocks.computeIfAbsent(location1, location -> {
|
||||
SomeBlock block = new SomeBlock();
|
||||
block.setLocation(location);
|
||||
block.setId(1);
|
||||
|
||||
@@ -9,7 +9,7 @@ import mc.utils.Table;
|
||||
|
||||
public class SomeWorld implements World {
|
||||
|
||||
private static final Location spawn = ProtocolObjectPool.getLocationPool().borrowObject().set(-790d, 256d, -263d + 16d);
|
||||
private static final Location spawn;
|
||||
private final Table<Integer, Integer, Chunk> chunkTable = new Table<>();
|
||||
|
||||
@Override
|
||||
@@ -36,4 +36,9 @@ public class SomeWorld implements World {
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
||||
static {
|
||||
spawn = new Location();
|
||||
spawn.set(-790d, 256d, -263d + 16d);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,10 @@
|
||||
<!--<logger name="io.netty.handler.logging.LoggingHandler" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>-->
|
||||
<!--<logger name="mc.protocol.handler.codec.ProtocolEncoder" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>
|
||||
<logger name="mc.protocol.handler.codec.ProtocolDecoder" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>-->
|
||||
</configuration>
|
||||
@@ -1 +1,5 @@
|
||||
apply from: rootDir.toPath().resolve('logic.gradle').toFile()
|
||||
|
||||
dependencies {
|
||||
api libs.objpool
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
public interface MultiObjectPool<T> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
public interface ObjectPool<T> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
public interface Passivable {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
@@ -1,10 +1,10 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
import org.apache.commons.pool2.PooledObject;
|
||||
|
||||
class PassivablePooledObjectFactory<T extends Passivable> extends SimplePooledObjectFactory<T> {
|
||||
public class PassivablePooledObjectFactory<T extends Passivable> extends SimplePooledObjectFactory<T> {
|
||||
|
||||
PassivablePooledObjectFactory(Class<T> clazz) {
|
||||
public PassivablePooledObjectFactory(Class<T> clazz) {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
@@ -1,4 +1,4 @@
|
||||
package mc.protocol.pool;
|
||||
package mc.utils.pool;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.pool2.BasePooledObjectFactory;
|
||||
Reference in New Issue
Block a user