Skip to content

Commit 3af5eba

Browse files
committed
Reuse codes, and add code for duplicator inventory
1 parent 3237b63 commit 3af5eba

File tree

4 files changed

+44
-94
lines changed

4 files changed

+44
-94
lines changed

nodes/node_battery_holder.lua

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,23 @@ 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-
local name = player:get_player_name()
84-
if minetest.is_protected(pos, name) then
85-
minetest.record_protection_violation(pos, name)
83+
if digtron.check_protected_and_record(pos, player) then
8684
return 0
8785
end
88-
8986
return stack:get_count()
9087
else
9188
return 0
9289
end
93-
9490
else
9591
return 0
9692
end
9793
end
9894
return 0
9995
end,
10096

101-
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
102-
local name = player:get_player_name()
103-
if minetest.is_protected(pos, name) then
104-
minetest.record_protection_violation(pos, name)
105-
return 0
106-
end
107-
108-
return count
109-
end,
110-
111-
allow_metadata_inventory_take = function(pos, _, _, stack, player)
112-
local name = player:get_player_name()
113-
if minetest.is_protected(pos, name) then
114-
minetest.record_protection_violation(pos, name)
115-
return 0
116-
end
97+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
11798

118-
return stack:get_count()
119-
end,
99+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
120100

121101

122102
can_dig = function(pos)

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: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,11 @@ minetest.register_node("digtron:inventory", set_logger({
7474
return inv:is_empty("main")
7575
end,
7676

77-
allow_metadata_inventory_put = function(pos, _, _, stack, player)
78-
local name = player:get_player_name()
79-
if minetest.is_protected(pos, name) then
80-
minetest.record_protection_violation(pos, name)
81-
return 0
82-
end
83-
84-
return stack:get_count()
85-
end,
86-
87-
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
88-
local name = player:get_player_name()
89-
if minetest.is_protected(pos, name) then
90-
minetest.record_protection_violation(pos, name)
91-
return 0
92-
end
93-
94-
return count
95-
end,
77+
allow_metadata_inventory_put = digtron.protected_allow_metadata_inventory_put,
9678

97-
allow_metadata_inventory_take = function(pos, _, _, stack, player)
98-
local name = player:get_player_name()
99-
if minetest.is_protected(pos, name) then
100-
minetest.record_protection_violation(pos, name)
101-
return 0
102-
end
79+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
10380

104-
return stack:get_count()
105-
end,
81+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
10682

10783
-- Pipeworks compatibility
10884
----------------------------------------------------------------
@@ -181,42 +157,19 @@ minetest.register_node("digtron:fuelstore", set_logger({
181157

182158
-- Only allow fuel items to be placed in fuel
183159
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
184-
local name = player:get_player_name()
185-
if minetest.is_protected(pos, name) then
186-
minetest.record_protection_violation(pos, name)
160+
if digtron.check_protected_and_record(pos, player) then
187161
return 0
188162
end
189163

190-
if listname == "fuel" then
191-
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
192-
193-
return stack:get_count()
194-
else
195-
return 0
196-
end
164+
if listname == "fuel" and minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
165+
return stack:get_count()
197166
end
198167
return 0
199168
end,
200169

201-
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
202-
local name = player:get_player_name()
203-
if minetest.is_protected(pos, name) then
204-
minetest.record_protection_violation(pos, name)
205-
return 0
206-
end
207-
208-
return count
209-
end,
210-
211-
allow_metadata_inventory_take = function(pos, _, _, stack, player)
212-
local name = player:get_player_name()
213-
if minetest.is_protected(pos, name) then
214-
minetest.record_protection_violation(pos, name)
215-
return 0
216-
end
170+
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
217171

218-
return stack:get_count()
219-
end,
172+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
220173

221174
can_dig = function(pos)
222175
local meta = minetest.get_meta(pos)
@@ -308,9 +261,7 @@ minetest.register_node("digtron:combined_storage", set_logger({
308261

309262
-- Only allow fuel items to be placed in fuel
310263
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
311-
local name = player:get_player_name()
312-
if minetest.is_protected(pos, name) then
313-
minetest.record_protection_violation(pos, name)
264+
if digtron.check_protected_and_record(pos, player) then
314265
return 0
315266
end
316267

@@ -325,9 +276,7 @@ minetest.register_node("digtron:combined_storage", set_logger({
325276
end,
326277

327278
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, _, count, player)
328-
local name = player:get_player_name()
329-
if minetest.is_protected(pos, name) then
330-
minetest.record_protection_violation(pos, name)
279+
if digtron.check_protected_and_record(pos, player) then
331280
return 0
332281
end
333282

@@ -344,15 +293,7 @@ minetest.register_node("digtron:combined_storage", set_logger({
344293
return 0
345294
end,
346295

347-
allow_metadata_inventory_take = function(pos, _, _, stack, player)
348-
local name = player:get_player_name()
349-
if minetest.is_protected(pos, name) then
350-
minetest.record_protection_violation(pos, name)
351-
return 0
352-
end
353-
354-
return stack:get_count()
355-
end,
296+
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
356297

357298
can_dig = function(pos)
358299
local meta = minetest.get_meta(pos)

util.lua

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

0 commit comments

Comments
 (0)