Skip to content

Commit 20d4bee

Browse files
authored
Added /gamemode command (#159)
- Other functional GameType/GameMode related cleanup Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com>
1 parent c12b261 commit 20d4bee

File tree

8 files changed

+100
-31
lines changed

8 files changed

+100
-31
lines changed

source/network/ServerSideNetworkHandler.cpp

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,17 @@ OnlinePlayer* ServerSideNetworkHandler::getPlayerByGUID(const RakNet::RakNetGUID
414414

415415
void ServerSideNetworkHandler::setupCommands()
416416
{
417-
m_commands["?"] = &ServerSideNetworkHandler::commandHelp;
418-
m_commands["help"] = &ServerSideNetworkHandler::commandHelp;
419-
m_commands["stats"] = &ServerSideNetworkHandler::commandStats;
420-
m_commands["time"] = &ServerSideNetworkHandler::commandTime;
421-
m_commands["seed"] = &ServerSideNetworkHandler::commandSeed;
422-
m_commands["tp"] = &ServerSideNetworkHandler::commandTP;
423-
m_commands["summon"] = &ServerSideNetworkHandler::commandSummon;
417+
m_commands["?"] = &ServerSideNetworkHandler::commandHelp;
418+
m_commands["help"] = &ServerSideNetworkHandler::commandHelp;
419+
m_commands["stats"] = &ServerSideNetworkHandler::commandStats;
420+
m_commands["time"] = &ServerSideNetworkHandler::commandTime;
421+
m_commands["seed"] = &ServerSideNetworkHandler::commandSeed;
422+
m_commands["tp"] = &ServerSideNetworkHandler::commandTP;
423+
m_commands["summon"] = &ServerSideNetworkHandler::commandSummon;
424+
m_commands["gamemode"] = &ServerSideNetworkHandler::commandGamemode;
424425
}
425426

426-
bool ServerSideNetworkHandler::checkPermissions(OnlinePlayer* player)
427+
bool ServerSideNetworkHandler::_checkPermissions(OnlinePlayer* player)
427428
{
428429
if (player->m_pPlayer != m_pMinecraft->m_pLocalPlayer)
429430
{
@@ -434,6 +435,22 @@ bool ServerSideNetworkHandler::checkPermissions(OnlinePlayer* player)
434435
return true;
435436
}
436437

438+
bool ServerSideNetworkHandler::_validateNum(OnlinePlayer* player, int value, int min, int max)
439+
{
440+
if (value < min)
441+
{
442+
sendMessage(player, Util::format("The number you have entered (%d) is too small, it must be at least %d", value, min));
443+
return false;
444+
}
445+
else if (value > max)
446+
{
447+
sendMessage(player, Util::format("The number you have entered (%d) is too big, it must be at most %d", value, max));
448+
return false;
449+
}
450+
451+
return true;
452+
}
453+
437454
void ServerSideNetworkHandler::commandHelp(OnlinePlayer* player, const std::vector<std::string>& parms)
438455
{
439456
std::stringstream ss;
@@ -479,7 +496,7 @@ void ServerSideNetworkHandler::commandTime(OnlinePlayer* player, const std::vect
479496
return;
480497
}
481498

482-
if (!checkPermissions(player)) return;
499+
if (!_checkPermissions(player)) return;
483500

484501
m_pLevel->setTime(t);
485502

@@ -517,7 +534,7 @@ void ServerSideNetworkHandler::commandTP(OnlinePlayer* player, const std::vector
517534
return;
518535
}
519536

520-
if (!checkPermissions(player)) return;
537+
if (!_checkPermissions(player)) return;
521538

522539
Vec3 pos = player->m_pPlayer->getPos(1.0f);
523540

@@ -564,7 +581,7 @@ void ServerSideNetworkHandler::commandSummon(OnlinePlayer* player, const std::ve
564581
return;
565582
}
566583

567-
if (!checkPermissions(player)) return;
584+
if (!_checkPermissions(player)) return;
568585

569586
std::string entityName;
570587
std::stringstream ss;
@@ -643,3 +660,36 @@ void ServerSideNetworkHandler::commandSummon(OnlinePlayer* player, const std::ve
643660

644661
sendMessage(player, ss.str());
645662
}
663+
664+
void ServerSideNetworkHandler::commandGamemode(OnlinePlayer* player, const std::vector<std::string>& parms)
665+
{
666+
if (!m_pLevel)
667+
return;
668+
669+
if (parms.size() != 1)
670+
{
671+
sendMessage(player, "Usage: /gamemode <mode>");
672+
return;
673+
}
674+
/*if (parms.size() < 1 || parms.size() > 2)
675+
{
676+
sendMessage(player, "Usage: /gamemode <mode> [player]");
677+
return;
678+
}*/
679+
680+
if (!_checkPermissions(player)) return;
681+
682+
Vec3 pos = player->m_pPlayer->getPos(1.0f);
683+
684+
GameType gameMode;
685+
std::stringstream ss;
686+
ss.str(parms[0]);
687+
ss >> (int&)gameMode;
688+
689+
if (!_validateNum(player, gameMode, GAME_TYPES_MIN, GAME_TYPES_MAX))
690+
return;
691+
692+
player->m_pPlayer->setPlayerGameType(gameMode);
693+
694+
sendMessage(player, "Your game mode has been updated");
695+
}

source/network/ServerSideNetworkHandler.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ typedef std::map<RakNet::RakNetGUID, OnlinePlayer*> OnlinePlayerMap;
3333
class ServerSideNetworkHandler : public NetEventCallback, public LevelListener
3434
{
3535
private:
36-
bool checkPermissions(OnlinePlayer* player);
36+
bool _checkPermissions(OnlinePlayer* player);
37+
bool _validateNum(OnlinePlayer* player, int value, int min, int max);
3738

3839
public:
3940

@@ -70,12 +71,13 @@ class ServerSideNetworkHandler : public NetEventCallback, public LevelListener
7071
void setupCommands();
7172

7273
// Commands
73-
void commandHelp (OnlinePlayer*, const std::vector<std::string>&);
74-
void commandStats (OnlinePlayer*, const std::vector<std::string>&);
75-
void commandTime (OnlinePlayer*, const std::vector<std::string>&);
76-
void commandSeed (OnlinePlayer*, const std::vector<std::string>&);
77-
void commandTP (OnlinePlayer*, const std::vector<std::string>&);
78-
void commandSummon (OnlinePlayer*, const std::vector<std::string>&);
74+
void commandHelp (OnlinePlayer*, const std::vector<std::string>&);
75+
void commandStats (OnlinePlayer*, const std::vector<std::string>&);
76+
void commandTime (OnlinePlayer*, const std::vector<std::string>&);
77+
void commandSeed (OnlinePlayer*, const std::vector<std::string>&);
78+
void commandTP (OnlinePlayer*, const std::vector<std::string>&);
79+
void commandSummon (OnlinePlayer*, const std::vector<std::string>&);
80+
void commandGamemode (OnlinePlayer*, const std::vector<std::string>&);
7981

8082
public:
8183
Minecraft* m_pMinecraft;

source/world/entity/LocalPlayer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ bool LocalPlayer::isImmobile() const
8787
return Player::isImmobile() || (m_pMinecraft->useController() && m_pMinecraft->m_pScreen); //!m_pMinecraft->m_bGrabbedMouse; // this works if we still set this when not using a mouse
8888
}
8989

90+
void LocalPlayer::setPlayerGameType(GameType gameType)
91+
{
92+
// @HACK: This info should be updated / stored in one place, and one place only.
93+
// Don't know what crack Notch was smoking.
94+
m_pMinecraft->setGameMode(gameType);
95+
96+
Player::setPlayerGameType(gameType);
97+
}
98+
9099
void LocalPlayer::animateRespawn()
91100
{
92101

source/world/entity/LocalPlayer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class LocalPlayer : public Player
3030
virtual bool isLocalPlayer() const override { return true; }
3131
virtual void drop(const ItemInstance* pItemInstance, bool b = false) override;
3232
virtual bool isImmobile() const override;
33+
virtual void setPlayerGameType(GameType gameType) override;
3334

3435
void calculateFlight(const Vec3& pos);
3536
void closeContainer(); //@HUH: oddly enough not a virtual/override

source/world/entity/Player.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Player : public Mob
5656
void drop();
5757
float getDestroySpeed() const { return 1.0f; }
5858
int getInventorySlot(int x) const;
59-
TilePos getRespawnPosition() { return m_respawnPos; }
59+
TilePos getRespawnPosition() const { return m_respawnPos; }
6060
int getScore() const { return m_score; }
6161
void prepareCustomTextures();
6262
void reallyDrop(ItemEntity* pEnt);
@@ -68,7 +68,7 @@ class Player : public Mob
6868
void take(Entity* pEnt, int x);
6969
void touch(Entity* pEnt);
7070
GameType getPlayerGameType() const { return _playerGameType; }
71-
void setPlayerGameType(GameType playerGameType) { _playerGameType = playerGameType; }
71+
virtual void setPlayerGameType(GameType playerGameType) { _playerGameType = playerGameType; }
7272
bool isSurvival() const { return getPlayerGameType() == GAME_TYPE_SURVIVAL; }
7373
bool isCreative() const { return getPlayerGameType() == GAME_TYPE_CREATIVE; }
7474
ItemInstance* getSelectedItem() const;

source/world/gamemode/GameType.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ enum GameType
66
GAME_TYPE_CREATIVE,
77
GAME_TYPE_ADVENTURE,
88
GAME_TYPE_SPECTATOR,
9-
GAME_TYPES_MAX
9+
GAME_TYPES_MIN = GAME_TYPE_SURVIVAL,
10+
GAME_TYPES_MAX = GAME_TYPE_CREATIVE
1011
};

source/world/item/Inventory.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ Inventory::Inventory(Player* pPlayer)
55
{
66
m_pPlayer = pPlayer;
77
m_selectedHotbarSlot = 0;
8-
m_bIsSurvival = false;
98

109
for (int i = 0; i < C_MAX_HOTBAR_ITEMS; i++)
1110
m_hotbar[i] = -1;
1211
}
1312

1413
void Inventory::prepareCreativeInventory()
1514
{
16-
m_bIsSurvival = false;
17-
1815
m_items.clear();
1916

2017
// Original list of items.
@@ -86,7 +83,6 @@ void Inventory::prepareCreativeInventory()
8683

8784
void Inventory::prepareSurvivalInventory()
8885
{
89-
m_bIsSurvival = true;
9086
m_items.clear();
9187
m_items.resize(C_NUM_SURVIVAL_SLOTS);
9288

@@ -105,10 +101,13 @@ void Inventory::prepareSurvivalInventory()
105101

106102
int Inventory::getNumSlots()
107103
{
108-
if (m_bIsSurvival)
104+
switch (_getGameMode())
105+
{
106+
case GAME_TYPE_SURVIVAL:
109107
return C_NUM_SURVIVAL_SLOTS;
110-
111-
return getNumItems();
108+
default:
109+
return getNumItems();
110+
}
112111
}
113112

114113
int Inventory::getNumItems()
@@ -132,7 +131,7 @@ void Inventory::clear()
132131
// but addResource's code is entirely different somehow. Did we write this from scratch?
133132
void Inventory::addItem(ItemInstance* pInst)
134133
{
135-
if (!m_bIsSurvival)
134+
if (_getGameMode() == GAME_TYPE_CREATIVE)
136135
{
137136
// Just get rid of the item.
138137
pInst->m_count = 0;
@@ -293,7 +292,7 @@ void Inventory::setQuickSlotIndexByItemId(int slotNo, int itemID)
293292
if (slotNo < 0 || slotNo >= C_MAX_HOTBAR_ITEMS)
294293
return;
295294

296-
if (m_bIsSurvival)
295+
if (_getGameMode() == GAME_TYPE_SURVIVAL)
297296
return; // TODO
298297

299298
for (int i = 0; i < getNumItems(); i++)
@@ -344,3 +343,8 @@ void Inventory::dropAll(bool butNotReally)
344343
}
345344
}
346345
}
346+
347+
GameType Inventory::_getGameMode() const
348+
{
349+
return m_pPlayer->getPlayerGameType();
350+
}

source/world/item/Inventory.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vector>
44
#include "world/item/ItemInstance.hpp"
55
#include "world/entity/Player.hpp"
6+
#include "world/gamemode/GameType.hpp"
67

78
class Entity;
89
class Player; // in case we're included from Player.hpp
@@ -52,12 +53,13 @@ class Inventory
5253
ItemInstance* getSelected() {
5354
return getSelectedItem();
5455
}
56+
private:
57+
GameType _getGameMode() const;
5558

5659
public:
5760
int m_selectedHotbarSlot;
5861
private:
5962
Player* m_pPlayer;
60-
bool m_bIsSurvival;
6163

6264
int m_hotbar[C_MAX_HOTBAR_ITEMS];
6365
std::vector<ItemInstance> m_items;

0 commit comments

Comments
 (0)