From 1c4e13dc67aab2b58d9829dc1da98ee1ca5f9b5b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 3 Jul 2024 05:15:15 +0300 Subject: [PATCH] add block.get_textures --- res/content/base/scripts/components/drop.lua | 10 +++++++++- src/graphics/render/ModelBatch.cpp | 2 ++ src/logic/scripting/lua/libblock.cpp | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 97167877..b55ff80b 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -12,7 +12,15 @@ mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation) mat4.rotate(rotation, {0, 0, 1}, math.random() * 360, rotation) rig:set_matrix(0, rotation) local icon = item.icon(dropitem.id) -rig:set_texture("$0", icon:gsub("block%-previews", "blocks"):gsub("base%:", "")) +if icon:find("^block%-previews%:") then + local bid = block.index(icon:sub(16)) + local textures = block.get_textures(bid) + for i,t in ipairs(textures) do + rig:set_texture("$"..tostring(i-1), "blocks:"..textures[i]) + end +else + rig:set_texture("$0", icon) +end function on_grounded(force) rig:set_matrix(0, mat4.rotate({0, 1, 0}, math.random()*360)) diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 704e6be0..10e63d48 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -139,6 +139,8 @@ void ModelBatch::setTexture(const std::string& name, setTexture(atlas->getTexture()); if (auto reg = atlas->getIf(name.substr(sep+1))) { region = *reg; + } else { + setTexture("blocks:notfound", varTextures); } } } diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 4f2b3902..c9efe92e 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -259,6 +259,18 @@ static int l_caption(lua::State* L) { return 0; } +static int l_get_textures(lua::State* L) { + if (auto def = require_block(L)) { + lua::createtable(L, 6, 0); + for (size_t i = 0; i < 6; i++) { + lua::pushstring(L, def->textureFaces[i]); + lua::rawseti(L, i+1); + } + return 1; + } + return 0; +} + const luaL_Reg blocklib [] = { {"index", lua::wrap}, {"name", lua::wrap}, @@ -282,5 +294,6 @@ const luaL_Reg blocklib [] = { {"get_size", lua::wrap}, {"is_segment", lua::wrap}, {"seek_origin", lua::wrap}, + {"get_textures", lua::wrap}, {NULL, NULL} };