From b49ba5dc142f09b99c44101c48b826354fd77664 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 16 Oct 2016 09:09:11 +0200 Subject: [PATCH 1/7] Shop: Allow server admins to open the owner formspec of shops Clean up the position entry on leave Add wrench support --- depends.txt | 1 + init.lua | 6 +++++- shop.lua | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/depends.txt b/depends.txt index 4846195..8eae95c 100644 --- a/depends.txt +++ b/depends.txt @@ -1,6 +1,7 @@ default moreores? technic_worldgen? +wrench? quartz? pipeworks? money? diff --git a/init.lua b/init.lua index 99ec6b1..f9fa96a 100644 --- a/init.lua +++ b/init.lua @@ -68,7 +68,11 @@ end -- Privs minetest.register_privilege("bitchange", "Can access to owned nodes of the bitchange mod") function bitchange.has_access(owner, player_name) - return (player_name == owner or owner == "" or minetest.get_player_privs(player_name).server or minetest.get_player_privs(player_name).bitchange) + if player_name == owner or owner == "" then + return true + end + local privs = minetest.get_player_privs(player_name) + return privs.server or privs.bitchange end print("[BitChange] Loaded.") diff --git a/shop.lua b/shop.lua index 3e29d1b..6e14635 100644 --- a/shop.lua +++ b/shop.lua @@ -303,7 +303,7 @@ minetest.register_node("bitchange:shop", { local player_name = clicker:get_player_name() local view = 0 exchange_shop[player_name] = pos - if player_name == meta:get_string("owner") then + if bitchange.has_access(meta:get_string("owner"), player_name) then if clicker:get_player_control().aux1 then view = 1 else @@ -355,3 +355,21 @@ minetest.register_craft({ {"default:chest_locked"}, } }) + +minetest.register_on_dieplayer(function(player) + local player_name = player:get_player_name() + exchange_shop[player_name] = nil +end) + +if minetest.get_modpath("wrench") and wrench then + local STRING = wrench.META_TYPE_STRING + wrench:register_node("bitchange:shop", { + lists = {"stock", "custm", "custm_ej", "cust_ow", "cust_og", "cust_ej"}, + metas = { + owner = STRING, + infotext = STRING, + title = STRING, + }, + owned = true + }) +end From b52f70c09dedca19d95f14f7bc22e5ac7cbb5f13 Mon Sep 17 00:00:00 2001 From: volkj Date: Wed, 9 Nov 2016 15:48:15 +0100 Subject: [PATCH 2/7] "deprecated" Warning removal and code cleanup substitute deprecated tile_images and height_min/max with the new ones --- README.txt | 4 +++- minecoins.lua | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.txt b/README.txt index 612d53a..ecf5a4a 100644 --- a/README.txt +++ b/README.txt @@ -18,4 +18,6 @@ Dependencies: money2 (optional) currency (optional) -Forum link: https://forum.minetest.net/viewtopic.php?id=7821 +Forum link: https://forum.minetest.net/viewtopic.php?id=7821 + +Requires Minetest 0.4.14 (stable) or newer. diff --git a/minecoins.lua b/minecoins.lua index 1c8f75d..104a717 100644 --- a/minecoins.lua +++ b/minecoins.lua @@ -5,7 +5,7 @@ -- Node definitions minetest.register_node("bitchange:minecoin_in_ground", { description = "MineCoin Ore", - tile_images = { "default_stone.png^bitchange_minecoin_in_ground.png" }, + tiles = { "default_stone.png^bitchange_minecoin_in_ground.png" }, is_ground_content = true, groups = {cracky=2}, sounds = default.node_sound_stone_defaults(), @@ -20,7 +20,7 @@ minetest.register_node("bitchange:minecoin_in_ground", { minetest.register_node("bitchange:mineninth_in_ground", { description = "MineNinth Ore", - tile_images = { "default_stone.png^bitchange_mineninth_in_ground.png" }, + tiles = { "default_stone.png^bitchange_mineninth_in_ground.png" }, is_ground_content = true, groups = {cracky=3}, sounds = default.node_sound_stone_defaults(), @@ -36,7 +36,7 @@ minetest.register_node("bitchange:mineninth_in_ground", { minetest.register_node("bitchange:minecoinblock", { description = "MineCoin Block", - tile_images = { "bitchange_minecoinblock.png" }, + tiles = { "bitchange_minecoinblock.png" }, is_ground_content = true, groups = {cracky=2}, sounds = default.node_sound_stone_defaults(), @@ -121,8 +121,8 @@ minetest.register_ore({ clust_scarcity = 15*15*15, clust_num_ores = 3, clust_size = 7, - height_max = -512, - height_min = -18000, + y_max = -512, + y_min = -18000, }) minetest.register_ore({ @@ -132,8 +132,8 @@ minetest.register_ore({ clust_scarcity = 12*12*12, clust_num_ores = 5, clust_size = 8, - height_max = -256, - height_min = -511, + y_max = -256, + y_min = -511, }) minetest.register_ore({ @@ -143,7 +143,7 @@ minetest.register_ore({ clust_scarcity = 13*13*13, clust_num_ores = 3, clust_size = 7, - height_max = 28000, - height_min = -255, + y_max = 28000, + y_min = -255, }) -end \ No newline at end of file +end From 70f31b7631801435847d521f3e8a8661f41df24d Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Fri, 9 Dec 2016 19:20:19 +0100 Subject: [PATCH 3/7] Shop: Clean up code, fix item repair cheat --- shop.lua | 137 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 76 insertions(+), 61 deletions(-) diff --git a/shop.lua b/shop.lua index 6e14635..07c39af 100644 --- a/shop.lua +++ b/shop.lua @@ -128,121 +128,134 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) if fields.exchange then 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 end - if not shop_inv:is_empty("cust_ej") or not shop_inv:is_empty("custm_ej") then - minetest.chat_send_player(player_name, "Exchange shop: Error, ejected items detected!") + if not shop_inv:is_empty("cust_ej") + or not shop_inv:is_empty("custm_ej") then + minetest.chat_send_player(player_name, + "One or multiple ejection fields are filled. ".. + "Please empty them or contact the shop owner.") + return end local player_inv = sender:get_inventory() local err_msg = "" local cust_ow = shop_inv:get_list("cust_ow") local cust_og = shop_inv:get_list("cust_og") - local cust_ow_legal = true - local cust_og_legal = true - --?shop configured well + + -- Check validness of stack "owner wants" + local cust_ow_ok = true for i1, item1 in pairs(cust_ow) do + local name1 = item1:get_name() for i2, item2 in pairs(cust_ow) do - if item1:get_name() == item2:get_name() and i1 ~= i2 and item1:get_name() ~= "" then - cust_ow_legal = false - --break + if name1 == "" then + break + end + if i1 ~= i2 and name1 == item2:get_name() then + cust_ow_ok = false + break end end - if not cust_ow_legal then + if not cust_ow_ok then + err_msg = "The field 'Owner needs' can not contain multiple ".. + "times the same items. Please contact the shop owner." break end end - if not cust_ow_legal then - err_msg = "The 'Owner needs' field can not contain multiple times the same items, contact the shop owner." - end - if err_msg == "" then --?shop configured well + + -- Check validness of stack "owner gives" + if err_msg == "" then + local cust_og_ok = true for i1, item1 in pairs(cust_og) do + local name1 = item1:get_name() for i2, item2 in pairs(cust_og) do - if item1:get_name() == item2:get_name() and i1 ~= i2 and item1:get_name() ~= "" then - cust_og_legal = false + if name1 == "" then + break + end + if i1 ~= i2 and name1 == item2:get_name() then + cust_og_ok = false break end end - if not cust_og_legal then + if not cust_og_ok then + err_msg = "The field 'Owner gives' can not contain multiple ".. + "times the same items. Please contact the shop owner." break end end - if not cust_og_legal then - err_msg = "The 'Owner gives' field can not contain multiple times the same items, contact the shop owner." - end end - if err_msg == "" then --?shop has space - local shop_has_space = true + + -- Check for space in the shop + if err_msg == "" then for i, item in pairs(cust_ow) do if not shop_inv:room_for_item("custm", item) then - shop_has_space = false + err_msg = "The stock in this shop is full. ".. + "Please contact the shop owner." break end end - if not shop_has_space then - err_msg = "The stock in the shop is full." - end end - if err_msg == "" then --?shop has items - local shop_has_items = true + + -- Check availability of the shop's items + if err_msg == "" then for i, item in pairs(cust_og) do if not shop_inv:contains_item("stock", item) then - shop_has_items = false + err_msg = "This shop is sold out." break end end - if not shop_has_items then - err_msg = "The shop is empty and can not give you anything." - end end - if err_msg == "" then --?player has space - local player_has_space = true + + -- Check for space in the player's inventory + if err_msg == "" then for i, item in pairs(cust_og) do if not player_inv:room_for_item("main", item) then - player_has_space = false + err_msg = "You do not have enough space in your inventory." break end end - if not player_has_space then - err_msg = "You do not have the space in your inventory." - end end - if err_msg == "" then --?player has items - local player_has_items = true + + -- Check availability of the player's items + if err_msg == "" then for i, item in pairs(cust_ow) do if not player_inv:contains_item("main", item) then - player_has_items = false + err_msg = "You do not have the required items." break end end - if not player_has_items then - err_msg = "You do not have the needed items." - end end - if err_msg == "" then --?exchange + + -- Do the exchange! + if err_msg == "" then local fully_exchanged = true for i, item in pairs(cust_ow) do - player_inv:remove_item("main", item) --player inv. to stock else to eject fields - if shop_inv:room_for_item("custm", item) then - shop_inv:add_item("custm", item) + local stack = player_inv:remove_item("main", item) + if shop_inv:room_for_item("custm", stack) then + shop_inv:add_item("custm", stack) else - shop_inv:add_item("custm_ej", item) + -- Move to ejection field + shop_inv:add_item("custm_ej", stack) fully_exchanged = false end end for i, item in pairs(cust_og) do - shop_inv:remove_item("stock", item) --stock to player inv. else to eject fields - if player_inv:room_for_item("main", item) then - player_inv:add_item("main", item) + local stack = shop_inv:remove_item("stock", item) + if player_inv:room_for_item("main", stack) then + player_inv:add_item("main", stack) else - shop_inv:add_item("cust_ej", item) + -- Move to ejection field + shop_inv:add_item("cust_ej", stack) fully_exchanged = false end end if not fully_exchanged then - err_msg = "Fatal error! Stocks are overflowing somewhere!" + err_msg = "Warning! Stacks are overflowing somewhere!" end end + + -- Throw error message if err_msg ~= "" then minetest.chat_send_player(player_name, "Exchange shop: "..err_msg) end @@ -290,12 +303,12 @@ minetest.register_node("bitchange:shop", { can_dig = function(pos,player) local meta = minetest.get_meta(pos); local inv = meta:get_inventory() - 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("cust_og") and inv:is_empty("cust_ej")) then + if inv:is_empty("stock") and inv:is_empty("custm") + and inv:is_empty("cust_ow") and inv:is_empty("custm_ej") + and inv:is_empty("cust_og") and inv:is_empty("cust_ej") then return true end - minetest.chat_send_player(player:get_player_name(), "Can not 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 end, on_rightclick = function(pos, node, clicker, itemstack) @@ -330,8 +343,9 @@ minetest.register_node("bitchange:shop", { return 0 end 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() end return 0 @@ -341,7 +355,8 @@ minetest.register_node("bitchange:shop", { return stack:get_count() end 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() end return 0 From 6f9cc0081a72c02087705ff78b7b3648d11d9b79 Mon Sep 17 00:00:00 2001 From: Foz Date: Sun, 29 Jan 2017 23:06:53 -0500 Subject: [PATCH 4/7] Prevent shops from trading tools deceptively This limits the tools a shop will accept or give to those whose wear is less than or equal to the tool shown in the Owner needs or Owner gives lists. This protects both owners and customers from unfair trades. Owners are protected from receiving worn out tools when a new tool is placed in the needs list. Customers are protected from receiving worn out tools when a new one is placed in the give list. --- shop.lua | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/shop.lua b/shop.lua index 07c39af..a809b93 100644 --- a/shop.lua +++ b/shop.lua @@ -5,6 +5,29 @@ local exchange_shop = {} +-- Tool wear aware replacement for contains_item. +local function list_contains_item(inv, listname, stack) + local list = inv:get_list(listname) + for i, list_stack in pairs(list) do + if list_stack:get_name() == stack:get_name() and + list_stack:get_count() >= stack:get_count() and + list_stack:get_wear() <= stack:get_wear() then + return i + end + end +end + +-- Tool wear aware replacement for remove_item. +local function list_remove_item(inv, listname, stack) + local index = list_contains_item(inv, listname, stack) + if index then + local list_stack = inv:get_stack(listname, index) + local removed_stack = list_stack:take_item(stack:get_count()) + inv:set_stack(listname, index, list_stack) + return removed_stack + end +end + local function get_exchange_shop_formspec(number,pos,title) local formspec = "" local name = "nodemeta:"..pos.x..","..pos.y..","..pos.z @@ -200,7 +223,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) -- Check availability of the shop's items if err_msg == "" then for i, item in pairs(cust_og) do - if not shop_inv:contains_item("stock", item) then + if not list_contains_item(shop_inv, "stock", item) then err_msg = "This shop is sold out." break end @@ -220,7 +243,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) -- Check availability of the player's items if err_msg == "" then for i, item in pairs(cust_ow) do - if not player_inv:contains_item("main", item) then + if not list_contains_item(player_inv, "main", item) then err_msg = "You do not have the required items." break end @@ -231,7 +254,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) if err_msg == "" then local fully_exchanged = true for i, item in pairs(cust_ow) do - local stack = player_inv:remove_item("main", item) + local stack = list_remove_item(player_inv, "main", item) if shop_inv:room_for_item("custm", stack) then shop_inv:add_item("custm", stack) else @@ -241,7 +264,7 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) end end for i, item in pairs(cust_og) do - local stack = shop_inv:remove_item("stock", item) + local stack = list_remove_item(shop_inv, "stock", item) if player_inv:room_for_item("main", stack) then player_inv:add_item("main", stack) else From 3814d994db0e9da9849ec3621c68038e1a862aa4 Mon Sep 17 00:00:00 2001 From: Foz Date: Fri, 17 Feb 2017 00:02:36 -0500 Subject: [PATCH 5/7] Fix handling of empty stacks and item accumulation Fixes SmallJoker/bitchange#9 --- shop.lua | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/shop.lua b/shop.lua index a809b93..e9650d4 100644 --- a/shop.lua +++ b/shop.lua @@ -7,25 +7,52 @@ local exchange_shop = {} -- Tool wear aware replacement for contains_item. local function list_contains_item(inv, listname, stack) + local count = stack:get_count() + if count == 0 then + return true + end + local list = inv:get_list(listname) - for i, list_stack in pairs(list) do - if list_stack:get_name() == stack:get_name() and - list_stack:get_count() >= stack:get_count() and - list_stack:get_wear() <= stack:get_wear() then - return i + local name = stack:get_name() + local wear = stack:get_wear() + for _, list_stack in pairs(list) do + if list_stack:get_name() == name and + list_stack:get_wear() <= wear then + if list_stack:get_count() >= count then + return true + else + count = count - list_stack:get_count() + end end end end -- Tool wear aware replacement for remove_item. local function list_remove_item(inv, listname, stack) - local index = list_contains_item(inv, listname, stack) - if index then - local list_stack = inv:get_stack(listname, index) - local removed_stack = list_stack:take_item(stack:get_count()) - inv:set_stack(listname, index, list_stack) - return removed_stack + local count = stack:get_count() + if count == 0 then + return stack end + local removed_stack = ItemStack(nil) + + local list = inv:get_list(listname) + local name = stack:get_name() + local wear = stack:get_wear() + for index, list_stack in pairs(list) do + if list_stack:get_name() == name and + list_stack:get_wear() <= wear then + if list_stack:get_count() >= count then + removed_stack:add_item(list_stack:take_item(count)) + inv:set_stack(listname, index, list_stack) + break + else + removed_stack:add_item(list_stack) + inv:set_stack(listname, index, ItemStack(nil)) + count = count - list_stack:get_count() + end + end + end + return removed_stack end local function get_exchange_shop_formspec(number,pos,title) From bf68097a75fdb0eef8d6c5c7635e72213c45d33f Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 3 Jun 2017 19:35:18 +0200 Subject: [PATCH 6/7] Shop: Avoid unhandled leftovers from ItemStack:add_item Pre-fix if https://github.com/minetest/minetest/pull/5774 gets merged somewhen Thanks to @nybble41 for providing this code --- shop.lua | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/shop.lua b/shop.lua index e9650d4..4de72c1 100644 --- a/shop.lua +++ b/shop.lua @@ -29,29 +29,35 @@ end -- Tool wear aware replacement for remove_item. local function list_remove_item(inv, listname, stack) - local count = stack:get_count() - if count == 0 then + local wanted = stack:get_count() + if wanted == 0 then return stack end - local removed_stack = ItemStack(nil) local list = inv:get_list(listname) local name = stack:get_name() local wear = stack:get_wear() + local remaining = wanted + local removed_wear = 0 + for index, list_stack in pairs(list) do if list_stack:get_name() == name and list_stack:get_wear() <= wear then - if list_stack:get_count() >= count then - removed_stack:add_item(list_stack:take_item(count)) - inv:set_stack(listname, index, list_stack) + local taken_stack = list_stack:take_item(remaining) + inv:set_stack(listname, index, list_stack) + + removed_wear = math.max(removed_wear, taken_stack:get_wear()) + remaining = remaining - taken_stack:get_count() + if remaining == 0 then break - else - removed_stack:add_item(list_stack) - inv:set_stack(listname, index, ItemStack(nil)) - count = count - list_stack:get_count() end end end + + -- Todo: Also remove kebab + local removed_stack = ItemStack(name) + removed_stack:set_count(wanted - remaining) + removed_stack:set_wear(removed_wear) return removed_stack end From 0fbda6a40ffab23e180cecec9cdf3b8c9c0f2107 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Mon, 7 Aug 2017 16:57:45 +0200 Subject: [PATCH 7/7] 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 --- shop.lua | 203 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 115 insertions(+), 88 deletions(-) diff --git a/shop.lua b/shop.lua index 4de72c1..6bfb04e 100644 --- a/shop.lua +++ b/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 --- https://forum.minetest.net/viewtopic.php?id=7002 ---License: WTFPL +--[[ + Exchange Shop [bitchange] + +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 = {} @@ -61,42 +79,48 @@ local function list_remove_item(inv, listname, stack) return removed_stack end -local function get_exchange_shop_formspec(number,pos,title) - local formspec = "" +local function get_exchange_shop_formspec(mode, pos, title) local name = "nodemeta:"..pos.x..","..pos.y..","..pos.z - if number == 1 then + if mode == "customer" then -- customer - formspec = ("size[8,9;]".. - "label[0,0;Exchange shop]".. - "label[1,0.5;Owner needs:]".. - "list["..name..";cust_ow;1,1;2,2;]".. - "button[3,2.4;2,1;exchange;Exchange]".. - "label[5,0.5;Owner gives:]".. - "list["..name..";cust_og;5,1;2,2;]".. - "label[0.7,3.5;Ejected items:]".. - "label[0.7,3.8;(Remove me!)]".. - "list["..name..";cust_ej;3,3.5;4,1;]".. - "list[current_player;main;0,5;8,4;]".. - "listring["..name..";custm_ej]".. - "listring[current_player;main]") - elseif number == 2 or number == 3 then + return ( + "size[8,9;]".. + "label[0,0;Exchange shop]".. + "label[1,0.5;Owner needs:]".. + "list["..name..";cust_ow;1,1;2,2;]".. + "button[3,2.4;2,1;exchange;Exchange]".. + "label[5,0.5;Owner gives:]".. + "list["..name..";cust_og;5,1;2,2;]".. + "label[0.7,3.5;Ejected items:]".. + "label[0.7,3.8;(Remove me!)]".. + "list["..name..";cust_ej;3,3.5;4,1;]".. + "list[current_player;main;0,5;8,4;]".. + "listring["..name..";custm_ej]".. + "listring[current_player;main]" + ) + end + if mode == "owner_custm" + or mode == "owner_stock" then -- owner - formspec = ("size[11,10;]".. - "label[0.3,0.1;Title:]".. - "field[1.5,0.5;3,0.5;title;;"..title.."]".. - "button[4.1,0.24;1,0.5;set_title;Set]".. - "label[0,0.7;You need:]".. - "list["..name..";cust_ow;0,1.2;2,2;]".. - "label[3,0.7;You give:]".. - "list["..name..";cust_og;3,1.2;2,2;]".. - "label[0.3,3.5;Ejected items: (Remove me!)]".. - "list["..name..";custm_ej;0,4;4,1;]".. - "label[6,0;You are viewing:]".. - "label[6,0.3;(Click to switch)]".. - "listring["..name..";custm_ej]".. - "listring[current_player;main]") - if number == 2 then + local formspec = ( + "size[11,10;]".. + "label[0.3,0.1;Title:]".. + "field[1.5,0.5;3,0.5;title;;"..title.."]".. + "field_close_on_enter[title;false]".. + "button[4.1,0.2;1,0.5;set_title;Set]".. + "label[0,0.7;You need:]".. + "list["..name..";cust_ow;0,1.2;2,2;]".. + "label[3,0.7;You give:]".. + "list["..name..";cust_og;3,1.2;2,2;]".. + "label[0,3.5;Ejected items: (Remove me!)]".. + "list["..name..";custm_ej;0,4;4,1;]".. + "label[6,0;You are viewing:]".. + "label[6,0.3;(Click to switch)]".. + "listring["..name..";custm_ej]".. + "listring[current_player;main]" + ) + if mode == "owner_custm" then formspec = (formspec.. "button[8.5,0.2;2.5,0.5;vstock;Customers stock]".. "list["..name..";custm;6,1;5,4;]".. @@ -109,47 +133,47 @@ local function get_exchange_shop_formspec(number,pos,title) "listring["..name..";stock]".. "listring[current_player;main]") end - formspec = (formspec.. - "label[1,5;Use (E) + (Right click) for customer interface]".. - "list[current_player;main;1,6;8,4;]") + return (formspec.. + "label[1,5;Use (E) + (Right click) for customer interface]".. + "list[current_player;main;1,6;8,4;]") end - return formspec + return "" end local function get_exchange_shop_tube_config(mode) - if bitchange.exchangeshop_pipeworks then - if mode == 1 then - return {choppy=2, oddly_breakable_by_hand=2, tubedevice=1, tubedevice_receiver=1} - else + 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 return { insert_object = function(pos, node, stack, direction) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - return inv:add_item("stock",stack) + return inv:add_item("stock", stack) end, can_insert = function(pos, node, stack, direction) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - return inv:room_for_item("stock",stack) + return inv:room_for_item("stock", stack) end, - input_inventory="custm", + input_inventory = "custm", connect_sides = {left=1, right=1, back=1, top=1, bottom=1} } end - else - if mode == 1 then - return {choppy=2,oddly_breakable_by_hand=2} - else - return { - insert_object = function(pos, node, stack, direction) - return false - end, - can_insert = function(pos, node, stack, direction) - return false - end, - connect_sides = {} - } - end + return { + insert_object = function() + return false + end, + can_insert = function() + return false + end, + connect_sides = {} + } end end @@ -157,12 +181,13 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) if formname ~= "bitchange:shop_formspec" then return end + 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 end - local pos = exchange_shop[player_name] local meta = minetest.get_meta(pos) local title = meta:get_string("title") or "" local shop_owner = meta:get_string("owner") @@ -171,12 +196,14 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields) return end - if fields.set_title then - if fields.title and title ~= fields.title then + if fields.title then + if title ~= 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 - meta:set_string("infotext", "Exchange shop (owned by "..shop_owner..")") + meta:set_string("infotext", "Exchange shop (owned by " + .. shop_owner ..")") end meta:set_string("title", minetest.formspec_escape(fields.title)) 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) end elseif bitchange.has_access(shop_owner, player_name) then - local num = 0 + local mode if fields.vcustm then - num = 2 + mode = "owner_custm" elseif fields.vstock then - num = 3 + mode = "owner_stock" else return 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) -minetest.register_node("bitchange:shop", { +minetest.register_node(":bitchange:shop", { description = "Shop", tiles = {"bitchange_shop_top.png", "bitchange_shop_top.png", "bitchange_shop_side.png", "bitchange_shop_side.png", "bitchange_shop_side.png", "bitchange_shop_front.png"}, paramtype2 = "facedir", - groups = get_exchange_shop_tube_config(1), - tube = get_exchange_shop_tube_config(2), + groups = get_exchange_shop_tube_config("groups"), + tube = get_exchange_shop_tube_config("tube"), sounds = default.node_sound_wood_defaults(), after_place_node = function(pos, placer) 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 return true 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 end, on_rightclick = function(pos, node, clicker, itemstack) local meta = minetest.get_meta(pos) local player_name = clicker:get_player_name() - local view = 0 - exchange_shop[player_name] = pos - if bitchange.has_access(meta:get_string("owner"), player_name) then - if clicker:get_player_control().aux1 then - view = 1 - else - view = 2 - end - else - view = 1 + + 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 - minetest.show_formspec(player_name, "bitchange:shop_formspec", get_exchange_shop_formspec(view, pos, meta:get_string("title"))) + exchange_shop[player_name] = pos + minetest.show_formspec(player_name, "bitchange:shop_formspec", + get_exchange_shop_formspec(mode, pos, meta:get_string("title"))) end, allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) local meta = minetest.get_meta(pos) @@ -395,7 +421,8 @@ minetest.register_node("bitchange:shop", { return stack:get_count() end 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 end 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() exchange_shop[player_name] = nil end)