core.register_on_punchnode(function(pos, node)
print("The local player punched a node!")
+ local itemstack = core.get_wielded_item()
+ --[[
+ -- getters
+ print(dump(itemstack:is_empty()))
+ print(dump(itemstack:get_name()))
+ print(dump(itemstack:get_count()))
+ print(dump(itemstack:get_wear()))
+ print(dump(itemstack:get_meta()))
+ print(dump(itemstack:get_metadata()))
+ print(dump(itemstack:is_known()))
+ --print(dump(itemstack:get_definition()))
+ print(dump(itemstack:get_tool_capabilities()))
+ print(dump(itemstack:to_string()))
+ print(dump(itemstack:to_table()))
+ -- setters
+ print(dump(itemstack:set_name("default:dirt")))
+ print(dump(itemstack:set_count("95")))
+ print(dump(itemstack:set_wear(934)))
+ print(dump(itemstack:get_meta()))
+ print(dump(itemstack:get_metadata()))
+ --]]
+ print(dump(itemstack:to_table()))
print("pos:" .. dump(pos))
print("node:" .. dump(node))
return false
* `minetest.after(time, func, ...)`
* Call the function `func` after `time` seconds, may be fractional
* Optional: Variable number of arguments that are passed to `func`
+
### Map
* `minetest.get_node(pos)`
* Returns the node at the given position as table in the format
for unloaded areas.
* `minetest.get_node_or_nil(pos)`
* Same as `get_node` but returns `nil` for unloaded areas.
+
+### Player
+* `minetest.get_wielded_item()`
+ * Returns the itemstack the local player is holding
+
### Misc.
* `minetest.parse_json(string[, nullvalue])`: returns something
* Convert a string containing JSON data into the Lua equivalent
#define GAMEDEF_HEADER
#include <string>
+#include <vector>
#include "irrlichttypes.h"
class IItemDefManager;
#include "lua_api/l_storage.h"
#include "lua_api/l_sound.h"
#include "lua_api/l_util.h"
+#include "lua_api/l_item.h"
ClientScripting::ClientScripting(Client *client):
ScriptApiBase()
ModApiClient::Initialize(L, top);
ModApiSound::Initialize(L, top);
ModApiStorage::Initialize(L, top);
+
+ LuaItemStack::Register(L);
}
#include "common/c_converter.h"
#include "common/c_types.h"
#include "nodedef.h"
-#include "itemdef.h"
#include "object_properties.h"
#include "cpp_api/s_node.h"
#include "lua_api/l_object.h"
}
/******************************************************************************/
-ItemStack read_item(lua_State* L, int index,Server* srv)
+ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
{
if(index < 0)
index = lua_gettop(L) + 1 + index;
{
// Convert from itemstring
std::string itemstring = lua_tostring(L, index);
- IItemDefManager *idef = srv->idef();
try
{
ItemStack item;
else if(lua_istable(L, index))
{
// Convert from table
- IItemDefManager *idef = srv->idef();
std::string name = getstringfield_default(L, index, "name", "");
int count = getintfield_default(L, index, "count", 1);
int wear = getintfield_default(L, index, "wear", 0);
if (items.size() < (u32) key) {
items.resize(key);
}
- items[key - 1] = read_item(L, -1, srv);
+ items[key - 1] = read_item(L, -1, srv->idef());
lua_pop(L, 1);
}
return items;
#include "irrlichttypes_bloated.h"
#include "util/string.h"
#include "itemgroup.h"
+#include "itemdef.h"
namespace Json { class Value; }
void push_hit_params (lua_State *L,
const HitParams ¶ms);
-ItemStack read_item (lua_State *L, int index, Server *srv);
+ItemStack read_item (lua_State *L, int index, IItemDefManager *idef);
struct TileAnimationParams read_animation_definition(lua_State *L, int index);
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L,-1, getServer());
+ item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L,-1, getServer());
+ item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if(!lua_isnil(L, -1)) {
try {
- item = read_item(L,-1, getServer());
+ item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer());
+ item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
PCALL_RES(lua_pcall(L, 4, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L,-1, getServer());
+ item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
PCALL_RES(lua_pcall(L, 4, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L,-1, getServer());
+ item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
return getScriptApiBase(L)->getClient();
}
#endif
+
+IGameDef *ModApiBase::getGameDef(lua_State *L)
+{
+ return getScriptApiBase(L)->getGameDef();
+}
+
Environment *ModApiBase::getEnv(lua_State *L)
{
return getScriptApiBase(L)->getEnv();
#include "common/c_types.h"
#include "common/c_internal.h"
+#include "gamedef.h"
extern "C" {
#include <lua.h>
static Client* getClient(lua_State *L);
#endif // !SERVER
+ static IGameDef* getGameDef(lua_State *L);
+
static Environment* getEnv(lua_State *L);
static GUIEngine* getGuiEngine(lua_State *L);
// When we are not loading the mod, this function returns "."
#include "gettext.h"
#include "common/c_converter.h"
#include "common/c_content.h"
+#include "lua_api/l_item.h"
int ModApiClient::l_get_current_modname(lua_State *L)
{
return 1;
}
+int ModApiClient::l_get_wielded_item(lua_State *L)
+{
+ Client *client = getClient(L);
+
+ Inventory local_inventory(client->idef());
+ client->getLocalInventory(local_inventory);
+
+ InventoryList *mlist = local_inventory.getList("main");
+
+ if (mlist && client->getPlayerItem() < mlist->getSize()) {
+ LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
+ } else {
+ LuaItemStack::create(L, ItemStack());
+ }
+ return 1;
+}
+
void ModApiClient::Initialize(lua_State *L, int top)
{
API_FCT(get_current_modname);
API_FCT(gettext);
API_FCT(get_node);
API_FCT(get_node_or_nil);
+ API_FCT(get_wielded_item);
}
// get_node_or_nil(pos)
static int l_get_node_or_nil(lua_State *L);
-
+ // get_wielded_item()
+ static int l_get_wielded_item(lua_State *L);
public:
static void Initialize(lua_State *L, int top);
// pos
//v3f pos = checkFloatPos(L, 1);
// item
- ItemStack item = read_item(L, 2,getServer(L));
+ ItemStack item = read_item(L, 2,getServer(L)->idef());
if(item.empty() || !item.isKnown(getServer(L)->idef()))
return 0;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3) - 1;
- ItemStack newitem = read_item(L, 4, getServer(L));
+ ItemStack newitem = read_item(L, 4, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
if(list != NULL && i >= 0 && i < (int) list->getSize()){
list->changeItem(i, newitem);
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3, getServer(L));
+ ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
if(list){
ItemStack leftover = list->addItem(item);
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3, getServer(L));
+ ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
if(list){
lua_pushboolean(L, list->roomForItem(item));
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3, getServer(L));
+ ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
if(list){
lua_pushboolean(L, list->containsItem(item));
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
- ItemStack item = read_item(L, 3, getServer(L));
+ ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
if(list){
ItemStack removed = list->removeItem(item);
{
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
- o->m_stack = read_item(L,2,getServer(L));
+ o->m_stack = read_item(L, 2, getGameDef(L)->idef());
lua_pushboolean(L, true);
return 1;
}
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- lua_pushinteger(L, item.getStackMax(getServer(L)->idef()));
+ lua_pushinteger(L, item.getStackMax(getGameDef(L)->idef()));
return 1;
}
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- lua_pushinteger(L, item.freeSpace(getServer(L)->idef()));
+ lua_pushinteger(L, item.freeSpace(getGameDef(L)->idef()));
return 1;
}
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- bool is_known = item.isKnown(getServer(L)->idef());
+ bool is_known = item.isKnown(getGameDef(L)->idef());
lua_pushboolean(L, is_known);
return 1;
}
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
const ToolCapabilities &prop =
- item.getToolCapabilities(getServer(L)->idef());
+ item.getToolCapabilities(getGameDef(L)->idef());
push_tool_capabilities(L, prop);
return 1;
}
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
int amount = lua_tointeger(L, 2);
- bool result = item.addWear(amount, getServer(L)->idef());
+ bool result = item.addWear(amount, getGameDef(L)->idef());
lua_pushboolean(L, result);
return 1;
}
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- ItemStack newitem = read_item(L,-1, getServer(L));
- ItemStack leftover = item.addItem(newitem, getServer(L)->idef());
+ ItemStack newitem = read_item(L, -1, getGameDef(L)->idef());
+ ItemStack leftover = item.addItem(newitem, getGameDef(L)->idef());
create(L, leftover);
return 1;
}
NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack;
- ItemStack newitem = read_item(L, 2, getServer(L));
+ ItemStack newitem = read_item(L, 2, getGameDef(L)->idef());
ItemStack restitem;
- bool fits = item.itemFits(newitem, &restitem, getServer(L)->idef());
+ bool fits = item.itemFits(newitem, &restitem, getGameDef(L)->idef());
lua_pushboolean(L, fits); // first return value
create(L, restitem); // second return value
return 2;
int LuaItemStack::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- ItemStack item = read_item(L, 1, getServer(L));
+ ItemStack item = read_item(L, 1, getGameDef(L)->idef());
LuaItemStack *o = new LuaItemStack(item);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
- INodeDefManager *ndef = getServer(L)->getNodeDefManager();
+ INodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
content_t c = ndef->getId(name);
lua_pushinteger(L, c);
NO_MAP_LOCK_REQUIRED;
content_t c = luaL_checkint(L, 1);
- INodeDefManager *ndef = getServer(L)->getNodeDefManager();
+ INodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
const char *name = ndef->get(c).name.c_str();
lua_pushstring(L, name);
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// Do it
- ItemStack item = read_item(L, 2, getServer(L));
+ ItemStack item = read_item(L, 2, getServer(L)->idef());
bool success = co->setWieldedItem(item);
if (success && co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
getServer(L)->SendInventory(((PlayerSAO*)co));