forked from mtcontrib/bitchange
Shop: Improve code readability
Make the entire code portable to other, custom mods Correct register_on_dieplayer to register_on_leaveplayer Do not close shop formspec when setting the title using enter
This commit is contained in:
parent
bf68097a75
commit
0fbda6a40f
135
shop.lua
135
shop.lua
@ -1,7 +1,25 @@
|
|||||||
--Created by Krock for the BitChange mod
|
--[[
|
||||||
--Parts of codes, images and ideas from Dan Duncombe's exchange shop
|
Exchange Shop [bitchange]
|
||||||
-- https://forum.minetest.net/viewtopic.php?id=7002
|
|
||||||
--License: WTFPL
|
This code is based on the idea of Dan Duncombe's exchange shop
|
||||||
|
https://web.archive.org/web/20160403113102/https://forum.minetest.net/viewtopic.php?id=7002
|
||||||
|
You do not need the entire bitchange mod to use this code.
|
||||||
|
Make sure you've got all textures to use it in a separate mod.
|
||||||
|
|
||||||
|
License: WTFPL
|
||||||
|
]]
|
||||||
|
|
||||||
|
|
||||||
|
if not bitchange then
|
||||||
|
-- Default values, if bitchange wasn't found.
|
||||||
|
bitchange = {}
|
||||||
|
bitchange.exchangeshop_pipeworks = true
|
||||||
|
|
||||||
|
function bitchange.has_access(owner, player_name)
|
||||||
|
return (player_name == owner or owner == ""
|
||||||
|
or minetest.get_player_privs(player_name).server)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local exchange_shop = {}
|
local exchange_shop = {}
|
||||||
|
|
||||||
@ -61,13 +79,13 @@ local function list_remove_item(inv, listname, stack)
|
|||||||
return removed_stack
|
return removed_stack
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_exchange_shop_formspec(number,pos,title)
|
local function get_exchange_shop_formspec(mode, pos, title)
|
||||||
local formspec = ""
|
|
||||||
local name = "nodemeta:"..pos.x..","..pos.y..","..pos.z
|
local name = "nodemeta:"..pos.x..","..pos.y..","..pos.z
|
||||||
|
|
||||||
if number == 1 then
|
if mode == "customer" then
|
||||||
-- customer
|
-- customer
|
||||||
formspec = ("size[8,9;]"..
|
return (
|
||||||
|
"size[8,9;]"..
|
||||||
"label[0,0;Exchange shop]"..
|
"label[0,0;Exchange shop]"..
|
||||||
"label[1,0.5;Owner needs:]"..
|
"label[1,0.5;Owner needs:]"..
|
||||||
"list["..name..";cust_ow;1,1;2,2;]"..
|
"list["..name..";cust_ow;1,1;2,2;]"..
|
||||||
@ -79,24 +97,30 @@ local function get_exchange_shop_formspec(number,pos,title)
|
|||||||
"list["..name..";cust_ej;3,3.5;4,1;]"..
|
"list["..name..";cust_ej;3,3.5;4,1;]"..
|
||||||
"list[current_player;main;0,5;8,4;]"..
|
"list[current_player;main;0,5;8,4;]"..
|
||||||
"listring["..name..";custm_ej]"..
|
"listring["..name..";custm_ej]"..
|
||||||
"listring[current_player;main]")
|
"listring[current_player;main]"
|
||||||
elseif number == 2 or number == 3 then
|
)
|
||||||
|
end
|
||||||
|
if mode == "owner_custm"
|
||||||
|
or mode == "owner_stock" then
|
||||||
-- owner
|
-- owner
|
||||||
formspec = ("size[11,10;]"..
|
local formspec = (
|
||||||
|
"size[11,10;]"..
|
||||||
"label[0.3,0.1;Title:]"..
|
"label[0.3,0.1;Title:]"..
|
||||||
"field[1.5,0.5;3,0.5;title;;"..title.."]"..
|
"field[1.5,0.5;3,0.5;title;;"..title.."]"..
|
||||||
"button[4.1,0.24;1,0.5;set_title;Set]"..
|
"field_close_on_enter[title;false]"..
|
||||||
|
"button[4.1,0.2;1,0.5;set_title;Set]"..
|
||||||
"label[0,0.7;You need:]"..
|
"label[0,0.7;You need:]"..
|
||||||
"list["..name..";cust_ow;0,1.2;2,2;]"..
|
"list["..name..";cust_ow;0,1.2;2,2;]"..
|
||||||
"label[3,0.7;You give:]"..
|
"label[3,0.7;You give:]"..
|
||||||
"list["..name..";cust_og;3,1.2;2,2;]"..
|
"list["..name..";cust_og;3,1.2;2,2;]"..
|
||||||
"label[0.3,3.5;Ejected items: (Remove me!)]"..
|
"label[0,3.5;Ejected items: (Remove me!)]"..
|
||||||
"list["..name..";custm_ej;0,4;4,1;]"..
|
"list["..name..";custm_ej;0,4;4,1;]"..
|
||||||
"label[6,0;You are viewing:]"..
|
"label[6,0;You are viewing:]"..
|
||||||
"label[6,0.3;(Click to switch)]"..
|
"label[6,0.3;(Click to switch)]"..
|
||||||
"listring["..name..";custm_ej]"..
|
"listring["..name..";custm_ej]"..
|
||||||
"listring[current_player;main]")
|
"listring[current_player;main]"
|
||||||
if number == 2 then
|
)
|
||||||
|
if mode == "owner_custm" then
|
||||||
formspec = (formspec..
|
formspec = (formspec..
|
||||||
"button[8.5,0.2;2.5,0.5;vstock;Customers stock]"..
|
"button[8.5,0.2;2.5,0.5;vstock;Customers stock]"..
|
||||||
"list["..name..";custm;6,1;5,4;]"..
|
"list["..name..";custm;6,1;5,4;]"..
|
||||||
@ -109,18 +133,23 @@ local function get_exchange_shop_formspec(number,pos,title)
|
|||||||
"listring["..name..";stock]"..
|
"listring["..name..";stock]"..
|
||||||
"listring[current_player;main]")
|
"listring[current_player;main]")
|
||||||
end
|
end
|
||||||
formspec = (formspec..
|
return (formspec..
|
||||||
"label[1,5;Use (E) + (Right click) for customer interface]"..
|
"label[1,5;Use (E) + (Right click) for customer interface]"..
|
||||||
"list[current_player;main;1,6;8,4;]")
|
"list[current_player;main;1,6;8,4;]")
|
||||||
end
|
end
|
||||||
return formspec
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
local function get_exchange_shop_tube_config(mode)
|
local function get_exchange_shop_tube_config(mode)
|
||||||
|
if mode == "groups" then
|
||||||
|
if bitchange.exchangeshop_pipeworks then
|
||||||
|
return {choppy=2, oddly_breakable_by_hand=2,
|
||||||
|
tubedevice=1, tubedevice_receiver=1}
|
||||||
|
end
|
||||||
|
return {choppy=2, oddly_breakable_by_hand=2}
|
||||||
|
end
|
||||||
|
if mode == "tube" then
|
||||||
if bitchange.exchangeshop_pipeworks then
|
if bitchange.exchangeshop_pipeworks then
|
||||||
if mode == 1 then
|
|
||||||
return {choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1}
|
|
||||||
else
|
|
||||||
return {
|
return {
|
||||||
insert_object = function(pos, node, stack, direction)
|
insert_object = function(pos, node, stack, direction)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
@ -136,33 +165,29 @@ local function get_exchange_shop_tube_config(mode)
|
|||||||
connect_sides = {left=1, right=1, back=1, top=1, bottom=1}
|
connect_sides = {left=1, right=1, back=1, top=1, bottom=1}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
else
|
|
||||||
if mode == 1 then
|
|
||||||
return {choppy=2,oddly_breakable_by_hand=2}
|
|
||||||
else
|
|
||||||
return {
|
return {
|
||||||
insert_object = function(pos, node, stack, direction)
|
insert_object = function()
|
||||||
return false
|
return false
|
||||||
end,
|
end,
|
||||||
can_insert = function(pos, node, stack, direction)
|
can_insert = function()
|
||||||
return false
|
return false
|
||||||
end,
|
end,
|
||||||
connect_sides = {}
|
connect_sides = {}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(sender, formname, fields)
|
minetest.register_on_player_receive_fields(function(sender, formname, fields)
|
||||||
if formname ~= "bitchange:shop_formspec" then
|
if formname ~= "bitchange:shop_formspec" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local player_name = sender:get_player_name()
|
local player_name = sender:get_player_name()
|
||||||
if not exchange_shop[player_name] then
|
local pos = exchange_shop[player_name]
|
||||||
|
if not pos then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local pos = exchange_shop[player_name]
|
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local title = meta:get_string("title") or ""
|
local title = meta:get_string("title") or ""
|
||||||
local shop_owner = meta:get_string("owner")
|
local shop_owner = meta:get_string("owner")
|
||||||
@ -171,12 +196,14 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if fields.set_title then
|
if fields.title then
|
||||||
if fields.title and title ~= fields.title then
|
if title ~= fields.title then
|
||||||
if fields.title ~= "" then
|
if fields.title ~= "" then
|
||||||
meta:set_string("infotext", "'"..fields.title.."' (owned by "..shop_owner..")")
|
meta:set_string("infotext", "'" .. fields.title
|
||||||
|
.. "' (owned by " .. shop_owner .. ")")
|
||||||
else
|
else
|
||||||
meta:set_string("infotext", "Exchange shop (owned by "..shop_owner..")")
|
meta:set_string("infotext", "Exchange shop (owned by "
|
||||||
|
.. shop_owner ..")")
|
||||||
end
|
end
|
||||||
meta:set_string("title", minetest.formspec_escape(fields.title))
|
meta:set_string("title", minetest.formspec_escape(fields.title))
|
||||||
end
|
end
|
||||||
@ -316,26 +343,27 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
|
|||||||
minetest.chat_send_player(player_name, "Exchange shop: "..err_msg)
|
minetest.chat_send_player(player_name, "Exchange shop: "..err_msg)
|
||||||
end
|
end
|
||||||
elseif bitchange.has_access(shop_owner, player_name) then
|
elseif bitchange.has_access(shop_owner, player_name) then
|
||||||
local num = 0
|
local mode
|
||||||
if fields.vcustm then
|
if fields.vcustm then
|
||||||
num = 2
|
mode = "owner_custm"
|
||||||
elseif fields.vstock then
|
elseif fields.vstock then
|
||||||
num = 3
|
mode = "owner_stock"
|
||||||
else
|
else
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
minetest.show_formspec(player_name, "bitchange:shop_formspec", get_exchange_shop_formspec(num, pos, title))
|
minetest.show_formspec(player_name, "bitchange:shop_formspec",
|
||||||
|
get_exchange_shop_formspec(mode, pos, title))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_node("bitchange:shop", {
|
minetest.register_node(":bitchange:shop", {
|
||||||
description = "Shop",
|
description = "Shop",
|
||||||
tiles = {"bitchange_shop_top.png", "bitchange_shop_top.png",
|
tiles = {"bitchange_shop_top.png", "bitchange_shop_top.png",
|
||||||
"bitchange_shop_side.png", "bitchange_shop_side.png",
|
"bitchange_shop_side.png", "bitchange_shop_side.png",
|
||||||
"bitchange_shop_side.png", "bitchange_shop_front.png"},
|
"bitchange_shop_side.png", "bitchange_shop_front.png"},
|
||||||
paramtype2 = "facedir",
|
paramtype2 = "facedir",
|
||||||
groups = get_exchange_shop_tube_config(1),
|
groups = get_exchange_shop_tube_config("groups"),
|
||||||
tube = get_exchange_shop_tube_config(2),
|
tube = get_exchange_shop_tube_config("tube"),
|
||||||
sounds = default.node_sound_wood_defaults(),
|
sounds = default.node_sound_wood_defaults(),
|
||||||
after_place_node = function(pos, placer)
|
after_place_node = function(pos, placer)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
@ -364,24 +392,22 @@ minetest.register_node("bitchange:shop", {
|
|||||||
and inv:is_empty("cust_og") and inv:is_empty("cust_ej") then
|
and inv:is_empty("cust_og") and inv:is_empty("cust_ej") then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
minetest.chat_send_player(player:get_player_name(), "Cannot dig exchange shop: one or multiple stocks are in use.")
|
minetest.chat_send_player(player:get_player_name(),
|
||||||
|
"Cannot dig exchange shop: one or multiple stocks are in use.")
|
||||||
return false
|
return false
|
||||||
end,
|
end,
|
||||||
on_rightclick = function(pos, node, clicker, itemstack)
|
on_rightclick = function(pos, node, clicker, itemstack)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local player_name = clicker:get_player_name()
|
local player_name = clicker:get_player_name()
|
||||||
local view = 0
|
|
||||||
|
local mode = "customer"
|
||||||
|
if bitchange.has_access(meta:get_string("owner"), player_name) and
|
||||||
|
not clicker:get_player_control().aux1 then
|
||||||
|
mode = "owner_custm"
|
||||||
|
end
|
||||||
exchange_shop[player_name] = pos
|
exchange_shop[player_name] = pos
|
||||||
if bitchange.has_access(meta:get_string("owner"), player_name) then
|
minetest.show_formspec(player_name, "bitchange:shop_formspec",
|
||||||
if clicker:get_player_control().aux1 then
|
get_exchange_shop_formspec(mode, pos, meta:get_string("title")))
|
||||||
view = 1
|
|
||||||
else
|
|
||||||
view = 2
|
|
||||||
end
|
|
||||||
else
|
|
||||||
view = 1
|
|
||||||
end
|
|
||||||
minetest.show_formspec(player_name, "bitchange:shop_formspec", get_exchange_shop_formspec(view, pos, meta:get_string("title")))
|
|
||||||
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)
|
||||||
@ -395,7 +421,8 @@ minetest.register_node("bitchange:shop", {
|
|||||||
return stack:get_count()
|
return stack:get_count()
|
||||||
end
|
end
|
||||||
if listname == "custm" then
|
if listname == "custm" then
|
||||||
minetest.chat_send_player(player:get_player_name(), "Exchange shop: Please press 'Customers stock' and insert your items there.")
|
minetest.chat_send_player(player:get_player_name(),
|
||||||
|
"Exchange shop: Please press 'Customers stock' and insert your items there.")
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
@ -427,7 +454,7 @@ minetest.register_craft({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
minetest.register_on_dieplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
exchange_shop[player_name] = nil
|
exchange_shop[player_name] = nil
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user