use of com.github.dirtpowered.dirtmv.data.protocol.PacketData in project DirtMultiversion by DirtPowered.
the class PacketUtil method readPacket.
public static PacketData readPacket(MinecraftVersion version, PacketInput buffer) throws IOException {
Preconditions.checkNotNull(version, "Version not provided");
short packetId = buffer.readUnsignedByte();
BaseProtocol protocol = ProtocolRegistry.getProtocolFromVersion(version);
Preconditions.checkNotNull(protocol, "Protocol %s is not registered", version);
DataType[] parts = protocol.dataTypes[packetId];
String packetMapping = PreNettyPacketNames.getPacketName(packetId);
String protocolName = protocol.getClass().getSimpleName();
Preconditions.checkNotNull(parts, "Unknown packet id %s (%s) in protocol %s", packetId, packetMapping, protocolName);
TypeHolder[] typeHolders = new TypeHolder[parts.length];
int i = 0;
while (i < parts.length) {
DataType dataType = parts[i];
typeHolders[i] = new TypeHolder(dataType.getType(), dataType.read(buffer));
i++;
}
return new PacketData(packetId, typeHolders);
}
use of com.github.dirtpowered.dirtmv.data.protocol.PacketData in project DirtMultiversion by DirtPowered.
the class ServerSession method sendDisconnectPacket.
private void sendDisconnectPacket(String message) {
PacketData kickPacket;
if (userData.getClientVersion().isNettyProtocol()) {
boolean state = userData.getProtocolState() == ProtocolState.LOGIN;
kickPacket = PacketUtil.createPacket(state ? 0x00 : 0x40, new TypeHolder[] { new TypeHolder<>(Type.V1_7_STRING, ChatUtils.legacyToJsonString(message)) });
} else {
kickPacket = PacketUtil.createPacket(0xFF, new TypeHolder[] { new TypeHolder<>(Type.STRING, message) });
}
sendPacket(kickPacket);
}
use of com.github.dirtpowered.dirtmv.data.protocol.PacketData in project DirtMultiversion by DirtPowered.
the class PacketDecoder method decode.
@Override
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> list) throws IOException {
boolean flag = packetDirection == PacketDirection.TO_CLIENT;
setUserProtocol(flag, buffer);
PacketInput inputBuffer = new NettyInputWrapper(buffer);
PacketData packet = PacketUtil.readPacket(flag ? main.getConfiguration().getServerVersion() : userData.getClientVersion(), inputBuffer);
setProtocolState(packet);
list.add(packet);
}
use of com.github.dirtpowered.dirtmv.data.protocol.PacketData in project DirtMultiversion by DirtPowered.
the class NettyPacketDecoder method decode.
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
if (byteBuf.readableBytes() == 0)
return;
PacketInput inputBuffer = new NettyInputWrapper(byteBuf);
int i = inputBuffer.readVarInt();
boolean flag = packetDirection == PacketDirection.TO_CLIENT;
ProtocolState protocolState = userData.getProtocolState();
if (protocolState == ProtocolState.HANDSHAKE) {
int readerIndex = byteBuf.readerIndex();
int protocol = inputBuffer.readVarInt();
byteBuf.readerIndex(readerIndex);
userData.setClientVersion(MinecraftVersion.fromNettyProtocolId(protocol));
}
PacketData packet;
if (flag) {
packet = readModernPacket(main.getConfiguration().getServerVersion(), protocolState, inputBuffer, packetDirection, i);
} else {
packet = readModernPacket(userData.getClientVersion(), protocolState, inputBuffer, packetDirection, i);
}
int readableBytes = byteBuf.readableBytes();
if (readableBytes > 0) {
byteBuf.skipBytes(readableBytes);
Logger.warn("skipping {} bytes for packet id: {}, direction: {}", readableBytes, i, packetDirection);
} else {
list.add(packet);
}
}
use of com.github.dirtpowered.dirtmv.data.protocol.PacketData in project DirtMultiversion by DirtPowered.
the class ProtocolBeta11To10 method registerTranslators.
@Override
public void registerTranslators() {
// handshake
addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.UTF8_STRING, data.read(Type.STRING, 0)) });
}
});
// handshake
addTranslator(0x02, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
}
});
// login
addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { set(Type.INT, 10), set(Type.UTF8_STRING, data.read(Type.STRING, 1)), set(Type.UTF8_STRING, "-"), data.read(2), data.read(3) });
}
});
// login
addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(3), data.read(4) });
}
});
// chat
addTranslator(0x03, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
}
});
// chat
addTranslator(0x03, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.UTF8_STRING, data.read(Type.STRING, 0)) });
}
});
// kick disconnect
addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
}
});
// named entity spawn
addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
}
});
// spawn painting
addTranslator(0x19, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x19, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5) });
}
});
// update sign
addTranslator(0x82, PacketDirection.TO_CLIENT, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
return PacketUtil.createPacket(0x82, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.STRING, data.read(Type.UTF8_STRING, 3)), set(Type.STRING, data.read(Type.UTF8_STRING, 4)), set(Type.STRING, data.read(Type.UTF8_STRING, 5)), set(Type.STRING, data.read(Type.UTF8_STRING, 6)) });
}
});
// window click
addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {
@Override
public PacketData translate(ServerSession session, PacketData data) {
boolean shiftClick = (data.read(Type.BYTE, 4) == 1);
// cancel shift-clicking
ItemStack itemStack;
if (!shiftClick) {
itemStack = data.read(Type.V1_3B_ITEM, 5);
} else {
/*
* sending invalid/other item than held item is forcing server to send window update packet
* It should prevent against all inventory desyncs
*/
itemStack = new ItemStack(99, 0, 1, null);
}
return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.V1_3B_ITEM, itemStack) });
}
});
}
Aggregations