forked from mtcontrib/bitchange
Shop: Clean up code, fix item repair cheat
This commit is contained in:
parent
b52f70c09d
commit
70f31b7631
137
shop.lua
137
shop.lua
@ -128,121 +128,134 @@ minetest.register_on_player_receive_fields(function(sender, formname, fields)
|
|||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
if not shop_inv:is_empty("cust_ej") or not shop_inv:is_empty("custm_ej") then
|
if not shop_inv:is_empty("cust_ej")
|
||||||
minetest.chat_send_player(player_name, "Exchange shop: Error, ejected items detected!")
|
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
|
end
|
||||||
local player_inv = sender:get_inventory()
|
local player_inv = sender:get_inventory()
|
||||||
local err_msg = ""
|
local err_msg = ""
|
||||||
local cust_ow = shop_inv:get_list("cust_ow")
|
local cust_ow = shop_inv:get_list("cust_ow")
|
||||||
local cust_og = shop_inv:get_list("cust_og")
|
local cust_og = shop_inv:get_list("cust_og")
|
||||||
local cust_ow_legal = true
|
|
||||||
local cust_og_legal = true
|
-- Check validness of stack "owner wants"
|
||||||
--?shop configured well
|
local cust_ow_ok = true
|
||||||
for i1, item1 in pairs(cust_ow) do
|
for i1, item1 in pairs(cust_ow) do
|
||||||
|
local name1 = item1:get_name()
|
||||||
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 name1 == "" then
|
||||||
cust_ow_legal = false
|
break
|
||||||
--break
|
end
|
||||||
|
if i1 ~= i2 and name1 == item2:get_name() then
|
||||||
|
cust_ow_ok = false
|
||||||
|
break
|
||||||
end
|
end
|
||||||
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
|
break
|
||||||
end
|
end
|
||||||
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."
|
-- Check validness of stack "owner gives"
|
||||||
end
|
if err_msg == "" then
|
||||||
if err_msg == "" then --?shop configured well
|
local cust_og_ok = true
|
||||||
for i1, item1 in pairs(cust_og) do
|
for i1, item1 in pairs(cust_og) do
|
||||||
|
local name1 = item1:get_name()
|
||||||
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 name1 == "" then
|
||||||
cust_og_legal = false
|
break
|
||||||
|
end
|
||||||
|
if i1 ~= i2 and name1 == item2:get_name() then
|
||||||
|
cust_og_ok = false
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
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
|
break
|
||||||
end
|
end
|
||||||
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
|
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
|
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
|
err_msg = "The stock in this shop is full. "..
|
||||||
|
"Please contact the shop owner."
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not shop_has_space then
|
|
||||||
err_msg = "The stock in the shop is full."
|
|
||||||
end
|
|
||||||
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
|
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
|
err_msg = "This shop is sold out."
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not shop_has_items then
|
|
||||||
err_msg = "The shop is empty and can not give you anything."
|
|
||||||
end
|
|
||||||
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
|
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
|
err_msg = "You do not have enough space in your inventory."
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not player_has_space then
|
|
||||||
err_msg = "You do not have the space in your inventory."
|
|
||||||
end
|
|
||||||
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
|
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
|
err_msg = "You do not have the required items."
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if not player_has_items then
|
|
||||||
err_msg = "You do not have the needed items."
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if err_msg == "" then --?exchange
|
|
||||||
|
-- Do the exchange!
|
||||||
|
if err_msg == "" then
|
||||||
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
|
local stack = player_inv:remove_item("main", item)
|
||||||
if shop_inv:room_for_item("custm", item) then
|
if shop_inv:room_for_item("custm", stack) then
|
||||||
shop_inv:add_item("custm", item)
|
shop_inv:add_item("custm", stack)
|
||||||
else
|
else
|
||||||
shop_inv:add_item("custm_ej", item)
|
-- Move to ejection field
|
||||||
|
shop_inv:add_item("custm_ej", stack)
|
||||||
fully_exchanged = false
|
fully_exchanged = false
|
||||||
end
|
end
|
||||||
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
|
local stack = shop_inv:remove_item("stock", item)
|
||||||
if player_inv:room_for_item("main", item) then
|
if player_inv:room_for_item("main", stack) then
|
||||||
player_inv:add_item("main", item)
|
player_inv:add_item("main", stack)
|
||||||
else
|
else
|
||||||
shop_inv:add_item("cust_ej", item)
|
-- Move to ejection field
|
||||||
|
shop_inv:add_item("cust_ej", stack)
|
||||||
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 = "Warning! Stacks are overflowing somewhere!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Throw error message
|
||||||
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
|
||||||
@ -290,12 +303,12 @@ 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")
|
||||||
inv:is_empty("custm_ej") and inv:is_empty("cust_ow") and
|
and inv:is_empty("cust_ow") and inv:is_empty("custm_ej")
|
||||||
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(), "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
|
return false
|
||||||
end,
|
end,
|
||||||
on_rightclick = function(pos, node, clicker, itemstack)
|
on_rightclick = function(pos, node, clicker, itemstack)
|
||||||
@ -330,8 +343,9 @@ 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
|
if bitchange.has_access(meta:get_string("owner"), player:get_player_name())
|
||||||
listname ~= "cust_ej" and listname ~= "custm_ej" then
|
and listname ~= "cust_ej"
|
||||||
|
and listname ~= "custm_ej" then
|
||||||
return stack:get_count()
|
return stack:get_count()
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
@ -341,7 +355,8 @@ 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
|
||||||
|
Loading…
Reference in New Issue
Block a user