allow table to reset itself

some don't know how to work the barter table, others block it intentionally.
Having the table expire and reset after certain time, solves that issue.
This commit is contained in:
SwissalpS 2020-02-14 02:25:45 +00:00 committed by Vanessa Dannenberg
parent e3aa39458e
commit 2d7d2dd206
2 changed files with 48 additions and 3 deletions

@ -2,3 +2,11 @@ currency
======== ========
Repo for Currency Mod Repo for Currency Mod
# Settings
Settings with default values:
```
# After how much idle-time barter table is reset (seconds)
barter.chest.expireafter 15 * 60
```

@ -5,6 +5,7 @@ local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua") local S, NS = dofile(MP.."/intllib.lua")
barter.chest = {} barter.chest = {}
barter.chest.expire_after = tonumber(minetest.settings:get('barter.chest.expireafter')) or 15 * 60
barter.chest.formspec = { barter.chest.formspec = {
main = "size[8,9]".. main = "size[8,9]"..
"list[current_name;pl1;0,0;3,4;]".. "list[current_name;pl1;0,0;3,4;]"..
@ -81,6 +82,8 @@ barter.chest.cancel = function(meta)
meta:set_string("pl2","") meta:set_string("pl2","")
meta:set_int("pl1step",0) meta:set_int("pl1step",0)
meta:set_int("pl2step",0) meta:set_int("pl2step",0)
meta:set_int("clean",1)
meta:set_int("timer",0)
end end
barter.chest.exchange = function(meta) barter.chest.exchange = function(meta)
@ -90,10 +93,20 @@ barter.chest.exchange = function(meta)
meta:set_string("pl2","") meta:set_string("pl2","")
meta:set_int("pl1step",0) meta:set_int("pl1step",0)
meta:set_int("pl2step",0) meta:set_int("pl2step",0)
meta:set_int("clean",1)
meta:set_int("timer",0)
end
barter.chest.start_timer = function(pos, meta)
meta:set_int("clean",0)
meta:set_int("timer",0)
local node_timer = minetest.get_node_timer(pos)
if node_timer:is_started() then return end
node_timer:start(22)
end end
minetest.register_node("currency:barter", { minetest.register_node("currency:barter", {
drawtype = "nodebox", drawtype = "nodebox",
description = S("Barter Table"), description = S("Barter Table"),
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
@ -118,13 +131,16 @@ minetest.register_node("currency:barter", {
meta:set_string("infotext", S("Barter Table")) meta:set_string("infotext", S("Barter Table"))
meta:set_string("pl1","") meta:set_string("pl1","")
meta:set_string("pl2","") meta:set_string("pl2","")
meta:set_int("clean",1)
meta:set_int("timer",0)
barter.chest.update_formspec(meta) barter.chest.update_formspec(meta)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("pl1", 3*4) inv:set_size("pl1", 12) -- 3*4
inv:set_size("pl2", 3*4) inv:set_size("pl2", 12) -- 3*4
end, end,
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
barter.chest.start_timer(pos, meta)
pl_receive_fields = function(n) pl_receive_fields = function(n)
if fields[n.."_start"] and meta:get_string(n) == "" then if fields[n.."_start"] and meta:get_string(n) == "" then
meta:set_string(n,sender:get_player_name()) meta:set_string(n,sender:get_player_name())
@ -152,18 +168,39 @@ minetest.register_node("currency:barter", {
end, end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
barter.chest.start_timer(pos, meta)
if not barter.chest.check_privilege(from_list,player:get_player_name(),meta) then return 0 end if not barter.chest.check_privilege(from_list,player:get_player_name(),meta) then return 0 end
if not barter.chest.check_privilege(to_list,player:get_player_name(),meta) then return 0 end if not barter.chest.check_privilege(to_list,player:get_player_name(),meta) then return 0 end
return count return count
end, end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player) allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
barter.chest.start_timer(pos, meta)
if not barter.chest.check_privilege(listname,player:get_player_name(),meta) then return 0 end if not barter.chest.check_privilege(listname,player:get_player_name(),meta) then return 0 end
return stack:get_count() return stack:get_count()
end, end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player) allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
barter.chest.start_timer(pos, meta)
if not barter.chest.check_privilege(listname,player:get_player_name(),meta) then return 0 end if not barter.chest.check_privilege(listname,player:get_player_name(),meta) then return 0 end
return stack:get_count() return stack:get_count()
end, end,
on_timer = function(pos, dtime)
local meta = minetest.get_meta(pos)
if 1 == meta:get_int("clean") then return false end
local timer = meta:get_int("timer")
timer = timer + dtime
if timer > barter.chest.expire_after then
-- attempt to return items to owners
barter.chest.cancel(meta)
-- also clear out items of offline users
local inv = meta:get_inventory()
inv:set_list("pl1", {})
inv:set_list("pl2", {})
return false
end
meta:set_int("timer",timer)
return true
end
}) })