Skip to content

Commit e788ee2

Browse files
ClobberXDSmallJoker
authored andcommittedAug 7, 2019
Client::Interact: Use InteractAction enum instead of numeric constants
This replaces the magic numbers used as interaction modes both client-side and server-side, primarily for the sake of ease-of-readability.
1 parent e462a9a commit e788ee2

File tree

5 files changed

+59
-60
lines changed

5 files changed

+59
-60
lines changed
 

‎src/client/client.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *
908908
*pkt << fov << wanted_range;
909909
}
910910

911-
void Client::interact(u8 action, const PointedThing& pointed)
911+
void Client::interact(InteractAction action, const PointedThing& pointed)
912912
{
913913
if(m_state != LC_Ready) {
914914
errorstream << "Client::interact() "
@@ -928,18 +928,11 @@ void Client::interact(u8 action, const PointedThing& pointed)
928928
[5] u32 length of the next item (plen)
929929
[9] serialized PointedThing
930930
[9 + plen] player position information
931-
actions:
932-
0: start digging (from undersurface) or use
933-
1: stop digging (all parameters ignored)
934-
2: digging completed
935-
3: place block or item (to abovesurface)
936-
4: use item
937-
5: perform secondary action of item
938931
*/
939932

940933
NetworkPacket pkt(TOSERVER_INTERACT, 1 + 2 + 0);
941934

942-
pkt << action;
935+
pkt << (u8)action;
943936
pkt << myplayer->getWieldIndex();
944937

945938
std::ostringstream tmp_os(std::ios::binary);
@@ -1203,7 +1196,7 @@ void Client::clearOutChatQueue()
12031196
}
12041197

12051198
void Client::sendChangePassword(const std::string &oldpassword,
1206-
const std::string &newpassword)
1199+
const std::string &newpassword)
12071200
{
12081201
LocalPlayer *player = m_env.getLocalPlayer();
12091202
if (player == NULL)

‎src/client/client.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
232232

233233
void Send(NetworkPacket* pkt);
234234

235-
void interact(u8 action, const PointedThing& pointed);
235+
void interact(InteractAction action, const PointedThing &pointed);
236236

237237
void sendNodemetaFields(v3s16 p, const std::string &formname,
238238
const StringMap &fields);

‎src/client/game.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -2962,7 +2962,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
29622962
shootline = core::line3d<f32>(player_eye_position,
29632963
player_eye_position + camera_direction * BS * d);
29642964
} else {
2965-
// prevent player pointing anything in front-view
2965+
// prevent player pointing anything in front-view
29662966
shootline = core::line3d<f32>(camera_position, camera_position);
29672967
}
29682968

@@ -3002,7 +3002,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
30023002
if (runData.digging) {
30033003
if (input->getLeftReleased()) {
30043004
infostream << "Left button released"
3005-
<< " (stopped digging)" << std::endl;
3005+
<< " (stopped digging)" << std::endl;
30063006
runData.digging = false;
30073007
} else if (pointed != runData.pointed_old) {
30083008
if (pointed.type == POINTEDTHING_NODE
@@ -3013,14 +3013,14 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
30133013
// Don't reset.
30143014
} else {
30153015
infostream << "Pointing away from node"
3016-
<< " (stopped digging)" << std::endl;
3016+
<< " (stopped digging)" << std::endl;
30173017
runData.digging = false;
30183018
hud->updateSelectionMesh(camera_offset);
30193019
}
30203020
}
30213021

30223022
if (!runData.digging) {
3023-
client->interact(1, runData.pointed_old);
3023+
client->interact(INTERACT_STOP_DIGGING, runData.pointed_old);
30243024
client->setCrack(-1, v3s16(0, 0, 0));
30253025
runData.dig_time = 0.0;
30263026
}
@@ -3048,7 +3048,7 @@ void Game::processPlayerInteraction(f32 dtime, bool show_hud, bool show_debug)
30483048
if (selected_def.usable && input->getLeftState()) {
30493049
if (input->getLeftClicked() && (!client->moddingEnabled()
30503050
|| !client->getScript()->on_item_use(selected_item, pointed)))
3051-
client->interact(4, pointed);
3051+
client->interact(INTERACT_USE, pointed);
30523052
} else if (pointed.type == POINTEDTHING_NODE) {
30533053
handlePointingAtNode(pointed, selected_item, hand_item, dtime);
30543054
} else if (pointed.type == POINTEDTHING_OBJECT) {
@@ -3169,7 +3169,7 @@ void Game::handlePointingAtNothing(const ItemStack &playerItem)
31693169
infostream << "Right Clicked in Air" << std::endl;
31703170
PointedThing fauxPointed;
31713171
fauxPointed.type = POINTEDTHING_NOTHING;
3172-
client->interact(5, fauxPointed);
3172+
client->interact(INTERACT_ACTIVATE, fauxPointed);
31733173
}
31743174

31753175

@@ -3216,7 +3216,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
32163216
&& !isKeyDown(KeyType::SNEAK)) {
32173217
// Report right click to server
32183218
if (nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) {
3219-
client->interact(3, pointed);
3219+
client->interact(INTERACT_PLACE, pointed);
32203220
}
32213221

32223222
infostream << "Launching custom inventory view" << std::endl;
@@ -3246,7 +3246,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
32463246

32473247
if (placed) {
32483248
// Report to server
3249-
client->interact(3, pointed);
3249+
client->interact(INTERACT_PLACE, pointed);
32503250
// Read the sound
32513251
soundmaker->m_player_rightpunch_sound =
32523252
def.sound_place;
@@ -3259,7 +3259,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
32593259

32603260
if (def.node_placement_prediction.empty() ||
32613261
nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) {
3262-
client->interact(3, pointed); // Report to server
3262+
client->interact(INTERACT_PLACE, pointed); // Report to server
32633263
} else {
32643264
soundmaker->m_player_rightpunch_sound =
32653265
def.sound_place_failed;
@@ -3462,11 +3462,11 @@ void Game::handlePointingAtObject(const PointedThing &pointed,
34623462
runData.time_from_last_punch = 0;
34633463

34643464
if (!disable_send)
3465-
client->interact(0, pointed);
3465+
client->interact(INTERACT_START_DIGGING, pointed);
34663466
}
34673467
} else if (input->getRightClicked()) {
34683468
infostream << "Right-clicked object" << std::endl;
3469-
client->interact(3, pointed); // place
3469+
client->interact(INTERACT_PLACE, pointed); // place
34703470
}
34713471
}
34723472

@@ -3509,7 +3509,7 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
35093509
runData.dig_instantly = runData.dig_time_complete == 0;
35103510
if (client->moddingEnabled() && client->getScript()->on_punchnode(nodepos, n))
35113511
return;
3512-
client->interact(0, pointed);
3512+
client->interact(INTERACT_START_DIGGING, pointed);
35133513
runData.digging = true;
35143514
runData.ldown_for_dig = true;
35153515
}
@@ -3568,7 +3568,7 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
35683568
MapNode wasnode = map.getNodeNoEx(nodepos, &is_valid_position);
35693569
if (is_valid_position) {
35703570
if (client->moddingEnabled() &&
3571-
client->getScript()->on_dignode(nodepos, wasnode)) {
3571+
client->getScript()->on_dignode(nodepos, wasnode)) {
35723572
return;
35733573
}
35743574

@@ -3584,7 +3584,7 @@ void Game::handleDigging(const PointedThing &pointed, const v3s16 &nodepos,
35843584
// implicit else: no prediction
35853585
}
35863586

3587-
client->interact(2, pointed);
3587+
client->interact(INTERACT_DIGGING_COMPLETED, pointed);
35883588

35893589
if (m_cache_enable_particles) {
35903590
const ContentFeatures &features =

‎src/network/networkprotocol.h

+15-5
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,20 @@ enum CSMRestrictionFlags : u64 {
954954
// When those are complete, this should return to only being a restriction on the
955955
// loading of client mods.
956956
CSM_RF_LOAD_CLIENT_MODS = 0x00000001, // Don't load client-provided mods or 'builtin'
957-
CSM_RF_CHAT_MESSAGES = 0x00000002, // Disable chat message sending from CSM
958-
CSM_RF_READ_ITEMDEFS = 0x00000004, // Disable itemdef lookups
959-
CSM_RF_READ_NODEDEFS = 0x00000008, // Disable nodedef lookups
960-
CSM_RF_LOOKUP_NODES = 0x00000010, // Limit node lookups
961-
CSM_RF_READ_PLAYERINFO = 0x00000020, // Disable player info lookups
957+
CSM_RF_CHAT_MESSAGES = 0x00000002, // Disable chat message sending from CSM
958+
CSM_RF_READ_ITEMDEFS = 0x00000004, // Disable itemdef lookups
959+
CSM_RF_READ_NODEDEFS = 0x00000008, // Disable nodedef lookups
960+
CSM_RF_LOOKUP_NODES = 0x00000010, // Limit node lookups
961+
CSM_RF_READ_PLAYERINFO = 0x00000020, // Disable player info lookups
962962
CSM_RF_ALL = 0xFFFFFFFF,
963963
};
964+
965+
enum InteractAction : u8
966+
{
967+
INTERACT_START_DIGGING, // 0: start digging (from undersurface) or use
968+
INTERACT_STOP_DIGGING, // 1: stop digging (all parameters ignored)
969+
INTERACT_DIGGING_COMPLETED, // 2: digging completed
970+
INTERACT_PLACE, // 3: place block or item (to abovesurface)
971+
INTERACT_USE, // 4: use item
972+
INTERACT_ACTIVATE // 5: rightclick air ("activate")
973+
};

‎src/network/serverpackethandler.cpp

+26-30
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ bool Server::checkInteractDistance(RemotePlayer *player, const f32 d, const std:
974974
return true;
975975
}
976976

977-
void Server::handleCommand_Interact(NetworkPacket* pkt)
977+
void Server::handleCommand_Interact(NetworkPacket *pkt)
978978
{
979979
/*
980980
[0] u16 command
@@ -983,18 +983,14 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
983983
[5] u32 length of the next item (plen)
984984
[9] serialized PointedThing
985985
[9 + plen] player position information
986-
actions:
987-
0: start digging (from undersurface) or use
988-
1: stop digging (all parameters ignored)
989-
2: digging completed
990-
3: place block or item (to abovesurface)
991-
4: use item
992-
5: rightclick air ("activate")
993986
*/
994-
u8 action;
987+
988+
InteractAction action;
995989
u16 item_i;
996-
*pkt >> action;
990+
991+
*pkt >> (u8 &)action;
997992
*pkt >> item_i;
993+
998994
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
999995
PointedThing pointed;
1000996
pointed.deSerialize(tmp_is);
@@ -1073,18 +1069,18 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
10731069
Make sure the player is allowed to do it
10741070
*/
10751071
if (!checkPriv(player->getName(), "interact")) {
1076-
actionstream<<player->getName()<<" attempted to interact with "
1077-
<<pointed.dump()<<" without 'interact' privilege"
1078-
<<std::endl;
1072+
actionstream << player->getName() << " attempted to interact with " <<
1073+
pointed.dump() << " without 'interact' privilege" << std::endl;
1074+
10791075
// Re-send block to revert change on client-side
10801076
RemoteClient *client = getClient(pkt->getPeerId());
10811077
// Digging completed -> under
1082-
if (action == 2) {
1078+
if (action == INTERACT_DIGGING_COMPLETED) {
10831079
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
10841080
client->SetBlockNotSent(blockpos);
10851081
}
10861082
// Placement -> above
1087-
else if (action == 3) {
1083+
else if (action == INTERACT_PLACE) {
10881084
v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
10891085
client->SetBlockNotSent(blockpos);
10901086
}
@@ -1098,10 +1094,10 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
10981094
static thread_local const bool enable_anticheat =
10991095
!g_settings->getBool("disable_anticheat");
11001096

1101-
if ((action == 0 || action == 2 || action == 3 || action == 4) &&
1097+
if ((action == INTERACT_START_DIGGING || action == INTERACT_DIGGING_COMPLETED ||
1098+
action == INTERACT_PLACE || action == INTERACT_USE) &&
11021099
enable_anticheat && !isSingleplayer()) {
1103-
float d = playersao->getEyePosition()
1104-
.getDistanceFrom(pointed_pos_under);
1100+
float d = playersao->getEyePosition().getDistanceFrom(pointed_pos_under);
11051101

11061102
if (!checkInteractDistance(player, d, pointed.dump())) {
11071103
// Re-send block to revert change on client-side
@@ -1121,7 +1117,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
11211117
/*
11221118
0: start digging or punch object
11231119
*/
1124-
if (action == 0) {
1120+
if (action == INTERACT_START_DIGGING) {
11251121
if (pointed.type == POINTEDTHING_NODE) {
11261122
MapNode n(CONTENT_IGNORE);
11271123
bool pos_ok;
@@ -1174,18 +1170,18 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
11741170
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, pointed_object));
11751171
}
11761172

1177-
} // action == 0
1173+
} // action == INTERACT_START_DIGGING
11781174

11791175
/*
11801176
1: stop digging
11811177
*/
1182-
else if (action == 1) {
1183-
} // action == 1
1178+
else if (action == INTERACT_STOP_DIGGING) {
1179+
} // action == INTERACT_STOP_DIGGING
11841180

11851181
/*
11861182
2: Digging completed
11871183
*/
1188-
else if (action == 2) {
1184+
else if (action == INTERACT_DIGGING_COMPLETED) {
11891185
// Only digging of nodes
11901186
if (pointed.type == POINTEDTHING_NODE) {
11911187
bool pos_ok;
@@ -1281,12 +1277,12 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
12811277
client->ResendBlockIfOnWire(blockpos);
12821278
}
12831279
}
1284-
} // action == 2
1280+
} // action == INTERACT_DIGGING_COMPLETED
12851281

12861282
/*
12871283
3: place block or right-click object
12881284
*/
1289-
else if (action == 3) {
1285+
else if (action == INTERACT_PLACE) {
12901286
ItemStack item = playersao->getWieldedItem();
12911287

12921288
// Reset build time counter
@@ -1335,12 +1331,12 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
13351331
client->ResendBlockIfOnWire(blockpos2);
13361332
}
13371333
}
1338-
} // action == 3
1334+
} // action == INTERACT_PLACE
13391335

13401336
/*
13411337
4: use
13421338
*/
1343-
else if (action == 4) {
1339+
else if (action == INTERACT_USE) {
13441340
ItemStack item = playersao->getWieldedItem();
13451341

13461342
actionstream << player->getName() << " uses " << item.name
@@ -1354,12 +1350,12 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
13541350
}
13551351
}
13561352

1357-
} // action == 4
1353+
} // action == INTERACT_USE
13581354

13591355
/*
13601356
5: rightclick air
13611357
*/
1362-
else if (action == 5) {
1358+
else if (action == INTERACT_ACTIVATE) {
13631359
ItemStack item = playersao->getWieldedItem();
13641360

13651361
actionstream << player->getName() << " activates "
@@ -1371,7 +1367,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
13711367
SendInventory(playersao);
13721368
}
13731369
}
1374-
}
1370+
} // action == INTERACT_ACTIVATE
13751371

13761372

13771373
/*

0 commit comments

Comments
 (0)
Please sign in to comment.