core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
core.registered_on_dignode, core.register_on_dignode = make_registration()
core.registered_on_punchnode, core.register_on_punchnode = make_registration()
+core.registered_on_placenode, core.register_on_placenode = make_registration()
print("[PREVIEW] Player connection completed")
end)
+core.register_on_placenode(function(pointed_thing, node)
+ print("The local player place a node!")
+ print("pointed_thing :" .. dump(pointed_thing))
+ print("node placed :" .. dump(node))
+ return false
+end)
+
-- This is an example function to ensure it's working properly, should be removed before merge
core.register_on_receiving_chat_messages(function(message)
print("[PREVIEW] Received message " .. message)
* Called when the local player punches a node
* Newest functions are called first
* If any function returns true, the punch is ignored
+* `minetest.register_on_placenode(function(pointed_thing, node))`
+ * Called when a node has been placed
### Sounds
* `minetest.sound_play(spec, parameters)`: returns a handle
* `spec` is a `SimpleSoundSpec`
// Read the sound
soundmaker->m_player_rightpunch_sound =
playeritem_def.sound_place;
+
+ if (client->moddingEnabled())
+ client->getScript()->on_placenode(pointed, playeritem_def);
} else {
soundmaker->m_player_rightpunch_sound =
SimpleSoundSpec();
#include "porting.h"
#include "mg_schematic.h"
#include "noise.h"
+#include "util/pointedthing.h"
#include <json/json.h>
struct EnumString es_TileAnimationType[] =
def.node_placement_prediction);
}
+/******************************************************************************/
+void push_item_definition(lua_State *L, const ItemDefinition &i)
+{
+ lua_newtable(L);
+ lua_pushstring(L, i.name.c_str());
+ lua_setfield(L, -2, "name");
+ lua_pushstring(L, i.description.c_str());
+ lua_setfield(L, -2, "description");
+}
+
/******************************************************************************/
void read_object_properties(lua_State *L, int index,
ObjectProperties *prop, IItemDefManager *idef)
}
lua_pop(L, 1); // Pop value
}
+
+void push_pointed_thing(lua_State *L, const PointedThing &pointed)
+{
+ lua_newtable(L);
+ if (pointed.type == POINTEDTHING_NODE) {
+ lua_pushstring(L, "node");
+ lua_setfield(L, -2, "type");
+ push_v3s16(L, pointed.node_undersurface);
+ lua_setfield(L, -2, "under");
+ push_v3s16(L, pointed.node_abovesurface);
+ lua_setfield(L, -2, "above");
+ } else if (pointed.type == POINTEDTHING_OBJECT) {
+ lua_pushstring(L, "object");
+ lua_setfield(L, -2, "type");
+ push_objectRef(L, pointed.object_id);
+ lua_setfield(L, -2, "ref");
+ } else {
+ lua_pushstring(L, "nothing");
+ lua_setfield(L, -2, "type");
+ }
+}
+
+void push_objectRef(lua_State *L, const u16 id)
+{
+ // Get core.object_refs[i]
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "object_refs");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_pushnumber(L, id);
+ lua_gettable(L, -2);
+ lua_remove(L, -2); // object_refs
+ lua_remove(L, -2); // core
+}
void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def,
ItemDefinition &def);
+void push_item_definition (lua_State *L,
+ const ItemDefinition &i);
void read_object_properties (lua_State *L, int index,
ObjectProperties *prop,
IItemDefManager *idef);
void read_json_value (lua_State *L, Json::Value &root,
int index, u8 recursion = 0);
+void push_pointed_thing (lua_State *L, const PointedThing &pointed);
+
+void push_objectRef (lua_State *L, const u16 id);
+
extern struct EnumString es_TileAnimationType[];
#endif /* C_CONTENT_H_ */
#include <stdio.h>
#include <cstdarg>
+#include "script/common/c_content.h"
#include <sstream>
if (cobj == NULL || cobj->getId() == 0) {
ObjectRef::create(L, cobj);
} else {
- objectrefGet(L, cobj->getId());
+ push_objectRef(L, cobj->getId());
}
}
-void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
-{
- // Get core.object_refs[i]
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "object_refs");
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_pushnumber(L, id);
- lua_gettable(L, -2);
- lua_remove(L, -2); // object_refs
- lua_remove(L, -2); // core
-}
-
Server* ScriptApiBase::getServer()
{
return dynamic_cast<Server *>(m_gamedef);
void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
- void objectrefGet(lua_State *L, u16 id);
RecursiveMutex m_luastackmutex;
std::string m_last_run_mod;
#include "client.h"
#include "common/c_converter.h"
#include "common/c_content.h"
+#include "s_item.h"
void ScriptApiClient::on_shutdown()
{
return blocked;
}
+bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ // Get core.registered_on_placenode
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_placenode");
+
+ // Push data
+ push_pointed_thing(L, pointed);
+ push_item_definition(L, item);
+
+ // Call functions
+ runCallbacks(2, RUN_CALLBACKS_MODE_OR);
+ return lua_toboolean(L, -1);
+}
+
void ScriptApiClient::setEnv(ClientEnvironment *env)
{
ScriptApiBase::setEnv(env);
#ifndef S_CLIENT_H_
#define S_CLIENT_H_
+#include "util/pointedthing.h"
#include "cpp_api/s_base.h"
#include "mapnode.h"
+#include "itemdef.h"
#include "util/string.h"
#ifdef _CRT_MSVCP_CURRENT
bool on_dignode(v3s16 p, MapNode node);
bool on_punchnode(v3s16 p, MapNode node);
+ bool on_placenode(const PointedThing &pointed, const ItemDefinition &item);
void setEnv(ClientEnvironment *env);
};
// Add object reference
// This should be userdata with metatable ObjectRef
- objectrefGet(L, id);
+ push_objectRef(L, id);
luaL_checktype(L, -1, LUA_TUSERDATA);
if (!luaL_checkudata(L, -1, "ObjectRef"))
luaL_typerror(L, -1, "ObjectRef");
{
lua_State* L = getStack();
- lua_newtable(L);
- if(pointed.type == POINTEDTHING_NODE)
- {
- lua_pushstring(L, "node");
- lua_setfield(L, -2, "type");
- push_v3s16(L, pointed.node_undersurface);
- lua_setfield(L, -2, "under");
- push_v3s16(L, pointed.node_abovesurface);
- lua_setfield(L, -2, "above");
- }
- else if(pointed.type == POINTEDTHING_OBJECT)
- {
- lua_pushstring(L, "object");
- lua_setfield(L, -2, "type");
- objectrefGet(L, pointed.object_id);
- lua_setfield(L, -2, "ref");
- }
- else
- {
- lua_pushstring(L, "nothing");
- lua_setfield(L, -2, "type");
- }
+ push_pointed_thing(L, pointed);
}