30
30
#include " Server.hpp"
31
31
#include " ServerBlock.hpp"
32
32
#include " ServerPlayer.hpp"
33
- #include " ServerWorld.hpp"
34
33
#include " ServerCommandHandler.hpp"
34
+ #include " WorldController.hpp"
35
35
36
36
void ServerCommandHandler::sendBlockDataUpdate (s32 x, s32 y, s32 z, const BlockData *blockData, const ClientInfo *client) const {
37
37
sf::Packet packet;
@@ -125,7 +125,7 @@ void ServerCommandHandler::setupCallbacks() {
125
125
s32 cx, cy, cz;
126
126
packet >> cx >> cy >> cz;
127
127
128
- m_world .sendRequestedData (client, cx, cy, cz);
128
+ getWorldForClient (client. id ) .sendRequestedData (client, cx, cy, cz);
129
129
});
130
130
131
131
m_server.setCommandCallback (Network::Command::PlayerInvUpdate, [this ](ClientInfo &client, sf::Packet &packet) {
@@ -146,22 +146,24 @@ void ServerCommandHandler::setupCallbacks() {
146
146
m_players.at (client.id ).setPosition (x, y, z);
147
147
});
148
148
149
- m_server.setCommandCallback (Network::Command::PlayerPlaceBlock, [this ](ClientInfo &, sf::Packet &packet) {
149
+ m_server.setCommandCallback (Network::Command::PlayerPlaceBlock, [this ](ClientInfo &client , sf::Packet &packet) {
150
150
s32 x, y, z;
151
151
u32 block;
152
152
packet >> x >> y >> z >> block;
153
- m_world.setBlock (x, y, z, block & 0xffff );
154
- m_world.setData (x, y, z, block >> 16 );
153
+
154
+ ServerWorld &world = getWorldForClient (client.id );
155
+ world.setBlock (x, y, z, block & 0xffff );
156
+ world.setData (x, y, z, block >> 16 );
155
157
156
158
sf::Packet answer;
157
159
answer << Network::Command::BlockUpdate << x << y << z << block;
158
160
m_server.sendToAllClients (answer);
159
161
});
160
162
161
- m_server.setCommandCallback (Network::Command::PlayerDigBlock, [this ](ClientInfo &, sf::Packet &packet) {
163
+ m_server.setCommandCallback (Network::Command::PlayerDigBlock, [this ](ClientInfo &client , sf::Packet &packet) {
162
164
s32 x, y, z;
163
165
packet >> x >> y >> z;
164
- m_world .setBlock (x, y, z, 0 );
166
+ getWorldForClient (client. id ) .setBlock (x, y, z, 0 );
165
167
166
168
sf::Packet answer;
167
169
answer << Network::Command::BlockUpdate << x << y << z << u32 (0 );
@@ -204,26 +206,28 @@ void ServerCommandHandler::setupCallbacks() {
204
206
u8 guiScale;
205
207
packet >> x >> y >> z >> screenWidth >> screenHeight >> guiScale;
206
208
207
- u16 id = m_world.getBlock (x, y, z);
208
- ((ServerBlock &)(m_registry.getBlock (id))).onBlockActivated ({x, y, z}, m_players.at (client.id ), m_world, client, screenWidth, screenHeight, guiScale);
209
+ ServerWorld &world = getWorldForClient (client.id );
210
+
211
+ u16 id = world.getBlock (x, y, z);
212
+ ((ServerBlock &)(m_registry.getBlock (id))).onBlockActivated ({x, y, z}, m_players.at (client.id ), world, client, screenWidth, screenHeight, guiScale);
209
213
});
210
214
211
- m_server.setCommandCallback (Network::Command::BlockInvUpdate, [this ](ClientInfo &, sf::Packet &packet) {
215
+ m_server.setCommandCallback (Network::Command::BlockInvUpdate, [this ](ClientInfo &client , sf::Packet &packet) {
212
216
gk::Vector3<s32> pos;
213
217
packet >> pos.x >> pos.y >> pos.z ;
214
218
215
- BlockData *data = m_world .getBlockData (pos.x , pos.y , pos.z );
219
+ BlockData *data = getWorldForClient (client. id ) .getBlockData (pos.x , pos.y , pos.z );
216
220
if (data)
217
221
packet >> data->inventory ;
218
222
else
219
223
DEBUG (" BlockInvUpdate: No block data found at" , pos.x , pos.y , pos.z );
220
224
});
221
225
222
- m_server.setCommandCallback (Network::Command::BlockDataUpdate, [this ](ClientInfo &, sf::Packet &packet) {
226
+ m_server.setCommandCallback (Network::Command::BlockDataUpdate, [this ](ClientInfo &client , sf::Packet &packet) {
223
227
gk::Vector3<s32> pos;
224
228
packet >> pos.x >> pos.y >> pos.z ;
225
229
226
- BlockData *data = m_world .getBlockData (pos.x , pos.y , pos.z );
230
+ BlockData *data = getWorldForClient (client. id ) .getBlockData (pos.x , pos.y , pos.z );
227
231
if (data) {
228
232
packet >> data->meta >> data->useAltTiles ;
229
233
}
@@ -278,3 +282,11 @@ void ServerCommandHandler::setupCallbacks() {
278
282
});
279
283
}
280
284
285
+ inline ServerWorld &ServerCommandHandler::getWorldForClient (u16 clientID) {
286
+ auto it = m_players.find (clientID);
287
+ if (it == m_players.end ())
288
+ throw EXCEPTION (" Player instance not found for client" , clientID);
289
+
290
+ return m_worldController.getWorld (it->second .dimension ());
291
+ }
292
+
0 commit comments