mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2026-02-11 03:16:14 +01:00
Added packet pre-encoding. Code style.
This commit is contained in:
31
src/main/java/ru/nanit/limbo/protocol/PreRenderedPacket.java
Normal file
31
src/main/java/ru/nanit/limbo/protocol/PreRenderedPacket.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package ru.nanit.limbo.protocol;
|
||||
|
||||
public class PreRenderedPacket implements PacketOut {
|
||||
|
||||
private final PacketOut packet;
|
||||
private byte[] message;
|
||||
|
||||
public PreRenderedPacket(PacketOut packet){
|
||||
this.packet = packet;
|
||||
}
|
||||
|
||||
public PacketOut getWrappedPacket(){
|
||||
return packet;
|
||||
}
|
||||
|
||||
public PreRenderedPacket render(){
|
||||
ByteMessage renderedMessage = ByteMessage.create();
|
||||
packet.encode(renderedMessage);
|
||||
this.message = renderedMessage.toByteArray();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
msg.writeBytes(message);
|
||||
}
|
||||
|
||||
public static PreRenderedPacket of(PacketOut packet){
|
||||
return new PreRenderedPacket(packet).render();
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,37 @@
|
||||
package ru.nanit.limbo.protocol.packets.play;
|
||||
|
||||
import ru.nanit.limbo.connection.ClientConnection;
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.PacketOut;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* This packet wrapper used only for ADD_PLAYER action
|
||||
* This packet was very simplified and using only for ADD_PLAYER action
|
||||
*/
|
||||
public class PacketPlayerInfo implements PacketOut {
|
||||
|
||||
private int gameMode;
|
||||
private ClientConnection connection;
|
||||
|
||||
public void setConnection(ClientConnection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
private int gameMode = 3;
|
||||
private String username = "";
|
||||
private UUID uuid;
|
||||
|
||||
public void setGameMode(int gameMode) {
|
||||
this.gameMode = gameMode;
|
||||
}
|
||||
|
||||
public void setUsername(String username){
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid){
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteMessage msg) {
|
||||
msg.writeVarInt(0);
|
||||
msg.writeVarInt(1);
|
||||
msg.writeUuid(connection.getUuid());
|
||||
msg.writeString(connection.getUsername());
|
||||
msg.writeUuid(uuid);
|
||||
msg.writeString(username);
|
||||
msg.writeVarInt(0);
|
||||
msg.writeVarInt(gameMode);
|
||||
msg.writeVarInt(60);
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import ru.nanit.limbo.protocol.ByteMessage;
|
||||
import ru.nanit.limbo.protocol.Packet;
|
||||
import ru.nanit.limbo.protocol.PreRenderedPacket;
|
||||
import ru.nanit.limbo.protocol.registry.State;
|
||||
import ru.nanit.limbo.util.Logger;
|
||||
|
||||
@@ -21,7 +22,13 @@ public class PacketEncoder extends MessageToByteEncoder<Packet> {
|
||||
if (registry == null) return;
|
||||
|
||||
ByteMessage msg = new ByteMessage(out);
|
||||
int packetId = registry.getPacketId(packet.getClass());
|
||||
int packetId;
|
||||
|
||||
if (packet instanceof PreRenderedPacket){
|
||||
packetId = registry.getPacketId(((PreRenderedPacket)packet).getWrappedPacket().getClass());
|
||||
} else {
|
||||
packetId = registry.getPacketId(packet.getClass());
|
||||
}
|
||||
|
||||
if (packetId == -1){
|
||||
Logger.warning("Undefined packet class: %s", packet.getClass().getName());
|
||||
|
||||
Reference in New Issue
Block a user