]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_item.h
LuaItemStack: Add __tostring metamethod (#8785)
[dragonfireclient.git] / src / script / lua_api / l_item.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "lua_api/l_base.h"
23 #include "inventory.h"  // ItemStack
24
25 class LuaItemStack : public ModApiBase {
26 private:
27         ItemStack m_stack;
28
29         static const char className[];
30         static const luaL_Reg methods[];
31
32         // Exported functions
33
34         // garbage collector
35         static int gc_object(lua_State *L);
36
37         // __tostring metamethod
38         static int mt_tostring(lua_State *L);
39
40         // is_empty(self) -> true/false
41         static int l_is_empty(lua_State *L);
42
43         // get_name(self) -> string
44         static int l_get_name(lua_State *L);
45
46         // set_name(self, name)
47         static int l_set_name(lua_State *L);
48
49         // get_count(self) -> number
50         static int l_get_count(lua_State *L);
51
52         // set_count(self, number)
53         static int l_set_count(lua_State *L);
54
55         // get_wear(self) -> number
56         static int l_get_wear(lua_State *L);
57
58         // set_wear(self, number)
59         static int l_set_wear(lua_State *L);
60
61         // get_meta(self) -> string
62         static int l_get_meta(lua_State *L);
63
64         // DEPRECATED
65         // get_metadata(self) -> string
66         static int l_get_metadata(lua_State *L);
67
68         // DEPRECATED
69         // set_metadata(self, string)
70         static int l_set_metadata(lua_State *L);
71
72         // get_description(self)
73         static int l_get_description(lua_State *L);
74
75         // clear(self) -> true
76         static int l_clear(lua_State *L);
77
78         // replace(self, itemstack or itemstring or table or nil) -> true
79         static int l_replace(lua_State *L);
80
81         // to_string(self) -> string
82         static int l_to_string(lua_State *L);
83
84         // to_table(self) -> table or nil
85         static int l_to_table(lua_State *L);
86
87         // get_stack_max(self) -> number
88         static int l_get_stack_max(lua_State *L);
89
90         // get_free_space(self) -> number
91         static int l_get_free_space(lua_State *L);
92
93         // is_known(self) -> true/false
94         // Checks if the item is defined.
95         static int l_is_known(lua_State *L);
96
97         // get_definition(self) -> table
98         // Returns the item definition table from core.registered_items,
99         // or a fallback one (name="unknown")
100         static int l_get_definition(lua_State *L);
101
102         // get_tool_capabilities(self) -> table
103         // Returns the effective tool digging properties.
104         // Returns those of the hand ("") if this item has none associated.
105         static int l_get_tool_capabilities(lua_State *L);
106
107         // add_wear(self, amount) -> true/false
108         // The range for "amount" is [0,65535]. Wear is only added if the item
109         // is a tool. Adding wear might destroy the item.
110         // Returns true if the item is (or was) a tool.
111         static int l_add_wear(lua_State *L);
112
113         // add_item(self, itemstack or itemstring or table or nil) -> itemstack
114         // Returns leftover item stack
115         static int l_add_item(lua_State *L);
116
117         // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
118         // First return value is true iff the new item fits fully into the stack
119         // Second return value is the would-be-left-over item stack
120         static int l_item_fits(lua_State *L);
121
122         // take_item(self, takecount=1) -> itemstack
123         static int l_take_item(lua_State *L);
124
125         // peek_item(self, peekcount=1) -> itemstack
126         static int l_peek_item(lua_State *L);
127
128 public:
129         LuaItemStack(const ItemStack &item);
130         ~LuaItemStack() = default;
131
132         const ItemStack& getItem() const;
133         ItemStack& getItem();
134
135         // LuaItemStack(itemstack or itemstring or table or nil)
136         // Creates an LuaItemStack and leaves it on top of stack
137         static int create_object(lua_State *L);
138         // Not callable from Lua
139         static int create(lua_State *L, const ItemStack &item);
140         static LuaItemStack* checkobject(lua_State *L, int narg);
141         static void Register(lua_State *L);
142
143 };
144
145 class ModApiItemMod : public ModApiBase {
146 private:
147         static int l_register_item_raw(lua_State *L);
148         static int l_unregister_item_raw(lua_State *L);
149         static int l_register_alias_raw(lua_State *L);
150         static int l_get_content_id(lua_State *L);
151         static int l_get_name_from_content_id(lua_State *L);
152 public:
153         static void Initialize(lua_State *L, int top);
154 };