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
This commit is contained in:
1F616EMO~nya 2024-08-22 12:32:24 +08:00 committed by GitHub
parent 9e6c2db897
commit 320a05f70d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 87 additions and 23 deletions

@ -65,7 +65,7 @@ local def = {
end,
-- Allow all items with energy storage to be placed in the inventory
allow_metadata_inventory_put = function(_, listname, _, stack)
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
if listname == "batteries" then
if not minetest.global_exists("technic") then
return 0
@ -80,11 +80,13 @@ local def = {
-- And specifically if they hold any charge
-- Disregard empty batteries, the player should know better
if md and md.charge > 0 then
if digtron.check_protected_and_record(pos, player) then
return 0
end
return stack:get_count()
else
return 0
end
else
return 0
end
@ -92,6 +94,10 @@ local def = {
return 0
end,
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
can_dig = function(pos)
local meta = minetest.get_meta(pos)

@ -259,7 +259,11 @@ minetest.register_node("digtron:builder", {
digtron.update_builder_item(pos)
end,
allow_metadata_inventory_put = function(pos, listname, index, stack)
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if digtron.check_protected_and_record(pos, player) then
return 0
end
local stack_name = stack:get_name()
if minetest.get_item_group(stack_name, "digtron") ~= 0 then
@ -284,10 +288,12 @@ minetest.register_node("digtron:builder", {
return 0
end,
allow_metadata_inventory_take = function(pos, listname, index)
allow_metadata_inventory_take = function(pos, listname, index, _, player)
if not digtron.check_protected_and_record(pos, player) then
node_inventory_table.pos = pos
local inv = minetest.get_inventory(node_inventory_table)
inv:set_stack(listname, index, ItemStack(""))
end
return 0
end,

@ -237,8 +237,9 @@ minetest.register_node("digtron:auto_controller", {
inv:set_size("stop", 1)
end,
allow_metadata_inventory_put = function(pos, listname, index, stack)
if minetest.get_item_group(stack:get_name(), "digtron") ~= 0 then
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if digtron.check_protected_and_record(pos, player)
or minetest.get_item_group(stack:get_name(), "digtron") ~= 0 then
return 0 -- pointless setting a Digtron node as a stop block
end
node_inventory_table.pos = pos
@ -247,10 +248,12 @@ minetest.register_node("digtron:auto_controller", {
return 0
end,
allow_metadata_inventory_take = function(pos, listname, index)
allow_metadata_inventory_take = function(pos, listname, index, _, player)
if not digtron.check_protected_and_record(pos, player) then
node_inventory_table.pos = pos
local inv = minetest.get_inventory(node_inventory_table)
inv:set_stack(listname, index, ItemStack(""))
end
return 0
end,

@ -76,7 +76,11 @@ minetest.register_node("digtron:duplicator", {
return inv:is_empty("main")
end,
allow_metadata_inventory_put = function(_, _, _, stack)
allow_metadata_inventory_put = function(pos, _, _, stack, player)
if digtron.check_protected_and_record(pos, player) then
return 0
end
if minetest.get_item_group(stack:get_name(), "digtron") > 0 then
return stack:get_count()
else
@ -84,6 +88,10 @@ minetest.register_node("digtron:duplicator", {
end
end,
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
on_receive_fields = function(pos, _, fields, sender)
local player_name = sender:get_player_name()
if fields.help then

@ -74,6 +74,12 @@ minetest.register_node("digtron:inventory", set_logger({
return inv:is_empty("main")
end,
allow_metadata_inventory_put = digtron.protected_allow_metadata_inventory_put,
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
-- Pipeworks compatibility
----------------------------------------------------------------
@ -150,17 +156,21 @@ minetest.register_node("digtron:fuelstore", set_logger({
end,
-- Only allow fuel items to be placed in fuel
allow_metadata_inventory_put = function(_, listname, _, stack)
if listname == "fuel" then
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
return stack:get_count()
else
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
if digtron.check_protected_and_record(pos, player) then
return 0
end
if listname == "fuel" and minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
return stack:get_count()
end
return 0
end,
allow_metadata_inventory_move = digtron.protected_allow_metadata_inventory_move,
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
can_dig = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
@ -252,7 +262,11 @@ minetest.register_node("digtron:combined_storage", set_logger({
end,
-- Only allow fuel items to be placed in fuel
allow_metadata_inventory_put = function(_, listname, _, stack)
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
if digtron.check_protected_and_record(pos, player) then
return 0
end
if listname == "fuel" then
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
return stack:get_count()
@ -263,7 +277,11 @@ minetest.register_node("digtron:combined_storage", set_logger({
return stack:get_count() -- otherwise, allow all drops
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, _, count)
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, _, count, player)
if digtron.check_protected_and_record(pos, player) then
return 0
end
if to_list == "main" then
return count
end
@ -277,6 +295,8 @@ minetest.register_node("digtron:combined_storage", set_logger({
return 0
end,
allow_metadata_inventory_take = digtron.protected_allow_metadata_inventory_take,
can_dig = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()

@ -431,3 +431,24 @@ digtron.show_offset_markers = function(pos, offset, period)
if entity ~= nil then entity:set_yaw(1.5708) end
end
end
digtron.check_protected_and_record = function(pos, player)
local name = player:get_player_name()
if minetest.is_protected(pos, name) then
minetest.record_protection_violation(pos, name)
return true
end
return false
end
digtron.protected_allow_metadata_inventory_put = function(pos, _, _, stack, player)
return digtron.check_protected_and_record(pos, player) and 0 or stack:get_count()
end
digtron.protected_allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
return digtron.check_protected_and_record(pos, player) and 0 or count
end
digtron.protected_allow_metadata_inventory_take = function(pos, _, _, stack, player)
return digtron.check_protected_and_record(pos, player) and 0 or stack:get_count()
end