update the parsing and fix chat response

This commit is contained in:
Aaron Kimbrell
2025-09-03 04:36:01 -05:00
parent d972b0de2f
commit a8cf629951

View File

@@ -559,23 +559,25 @@ namespace DEVGMCommands {
} }
} }
std::optional<float> ParseRelativeAxis(const float sourcePos, const std::string& toParse) { // Parse coordinates with support for relative positioning (~)
if (toParse.empty()) return std::nullopt; std::optional<float> ParseRelativeAxis(const float currentValue, const std::string& rawCoord) {
if (rawCoord.empty()) return std::nullopt;
// relative offset from current position std::string coord = rawCoord;
if (toParse[0] == '~') { // Remove any '+' characters to simplify parsing, since they don't affect the value
if (toParse.size() == 1) return sourcePos; coord.erase(std::remove(coord.begin(), coord.end(), '+'), coord.end());
if (coord[0] == '~') {
if (toParse.size() < 3 || !(toParse[1] != '+' || toParse[1] != '-')) return std::nullopt; if (coord.length() == 1) {
return currentValue;
const auto offset = GeneralUtils::TryParse<float>(toParse.substr(2)); } else {
if (!offset.has_value()) return std::nullopt; auto offsetOpt = GeneralUtils::TryParse<float>(coord.substr(1));
if (!offsetOpt) return std::nullopt;
bool isNegative = toParse[1] == '-'; return currentValue + offsetOpt.value();
return isNegative ? sourcePos - offset.value() : sourcePos + offset.value(); }
} else {
auto absoluteOpt = GeneralUtils::TryParse<float>(coord);
if (!absoluteOpt) return std::nullopt;
return absoluteOpt.value();
} }
return GeneralUtils::TryParse<float>(toParse);
} }
void Teleport(Entity* entity, const SystemAddress& sysAddr, const std::string args) { void Teleport(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
@@ -1667,15 +1669,17 @@ namespace DEVGMCommands {
void Execute(Entity* entity, const SystemAddress& sysAddr, const std::string args) { void Execute(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
if (args.empty()) { if (args.empty()) {
ChatPackets::SendSystemMessage(sysAddr, u"Usage: /execute <subcommand> ... run <command>"); ChatPackets::SendSystemMessage(sysAddr,
ChatPackets::SendSystemMessage(sysAddr, u"Subcommands:"); u"Usage: /execute <subcommand> ... run <command>\n"
ChatPackets::SendSystemMessage(sysAddr, u" as <playername> - Execute as different player"); u"Subcommands:\n"
ChatPackets::SendSystemMessage(sysAddr, u" at <playername> - Execute from player's position"); u" as <playername> - Execute as different player\n"
ChatPackets::SendSystemMessage(sysAddr, u" positioned <x> <y> <z> - Execute from coordinates (absolute or relative with ~)"); u" at <playername> - Execute from player's position\n"
ChatPackets::SendSystemMessage(sysAddr, u"Examples:"); u" positioned <x> <y> <z> - Execute from coordinates (absolute or relative with ~)\n"
ChatPackets::SendSystemMessage(sysAddr, u" /execute as Player1 run pos"); u"Examples:\n"
ChatPackets::SendSystemMessage(sysAddr, u" /execute at Player2 positioned 100 200 300 run spawn 1234"); u" /execute as Player1 run pos\n"
ChatPackets::SendSystemMessage(sysAddr, u" /execute positioned ~5 ~10 ~ run spawn 1234"); u" /execute at Player2 positioned 100 200 300 run spawn 1234\n"
u" /execute positioned ~5 ~10 ~ run spawn 1234"
);
return; return;
} }
@@ -1738,48 +1742,18 @@ namespace DEVGMCommands {
ChatPackets::SendSystemMessage(sysAddr, u"Error: 'positioned' requires x, y, z coordinates"); ChatPackets::SendSystemMessage(sysAddr, u"Error: 'positioned' requires x, y, z coordinates");
return; return;
} }
try { auto xOpt = ParseRelativeAxis(execPosition.x, splitArgs[i + 1]);
// Parse coordinates with support for relative positioning (~) auto yOpt = ParseRelativeAxis(execPosition.y, splitArgs[i + 2]);
auto parseCoordinate = [&](const std::string& coord, float currentValue) -> float { auto zOpt = ParseRelativeAxis(execPosition.z, splitArgs[i + 3]);
if (coord.empty()) {
throw std::invalid_argument("Empty coordinate"); if (!xOpt || !yOpt || !zOpt) {
}
if (coord[0] == '~') {
// Relative coordinate
if (coord.length() == 1) {
// Just "~" means current position (offset 0)
return currentValue;
} else {
// "~<offset>" means current position + offset
std::string offsetStr = coord.substr(1);
float offset = std::stof(offsetStr);
if (!std::isfinite(offset)) {
throw std::invalid_argument("Invalid offset");
}
return currentValue + offset;
}
} else {
// Absolute coordinate
float absolute = std::stof(coord);
if (!std::isfinite(absolute)) {
throw std::invalid_argument("Invalid absolute coordinate");
}
return absolute;
}
};
float x = parseCoordinate(splitArgs[i + 1], execPosition.x);
float y = parseCoordinate(splitArgs[i + 2], execPosition.y);
float z = parseCoordinate(splitArgs[i + 3], execPosition.z);
execPosition = NiPoint3(x, y, z);
positionOverridden = true;
} catch (const std::exception&) {
ChatPackets::SendSystemMessage(sysAddr, u"Error: Invalid coordinates for 'positioned'. Use numeric values or relative coordinates with ~."); ChatPackets::SendSystemMessage(sysAddr, u"Error: Invalid coordinates for 'positioned'. Use numeric values or relative coordinates with ~.");
return; return;
} }
execPosition = NiPoint3(xOpt.value(), yOpt.value(), zOpt.value());
positionOverridden = true;
i += 4; i += 4;