|
3 | 3 | #include <mc/world/actor/state/PropertySyncData.h> |
4 | 4 |
|
5 | 5 |
|
| 6 | +#include "ll/api/base/StdInt.h" |
6 | 7 | #include "ll/api/event/player/PlayerDisconnectEvent.h" |
7 | 8 | #include "ll/api/io/LoggerRegistry.h" |
8 | 9 | #include "ll/api/mod/RegisterHelper.h" |
|
29 | 30 | #include <ll/api/mod/NativeMod.h> |
30 | 31 | #include <ll/api/service/Bedrock.h> |
31 | 32 | #include <mc/entity/components/SynchedActorDataComponent.h> |
| 33 | +#include <mc/network/LoopbackPacketSender.h> |
32 | 34 | #include <mc/network/MinecraftPackets.h> |
| 35 | +#include <mc/network/NetworkIdentifierWithSubId.h> |
33 | 36 | #include <mc/network/PacketSender.h> |
34 | 37 | #include <mc/network/packet/AddActorPacket.h> |
35 | | -#include <mc/network/packet/BookEditPacket.h> |
36 | 38 | #include <mc/network/packet/InventoryContentPacket.h> |
37 | | -#include <mc/network/packet/LegacyTelemetryEventPacket.h> |
38 | | -#include <mc/network/packet/LoginPacket.h> |
| 39 | +#include <mc/network/packet/PlayerListPacket.h> |
39 | 40 | #include <mc/network/packet/SetActorDataPacket.h> |
40 | 41 | #include <mc/network/packet/SyncedAttribute.h> |
41 | 42 | #include <mc/scripting/ServerScriptManager.h> |
|
49 | 50 | #include <mc/world/actor/DataItemType.h> |
50 | 51 | #include <mc/world/actor/SynchedActorData.h> |
51 | 52 | #include <mc/world/actor/player/Player.h> |
| 53 | +#include <mc/world/actor/player/PlayerListEntry.h> |
52 | 54 | #include <mc/world/actor/state/PropertySyncData.h> |
53 | 55 | #include <mc/world/attribute/AttributeInstanceHandle.h> |
54 | 56 | #include <mc/world/item/NetworkItemStackDescriptor.h> |
55 | 57 | #include <memory> |
56 | 58 | #include <stdexcept> |
57 | 59 |
|
58 | 60 |
|
| 61 | + |
59 | 62 | #include <string> |
60 | 63 |
|
61 | 64 |
|
62 | 65 | // Bossbar: https://github.com/GroupMountain/GMLIB-Release/blob/develop/src/Server/PlayerAPI.cc#L485-L566 |
63 | 66 |
|
64 | 67 | auto dedicatedServerLogger = ll::io::LoggerRegistry::getInstance().getOrCreate("DedicatedServer"); |
65 | 68 |
|
66 | | -void translateEntityNameTag(std::vector<std::unique_ptr<DataItem>>& mData, Player* player) { |
| 69 | +using MData = std::vector<std::unique_ptr<DataItem>>&; |
| 70 | + |
| 71 | +int64 findNameTagDataItem(MData mData) { |
67 | 72 | auto it = std::find_if(mData.begin(), mData.end(), [&](const std::unique_ptr<DataItem>& item) { |
68 | 73 | return item && item->getId() == (ushort)ActorDataIDs::Name; |
69 | 74 | }); |
70 | 75 |
|
71 | 76 | if (it != mData.end()) { |
72 | | - size_t index = std::distance(mData.begin(), it); |
| 77 | + return std::distance(mData.begin(), it); |
| 78 | + } else { |
| 79 | + return -1; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +bool isNameTagTranslatable(MData mData, int64 index) { |
| 84 | + if (index == (-1)) return false; |
| 85 | + |
| 86 | + auto& item = *mData[index]; |
| 87 | + auto nameTag = *item.getData<std::string>(); |
| 88 | + if (nameTag == "notrans") return false; |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +void translateEntityNameTag(MData mData, int64 index, Player* player) { |
| 93 | + if (isNameTagTranslatable(mData, index)) { |
| 94 | + auto nameTag = mData[index]->getData<std::string>(); |
| 95 | + dedicatedServerLogger->info("Translating name tag from {} for {}", nameTag->data(), player->getRealName()); |
| 96 | + |
73 | 97 | (mData)[index] = DataItem::create( |
74 | 98 | ActorDataIDs::Name, |
75 | | - std::string("Custom entity name for " + player->getRealName() + " " + player->getLocaleCode()) |
| 99 | + std::string("For " + player->getRealName() + " " + player->getLocaleCode()) |
76 | 100 | ); |
77 | 101 | } else { |
78 | 102 | dedicatedServerLogger->info("Not found name tag for data with size {}", mData.size()); |
79 | 103 | } |
80 | 104 | } |
81 | 105 |
|
| 106 | +std::string translatePlayerNameTag(const std::string& nameTag, Player* forPlayer) { |
| 107 | + return nameTag + " For " + forPlayer->getRealName(); |
| 108 | +} |
82 | 109 |
|
83 | | -LL_AUTO_TYPE_INSTANCE_HOOK( |
84 | | - PacketSenderHook, |
85 | | - ll::memory::HookPriority::High, |
86 | | - NetworkSystem, |
87 | | - &NetworkSystem::send, |
88 | | - void, |
89 | | - const ::NetworkIdentifier& id, |
90 | | - const Packet& packet, |
91 | | - ::SubClientId subClientId |
92 | | -) { |
| 110 | + |
| 111 | +ServerPlayer* getPlayerFromNetworkId(::NetworkIdentifier const& id, ::SubClientId recipientSubId) { |
93 | 112 | auto serverNetworkHandler = ll::service::getServerNetworkHandler(); |
94 | 113 | if (!serverNetworkHandler) { |
95 | | - return origin(id, packet, subClientId); |
| 114 | + return nullptr; |
96 | 115 | } |
97 | 116 |
|
98 | | - ServerPlayer* player = serverNetworkHandler->_getServerPlayer(id, subClientId); |
| 117 | + ServerPlayer* player = serverNetworkHandler->_getServerPlayer(id, recipientSubId); |
99 | 118 | if (!player) { |
100 | | - return origin(id, packet, subClientId); |
| 119 | + return nullptr; |
101 | 120 | } |
102 | 121 |
|
103 | | - auto packetId = packet.getId(); |
104 | | - dedicatedServerLogger->info("Sending {} to {}", packet.getName(), player->getRealName()); |
105 | | - |
106 | | - if (packetId == MinecraftPacketIds::AddActor || packetId == MinecraftPacketIds::LegacyTelemetryEvent |
107 | | - || packetId == MinecraftPacketIds::CommandOutput) { |
108 | | - BinaryStream bs; |
109 | | - packet.write(bs); |
110 | | - dedicatedServerLogger->info("Text data {}", bs.mBuffer.data()); |
111 | | - } |
112 | | - |
113 | | - if (packetId == MinecraftPacketIds::SetActorData || packetId == MinecraftPacketIds::AddActor |
114 | | - || packetId == MinecraftPacketIds::InventoryContent || packetId == MinecraftPacketIds::ActorEvent |
115 | | - || packetId == MinecraftPacketIds::SyncActorProperty) { |
116 | | - |
| 122 | + return player; |
| 123 | +} |
117 | 124 |
|
118 | | - if (packetId == MinecraftPacketIds::SetActorData) { |
119 | | - auto& casted = static_cast<const SetActorDataPacket&>(packet); |
| 125 | +LL_AUTO_TYPE_INSTANCE_HOOK( |
| 126 | + SetActorDataPacketSendHook, |
| 127 | + ll::memory::HookPriority::High, |
| 128 | + LoopbackPacketSender, |
| 129 | + &LoopbackPacketSender::$sendToClients, |
| 130 | + void, |
| 131 | + ::std::vector<::NetworkIdentifierWithSubId> const& ids, |
| 132 | + ::Packet const& packet |
| 133 | +) { |
| 134 | + auto packetId = packet.getId(); |
120 | 135 |
|
121 | | - // BinaryStream bs; |
122 | | - // packet.write(bs); |
123 | | - // SetActorDataPacket newPacket{casted.mId, casted.mPackedItems, casted.mSynchedProperties, casted.mTick, |
124 | | - // false}; |
| 136 | + if (packetId == MinecraftPacketIds::SetActorData) { |
125 | 137 |
|
126 | | - // BinaryStream bs2; |
127 | | - // newPacket.write(bs2); |
128 | | - // dedicatedServerLogger->info("Text data {}", bs2.mBuffer.data()); |
| 138 | + auto& casted = static_cast<const SetActorDataPacket&>(packet); |
| 139 | + auto& mData = const_cast<MData>(casted.mPackedItems); |
| 140 | + auto index = findNameTagDataItem(mData); |
129 | 141 |
|
130 | | - // newPacket._read(bs); |
| 142 | + if (isNameTagTranslatable(mData, index)) { |
| 143 | + dedicatedServerLogger->info("SetActorDataHook: Sending {} ", packet.getName()); |
131 | 144 |
|
132 | | - for (auto& item : casted.mPackedItems) { |
133 | | - if (item) { |
134 | | - auto itemDataId = item->getId(); |
135 | | - if (itemDataId == (ushort)ActorDataIDs::Name) { |
136 | | - item->setData<std::string>("Translate and set name here"); |
137 | | - dedicatedServerLogger->info( |
138 | | - "Text nameTag data with content {}", |
139 | | - item->getData<std::string>()->data() |
140 | | - ); |
141 | | - } |
| 145 | + for (auto& id : ids) { |
| 146 | + auto player = getPlayerFromNetworkId(id.id, id.subClientId); |
| 147 | + if (player) { |
| 148 | + translateEntityNameTag(mData, index, player); |
| 149 | + this->sendTo(id.id, id.subClientId, const_cast<Packet&>(packet)); |
| 150 | + } else { |
| 151 | + dedicatedServerLogger->info("SetActorDataHook: Unable to find player, skipping"); |
142 | 152 | } |
143 | 153 | } |
| 154 | + return; |
| 155 | + } |
| 156 | + } |
| 157 | + return origin(ids, packet); |
| 158 | +} |
144 | 159 |
|
145 | | - dedicatedServerLogger->info("Data size {}", casted.mPackedItems.size()); |
146 | 160 |
|
147 | | - return origin(id, casted, subClientId); |
148 | | - } |
| 161 | +LL_AUTO_TYPE_INSTANCE_HOOK( |
| 162 | + PacketSenderHook, |
| 163 | + ll::memory::HookPriority::High, |
| 164 | + NetworkSystem, |
| 165 | + &NetworkSystem::send, |
| 166 | + void, |
| 167 | + const ::NetworkIdentifier& id, |
| 168 | + const Packet& packet, |
| 169 | + ::SubClientId subId |
| 170 | +) { |
| 171 | + auto packetId = packet.getId(); |
149 | 172 |
|
150 | | - // if (packetId == MinecraftPacketIds::AddActor) { |
151 | | - // BinaryStream bs; |
152 | | - // packet.write(bs); |
153 | | - // AddActorPacket newPacket{}; |
154 | | - // newPacket.read(bs); |
155 | | - |
156 | | - // for (auto& item : *newPacket.mData) { |
157 | | - // if (item) { |
158 | | - // auto itemDataId = item->getId(); |
159 | | - // if (itemDataId == (ushort)ActorDataIDs::Name) { |
160 | | - // // item->setData<std::string>("Custom name HAAH LL"); |
161 | | - // dedicatedServerLogger->info( |
162 | | - // "Text nameTag data with id {} and content {}", |
163 | | - // itemDataId, |
164 | | - // item->getData<std::string>()->data() |
165 | | - // ); |
166 | | - // } |
167 | | - // } |
168 | | - // } |
| 173 | + if (packetId == MinecraftPacketIds::AddActor || packetId == MinecraftPacketIds::InventoryContent |
| 174 | + || packetId == MinecraftPacketIds::PlayerList) { |
| 175 | + auto player = getPlayerFromNetworkId(id, subId); |
| 176 | + if (!player) return origin(id, packet, subId); |
169 | 177 |
|
170 | | - // BinaryStream bs2; |
171 | | - // newPacket.write(bs2); |
| 178 | + dedicatedServerLogger->info("Sending {} to {}", packet.getName(), player->getRealName()); |
172 | 179 |
|
173 | | - // dedicatedServerLogger->info("Buffer1 '{}' '{}' ", bs.mBuffer.data(), bs2.mBuffer.data()); |
| 180 | + // if (packetId == MinecraftPacketIds::PlayerList) { |
| 181 | + // auto& casted = static_cast<const PlayerListPacket&>(packet); |
174 | 182 |
|
175 | | - // return origin(id, packet, subClientId); |
| 183 | + // for (auto& entry : *casted.mEntries) { |
| 184 | + // (*entry.mName) = translatePlayerNameTag(*entry.mName, player); |
| 185 | + // } |
176 | 186 | // } |
177 | 187 |
|
178 | 188 | if (packetId == MinecraftPacketIds::AddActor) { |
179 | 189 | auto& casted = static_cast<const AddActorPacket&>(packet); |
180 | 190 | auto& mData = casted.mEntityData->mData.get()->mData->mItemsArray; |
| 191 | + auto index = findNameTagDataItem(mData); |
181 | 192 |
|
182 | | - translateEntityNameTag(mData, player); |
| 193 | + translateEntityNameTag(mData, index, player); |
183 | 194 |
|
184 | | - return origin(id, casted, subClientId); |
| 195 | + return origin(id, casted, subId); |
185 | 196 | } |
186 | 197 |
|
187 | | - // if (packetId == MinecraftPacketIds::BookEdit) { |
188 | | - // BookEditPacket actorData(static_cast<const BookEditPacket&>(packet)); |
189 | | - // dedicatedServerLogger->info("Got properties"); |
190 | | - // } |
191 | | - |
| 198 | + if (packetId == MinecraftPacketIds::InventoryContent) { |
| 199 | + auto& casted = (static_cast<const InventoryContentPacket&>(packet)); |
192 | 200 |
|
193 | | - // if (packetId == MinecraftPacketIds::Login) { |
194 | | - // LoginPacket actorData(static_cast<const LoginPacket&>(packet)); |
195 | | - // dedicatedServerLogger->info("Got properties"); |
196 | | - // } |
| 201 | + for (auto& slot : *casted.mSlots) { |
| 202 | + slot.mImpl; |
| 203 | + } |
197 | 204 |
|
198 | | - // if (packetId == MinecraftPacketIds::InventoryContent) { |
199 | | - // InventoryContentPacket casted = (static_cast<const InventoryContentPacket&>(packet)); |
200 | | - // InventoryContentPacket copiedAndCasted(casted); |
201 | | - // // Modify inventory content |
202 | | - // return origin(id, copiedAndCasted, subClientId); |
203 | | - // } |
| 205 | + return origin(id, casted, subId); |
| 206 | + } |
204 | 207 | } |
205 | | - return origin(id, packet, subClientId); |
| 208 | + return origin(id, packet, subId); |
206 | 209 | } |
207 | 210 |
|
208 | 211 |
|
|
0 commit comments