1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add wear bar color API (#13328)

---------

Co-authored-by: Muhammad Rifqi Priyo Susanto <muhammadrifqipriyosusanto@gmail.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
Co-authored-by: grorp <gregor.parzefall@posteo.de>
This commit is contained in:
techno-sam 2024-02-02 12:21:00 -08:00 committed by GitHub
parent e10d8080ba
commit 176e674a51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 598 additions and 26 deletions

View file

@ -376,6 +376,22 @@ int LuaItemStack::l_add_wear_by_uses(lua_State *L)
return 1;
}
// get_wear_bar_params(self) -> table
// Returns the effective wear bar parameters.
// Returns nil if this item has none associated.
int LuaItemStack::l_get_wear_bar_params(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkObject<LuaItemStack>(L, 1);
ItemStack &item = o->m_stack;
auto params = item.getWearBarParams(getGameDef(L)->idef());
if (params.has_value()) {
push_wear_bar_params(L, *params);
return 1;
}
return 0;
}
// add_item(self, itemstack or itemstring or table or nil) -> itemstack
// Returns leftover item stack
int LuaItemStack::l_add_item(lua_State *L)
@ -551,6 +567,7 @@ const luaL_Reg LuaItemStack::methods[] = {
luamethod(LuaItemStack, get_tool_capabilities),
luamethod(LuaItemStack, add_wear),
luamethod(LuaItemStack, add_wear_by_uses),
luamethod(LuaItemStack, get_wear_bar_params),
luamethod(LuaItemStack, add_item),
luamethod(LuaItemStack, item_fits),
luamethod(LuaItemStack, take_item),

View file

@ -125,6 +125,11 @@ private:
// Returns true if the item is (or was) a tool.
static int l_add_wear_by_uses(lua_State *L);
// get_wear_bar_params(self) -> table
// Returns the effective wear bar parameters.
// Returns nil if this item has none associated.
static int l_get_wear_bar_params(lua_State *L);
// add_item(self, itemstack or itemstring or table or nil) -> itemstack
// Returns leftover item stack
static int l_add_item(lua_State *L);

View file

@ -22,6 +22,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_itemstackmeta.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "common/c_converter.h"
#include "tool.h"
/*
ItemStackMetaRef
@ -58,6 +60,20 @@ int ItemStackMetaRef::l_set_tool_capabilities(lua_State *L)
return 0;
}
int ItemStackMetaRef::l_set_wear_bar_params(lua_State *L)
{
ItemStackMetaRef *metaref = checkObject<ItemStackMetaRef>(L, 1);
if (lua_isnoneornil(L, 2)) {
metaref->clearWearBarParams();
} else if (lua_istable(L, 2) || lua_isstring(L, 2)) {
metaref->setWearBarParams(read_wear_bar_params(L, 2));
} else {
luaL_typerror(L, 2, "table, ColorString, or nil");
}
return 0;
}
ItemStackMetaRef::ItemStackMetaRef(LuaItemStack *istack): istack(istack)
{
istack->grab();
@ -102,5 +118,6 @@ const luaL_Reg ItemStackMetaRef::methods[] = {
luamethod(MetaDataRef, from_table),
luamethod(MetaDataRef, equals),
luamethod(ItemStackMetaRef, set_tool_capabilities),
luamethod(ItemStackMetaRef, set_wear_bar_params),
{0,0}
};

View file

@ -49,8 +49,19 @@ private:
istack->getItem().metadata.clearToolCapabilities();
}
void setWearBarParams(const WearBarParams &params)
{
istack->getItem().metadata.setWearBarParams(params);
}
void clearWearBarParams()
{
istack->getItem().metadata.clearWearBarParams();
}
// Exported functions
static int l_set_tool_capabilities(lua_State *L);
static int l_set_wear_bar_params(lua_State *L);
public:
// takes a reference
ItemStackMetaRef(LuaItemStack *istack);