[Revision] Applying the recommended changes from pull request #110 (return current value if no argument given, moving the methods to their correct location)

This commit is contained in:
lonaasan 2024-09-09 11:27:14 +02:00
parent 98a395721e
commit 732fc2a9d7
4 changed files with 40 additions and 28 deletions

View File

@ -52,7 +52,7 @@ ClientCommandProcessor::ClientCommandProcessor(UniverseClientPtr universeClient,
{"enabletech", bind(&ClientCommandProcessor::enableTech, this, _1)},
{"upgradeship", bind(&ClientCommandProcessor::upgradeShip, this, _1)},
{"swap", bind(&ClientCommandProcessor::swap, this, _1)},
{"respawnInWorld", bind(&ClientCommandProcessor::respawnInWorld, this)}
{"respawnInWorld", bind(&ClientCommandProcessor::respawnInWorld, this, _1)}
};
}
@ -428,25 +428,37 @@ String ClientCommandProcessor::swap(String const& argumentsString) {
return "Failed to swap player";
}
String ClientCommandProcessor::respawnInWorld() {
String ClientCommandProcessor::respawnInWorld(String const& argumentsString) {
auto arguments = m_parser.tokenizeToStringList(argumentsString);
WorldClientPtr worldClient = m_universeClient->worldClient();
// Make sure we got the worldClient
if (!worldClient) {
return "Error: Unable to access world client.";
if (arguments.size() == 0) {
const std::string stringResult = worldClient->respawnInWorld() ? "true" : "false";
return "Respawn in this world is currently " + stringResult; // return the current state of the respawn value when no argument is given
}
if (arguments.size() > 1) {
return "Too many arguments for this command!"; // we dont wanna have too much, right?
}
if (worldClient->toggleRespawnInWorld()) {
// behold: probably one of the least efficient ways to convert a Star::String to a boolean
bool value;
if (arguments[0].toLower() == "true") {
value = true;
} else if(arguments[0].toLower() == "false") {
value = false;
}
else {
return "Invalid argument!"; // at least we get validation if it was not a boolean
}
bool result = worldClient->setRespawnInWorld(value);
// Convert boolean to string for the response
const std::string result = worldClient->respawnInWorld() ? "true" : "false";
const std::string stringResult = result ? "true" : "false";
return "Successfully switched respawn in this world to " + result;
}
else
return "Failed to switch respawn in this world!";
// This should never trigger, but its better to have it than not :3
return "Something unforseen happend!";
return "Successfully set respawn in this world to " + stringResult;
}
}

View File

@ -58,7 +58,7 @@ private:
String enableTech(String const& argumentsString);
String upgradeShip(String const& argumentsString);
String swap(String const& argumentsString);
String respawnInWorld();
String respawnInWorld(String const& argumentsString);
UniverseClientPtr m_universeClient;
CinematicPtr m_cinematicOverlay;

View File

@ -142,6 +142,16 @@ bool WorldClient::respawnInWorld() const {
return m_respawnInWorld;
}
bool WorldClient::setRespawnInWorld(bool value = NULL) {
if (value != NULL)
m_respawnInWorld = value;
else
m_respawnInWorld ^= true; // dont know if we still want to set the respawn if no argument is given here
return m_respawnInWorld;
}
void WorldClient::removeEntity(EntityId entityId, bool andDie) {
auto entity = m_entityMap->entity(entityId);
if (!entity)
@ -2421,16 +2431,4 @@ void WorldClient::setupForceRegions() {
}
}
bool WorldClient::toggleRespawnInWorld() {
// Setting oldValue to check if m_respawnInWorld triggered correctly later
const bool oldValue = respawnInWorld();
m_respawnInWorld ^= true;
if (respawnInWorld() != oldValue) {
return true;
}
return false;
}
}

View File

@ -109,6 +109,8 @@ public:
void reviveMainPlayer();
bool respawnInWorld() const;
bool setRespawnInWorld(bool value);
void removeEntity(EntityId entityId, bool andDie);
WorldTemplateConstPtr currentTemplate() const;
@ -176,7 +178,7 @@ public:
typedef std::function<bool(PlayerPtr, StringView)> BroadcastCallback;
BroadcastCallback& broadcastCallback();
bool toggleRespawnInWorld();
private:
static const float DropDist;