ModeCommand.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "ModeCommand.h"
  2. #include "Chat.h"
  3. #include "Game.h"
  4. #include "Player.h"
  5. ModeCommand::ModeCommand()
  6. : ChatCommand(
  7. "gamemode", "sets the gamemode to 0 (normal) or 1 (cheats)", 10)
  8. {
  9. addParam(new PlayerNameParameter(true));
  10. addParam(new IntegerParameter("mode",
  11. "0 (normal) or 1 (cheats)",
  12. false,
  13. [](Entity* actor) {
  14. if (actor) return dynamic_cast<Player*>(actor)->getGameMode();
  15. return 0;
  16. }));
  17. }
  18. bool ModeCommand::execute(
  19. Framework::RCArray<Framework::Text>& params, Entity* zActor) const
  20. {
  21. if (zActor && params.getEntryCount() == 1)
  22. {
  23. Player* p = dynamic_cast<Player*>(zActor);
  24. int mode = (int)*params.z(0);
  25. if (mode < 0 || mode > 1)
  26. {
  27. Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
  28. Game::INSTANCE->zChat()->sendMessageTo(
  29. message, zActor, Chat::CHANNEL_INFO);
  30. message = "gamemode must be 0 or 1";
  31. Game::INSTANCE->zChat()->sendMessageTo(
  32. message, zActor, Chat::CHANNEL_INFO);
  33. return false;
  34. }
  35. p->setGameMode(mode);
  36. Framework::Text message = "gamemode for player ";
  37. message.append() << p->getName() << " is now set to " << mode;
  38. Game::INSTANCE->zChat()->sendMessageTo(
  39. message, zActor, Chat::CHANNEL_INFO);
  40. }
  41. else if (params.getEntryCount() == 2)
  42. {
  43. Player* p = Game::INSTANCE->zPlayerByName(*params.z(0));
  44. if (!p)
  45. {
  46. Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
  47. Game::INSTANCE->zChat()->sendMessageTo(
  48. message, zActor, Chat::CHANNEL_INFO);
  49. message = "";
  50. message.append() << "Player " << *params.z(0) << " not found";
  51. Game::INSTANCE->zChat()->sendMessageTo(
  52. message, zActor, Chat::CHANNEL_INFO);
  53. return false;
  54. }
  55. int mode = (int)*params.z(1);
  56. if (mode < 0 || mode > 1)
  57. {
  58. Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
  59. Game::INSTANCE->zChat()->sendMessageTo(
  60. message, zActor, Chat::CHANNEL_INFO);
  61. message = "gamemode must be 0 or 1";
  62. Game::INSTANCE->zChat()->sendMessageTo(
  63. message, zActor, Chat::CHANNEL_INFO);
  64. return false;
  65. }
  66. p->setGameMode(mode);
  67. Framework::Text message = "gamemode for player ";
  68. message.append() << p->getName() << " is now set to " << mode;
  69. Game::INSTANCE->zChat()->sendMessageTo(
  70. message, zActor, Chat::CHANNEL_INFO);
  71. }
  72. else
  73. {
  74. Framework::Text message = "Usage: gamemode [<playerName>] <0/1>";
  75. Game::INSTANCE->zChat()->sendMessageTo(
  76. message, zActor, Chat::CHANNEL_INFO);
  77. return false;
  78. }
  79. return true;
  80. }