2017-08-23 00:20:30 +02:00
|
|
|
-- Farebox mod
|
|
|
|
|
|
|
|
-- adds a box that emmits a mesecons signal when the requested
|
|
|
|
-- quantity is paid. Ideal to charge admission from visitors to your
|
|
|
|
-- buildings
|
|
|
|
farebox = {}
|
2018-08-07 18:58:48 +02:00
|
|
|
-- Is used to keep the formspec information server-side
|
|
|
|
farebox.players = {}
|
|
|
|
|
2017-08-23 00:20:30 +02:00
|
|
|
function farebox.show_formspec(pos, player)
|
2018-08-07 18:11:39 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local owner = meta:get_string("owner")
|
2018-08-07 18:58:48 +02:00
|
|
|
local player_name = player:get_player_name()
|
|
|
|
|
|
|
|
farebox.players[player_name] = vector.new(pos)
|
|
|
|
if player_name == owner then
|
|
|
|
local loc = "nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z
|
|
|
|
minetest.show_formspec(player_name, "farebox:farebox",
|
|
|
|
"size[8,10]"..
|
|
|
|
"label[0.5,0.5; Entrance fee:]" ..
|
|
|
|
"list[" .. loc .. ";request;2.5,0.25;1,1;]" ..
|
2018-08-07 18:11:39 +02:00
|
|
|
"button_exit[6,0.25;2,1;open;Open]" ..
|
2018-08-07 18:58:48 +02:00
|
|
|
"list[" .. loc .. ";main;0,1.5;8,4]" ..
|
2018-08-07 18:11:39 +02:00
|
|
|
"list[current_player;main;0,5.75;8,1;]" ..
|
|
|
|
"list[current_player;main;0,7;8,3;8]" ..
|
|
|
|
"listring[]" .. default.get_hotbar_bg(0, 4.25)
|
2018-08-07 18:58:48 +02:00
|
|
|
)
|
2018-08-07 18:11:39 +02:00
|
|
|
else
|
2018-08-07 18:58:48 +02:00
|
|
|
minetest.show_formspec(player_name, "farebox:farebox",
|
|
|
|
"size[8,4]" ..
|
2018-08-07 18:11:39 +02:00
|
|
|
"label[0.5,1.5; Owner Wants:]" ..
|
|
|
|
"item_image_button[2.5,1.25;1,1;" ..
|
2018-08-07 18:58:48 +02:00
|
|
|
inv:get_stack("request", 1):get_name() .. ";buy;\n\n\b\b\b\b\b" ..
|
|
|
|
inv:get_stack("request", 1):get_count() .. "]" ..
|
2018-08-07 18:11:39 +02:00
|
|
|
"label[3.5,1.5; (Click on the item to pay)]"
|
2018-08-07 18:58:48 +02:00
|
|
|
)
|
2018-08-07 18:11:39 +02:00
|
|
|
end
|
2017-08-23 00:20:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-08-07 18:11:39 +02:00
|
|
|
farebox.rules = {
|
|
|
|
{x=0, y=-2, z=0},
|
2018-08-07 18:58:48 +02:00
|
|
|
{x=0, y=2, z=0},
|
2018-08-07 18:11:39 +02:00
|
|
|
}
|
2017-08-23 00:20:30 +02:00
|
|
|
|
2017-08-31 12:51:07 +02:00
|
|
|
function farebox.open_faregate(pos)
|
2018-08-07 18:11:39 +02:00
|
|
|
node = minetest.get_node(pos)
|
|
|
|
node.name = "farebox:faregate_open"
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
minetest.sound_play("doors_steel_door_open", {
|
|
|
|
pos = pos,
|
|
|
|
gain = 0.3,
|
|
|
|
max_hear_distance = 10
|
|
|
|
})
|
2017-08-31 12:51:07 +02:00
|
|
|
end
|
|
|
|
function farebox.close_faregate(pos)
|
2018-08-07 18:11:39 +02:00
|
|
|
node = minetest.get_node(pos)
|
|
|
|
node.name = "farebox:faregate"
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
minetest.sound_play("doors_steel_door_close", {
|
|
|
|
pos = pos,
|
|
|
|
gain = 0.3,
|
|
|
|
max_hear_distance = 10
|
|
|
|
})
|
2017-08-31 12:51:07 +02:00
|
|
|
end
|
|
|
|
|
2018-08-07 18:58:48 +02:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, pressed)
|
|
|
|
if formname ~= "farebox:farebox" then
|
|
|
|
return -- Not My Job
|
|
|
|
end
|
|
|
|
local pname = player:get_player_name()
|
|
|
|
local pos = farebox.players[pname]
|
|
|
|
if not pos then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local nodename = minetest.get_node(pos).name
|
|
|
|
if nodename ~= "farebox:farebox" and nodename ~= "farebox:faregate" then
|
|
|
|
-- Node was removed. Make reference invalid.
|
|
|
|
farebox.players[pname] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-08-07 18:11:39 +02:00
|
|
|
local pinv = player:get_inventory()
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
2018-08-07 18:58:48 +02:00
|
|
|
local owner = meta:get_string("owner")
|
2018-08-07 18:11:39 +02:00
|
|
|
local open = false
|
|
|
|
if pressed.buy then
|
2018-08-07 18:58:48 +02:00
|
|
|
local request = inv:get_stack("request", 1)
|
|
|
|
if pinv:contains_item("main", request)
|
|
|
|
and inv:room_for_item("main", request) then
|
2018-08-07 18:11:39 +02:00
|
|
|
if not (creative and creative.is_enabled_for
|
|
|
|
and creative.is_enabled_for(pname)) then
|
2018-08-07 18:58:48 +02:00
|
|
|
pinv:remove_item("main", request)
|
2018-08-07 18:11:39 +02:00
|
|
|
end
|
2018-08-07 18:58:48 +02:00
|
|
|
inv:add_item("main", request)
|
2018-08-07 18:11:39 +02:00
|
|
|
open = true
|
2018-08-07 18:58:48 +02:00
|
|
|
elseif not pinv:contains_item("main", request) then
|
2018-08-07 18:11:39 +02:00
|
|
|
minetest.chat_send_player(pname, "You don't have enough items to enter")
|
2018-08-07 18:58:48 +02:00
|
|
|
elseif not inv:room_for_item("main", request) then
|
2018-08-07 18:11:39 +02:00
|
|
|
minetest.chat_send_player(pname, "Owner's inventory is full")
|
|
|
|
end
|
|
|
|
end
|
2018-08-07 18:58:48 +02:00
|
|
|
if (pressed.open and pname == owner) or open then
|
2018-08-07 18:11:39 +02:00
|
|
|
minetest.chat_send_player(pname, "Payment accepted.")
|
|
|
|
if nodename == "farebox:farebox" then
|
2018-08-07 18:58:48 +02:00
|
|
|
mesecon.receptor_on(pos, farebox.rules)
|
2018-08-07 18:11:39 +02:00
|
|
|
minetest.after(1, mesecon.receptor_off, pos, farebox.rules)
|
|
|
|
elseif nodename == "farebox:faregate" then
|
|
|
|
farebox.open_faregate(pos)
|
|
|
|
end
|
2018-08-07 18:58:48 +02:00
|
|
|
minetest.close_formspec(pname, formname)
|
2018-08-07 18:11:39 +02:00
|
|
|
end
|
2017-08-23 00:20:30 +02:00
|
|
|
end)
|
|
|
|
|
2018-08-07 18:58:48 +02:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
farebox.players[player:get_player_name()] = nil
|
|
|
|
end)
|
2017-08-23 00:20:30 +02:00
|
|
|
|
|
|
|
minetest.register_node("farebox:farebox", {
|
2018-08-07 18:11:39 +02:00
|
|
|
description = "Farebox",
|
|
|
|
tiles = {
|
|
|
|
"default_steel_block.png", "default_steel_block.png",
|
|
|
|
"default_steel_block.png", "default_steel_block.png",
|
|
|
|
"default_steel_block.png", "farebox_front.png"
|
|
|
|
},
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {cracky=2},
|
|
|
|
legacy_facedir_simple = true,
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
mesecons = {
|
|
|
|
receptor = {
|
|
|
|
state = mesecon.state.off,
|
|
|
|
rules = farebox.rules
|
|
|
|
}
|
|
|
|
},
|
|
|
|
can_dig = can_dig,
|
|
|
|
after_place_node = function(pos, player, _)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local player_name = player:get_player_name()
|
2017-08-23 00:20:30 +02:00
|
|
|
|
2018-08-07 18:11:39 +02:00
|
|
|
meta:set_string("owner", player_name)
|
|
|
|
meta:set_string("infotext", "Owned by "..player_name)
|
2017-08-23 00:20:30 +02:00
|
|
|
|
2018-08-07 18:11:39 +02:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("request", 1)
|
|
|
|
inv:set_size("main", 32)
|
|
|
|
end,
|
|
|
|
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
|
|
|
|
farebox.show_formspec(pos, player)
|
|
|
|
end,
|
2017-08-23 00:20:30 +02:00
|
|
|
})
|
|
|
|
|
2018-08-07 18:11:39 +02:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "farebox:farebox",
|
|
|
|
recipe = {
|
|
|
|
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
|
|
|
|
{"default:steel_ingot", "", "default:steel_ingot"},
|
|
|
|
{"default:steel_ingot", "mesecons:mesecon", "default:steel_ingot"},
|
|
|
|
}
|
2017-08-23 00:32:50 +02:00
|
|
|
})
|
2018-08-07 18:11:39 +02:00
|
|
|
|
2017-08-31 12:51:07 +02:00
|
|
|
local modpath = minetest.get_modpath("farebox")
|
|
|
|
dofile(modpath .. "/faregate.lua")
|