Skip to content

Commit dc5bc6c

Browse files
Dumbeldornerzhul
authored andcommittedApr 29, 2017
[CSM] Add event on_place_node API lua (#5548)
* [CSM] Add event on_place_node API lua
1 parent ecf0825 commit dc5bc6c

File tree

12 files changed

+88
-37
lines changed

12 files changed

+88
-37
lines changed
 

Diff for: ‎builtin/client/register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ core.registered_on_damage_taken, core.register_on_damage_taken = make_registrati
6969
core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
7070
core.registered_on_dignode, core.register_on_dignode = make_registration()
7171
core.registered_on_punchnode, core.register_on_punchnode = make_registration()
72+
core.registered_on_placenode, core.register_on_placenode = make_registration()

Diff for: ‎clientmods/preview/init.lua

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ core.register_on_connect(function()
1010
print("[PREVIEW] Player connection completed")
1111
end)
1212

13+
core.register_on_placenode(function(pointed_thing, node)
14+
print("The local player place a node!")
15+
print("pointed_thing :" .. dump(pointed_thing))
16+
print("node placed :" .. dump(node))
17+
return false
18+
end)
19+
1320
-- This is an example function to ensure it's working properly, should be removed before merge
1421
core.register_on_receiving_chat_messages(function(message)
1522
print("[PREVIEW] Received message " .. message)

Diff for: ‎doc/client_lua_api.md

+2
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ Call these functions only at load time!
667667
* Called when the local player punches a node
668668
* Newest functions are called first
669669
* If any function returns true, the punch is ignored
670+
* `minetest.register_on_placenode(function(pointed_thing, node))`
671+
* Called when a node has been placed
670672
### Sounds
671673
* `minetest.sound_play(spec, parameters)`: returns a handle
672674
* `spec` is a `SimpleSoundSpec`

Diff for: ‎src/game.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3730,6 +3730,9 @@ void Game::handlePointingAtNode(const PointedThing &pointed, const ItemDefinitio
37303730
// Read the sound
37313731
soundmaker->m_player_rightpunch_sound =
37323732
playeritem_def.sound_place;
3733+
3734+
if (client->moddingEnabled())
3735+
client->getScript()->on_placenode(pointed, playeritem_def);
37333736
} else {
37343737
soundmaker->m_player_rightpunch_sound =
37353738
SimpleSoundSpec();

Diff for: ‎src/script/common/c_content.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#include "porting.h"
3333
#include "mg_schematic.h"
3434
#include "noise.h"
35+
#include "util/pointedthing.h"
3536
#include <json/json.h>
3637

3738
struct EnumString es_TileAnimationType[] =
@@ -117,6 +118,16 @@ void read_item_definition(lua_State* L, int index,
117118
def.node_placement_prediction);
118119
}
119120

121+
/******************************************************************************/
122+
void push_item_definition(lua_State *L, const ItemDefinition &i)
123+
{
124+
lua_newtable(L);
125+
lua_pushstring(L, i.name.c_str());
126+
lua_setfield(L, -2, "name");
127+
lua_pushstring(L, i.description.c_str());
128+
lua_setfield(L, -2, "description");
129+
}
130+
120131
/******************************************************************************/
121132
void read_object_properties(lua_State *L, int index,
122133
ObjectProperties *prop, IItemDefManager *idef)
@@ -1427,3 +1438,36 @@ void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
14271438
}
14281439
lua_pop(L, 1); // Pop value
14291440
}
1441+
1442+
void push_pointed_thing(lua_State *L, const PointedThing &pointed)
1443+
{
1444+
lua_newtable(L);
1445+
if (pointed.type == POINTEDTHING_NODE) {
1446+
lua_pushstring(L, "node");
1447+
lua_setfield(L, -2, "type");
1448+
push_v3s16(L, pointed.node_undersurface);
1449+
lua_setfield(L, -2, "under");
1450+
push_v3s16(L, pointed.node_abovesurface);
1451+
lua_setfield(L, -2, "above");
1452+
} else if (pointed.type == POINTEDTHING_OBJECT) {
1453+
lua_pushstring(L, "object");
1454+
lua_setfield(L, -2, "type");
1455+
push_objectRef(L, pointed.object_id);
1456+
lua_setfield(L, -2, "ref");
1457+
} else {
1458+
lua_pushstring(L, "nothing");
1459+
lua_setfield(L, -2, "type");
1460+
}
1461+
}
1462+
1463+
void push_objectRef(lua_State *L, const u16 id)
1464+
{
1465+
// Get core.object_refs[i]
1466+
lua_getglobal(L, "core");
1467+
lua_getfield(L, -1, "object_refs");
1468+
luaL_checktype(L, -1, LUA_TTABLE);
1469+
lua_pushnumber(L, id);
1470+
lua_gettable(L, -2);
1471+
lua_remove(L, -2); // object_refs
1472+
lua_remove(L, -2); // core
1473+
}

Diff for: ‎src/script/common/c_content.h

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ void push_tool_capabilities (lua_State *L,
8888

8989
void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def,
9090
ItemDefinition &def);
91+
void push_item_definition (lua_State *L,
92+
const ItemDefinition &i);
9193
void read_object_properties (lua_State *L, int index,
9294
ObjectProperties *prop,
9395
IItemDefManager *idef);
@@ -162,6 +164,10 @@ bool push_json_value (lua_State *L,
162164
void read_json_value (lua_State *L, Json::Value &root,
163165
int index, u8 recursion = 0);
164166

167+
void push_pointed_thing (lua_State *L, const PointedThing &pointed);
168+
169+
void push_objectRef (lua_State *L, const u16 id);
170+
165171
extern struct EnumString es_TileAnimationType[];
166172

167173
#endif /* C_CONTENT_H_ */

Diff for: ‎src/script/cpp_api/s_base.cpp

+2-13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern "C" {
4242

4343
#include <stdio.h>
4444
#include <cstdarg>
45+
#include "script/common/c_content.h"
4546
#include <sstream>
4647

4748

@@ -320,22 +321,10 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
320321
if (cobj == NULL || cobj->getId() == 0) {
321322
ObjectRef::create(L, cobj);
322323
} else {
323-
objectrefGet(L, cobj->getId());
324+
push_objectRef(L, cobj->getId());
324325
}
325326
}
326327

327-
void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
328-
{
329-
// Get core.object_refs[i]
330-
lua_getglobal(L, "core");
331-
lua_getfield(L, -1, "object_refs");
332-
luaL_checktype(L, -1, LUA_TTABLE);
333-
lua_pushnumber(L, id);
334-
lua_gettable(L, -2);
335-
lua_remove(L, -2); // object_refs
336-
lua_remove(L, -2); // core
337-
}
338-
339328
Server* ScriptApiBase::getServer()
340329
{
341330
return dynamic_cast<Server *>(m_gamedef);

Diff for: ‎src/script/cpp_api/s_base.h

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ class ScriptApiBase {
115115
void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
116116

117117
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
118-
void objectrefGet(lua_State *L, u16 id);
119118

120119
RecursiveMutex m_luastackmutex;
121120
std::string m_last_run_mod;

Diff for: ‎src/script/cpp_api/s_client.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include "client.h"
2424
#include "common/c_converter.h"
2525
#include "common/c_content.h"
26+
#include "s_item.h"
2627

2728
void ScriptApiClient::on_shutdown()
2829
{
@@ -189,6 +190,23 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
189190
return blocked;
190191
}
191192

193+
bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
194+
{
195+
SCRIPTAPI_PRECHECKHEADER
196+
197+
// Get core.registered_on_placenode
198+
lua_getglobal(L, "core");
199+
lua_getfield(L, -1, "registered_on_placenode");
200+
201+
// Push data
202+
push_pointed_thing(L, pointed);
203+
push_item_definition(L, item);
204+
205+
// Call functions
206+
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
207+
return lua_toboolean(L, -1);
208+
}
209+
192210
void ScriptApiClient::setEnv(ClientEnvironment *env)
193211
{
194212
ScriptApiBase::setEnv(env);

Diff for: ‎src/script/cpp_api/s_client.h

+3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#ifndef S_CLIENT_H_
2222
#define S_CLIENT_H_
2323

24+
#include "util/pointedthing.h"
2425
#include "cpp_api/s_base.h"
2526
#include "mapnode.h"
27+
#include "itemdef.h"
2628
#include "util/string.h"
2729

2830
#ifdef _CRT_MSVCP_CURRENT
@@ -51,6 +53,7 @@ class ScriptApiClient : virtual public ScriptApiBase
5153

5254
bool on_dignode(v3s16 p, MapNode node);
5355
bool on_punchnode(v3s16 p, MapNode node);
56+
bool on_placenode(const PointedThing &pointed, const ItemDefinition &item);
5457

5558
void setEnv(ClientEnvironment *env);
5659
};

Diff for: ‎src/script/cpp_api/s_entity.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
5757

5858
// Add object reference
5959
// This should be userdata with metatable ObjectRef
60-
objectrefGet(L, id);
60+
push_objectRef(L, id);
6161
luaL_checktype(L, -1, LUA_TUSERDATA);
6262
if (!luaL_checkudata(L, -1, "ObjectRef"))
6363
luaL_typerror(L, -1, "ObjectRef");

Diff for: ‎src/script/cpp_api/s_item.cpp

+1-22
Original file line numberDiff line numberDiff line change
@@ -249,27 +249,6 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
249249
{
250250
lua_State* L = getStack();
251251

252-
lua_newtable(L);
253-
if(pointed.type == POINTEDTHING_NODE)
254-
{
255-
lua_pushstring(L, "node");
256-
lua_setfield(L, -2, "type");
257-
push_v3s16(L, pointed.node_undersurface);
258-
lua_setfield(L, -2, "under");
259-
push_v3s16(L, pointed.node_abovesurface);
260-
lua_setfield(L, -2, "above");
261-
}
262-
else if(pointed.type == POINTEDTHING_OBJECT)
263-
{
264-
lua_pushstring(L, "object");
265-
lua_setfield(L, -2, "type");
266-
objectrefGet(L, pointed.object_id);
267-
lua_setfield(L, -2, "ref");
268-
}
269-
else
270-
{
271-
lua_pushstring(L, "nothing");
272-
lua_setfield(L, -2, "type");
273-
}
252+
push_pointed_thing(L, pointed);
274253
}
275254

0 commit comments

Comments
 (0)
Please sign in to comment.