From 70f31b7631801435847d521f3e8a8661f41df24d Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Fri, 9 Dec 2016 19:20:19 +0100 Subject: [PATCH] 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