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.
20 #include "lua_api/l_item.h"
21 #include "lua_api/l_itemstackmeta.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
28 #include "inventory.h"
33 int LuaItemStack::gc_object(lua_State *L)
35 LuaItemStack *o = *(LuaItemStack **)(lua_touserdata(L, 1));
40 // is_empty(self) -> true/false
41 int LuaItemStack::l_is_empty(lua_State *L)
44 LuaItemStack *o = checkobject(L, 1);
45 ItemStack &item = o->m_stack;
46 lua_pushboolean(L, item.empty());
50 // get_name(self) -> string
51 int LuaItemStack::l_get_name(lua_State *L)
54 LuaItemStack *o = checkobject(L, 1);
55 ItemStack &item = o->m_stack;
56 lua_pushstring(L, item.name.c_str());
60 // set_name(self, name)
61 int LuaItemStack::l_set_name(lua_State *L)
64 LuaItemStack *o = checkobject(L, 1);
65 ItemStack &item = o->m_stack;
68 item.name = luaL_checkstring(L, 2);
69 if (item.name.empty() || item.empty()) {
74 lua_pushboolean(L, status);
78 // get_count(self) -> number
79 int LuaItemStack::l_get_count(lua_State *L)
82 LuaItemStack *o = checkobject(L, 1);
83 ItemStack &item = o->m_stack;
84 lua_pushinteger(L, item.count);
88 // set_count(self, number)
89 int LuaItemStack::l_set_count(lua_State *L)
92 LuaItemStack *o = checkobject(L, 1);
93 ItemStack &item = o->m_stack;
96 lua_Integer count = luaL_checkinteger(L, 2);
97 if (count > 0 && count <= 65535) {
105 lua_pushboolean(L, status);
109 // get_wear(self) -> number
110 int LuaItemStack::l_get_wear(lua_State *L)
112 NO_MAP_LOCK_REQUIRED;
113 LuaItemStack *o = checkobject(L, 1);
114 ItemStack &item = o->m_stack;
115 lua_pushinteger(L, item.wear);
119 // set_wear(self, number)
120 int LuaItemStack::l_set_wear(lua_State *L)
122 NO_MAP_LOCK_REQUIRED;
123 LuaItemStack *o = checkobject(L, 1);
124 ItemStack &item = o->m_stack;
127 lua_Integer wear = luaL_checkinteger(L, 2);
136 lua_pushboolean(L, status);
140 // get_meta(self) -> string
141 int LuaItemStack::l_get_meta(lua_State *L)
143 NO_MAP_LOCK_REQUIRED;
144 LuaItemStack *o = checkobject(L, 1);
145 ItemStackMetaRef::create(L, &o->m_stack);
150 // get_metadata(self) -> string
151 int LuaItemStack::l_get_metadata(lua_State *L)
153 NO_MAP_LOCK_REQUIRED;
154 LuaItemStack *o = checkobject(L, 1);
155 ItemStack &item = o->m_stack;
156 const std::string &value = item.metadata.getString("");
157 lua_pushlstring(L, value.c_str(), value.size());
162 // set_metadata(self, string)
163 int LuaItemStack::l_set_metadata(lua_State *L)
165 NO_MAP_LOCK_REQUIRED;
166 LuaItemStack *o = checkobject(L, 1);
167 ItemStack &item = o->m_stack;
170 const char *ptr = luaL_checklstring(L, 2, &len);
171 item.metadata.setString("", std::string(ptr, len));
173 lua_pushboolean(L, true);
177 // get_description(self)
178 int LuaItemStack::l_get_description(lua_State *L)
180 NO_MAP_LOCK_REQUIRED;
181 LuaItemStack *o = checkobject(L, 1);
182 std::string desc = o->m_stack.getDescription(getGameDef(L)->idef());
183 lua_pushstring(L, desc.c_str());
187 // clear(self) -> true
188 int LuaItemStack::l_clear(lua_State *L)
190 NO_MAP_LOCK_REQUIRED;
191 LuaItemStack *o = checkobject(L, 1);
193 lua_pushboolean(L, true);
197 // replace(self, itemstack or itemstring or table or nil) -> true
198 int LuaItemStack::l_replace(lua_State *L)
200 NO_MAP_LOCK_REQUIRED;
201 LuaItemStack *o = checkobject(L, 1);
202 o->m_stack = read_item(L, 2, getGameDef(L)->idef());
203 lua_pushboolean(L, true);
207 // to_string(self) -> string
208 int LuaItemStack::l_to_string(lua_State *L)
210 NO_MAP_LOCK_REQUIRED;
211 LuaItemStack *o = checkobject(L, 1);
212 std::string itemstring = o->m_stack.getItemString();
213 lua_pushstring(L, itemstring.c_str());
217 // to_table(self) -> table or nil
218 int LuaItemStack::l_to_table(lua_State *L)
220 NO_MAP_LOCK_REQUIRED;
221 LuaItemStack *o = checkobject(L, 1);
222 const ItemStack &item = o->m_stack;
230 lua_pushstring(L, item.name.c_str());
231 lua_setfield(L, -2, "name");
232 lua_pushinteger(L, item.count);
233 lua_setfield(L, -2, "count");
234 lua_pushinteger(L, item.wear);
235 lua_setfield(L, -2, "wear");
237 const std::string &metadata_str = item.metadata.getString("");
238 lua_pushlstring(L, metadata_str.c_str(), metadata_str.size());
239 lua_setfield(L, -2, "metadata");
242 const StringMap &fields = item.metadata.getStrings();
243 for (const auto &field : fields) {
244 const std::string &name = field.first;
247 const std::string &value = field.second;
248 lua_pushlstring(L, name.c_str(), name.size());
249 lua_pushlstring(L, value.c_str(), value.size());
252 lua_setfield(L, -2, "meta");
257 // get_stack_max(self) -> number
258 int LuaItemStack::l_get_stack_max(lua_State *L)
260 NO_MAP_LOCK_REQUIRED;
261 LuaItemStack *o = checkobject(L, 1);
262 ItemStack &item = o->m_stack;
263 lua_pushinteger(L, item.getStackMax(getGameDef(L)->idef()));
267 // get_free_space(self) -> number
268 int LuaItemStack::l_get_free_space(lua_State *L)
270 NO_MAP_LOCK_REQUIRED;
271 LuaItemStack *o = checkobject(L, 1);
272 ItemStack &item = o->m_stack;
273 lua_pushinteger(L, item.freeSpace(getGameDef(L)->idef()));
277 // is_known(self) -> true/false
278 // Checks if the item is defined.
279 int LuaItemStack::l_is_known(lua_State *L)
281 NO_MAP_LOCK_REQUIRED;
282 LuaItemStack *o = checkobject(L, 1);
283 ItemStack &item = o->m_stack;
284 bool is_known = item.isKnown(getGameDef(L)->idef());
285 lua_pushboolean(L, is_known);
289 // get_definition(self) -> table
290 // Returns the item definition table from registered_items,
291 // or a fallback one (name="unknown")
292 int LuaItemStack::l_get_definition(lua_State *L)
294 NO_MAP_LOCK_REQUIRED;
295 LuaItemStack *o = checkobject(L, 1);
296 ItemStack &item = o->m_stack;
298 // Get registered_items[name]
299 lua_getglobal(L, "core");
300 lua_getfield(L, -1, "registered_items");
301 luaL_checktype(L, -1, LUA_TTABLE);
302 lua_getfield(L, -1, item.name.c_str());
306 lua_getfield(L, -1, "unknown");
311 // get_tool_capabilities(self) -> table
312 // Returns the effective tool digging properties.
313 // Returns those of the hand ("") if this item has none associated.
314 int LuaItemStack::l_get_tool_capabilities(lua_State *L)
316 NO_MAP_LOCK_REQUIRED;
317 LuaItemStack *o = checkobject(L, 1);
318 ItemStack &item = o->m_stack;
319 const ToolCapabilities &prop =
320 item.getToolCapabilities(getGameDef(L)->idef());
321 push_tool_capabilities(L, prop);
325 // add_wear(self, amount) -> true/false
326 // The range for "amount" is [0,65535]. Wear is only added if the item
327 // is a tool. Adding wear might destroy the item.
328 // Returns true if the item is (or was) a tool.
329 int LuaItemStack::l_add_wear(lua_State *L)
331 NO_MAP_LOCK_REQUIRED;
332 LuaItemStack *o = checkobject(L, 1);
333 ItemStack &item = o->m_stack;
334 int amount = lua_tointeger(L, 2);
335 bool result = item.addWear(amount, getGameDef(L)->idef());
336 lua_pushboolean(L, result);
340 // add_item(self, itemstack or itemstring or table or nil) -> itemstack
341 // Returns leftover item stack
342 int LuaItemStack::l_add_item(lua_State *L)
344 NO_MAP_LOCK_REQUIRED;
345 LuaItemStack *o = checkobject(L, 1);
346 ItemStack &item = o->m_stack;
347 ItemStack newitem = read_item(L, -1, getGameDef(L)->idef());
348 ItemStack leftover = item.addItem(newitem, getGameDef(L)->idef());
353 // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
354 // First return value is true iff the new item fits fully into the stack
355 // Second return value is the would-be-left-over item stack
356 int LuaItemStack::l_item_fits(lua_State *L)
358 NO_MAP_LOCK_REQUIRED;
359 LuaItemStack *o = checkobject(L, 1);
360 ItemStack &item = o->m_stack;
361 ItemStack newitem = read_item(L, 2, getGameDef(L)->idef());
363 bool fits = item.itemFits(newitem, &restitem, getGameDef(L)->idef());
364 lua_pushboolean(L, fits); // first return value
365 create(L, restitem); // second return value
369 // take_item(self, takecount=1) -> itemstack
370 int LuaItemStack::l_take_item(lua_State *L)
372 NO_MAP_LOCK_REQUIRED;
373 LuaItemStack *o = checkobject(L, 1);
374 ItemStack &item = o->m_stack;
376 if(!lua_isnone(L, 2))
377 takecount = luaL_checkinteger(L, 2);
378 ItemStack taken = item.takeItem(takecount);
383 // peek_item(self, peekcount=1) -> itemstack
384 int LuaItemStack::l_peek_item(lua_State *L)
386 NO_MAP_LOCK_REQUIRED;
387 LuaItemStack *o = checkobject(L, 1);
388 ItemStack &item = o->m_stack;
390 if(!lua_isnone(L, 2))
391 peekcount = lua_tointeger(L, 2);
392 ItemStack peekaboo = item.peekItem(peekcount);
397 LuaItemStack::LuaItemStack(const ItemStack &item):
402 const ItemStack& LuaItemStack::getItem() const
406 ItemStack& LuaItemStack::getItem()
411 // LuaItemStack(itemstack or itemstring or table or nil)
412 // Creates an LuaItemStack and leaves it on top of stack
413 int LuaItemStack::create_object(lua_State *L)
415 NO_MAP_LOCK_REQUIRED;
417 if (!lua_isnone(L, 1))
418 item = read_item(L, 1, getGameDef(L)->idef());
419 LuaItemStack *o = new LuaItemStack(item);
420 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
421 luaL_getmetatable(L, className);
422 lua_setmetatable(L, -2);
425 // Not callable from Lua
426 int LuaItemStack::create(lua_State *L, const ItemStack &item)
428 NO_MAP_LOCK_REQUIRED;
429 LuaItemStack *o = new LuaItemStack(item);
430 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
431 luaL_getmetatable(L, className);
432 lua_setmetatable(L, -2);
436 LuaItemStack* LuaItemStack::checkobject(lua_State *L, int narg)
438 luaL_checktype(L, narg, LUA_TUSERDATA);
439 void *ud = luaL_checkudata(L, narg, className);
440 if(!ud) luaL_typerror(L, narg, className);
441 return *(LuaItemStack**)ud; // unbox pointer
444 void LuaItemStack::Register(lua_State *L)
447 int methodtable = lua_gettop(L);
448 luaL_newmetatable(L, className);
449 int metatable = lua_gettop(L);
451 lua_pushliteral(L, "__metatable");
452 lua_pushvalue(L, methodtable);
453 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
455 lua_pushliteral(L, "__index");
456 lua_pushvalue(L, methodtable);
457 lua_settable(L, metatable);
459 lua_pushliteral(L, "__gc");
460 lua_pushcfunction(L, gc_object);
461 lua_settable(L, metatable);
463 lua_pop(L, 1); // drop metatable
465 luaL_openlib(L, 0, methods, 0); // fill methodtable
466 lua_pop(L, 1); // drop methodtable
468 // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil))
469 lua_register(L, className, create_object);
472 const char LuaItemStack::className[] = "ItemStack";
473 const luaL_Reg LuaItemStack::methods[] = {
474 luamethod(LuaItemStack, is_empty),
475 luamethod(LuaItemStack, get_name),
476 luamethod(LuaItemStack, set_name),
477 luamethod(LuaItemStack, get_count),
478 luamethod(LuaItemStack, set_count),
479 luamethod(LuaItemStack, get_wear),
480 luamethod(LuaItemStack, set_wear),
481 luamethod(LuaItemStack, get_meta),
482 luamethod(LuaItemStack, get_metadata),
483 luamethod(LuaItemStack, set_metadata),
484 luamethod(LuaItemStack, get_description),
485 luamethod(LuaItemStack, clear),
486 luamethod(LuaItemStack, replace),
487 luamethod(LuaItemStack, to_string),
488 luamethod(LuaItemStack, to_table),
489 luamethod(LuaItemStack, get_stack_max),
490 luamethod(LuaItemStack, get_free_space),
491 luamethod(LuaItemStack, is_known),
492 luamethod(LuaItemStack, get_definition),
493 luamethod(LuaItemStack, get_tool_capabilities),
494 luamethod(LuaItemStack, add_wear),
495 luamethod(LuaItemStack, add_item),
496 luamethod(LuaItemStack, item_fits),
497 luamethod(LuaItemStack, take_item),
498 luamethod(LuaItemStack, peek_item),
506 // register_item_raw({lots of stuff})
507 int ModApiItemMod::l_register_item_raw(lua_State *L)
509 NO_MAP_LOCK_REQUIRED;
510 luaL_checktype(L, 1, LUA_TTABLE);
513 // Get the writable item and node definition managers from the server
514 IWritableItemDefManager *idef =
515 getServer(L)->getWritableItemDefManager();
516 NodeDefManager *ndef =
517 getServer(L)->getWritableNodeDefManager();
519 // Check if name is defined
521 lua_getfield(L, table, "name");
522 if(lua_isstring(L, -1)){
523 name = readParam<std::string>(L, -1);
525 throw LuaError("register_item_raw: name is not defined or not a string");
528 // Check if on_use is defined
531 // Set a distinctive default value to check if this is set
532 def.node_placement_prediction = "__default";
534 // Read the item definition
535 read_item_definition(L, table, def, def);
537 // Default to having client-side placement prediction for nodes
538 // ("" in item definition sets it off)
539 if(def.node_placement_prediction == "__default"){
540 if(def.type == ITEM_NODE)
541 def.node_placement_prediction = name;
543 def.node_placement_prediction = "";
546 // Register item definition
547 idef->registerItem(def);
549 // Read the node definition (content features) and register it
550 if (def.type == ITEM_NODE) {
551 ContentFeatures f = read_content_features(L, table);
552 // when a mod reregisters ignore, only texture changes and such should
554 if (f.name == "ignore")
557 content_t id = ndef->set(f.name, f);
559 if (id > MAX_REGISTERED_CONTENT) {
560 throw LuaError("Number of registerable nodes ("
561 + itos(MAX_REGISTERED_CONTENT+1)
562 + ") exceeded (" + name + ")");
566 return 0; /* number of results */
569 // unregister_item(name)
570 int ModApiItemMod::l_unregister_item_raw(lua_State *L)
572 NO_MAP_LOCK_REQUIRED;
573 std::string name = luaL_checkstring(L, 1);
575 IWritableItemDefManager *idef =
576 getServer(L)->getWritableItemDefManager();
578 // Unregister the node
579 if (idef->get(name).type == ITEM_NODE) {
580 NodeDefManager *ndef =
581 getServer(L)->getWritableNodeDefManager();
582 ndef->removeNode(name);
585 idef->unregisterItem(name);
587 return 0; /* number of results */
590 // register_alias_raw(name, convert_to_name)
591 int ModApiItemMod::l_register_alias_raw(lua_State *L)
593 NO_MAP_LOCK_REQUIRED;
594 std::string name = luaL_checkstring(L, 1);
595 std::string convert_to = luaL_checkstring(L, 2);
597 // Get the writable item definition manager from the server
598 IWritableItemDefManager *idef =
599 getServer(L)->getWritableItemDefManager();
601 idef->registerAlias(name, convert_to);
603 return 0; /* number of results */
606 // get_content_id(name)
607 int ModApiItemMod::l_get_content_id(lua_State *L)
609 NO_MAP_LOCK_REQUIRED;
610 std::string name = luaL_checkstring(L, 1);
612 const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
613 content_t content_id;
614 if (!ndef->getId(name, content_id))
615 throw LuaError("Unknown node: " + name);
617 lua_pushinteger(L, content_id);
618 return 1; /* number of results */
621 // get_name_from_content_id(name)
622 int ModApiItemMod::l_get_name_from_content_id(lua_State *L)
624 NO_MAP_LOCK_REQUIRED;
625 content_t c = luaL_checkint(L, 1);
627 const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
628 const char *name = ndef->get(c).name.c_str();
630 lua_pushstring(L, name);
631 return 1; /* number of results */
634 void ModApiItemMod::Initialize(lua_State *L, int top)
636 API_FCT(register_item_raw);
637 API_FCT(unregister_item_raw);
638 API_FCT(register_alias_raw);
639 API_FCT(get_content_id);
640 API_FCT(get_name_from_content_id);