This commit is contained in:
SmallJoker 2014-11-29 18:26:02 +01:00
parent 5761f2d9fe
commit 20fcf0a0a0
1 changed files with 58 additions and 52 deletions

110
shop.lua
View File

@ -8,7 +8,7 @@ local exchange_shop = {}
local function get_exchange_shop_formspec(number,pos,title) local function get_exchange_shop_formspec(number,pos,title)
local formspec = "" 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 number == 1 then
-- customer -- customer
formspec = ("size[8,9;]".. formspec = ("size[8,9;]"..
"label[0,0;Exchange shop]".. "label[0,0;Exchange shop]"..
@ -21,12 +21,12 @@ local function get_exchange_shop_formspec(number,pos,title)
"label[0.7,3.8;(Remove me!)]".. "label[0.7,3.8;(Remove me!)]"..
"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;]")
elseif(number == 2 or number == 3) then elseif number == 2 or number == 3 then
-- owner -- owner
formspec = ("size[11,10;]".. formspec = ("size[11,10;]"..
"label[0.2,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,0.2;1,0.5;set_title;Set]".. "button[4.1,0.24;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:]"..
@ -35,7 +35,7 @@ local function get_exchange_shop_formspec(number,pos,title)
"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)]")
if(number == 2) then if number == 2 then
formspec = (formspec.."button[8.5,0.2;2.5,0.5;vstock;Customers stock]".. formspec = (formspec.."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;]")
else else
@ -50,8 +50,8 @@ local function get_exchange_shop_formspec(number,pos,title)
end end
local function get_exchange_shop_tube_config(mode) local function get_exchange_shop_tube_config(mode)
if(bitchange_exchangeshop_pipeworks) then if bitchange_exchangeshop_pipeworks then
if(mode == 1) then if mode == 1 then
return {choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1} return {choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1}
else else
return { return {
@ -70,7 +70,7 @@ local function get_exchange_shop_tube_config(mode)
} }
end end
else else
if(mode == 1) then if mode == 1 then
return {choppy=2,oddly_breakable_by_hand=2} return {choppy=2,oddly_breakable_by_hand=2}
else else
return { return {
@ -87,7 +87,7 @@ local function get_exchange_shop_tube_config(mode)
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()
@ -95,14 +95,14 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
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")
if(fields.quit) then if fields.quit then
exchange_shop[player_name] = nil exchange_shop[player_name] = nil
return return
end end
if(fields.set_title) then if fields.set_title then
if(fields.title and title ~= fields.title) then if fields.title and 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..")")
@ -111,7 +111,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
end end
end end
if(fields.exchange) then if fields.exchange then
local shop_inv = meta:get_inventory() local shop_inv = meta:get_inventory()
if shop_inv:is_empty("cust_ow") and shop_inv:is_empty("cust_og") then if shop_inv:is_empty("cust_ow") and shop_inv:is_empty("cust_og") then
return return
@ -128,87 +128,87 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
--?shop configured well --?shop configured well
for i1, item1 in pairs(cust_ow) do for i1, item1 in pairs(cust_ow) do
for i2, item2 in pairs(cust_ow) do for i2, item2 in pairs(cust_ow) do
if (item1:get_name() == item2:get_name() and i1 ~= i2 and item1:get_name() ~= "") then if item1:get_name() == item2:get_name() and i1 ~= i2 and item1:get_name() ~= "" then
cust_ow_legal = false cust_ow_legal = false
--break --break
end end
end end
if(not cust_ow_legal) then if not cust_ow_legal then
break break
end end
end end
if(not cust_ow_legal) then if not cust_ow_legal then
err_msg = "The 'Owner needs' field can not contain multiple times the same items, contact the shop owner." err_msg = "The 'Owner needs' field can not contain multiple times the same items, contact the shop owner."
end end
if(err_msg == "") then --?shop configured well if err_msg == "" then --?shop configured well
for i1, item1 in pairs(cust_og) do for i1, item1 in pairs(cust_og) do
for i2, item2 in pairs(cust_og) do for i2, item2 in pairs(cust_og) do
if (item1:get_name() == item2:get_name() and i1 ~= i2 and item1:get_name() ~= "") then if item1:get_name() == item2:get_name() and i1 ~= i2 and item1:get_name() ~= "" then
cust_og_legal = false cust_og_legal = false
break break
end end
end end
if(not cust_og_legal) then if not cust_og_legal then
break break
end end
end end
if(not cust_og_legal) then if not cust_og_legal then
err_msg = "The 'Owner gives' field can not contain multiple times the same items, contact the shop owner." err_msg = "The 'Owner gives' field can not contain multiple times the same items, contact the shop owner."
end end
end end
if(err_msg == "") then --?shop has space if err_msg == "" then --?shop has space
local shop_has_space = true local shop_has_space = true
for i, item in pairs(cust_ow) do for i, item in pairs(cust_ow) do
if (not shop_inv:room_for_item("custm",item)) then if not shop_inv:room_for_item("custm", item) then
shop_has_space = false shop_has_space = false
break break
end end
end end
if(not shop_has_space) then if not shop_has_space then
err_msg = "The stock in the shop is full." err_msg = "The stock in the shop is full."
end end
end end
if(err_msg == "") then --?shop has items if err_msg == "" then --?shop has items
local shop_has_items = true local shop_has_items = true
for i, item in pairs(cust_og) do for i, item in pairs(cust_og) do
if (not shop_inv:contains_item("stock",item)) then if not shop_inv:contains_item("stock", item) then
shop_has_items = false shop_has_items = false
break break
end end
end end
if(not shop_has_items) then if not shop_has_items then
err_msg = "The shop is empty and can not give you anything." err_msg = "The shop is empty and can not give you anything."
end end
end end
if(err_msg == "") then --?player has space if err_msg == "" then --?player has space
local player_has_space = true local player_has_space = true
for i, item in pairs(cust_og) do for i, item in pairs(cust_og) do
if (not player_inv:room_for_item("main",item)) then if not player_inv:room_for_item("main", item) then
player_has_space = false player_has_space = false
break break
end end
end end
if(not player_has_space) then if not player_has_space then
err_msg = "You do not have the space in your inventory." err_msg = "You do not have the space in your inventory."
end end
end end
if(err_msg == "") then --?player has items if err_msg == "" then --?player has items
local player_has_items = true local player_has_items = true
for i, item in pairs(cust_ow) do for i, item in pairs(cust_ow) do
if (not player_inv:contains_item("main",item)) then if not player_inv:contains_item("main", item) then
player_has_items = false player_has_items = false
break break
end end
end end
if(not player_has_items) then if not player_has_items then
err_msg = "You do not have the needed items." err_msg = "You do not have the needed items."
end end
end end
if(err_msg == "") then --?exchange if err_msg == "" then --?exchange
local fully_exchanged = true local fully_exchanged = true
for i, item in pairs(cust_ow) do for i, item in pairs(cust_ow) do
player_inv:remove_item("main", item) --player inv. to stock else to eject fields player_inv:remove_item("main", item) --player inv. to stock else to eject fields
if (shop_inv:room_for_item("custm",item)) then if shop_inv:room_for_item("custm", item) then
shop_inv:add_item("custm", item) shop_inv:add_item("custm", item)
else else
shop_inv:add_item("custm_ej", item) shop_inv:add_item("custm_ej", item)
@ -217,24 +217,30 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
end end
for i, item in pairs(cust_og) do for i, item in pairs(cust_og) do
shop_inv:remove_item("stock", item) --stock to player inv. else to eject fields shop_inv:remove_item("stock", item) --stock to player inv. else to eject fields
if (player_inv:room_for_item("main",item)) then if player_inv:room_for_item("main", item) then
player_inv:add_item("main", item) player_inv:add_item("main", item)
else else
shop_inv:add_item("cust_ej", item) shop_inv:add_item("cust_ej", item)
fully_exchanged = false fully_exchanged = false
end end
end end
if(not fully_exchanged) then if not fully_exchanged then
err_msg = "Fatal error! Stocks are overflowing somewhere!" err_msg = "Fatal error! Stocks are overflowing somewhere!"
end end
end end
if(err_msg ~= "") then if err_msg ~= "" then
minetest.chat_send_player(player_name, "Exchange shop: "..err_msg) minetest.chat_send_player(player_name, "Exchange shop: "..err_msg)
end end
elseif(fields.vstock and bitchange_has_access(shop_owner, player_name) and not fields.quit) then elseif bitchange_has_access(shop_owner, player_name) then
minetest.show_formspec(sender:get_player_name(),"bitchange:shop_formspec",get_exchange_shop_formspec(3, pos, title)) local num = 0
elseif(fields.vcustm and bitchange_has_access(shop_owner, player_name) and not fields.quit) then if fields.vcustm then
minetest.show_formspec(sender:get_player_name(),"bitchange:shop_formspec",get_exchange_shop_formspec(2, pos, title)) num = 2
elseif fields.vstock then
num = 3
else
return
end
minetest.show_formspec(player_name, "bitchange:shop_formspec", get_exchange_shop_formspec(num, pos, title))
end end
end) end)
@ -269,14 +275,13 @@ minetest.register_node("bitchange:shop", {
can_dig = function(pos,player) can_dig = function(pos,player)
local meta = minetest.get_meta(pos); local meta = minetest.get_meta(pos);
local inv = meta:get_inventory() local inv = meta:get_inventory()
if(inv:is_empty("stock") and inv:is_empty("custm") and if (inv:is_empty("stock") and inv:is_empty("custm") and
inv:is_empty("custm_ej") and inv:is_empty("cust_ow") and inv:is_empty("custm_ej") and inv:is_empty("cust_ow") and
inv:is_empty("cust_og") and inv:is_empty("cust_ej")) then inv:is_empty("cust_og") and inv:is_empty("cust_ej")) then
return true return true
else
minetest.chat_send_player(player:get_player_name(), "Can not dig exchange shop, one or multiple stocks are in use.")
return false
end end
minetest.chat_send_player(player:get_player_name(), "Can not dig exchange shop, one or multiple stocks are in use.")
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)
@ -284,7 +289,7 @@ minetest.register_node("bitchange:shop", {
local view = 0 local view = 0
exchange_shop[player_name] = pos exchange_shop[player_name] = pos
if player_name == meta:get_string("owner") then if player_name == meta:get_string("owner") then
if(clicker:get_player_control().aux1) then if clicker:get_player_control().aux1 then
view = 1 view = 1
else else
view = 2 view = 2
@ -310,7 +315,8 @@ minetest.register_node("bitchange:shop", {
return 0 return 0
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if (bitchange_has_access(meta:get_string("owner"), player:get_player_name()) and (listname ~= "cust_ej") and (listname ~= "custm_ej")) then if bitchange_has_access(meta:get_string("owner"), player:get_player_name()) and
listname ~= "cust_ej" and listname ~= "custm_ej" then
return stack:get_count() return stack:get_count()
end end
return 0 return 0
@ -320,7 +326,7 @@ minetest.register_node("bitchange:shop", {
return stack:get_count() return stack:get_count()
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if (bitchange_has_access(meta:get_string("owner"), player:get_player_name()) or (listname == "cust_ej")) then if bitchange_has_access(meta:get_string("owner"), player:get_player_name()) or listname == "cust_ej" then
return stack:get_count() return stack:get_count()
end end
return 0 return 0
@ -328,9 +334,9 @@ minetest.register_node("bitchange:shop", {
}) })
minetest.register_craft({ minetest.register_craft({
output = 'bitchange:shop', output = "bitchange:shop",
recipe = { recipe = {
{'default:sign_wall'}, {"default:sign_wall"},
{'default:chest_locked'}, {"default:chest_locked"},
} }
}) })