Skip to content

Commit

Permalink
cache block.index, block.name, item.index, item.name
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 12, 2024
1 parent 913f942 commit 086c20c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 22 additions & 1 deletion res/scripts/post_content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local names = {
"hidden", "draw-group", "picking-item", "surface-replacement", "script-name",
"ui-layout", "inventory-size", "tick-interval", "overlay-texture",
"translucent", "fields", "particles", "icon-type", "icon", "placing-block",
"stack-size"
"stack-size", "name"
}
for name, _ in pairs(user_props) do
table.insert(names, name)
Expand Down Expand Up @@ -40,3 +40,24 @@ make_read_only(block.properties)
for k,v in pairs(block.properties) do
make_read_only(v)
end

local function cache_names(library)
local indices = {}
local names = {}
for id=0,library.defs_count()-1 do
local name = library.properties[id].name
indices[name] = id
names[id] = name
end

function library.name(id)
return names[id]
end

function library.index(name)
return indices[name]
end
end

cache_names(block)
cache_names(item)
8 changes: 8 additions & 0 deletions src/content/ContentBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,18 @@ std::unique_ptr<Content> ContentBuilder::build() {
for (Block* def : blockDefsIndices) {
def->rt.pickingItem = content->items.require(def->pickingItem).rt.id;
def->rt.surfaceReplacement = content->blocks.require(def->surfaceReplacement).rt.id;
if (def->properties == nullptr) {
def->properties = dv::object();
}
def->properties["name"] = def->name;
}

for (ItemDef* def : itemDefsIndices) {
def->rt.placingBlock = content->blocks.require(def->placingBlock).rt.id;
if (def->properties == nullptr) {
def->properties = dv::object();
}
def->properties["name"] = def->name;
}

for (auto& [name, def] : content->generators.getDefs()) {
Expand Down

0 comments on commit 086c20c

Please sign in to comment.