Skip to content

Commit 320a05f

Browse files
authored
Apply area protections on storage nodes (#108)
* Apply area protections on storage nodes * Fix player undefined * Reuse codes, and add code for duplicator inventory * Add code for controller and builder * Fix luacheck
1 parent 9e6c2db commit 320a05f

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

nodes/node_battery_holder.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ local def = {
6565
end,
6666

6767
-- Allow all items with energy storage to be placed in the inventory
68-
allow_metadata_inventory_put = function(_, listname, _, stack)
68+
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
6969
if listname == "batteries" then
7070
if not minetest.global_exists("technic") then
7171
return 0
@@ -80,18 +80,24 @@ local def = {
8080
-- And specifically if they hold any charge
8181
-- Disregard empty batteries, the player should know better
8282
if md and md.charge > 0 then
83+
if digtron.check_protected_and_record(pos, player) then
84+
return 0
85+
end
8386
return stack:get_count()
8487
else
8588
return 0
8689
end
87-
8890
else
8991
return 0
9092
end
9193
end
9294
return 0
9395
end,
9496

97+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
98+
99+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
100+
95101

96102
can_dig = function(pos)
97103
local meta = minetest.get_meta(pos)

nodes/node_builders.lua

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@ minetest.register_node("digtron:builder", {
259259
digtron.update_builder_item(pos)
260260
end,
261261

262-
allow_metadata_inventory_put = function(pos, listname, index, stack)
262+
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
263+
if digtron.check_protected_and_record(pos, player) then
264+
return 0
265+
end
266+
263267
local stack_name = stack:get_name()
264268

265269
if minetest.get_item_group(stack_name, "digtron") ~= 0 then
@@ -284,10 +288,12 @@ minetest.register_node("digtron:builder", {
284288
return 0
285289
end,
286290

287-
allow_metadata_inventory_take = function(pos, listname, index)
288-
node_inventory_table.pos = pos
289-
local inv = minetest.get_inventory(node_inventory_table)
290-
inv:set_stack(listname, index, ItemStack(""))
291+
allow_metadata_inventory_take = function(pos, listname, index, _, player)
292+
if not digtron.check_protected_and_record(pos, player) then
293+
node_inventory_table.pos = pos
294+
local inv = minetest.get_inventory(node_inventory_table)
295+
inv:set_stack(listname, index, ItemStack(""))
296+
end
291297
return 0
292298
end,
293299

nodes/node_controllers.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ minetest.register_node("digtron:auto_controller", {
237237
inv:set_size("stop", 1)
238238
end,
239239

240-
allow_metadata_inventory_put = function(pos, listname, index, stack)
241-
if minetest.get_item_group(stack:get_name(), "digtron") ~= 0 then
240+
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
241+
if digtron.check_protected_and_record(pos, player)
242+
or minetest.get_item_group(stack:get_name(), "digtron") ~= 0 then
242243
return 0 -- pointless setting a Digtron node as a stop block
243244
end
244245
node_inventory_table.pos = pos
@@ -247,10 +248,12 @@ minetest.register_node("digtron:auto_controller", {
247248
return 0
248249
end,
249250

250-
allow_metadata_inventory_take = function(pos, listname, index)
251-
node_inventory_table.pos = pos
252-
local inv = minetest.get_inventory(node_inventory_table)
253-
inv:set_stack(listname, index, ItemStack(""))
251+
allow_metadata_inventory_take = function(pos, listname, index, _, player)
252+
if not digtron.check_protected_and_record(pos, player) then
253+
node_inventory_table.pos = pos
254+
local inv = minetest.get_inventory(node_inventory_table)
255+
inv:set_stack(listname, index, ItemStack(""))
256+
end
254257
return 0
255258
end,
256259

nodes/node_duplicator.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,22 @@ minetest.register_node("digtron:duplicator", {
7676
return inv:is_empty("main")
7777
end,
7878

79-
allow_metadata_inventory_put = function(_, _, _, stack)
79+
allow_metadata_inventory_put = function(pos, _, _, stack, player)
80+
if digtron.check_protected_and_record(pos, player) then
81+
return 0
82+
end
83+
8084
if minetest.get_item_group(stack:get_name(), "digtron") > 0 then
8185
return stack:get_count()
8286
else
8387
return 0
8488
end
8589
end,
8690

91+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
92+
93+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
94+
8795
on_receive_fields = function(pos, _, fields, sender)
8896
local player_name = sender:get_player_name()
8997
if fields.help then

nodes/node_storage.lua

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ minetest.register_node("digtron:inventory", set_logger({
7474
return inv:is_empty("main")
7575
end,
7676

77+
allow_metadata_inventory_put = digtron.protected_allow_metadata_inventory_put,
78+
79+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
80+
81+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
82+
7783
-- Pipeworks compatibility
7884
----------------------------------------------------------------
7985

@@ -150,17 +156,21 @@ minetest.register_node("digtron:fuelstore", set_logger({
150156
end,
151157

152158
-- Only allow fuel items to be placed in fuel
153-
allow_metadata_inventory_put = function(_, listname, _, stack)
154-
if listname == "fuel" then
155-
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
156-
return stack:get_count()
157-
else
158-
return 0
159-
end
159+
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
160+
if digtron.check_protected_and_record(pos, player) then
161+
return 0
162+
end
163+
164+
if listname == "fuel" and minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
165+
return stack:get_count()
160166
end
161167
return 0
162168
end,
163169

170+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
171+
172+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
173+
164174
can_dig = function(pos)
165175
local meta = minetest.get_meta(pos)
166176
local inv = meta:get_inventory()
@@ -252,7 +262,11 @@ minetest.register_node("digtron:combined_storage", set_logger({
252262
end,
253263

254264
-- Only allow fuel items to be placed in fuel
255-
allow_metadata_inventory_put = function(_, listname, _, stack)
265+
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
266+
if digtron.check_protected_and_record(pos, player) then
267+
return 0
268+
end
269+
256270
if listname == "fuel" then
257271
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
258272
return stack:get_count()
@@ -263,7 +277,11 @@ minetest.register_node("digtron:combined_storage", set_logger({
263277
return stack:get_count() -- otherwise, allow all drops
264278
end,
265279

266-
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, _, count)
280+
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, _, count, player)
281+
if digtron.check_protected_and_record(pos, player) then
282+
return 0
283+
end
284+
267285
if to_list == "main" then
268286
return count
269287
end
@@ -277,6 +295,8 @@ minetest.register_node("digtron:combined_storage", set_logger({
277295
return 0
278296
end,
279297

298+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
299+
280300
can_dig = function(pos)
281301
local meta = minetest.get_meta(pos)
282302
local inv = meta:get_inventory()

util.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,24 @@ digtron.show_offset_markers = function(pos, offset, period)
431431
if entity ~= nil then entity:set_yaw(1.5708) end
432432
end
433433
end
434+
435+
digtron.check_protected_and_record = function(pos, player)
436+
local name = player:get_player_name()
437+
if minetest.is_protected(pos, name) then
438+
minetest.record_protection_violation(pos, name)
439+
return true
440+
end
441+
return false
442+
end
443+
444+
digtron.protected_allow_metadata_inventory_put = function(pos, _, _, stack, player)
445+
return digtron.check_protected_and_record(pos, player) and 0 or stack:get_count()
446+
end
447+
448+
digtron.protected_allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
449+
return digtron.check_protected_and_record(pos, player) and 0 or count
450+
end
451+
452+
digtron.protected_allow_metadata_inventory_take = function(pos, _, _, stack, player)
453+
return digtron.check_protected_and_record(pos, player) and 0 or stack:get_count()
454+
end

0 commit comments

Comments
 (0)