modify universe.sendPacket

This commit is contained in:
ErodeesFleurs 2024-03-19 16:42:06 +08:00
parent e068172a09
commit 876cf6343d
2 changed files with 14 additions and 11 deletions

View File

@ -167,17 +167,20 @@ PacketPtr createPacket(PacketType type) {
} }
} }
PacketPtr createPacket(PacketType type, JsonArray const& args) { PacketPtr createPacket(PacketType type, Maybe<Json> const& args) {
if (!args)
return createPacket(type);
switch (type) { switch (type) {
case PacketType::Pause: return make_shared<PausePacket>(args[0].toBool()); case PacketType::Pause: return make_shared<PausePacket>(args->getBool("pause"), args->getFloat("timescale", 1.0f));
case PacketType::ServerInfo: return make_shared<ServerInfoPacket>(args[0].toUInt(), args[1].toUInt()); case PacketType::ServerInfo: return make_shared<ServerInfoPacket>(args->getUInt("players"), args->getUInt("maxPlayers"));
case PacketType::GiveItem: return make_shared<GiveItemPacket>(ItemDescriptor(args[0])); case PacketType::GiveItem: return make_shared<GiveItemPacket>(ItemDescriptor(args->getObject("ItemDescriptor")));
case PacketType::UpdateTileProtection: return make_shared<UpdateTileProtectionPacket>(args[0].toUInt(), args[1].toBool()); case PacketType::UpdateTileProtection: return make_shared<UpdateTileProtectionPacket>(args->getUInt("dungeonId"), args->getBool("protected"));
case PacketType::SetDungeonGravity: return make_shared<SetDungeonGravityPacket>(args[0].toUInt(), args[0].toFloat()); case PacketType::SetDungeonGravity: return make_shared<SetDungeonGravityPacket>(args->getUInt("dungeonId"), args->getFloat("gravity"));
case PacketType::SetDungeonBreathable: return make_shared<SetDungeonBreathablePacket>(args[0].toUInt(), args[0].toBool()); case PacketType::SetDungeonBreathable: return make_shared<SetDungeonBreathablePacket>(args->getUInt("dungeonId"), args->getBool("breathable"));
case PacketType::SetPlayerStart: return make_shared<SetPlayerStartPacket>(Vec2F{args[0].toArray()[0].toFloat(), args[0].toArray()[0].toFloat()}, args[1].toBool()); case PacketType::SetPlayerStart: return make_shared<SetPlayerStartPacket>(Vec2F{args->getArray("position")[0].toFloat(), args->getArray("position")[1].toFloat()}, args->getBool("respawnInWorld"));
case PacketType::EntityMessage: return make_shared<EntityMessagePacket>(EntityId(args[0].toUInt()), args[1].toString(), args[2].toArray(), Uuid(args[3].toString())); case PacketType::EntityMessage: return make_shared<EntityMessagePacket>(EntityId(args->getInt("entityId")), args->getString("message"), args->get("JsonArray").toArray(), Uuid(args->getString("Uuid")));
case PacketType::UpdateWorldProperties: return make_shared<UpdateWorldPropertiesPacket>(args[0].toObject()); case PacketType::UpdateWorldProperties: return make_shared<UpdateWorldPropertiesPacket>(args->getObject("updatedProperties"));
default: default:
throw StarPacketException(strf("Unrecognized packet type {}", (unsigned int)type)); throw StarPacketException(strf("Unrecognized packet type {}", (unsigned int)type));
} }

View File

@ -139,7 +139,7 @@ struct Packet {
}; };
PacketPtr createPacket(PacketType type); PacketPtr createPacket(PacketType type);
PacketPtr createPacket(PacketType type, JsonArray const& args); PacketPtr createPacket(PacketType type, Maybe<Json> const& args);
template <PacketType PacketT> template <PacketType PacketT>
struct PacketBase : public Packet { struct PacketBase : public Packet {