Skip to content

Commit 06a5ece

Browse files
committedNov 2, 2013
Add basic protection support to builtin
1 parent 2636c92 commit 06a5ece

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
 

‎builtin/item.lua

+18
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
218218
place_to = {x = under.x, y = under.y, z = under.z}
219219
end
220220

221+
if minetest.is_protected(place_to, placer:get_player_name()) then
222+
minetest.log("action", placer:get_player_name()
223+
.. " tried to place " .. def.name
224+
.. " at protected position "
225+
.. minetest.pos_to_string(place_to))
226+
minetest.record_protection_violation(place_to, placer:get_player_name())
227+
return itemstack
228+
end
229+
221230
minetest.log("action", placer:get_player_name() .. " places node "
222231
.. def.name .. " at " .. minetest.pos_to_string(place_to))
223232

@@ -377,6 +386,15 @@ function minetest.node_dig(pos, node, digger)
377386
return
378387
end
379388

389+
if minetest.is_protected(pos, digger:get_player_name()) then
390+
minetest.log("action", digger:get_player_name()
391+
.. " tried to dig " .. node.name
392+
.. " at protected position "
393+
.. minetest.pos_to_string(pos))
394+
minetest.record_protection_violation(pos, digger:get_player_name())
395+
return
396+
end
397+
380398
minetest.log('action', digger:get_player_name() .. " digs "
381399
.. node.name .. " at " .. minetest.pos_to_string(pos))
382400

‎builtin/misc.lua

+11
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,14 @@ function minetest.setting_get_pos(name)
106106
return minetest.string_to_pos(value)
107107
end
108108

109+
-- To be overriden by protection mods
110+
function minetest.is_protected(pos, name)
111+
return false
112+
end
113+
114+
function minetest.record_protection_violation(pos, name)
115+
for _, func in pairs(minetest.registered_on_protection_violation) do
116+
func(pos, name)
117+
end
118+
end
119+

‎builtin/misc_register.lua

+1
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,5 @@ minetest.registered_on_player_receive_fields, minetest.register_on_player_receiv
344344
minetest.registered_on_cheats, minetest.register_on_cheat = make_registration()
345345
minetest.registered_on_crafts, minetest.register_on_craft = make_registration()
346346
minetest.registered_craft_predicts, minetest.register_craft_predict = make_registration()
347+
minetest.registered_on_protection_violation, minetest.register_on_protection_violation = make_registration()
347348

‎doc/lua_api.txt

+23
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,13 @@ minetest.register_on_craft(func(itemstack, player, old_craft_grid, craft_inv))
11541154
minetest.register_craft_predict(func(itemstack, player, old_craft_grid, craft_inv))
11551155
^ The same as before, except that it is called before the player crafts, to make
11561156
^ craft prediction, and it should not change anything.
1157+
minetest.register_on_protection_violation(func(pos, name))
1158+
^ Called by builtin and mods when a player violates protection at a position
1159+
(eg, digs a node or punches a protected entity).
1160+
^ The registered functions can be called using minetest.record_protection_violation
1161+
^ The provided function should check that the position is protected by the mod
1162+
calling this function before it prints a message, if it does, to allow for
1163+
multiple protection mods.
11571164

11581165
Other registration functions:
11591166
minetest.register_chatcommand(cmd, chatcommand definition)
@@ -1483,6 +1490,22 @@ minetest.deserialize(string) -> table
14831490
^ Example: deserialize('return { ["foo"] = "bar" }') -> {foo='bar'}
14841491
^ Example: deserialize('print("foo")') -> nil (function call fails)
14851492
^ error:[string "print("foo")"]:1: attempt to call global 'print' (a nil value)
1493+
minetest.is_protected(pos, name) -> bool
1494+
^ This function should be overriden by protection mods and should be used to
1495+
check if a player can interact at a position.
1496+
^ This function should call the old version of itself if the position is not
1497+
protected by the mod.
1498+
^ Example:
1499+
local old_is_protected = minetest.is_protected
1500+
function minetest.is_protected(pos, name)
1501+
if mymod:position_protected_from(pos, name) then
1502+
return true
1503+
end
1504+
return old_is_protected(pos, name)
1505+
end
1506+
minetest.record_protection_violation(pos, name)
1507+
^ This function calls functions registered with
1508+
minetest.register_on_protection_violation.
14861509

14871510
Global objects:
14881511
minetest.env - EnvRef of the server environment and world.

0 commit comments

Comments
 (0)
Please sign in to comment.