Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
l_item.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 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#include "util/pointer.h"
25
27private:
29
30 LuaItemStack(const ItemStack &item);
31 ~LuaItemStack() = default;
32
33 static const luaL_Reg methods[];
34
35 // Exported functions
36
37 // garbage collector
38 static int gc_object(lua_State *L);
39
40 // __tostring metamethod
41 static int mt_tostring(lua_State *L);
42
43 // is_empty(self) -> true/false
44 static int l_is_empty(lua_State *L);
45
46 // get_name(self) -> string
47 static int l_get_name(lua_State *L);
48
49 // set_name(self, name)
50 static int l_set_name(lua_State *L);
51
52 // get_count(self) -> number
53 static int l_get_count(lua_State *L);
54
55 // set_count(self, number)
56 static int l_set_count(lua_State *L);
57
58 // get_wear(self) -> number
59 static int l_get_wear(lua_State *L);
60
61 // set_wear(self, number)
62 static int l_set_wear(lua_State *L);
63
64 // get_meta(self) -> string
65 static int l_get_meta(lua_State *L);
66
67 // DEPRECATED
68 // get_metadata(self) -> string
69 static int l_get_metadata(lua_State *L);
70
71 // DEPRECATED
72 // set_metadata(self, string)
73 static int l_set_metadata(lua_State *L);
74
75 // get_description(self)
76 static int l_get_description(lua_State *L);
77
78 // get_short_description(self)
79 static int l_get_short_description(lua_State *L);
80
81 // clear(self) -> true
82 static int l_clear(lua_State *L);
83
84 // replace(self, itemstack or itemstring or table or nil) -> true
85 static int l_replace(lua_State *L);
86
87 // to_string(self) -> string
88 static int l_to_string(lua_State *L);
89
90 // to_table(self) -> table or nil
91 static int l_to_table(lua_State *L);
92
93 // get_stack_max(self) -> number
94 static int l_get_stack_max(lua_State *L);
95
96 // get_free_space(self) -> number
97 static int l_get_free_space(lua_State *L);
98
99 // is_known(self) -> true/false
100 // Checks if the item is defined.
101 static int l_is_known(lua_State *L);
102
103 // get_definition(self) -> table
104 // Returns the item definition table from core.registered_items,
105 // or a fallback one (name="unknown")
106 static int l_get_definition(lua_State *L);
107
108 // get_tool_capabilities(self) -> table
109 // Returns the effective tool digging properties.
110 // Returns those of the hand ("") if this item has none associated.
111 static int l_get_tool_capabilities(lua_State *L);
112
113 // add_wear(self, amount) -> true/false
114 // The range for "amount" is [0,65536]. Wear is only added if the item
115 // is a tool. Adding wear might destroy the item.
116 // Returns true if the item is (or was) a tool.
117 static int l_add_wear(lua_State *L);
118
119 // add_wear_by_uses(self, max_uses) -> true/false
120 // The range for "max_uses" is [0,65536].
121 // Adds wear to the item in such a way that, if
122 // only this function is called to add wear, the item
123 // will be destroyed exactly after `max_uses` times of calling it.
124 // No-op if `max_uses` is 0 or item is not a tool.
125 // Returns true if the item is (or was) a tool.
126 static int l_add_wear_by_uses(lua_State *L);
127
128 // get_wear_bar_params(self) -> table
129 // Returns the effective wear bar parameters.
130 // Returns nil if this item has none associated.
131 static int l_get_wear_bar_params(lua_State *L);
132
133 // add_item(self, itemstack or itemstring or table or nil) -> itemstack
134 // Returns leftover item stack
135 static int l_add_item(lua_State *L);
136
137 // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
138 // First return value is true iff the new item fits fully into the stack
139 // Second return value is the would-be-left-over item stack
140 static int l_item_fits(lua_State *L);
141
142 // take_item(self, takecount=1) -> itemstack
143 static int l_take_item(lua_State *L);
144
145 // peek_item(self, peekcount=1) -> itemstack
146 static int l_peek_item(lua_State *L);
147
148 // equals(self, other) -> bool
149 static int l_equals(lua_State *L);
150
151public:
153
154 inline const ItemStack& getItem() const { return m_stack; }
155 inline ItemStack& getItem() { return m_stack; }
156
157 // LuaItemStack(itemstack or itemstring or table or nil)
158 // Creates an LuaItemStack and leaves it on top of stack
159 static int create_object(lua_State *L);
160 // Not callable from Lua
161 static int create(lua_State *L, const ItemStack &item);
162
163 static void *packIn(lua_State *L, int idx);
164 static void packOut(lua_State *L, void *ptr);
165
166 static void Register(lua_State *L);
167
168 static const char className[];
169};
170
171class ModApiItem : public ModApiBase {
172private:
173 static int l_register_item_raw(lua_State *L);
174 static int l_unregister_item_raw(lua_State *L);
175 static int l_register_alias_raw(lua_State *L);
176 static int l_get_content_id(lua_State *L);
177 static int l_get_name_from_content_id(lua_State *L);
178
179public:
180 static void Initialize(lua_State *L, int top);
181 static void InitializeAsync(lua_State *L, int top);
182 static void InitializeClient(lua_State *L, int top);
183};
#define DISABLE_CLASS_COPY(C)
Definition: basic_macros.h:35
Definition: pointer.h:269
Definition: l_item.h:26
static int l_set_wear(lua_State *L)
Definition: l_item.cpp:130
static const char className[]
Definition: l_item.h:168
~LuaItemStack()=default
const ItemStack & getItem() const
Definition: l_item.h:154
static int l_add_wear(lua_State *L)
Definition: l_item.cpp:354
static int l_take_item(lua_State *L)
Definition: l_item.cpp:430
static int l_get_free_space(lua_State *L)
Definition: l_item.cpp:293
static int gc_object(lua_State *L)
Definition: l_item.cpp:34
static int l_add_item(lua_State *L)
Definition: l_item.cpp:402
ItemStack m_stack
Definition: l_item.h:28
static int l_equals(lua_State *L)
Definition: l_item.cpp:458
static int create(lua_State *L, const ItemStack &item)
Definition: l_item.cpp:510
static int l_set_name(lua_State *L)
Definition: l_item.cpp:71
static int l_set_count(lua_State *L)
Definition: l_item.cpp:99
static int l_item_fits(lua_State *L)
Definition: l_item.cpp:416
static int l_get_tool_capabilities(lua_State *L)
Definition: l_item.cpp:339
static int l_add_wear_by_uses(lua_State *L)
Definition: l_item.cpp:372
static int l_to_string(lua_State *L)
Definition: l_item.cpp:233
static int l_get_short_description(lua_State *L)
Definition: l_item.cpp:203
ItemStack & getItem()
Definition: l_item.h:155
static void packOut(lua_State *L, void *ptr)
Definition: l_item.cpp:526
static int create_object(lua_State *L)
Definition: l_item.cpp:496
static int l_set_metadata(lua_State *L)
Definition: l_item.cpp:176
static int mt_tostring(lua_State *L)
Definition: l_item.cpp:42
static int l_get_name(lua_State *L)
Definition: l_item.cpp:61
static int l_clear(lua_State *L)
Definition: l_item.cpp:213
static int l_is_empty(lua_State *L)
Definition: l_item.cpp:51
static int l_replace(lua_State *L)
Definition: l_item.cpp:223
static int l_get_wear(lua_State *L)
Definition: l_item.cpp:120
static int l_get_count(lua_State *L)
Definition: l_item.cpp:89
static const luaL_Reg methods[]
Definition: l_item.h:33
static int l_get_definition(lua_State *L)
Definition: l_item.cpp:317
static int l_get_stack_max(lua_State *L)
Definition: l_item.cpp:283
static int l_get_description(lua_State *L)
Definition: l_item.cpp:193
static int l_to_table(lua_State *L)
Definition: l_item.cpp:243
static void * packIn(lua_State *L, int idx)
Definition: l_item.cpp:520
static int l_get_wear_bar_params(lua_State *L)
Definition: l_item.cpp:387
static int l_get_metadata(lua_State *L)
Definition: l_item.cpp:161
static int l_get_meta(lua_State *L)
Definition: l_item.cpp:151
static int l_is_known(lua_State *L)
Definition: l_item.cpp:304
static int l_peek_item(lua_State *L)
Definition: l_item.cpp:444
Definition: l_base.h:43
Definition: l_item.h:171
static int l_register_alias_raw(lua_State *L)
Definition: l_item.cpp:677
static void InitializeAsync(lua_State *L, int top)
Definition: l_item.cpp:740
static int l_unregister_item_raw(lua_State *L)
Definition: l_item.cpp:656
static int l_register_item_raw(lua_State *L)
Definition: l_item.cpp:589
static void InitializeClient(lua_State *L, int top)
Definition: l_item.cpp:747
static int l_get_content_id(lua_State *L)
Definition: l_item.cpp:693
static void Initialize(lua_State *L, int top)
Definition: l_item.cpp:731
static int l_get_name_from_content_id(lua_State *L)
Definition: l_item.cpp:719
#define idx(x, y)
Definition: noise.cpp:552
Definition: inventory.h:34