use of com.github.dirtpowered.dirtmv.data.protocol.TypeHolder in project DirtMultiversion by DirtPowered.
the class PacketUtil method readModernPacket.
public static PacketData readModernPacket(MinecraftVersion ver, ProtocolState state, PacketInput buf, PacketDirection dir, int id) throws IOException {
BaseProtocol protocol = ProtocolRegistry.getProtocolFromVersion(ver);
DataType[] parts = protocol.getStateDependedProtocol().getInstruction(id, state, dir);
if (parts == null) {
Logger.warn("Unknown packet id {} ({}), state: {}, direction: {}", StringUtils.intToHexStr(id), id, state, dir);
return new PacketData(0);
}
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(buf));
i++;
}
return new PacketData(id, typeHolders);
}
use of com.github.dirtpowered.dirtmv.data.protocol.TypeHolder 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.TypeHolder 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.TypeHolder 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) });
}
});
}
use of com.github.dirtpowered.dirtmv.data.protocol.TypeHolder in project DirtMultiversion by DirtPowered.
the class ProtocolBeta14To13 method registerTranslators.
@Override
public void registerTranslators() {
// 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, 13), data.read(1), 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), data.read(1), data.read(2), data.read(3) });
}
});
}
Aggregations