3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "lua_api/l_base.h"
23 #include "inventory.h" // ItemStack
25 class LuaItemStack : public ModApiBase {
29 static const char className[];
30 static const luaL_Reg methods[];
35 static int gc_object(lua_State *L);
37 // __tostring metamethod
38 static int mt_tostring(lua_State *L);
40 // is_empty(self) -> true/false
41 static int l_is_empty(lua_State *L);
43 // get_name(self) -> string
44 static int l_get_name(lua_State *L);
46 // set_name(self, name)
47 static int l_set_name(lua_State *L);
49 // get_count(self) -> number
50 static int l_get_count(lua_State *L);
52 // set_count(self, number)
53 static int l_set_count(lua_State *L);
55 // get_wear(self) -> number
56 static int l_get_wear(lua_State *L);
58 // set_wear(self, number)
59 static int l_set_wear(lua_State *L);
61 // get_meta(self) -> string
62 static int l_get_meta(lua_State *L);
65 // get_metadata(self) -> string
66 static int l_get_metadata(lua_State *L);
69 // set_metadata(self, string)
70 static int l_set_metadata(lua_State *L);
72 // get_description(self)
73 static int l_get_description(lua_State *L);
75 // get_short_description(self)
76 static int l_get_short_description(lua_State *L);
78 // clear(self) -> true
79 static int l_clear(lua_State *L);
81 // replace(self, itemstack or itemstring or table or nil) -> true
82 static int l_replace(lua_State *L);
84 // to_string(self) -> string
85 static int l_to_string(lua_State *L);
87 // to_table(self) -> table or nil
88 static int l_to_table(lua_State *L);
90 // get_stack_max(self) -> number
91 static int l_get_stack_max(lua_State *L);
93 // get_free_space(self) -> number
94 static int l_get_free_space(lua_State *L);
96 // is_known(self) -> true/false
97 // Checks if the item is defined.
98 static int l_is_known(lua_State *L);
100 // get_definition(self) -> table
101 // Returns the item definition table from core.registered_items,
102 // or a fallback one (name="unknown")
103 static int l_get_definition(lua_State *L);
105 // get_tool_capabilities(self) -> table
106 // Returns the effective tool digging properties.
107 // Returns those of the hand ("") if this item has none associated.
108 static int l_get_tool_capabilities(lua_State *L);
110 // add_wear(self, amount) -> true/false
111 // The range for "amount" is [0,65536]. Wear is only added if the item
112 // is a tool. Adding wear might destroy the item.
113 // Returns true if the item is (or was) a tool.
114 static int l_add_wear(lua_State *L);
116 // add_wear_by_uses(self, max_uses) -> true/false
117 // The range for "max_uses" is [0,65536].
118 // Adds wear to the item in such a way that, if
119 // only this function is called to add wear, the item
120 // will be destroyed exactly after `max_uses` times of calling it.
121 // No-op if `max_uses` is 0 or item is not a tool.
122 // Returns true if the item is (or was) a tool.
123 static int l_add_wear_by_uses(lua_State *L);
125 // add_item(self, itemstack or itemstring or table or nil) -> itemstack
126 // Returns leftover item stack
127 static int l_add_item(lua_State *L);
129 // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
130 // First return value is true iff the new item fits fully into the stack
131 // Second return value is the would-be-left-over item stack
132 static int l_item_fits(lua_State *L);
134 // take_item(self, takecount=1) -> itemstack
135 static int l_take_item(lua_State *L);
137 // peek_item(self, peekcount=1) -> itemstack
138 static int l_peek_item(lua_State *L);
141 LuaItemStack(const ItemStack &item);
142 ~LuaItemStack() = default;
144 const ItemStack& getItem() const;
145 ItemStack& getItem();
147 // LuaItemStack(itemstack or itemstring or table or nil)
148 // Creates an LuaItemStack and leaves it on top of stack
149 static int create_object(lua_State *L);
150 // Not callable from Lua
151 static int create(lua_State *L, const ItemStack &item);
152 static LuaItemStack* checkobject(lua_State *L, int narg);
154 static void *packIn(lua_State *L, int idx);
155 static void packOut(lua_State *L, void *ptr);
157 static void Register(lua_State *L);
160 class ModApiItemMod : public ModApiBase {
162 static int l_register_item_raw(lua_State *L);
163 static int l_unregister_item_raw(lua_State *L);
164 static int l_register_alias_raw(lua_State *L);
165 static int l_get_content_id(lua_State *L);
166 static int l_get_name_from_content_id(lua_State *L);
169 static void Initialize(lua_State *L, int top);
170 static void InitializeAsync(lua_State *L, int top);