replace 'units' with 'blocks', 'items' for some reason

This commit is contained in:
MihailRis 2026-11-03 22:01:43 +03:00
parent 8268118735
commit ecc4c1794e
4 changed files with 39 additions and 16 deletions

View file

@ -328,21 +328,34 @@ static int l_delete_world(lua::State* L) {
return 0;
}
static ContentRemovalRequest parse_content_removal_request(lua::State* L, int idx) {
static ContentRemovalRequest parse_content_removal_request(
lua::State* L, int idx
) {
ContentRemovalRequest request;
if (!lua::istable(L, idx)) {
throw std::runtime_error("table expected as argument");
}
if (!lua::getfield(L, "units", idx)) {
throw std::runtime_error("table with 'units' field expected");
}
if (!lua::istable(L, -1)) {
throw std::runtime_error("'units' field must be a table");
}
int len = lua::objlen(L, -1);
for (int i = 0; i < len; i++) {
lua::rawgeti(L, i + 1);
request.units.emplace_back(lua::require_lstring(L, -1));
lua::pushnil(L);
while (lua::next(L, idx)) {
auto key = lua::require_lstring(L, -2);
int len = lua::objlen(L, -1);
for (int i = 0; i < len; i++) {
lua::rawgeti(L, i + 1);
auto value = lua::require_lstring(L, -1);
if (key == "blocks") {
request.blocks.emplace_back(value);
} else if (key == "items") {
request.items.emplace_back(value);
} else {
throw std::runtime_error(
"invalid key in content removal request: " +
std::string(key)
);
}
lua::pop(L);
}
lua::pop(L);
}
return request;

View file

@ -249,8 +249,10 @@ static void replace_all(
void WorldFiles::deleteContent(const ContentRemovalRequest& request) {
auto root = io::read_json(getIndicesFile());
for (const auto& unit : request.units) {
for (const auto& unit : request.blocks) {
replace_all(root["blocks"], unit, CORE_AIR);
}
for (const auto& unit : request.items) {
replace_all(root["items"], unit, CORE_EMPTY);
}
io::write_json(getIndicesFile(), root);

View file

@ -24,7 +24,8 @@ struct WorldInfo;
struct DebugSettings;
struct ContentRemovalRequest {
std::vector<std::string> units;
std::vector<std::string> blocks;
std::vector<std::string> items;
};
class WorldFiles {