Skip to content

Commit

Permalink
Add get_wielded_item
Browse files Browse the repository at this point in the history
  • Loading branch information
red-001 authored and nerzhul committed Mar 13, 2017
1 parent 44ca9c9 commit 88df9fb
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 33 deletions.
22 changes: 22 additions & 0 deletions clientmods/preview/init.lua
Expand Up @@ -60,6 +60,28 @@ end)

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
Expand Down
6 changes: 6 additions & 0 deletions doc/client_lua_api.txt
Expand Up @@ -721,13 +721,19 @@ Call these functions only at load time!
* `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
`{name="node_name", param1=0, param2=0}`, returns `{name="ignore", param1=0, param2=0}`
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
Expand Down
1 change: 1 addition & 0 deletions src/gamedef.h
Expand Up @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define GAMEDEF_HEADER

#include <string>
#include <vector>
#include "irrlichttypes.h"

class IItemDefManager;
Expand Down
3 changes: 3 additions & 0 deletions src/script/clientscripting.cpp
Expand Up @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#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()
Expand Down Expand Up @@ -55,4 +56,6 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
ModApiClient::Initialize(L, top);
ModApiSound::Initialize(L, top);
ModApiStorage::Initialize(L, top);

LuaItemStack::Register(L);
}
7 changes: 2 additions & 5 deletions src/script/common/c_content.cpp
Expand Up @@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#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"
Expand Down Expand Up @@ -784,7 +783,7 @@ bool string_to_enum(const EnumString *spec, int &result,
}

/******************************************************************************/
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;
Expand All @@ -803,7 +802,6 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
{
// Convert from itemstring
std::string itemstring = lua_tostring(L, index);
IItemDefManager *idef = srv->idef();
try
{
ItemStack item;
Expand All @@ -820,7 +818,6 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
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);
Expand Down Expand Up @@ -1187,7 +1184,7 @@ std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
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;
Expand Down
3 changes: 2 additions & 1 deletion src/script/common/c_content.h
Expand Up @@ -38,6 +38,7 @@ extern "C" {
#include "irrlichttypes_bloated.h"
#include "util/string.h"
#include "itemgroup.h"
#include "itemdef.h"

namespace Json { class Value; }

Expand Down Expand Up @@ -77,7 +78,7 @@ void push_dig_params (lua_State *L,
void push_hit_params (lua_State *L,
const HitParams &params);

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);

Expand Down
12 changes: 6 additions & 6 deletions src/script/cpp_api/s_item.cpp
Expand Up @@ -47,7 +47,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
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);
}
Expand All @@ -74,7 +74,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
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);
}
Expand All @@ -101,7 +101,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
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);
}
Expand All @@ -127,7 +127,7 @@ bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *use
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);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
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);
}
Expand Down Expand Up @@ -191,7 +191,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/script/lua_api/l_base.cpp
Expand Up @@ -43,6 +43,12 @@ Client *ModApiBase::getClient(lua_State *L)
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();
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_base.h
Expand Up @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "common/c_types.h"
#include "common/c_internal.h"
#include "gamedef.h"

extern "C" {
#include <lua.h>
Expand All @@ -46,6 +47,8 @@ class ModApiBase {
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 "."
Expand Down
19 changes: 19 additions & 0 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#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)
{
Expand Down Expand Up @@ -132,6 +133,23 @@ int ModApiClient::l_get_node_or_nil(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);
Expand All @@ -143,4 +161,5 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(gettext);
API_FCT(get_node);
API_FCT(get_node_or_nil);
API_FCT(get_wielded_item);
}
3 changes: 2 additions & 1 deletion src/script/lua_api/l_client.h
Expand Up @@ -53,7 +53,8 @@ class ModApiClient: public ModApiBase
// 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);
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_env.cpp
Expand Up @@ -472,7 +472,7 @@ int ModApiEnvMod::l_add_item(lua_State *L)
// 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;

Expand Down
10 changes: 5 additions & 5 deletions src/script/lua_api/l_inventory.cpp
Expand Up @@ -194,7 +194,7 @@ int InvRef::l_set_stack(lua_State *L)
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);
Expand Down Expand Up @@ -295,7 +295,7 @@ int InvRef::l_add_item(lua_State *L)
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);
Expand All @@ -315,7 +315,7 @@ int InvRef::l_room_for_item(lua_State *L)
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));
Expand All @@ -332,7 +332,7 @@ int InvRef::l_contains_item(lua_State *L)
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));
Expand All @@ -349,7 +349,7 @@ int InvRef::l_remove_item(lua_State *L)
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);
Expand Down

0 comments on commit 88df9fb

Please sign in to comment.