1
0
mirror of https://github.com/SmallJoker/exchange_shop.git synced 2024-11-13 06:00:25 +01:00

First version 🐈

This commit is contained in:
SmallJoker 2018-07-08 09:30:13 +02:00
commit 89d08b907b
11 changed files with 594 additions and 0 deletions

17
README.txt Normal file
View File

@ -0,0 +1,17 @@
Exchange Shop
===============
This mod adds an improved ("currency" compatible) shop to your world.
Features:
* 4 buyer and 4 seller slots
* Much storage capacity
* Custom shop title settable
* pipeworks compatibilty
Optional dependencies:
* currency
* bitchange
* wrench
License: CC0 (for everything)

97
currency_migrate.lua Normal file
View File

@ -0,0 +1,97 @@
-- Combine stacks into a new list
local function compress_list(list)
local items = {}
local new_list = {}
for i, stack in pairs(list or {}) do
if not stack:is_empty() then
if stack:get_stack_max() == 1 then
table.insert(new_list, stack)
else
items[stack:get_name()] = (items[stack:get_name()] or 0)
+ stack:get_count()
end
end
end
for name, count in pairs(items) do
local max = ItemStack(name):get_stack_max()
repeat
local take = math.min(max, count)
local stack = ItemStack(name)
stack:set_count(take)
table.insert(new_list, stack)
count = count - take
until count == 0
end
return new_list
end
local function list_add_list(inv, list_name, list)
local leftover_list = {}
for i, stack in pairs(list or {}) do
local leftover = inv:add_item(list_name, stack)
if not leftover:is_empty() then
table.insert(leftover_list, leftover)
end
end
if #leftover_list > 0 then
minetest.log("warning", "[exchange_shop] List " .. list_name
.. " is full. Possible item loss!")
end
return leftover_list
end
local function migrate_shop_node(pos, node)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local title = meta:get_string("infotext")
local inv = meta:get_inventory()
local def = minetest.registered_nodes[exchange_shop.shopname]
-- Create new slots
def.on_construct(pos)
meta:set_string("owner", owner)
meta:set_string("infotext", title)
list_add_list(inv, "custm", inv:get_list("customers_gave"))
inv:set_size("customers_gave", 0)
local new_owner_gives = compress_list(inv:get_list("owner_gives"))
local new_owner_wants = compress_list(inv:get_list("owner_wants"))
local dst_gives = "cust_og"
local dst_wants = "cust_ow"
if #new_owner_gives > 4 or #new_owner_wants > 4 then
-- Not enough space (from 6 slots to 4)
-- redirect everything to the stock
dst_gives = "stock"
dst_wants = "custm"
end
list_add_list(inv, dst_gives, new_owner_gives)
list_add_list(inv, dst_wants, new_owner_wants)
inv:set_size("owner_gives", 0)
inv:set_size("owner_takes", 0)
node.name = exchange_shop.shopname
minetest.swap_node(pos, node)
end
minetest.register_lbm({
label = "currency shop to exchange shop migration",
name = "exchange_shop:currency_migrate",
nodenames = { "currency:shop" },
run_at_every_load = true, -- TODO this for testing only
action = migrate_shop_node
})
-- Clean up garbage
minetest.register_on_joinplayer(function(player)
local inv = player:get_inventory()
for i, name in pairs({"customer_gives", "customer_gets"}) do
if inv:get_size(name) > 0 then
local leftover = list_add_list(inv, "main", inv:get_list(name))
list_add_list(inv, "craft", leftover)
inv:set_size(name, 0)
end
end
end)

19
currency_override.lua Normal file
View File

@ -0,0 +1,19 @@
local def = table.copy(minetest.registered_nodes["currency:shop"])
def.groups.not_in_creative_inventory = 1
minetest.override_item("currency:shop", {
groups = def.groups,
on_construct = function() end,
after_place_node = function(pos, ...)
local node = minetest.get_node(pos)
node.name = exchange_shop.shopname
minetest.swap_node(pos, node)
local new_def = minetest.registered_nodes[exchange_shop.shopname]
if new_def.on_construct then
new_def.on_construct(pos)
end
new_def.after_place_node(pos, unpack({...}))
end
})

3
depends.txt Normal file
View File

@ -0,0 +1,3 @@
currency?
bitchange?
wrench?

27
init.lua Normal file
View File

@ -0,0 +1,27 @@
exchange_shop = {}
exchange_shop.storage_size = 5 * 4
exchange_shop.shopname = "exchange_shop:shop"
local modpath = minetest.get_modpath("exchange_shop")
local has_currency = minetest.get_modpath("currency")
local has_bitchange = minetest.get_modpath("bitchange")
local migrate_currency = true -- TODO testing!
local slow_migrate_currency = false
if has_bitchange then
minetest.register_alias("exchange_shop:shop", "bitchange:shop")
exchange_shop.shopname = "bitchange:shop"
else
dofile(modpath .. "/shop_functions.lua")
dofile(modpath .. "/shop.lua")
end
if has_currency then
if migrate_currency then
dofile(modpath .. "/currency_migrate.lua")
end
if slow_migrate_currency then
dofile(modpath .. "/currency_override.lua")
end
end

2
mod.conf Normal file
View File

@ -0,0 +1,2 @@
name = exchange_shop
optional_depends = currency, bitchange, wrench

267
shop.lua Executable file
View File

@ -0,0 +1,267 @@
--[[
Exchange Shop
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
]]
local shop_positions = {}
local function get_exchange_shop_formspec(mode, pos, title)
local name = "nodemeta:"..pos.x..","..pos.y..","..pos.z
local function listring(src)
return "listring[".. name ..";" .. src .. "]" ..
"listring[current_player;main]"
end
if mode == "customer" then
-- customer
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("cust_ej")
)
end
if mode == "owner_custm"
or mode == "owner_stock" then
-- owner
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("custm_ej")
)
if mode == "owner_custm" then
formspec = (formspec..
"button[8.5,0.2;2.5,0.5;view_stock;Customers stock]"..
"list["..name..";custm;6,1;5,4;]"..
listring("custm"))
else
formspec = (formspec..
"button[8.5,0.2;2.5,0.5;view_custm;Your stock]"..
"list["..name..";stock;6,1;5,4;]"..
listring("stock"))
end
return (formspec..
"label[1,5;Use (E) + (Right click) for customer interface]"..
"list[current_player;main;1,6;8,4;]")
end
return ""
end
minetest.register_on_player_receive_fields(function(sender, formname, fields)
if formname ~= "exchange_shop:shop_formspec" then
return
end
local player_name = sender:get_player_name()
local pos = shop_positions[player_name]
if not pos then
return
end
if (fields.quit and fields.quit ~= "") or
minetest.get_node(pos).name ~= "exchange_shop:shop" then
shop_positions[player_name] = nil
return
end
local meta = minetest.get_meta(pos)
local title = meta:get_string("title")
local shop_owner = meta:get_string("owner")
if fields.title and exchange_shop.has_access(meta, player_name) then
-- Limit title length
fields.title = fields.title:sub(1, 80)
if title ~= fields.title then
if fields.title ~= "" then
meta:set_string("infotext", "'" .. fields.title
.. "' (owned by " .. shop_owner .. ")")
else
meta:set_string("infotext", "Exchange shop (owned by "
.. shop_owner ..")")
end
meta:set_string("title", minetest.formspec_escape(fields.title))
end
end
if fields.exchange then
local shop_inv = meta:get_inventory()
local player_inv = sender:get_inventory()
if shop_inv:is_empty("cust_ow")
and shop_inv:is_empty("cust_og") then
return
end
local err_msg = exchange_shop.exchange_action(player_inv, shop_inv)
-- Throw error message
if err_msg then
minetest.chat_send_player(player_name, minetest.colorize("#F33",
"Exchange shop: " .. err_msg))
end
elseif exchange_shop.has_access(meta, player_name) then
local mode
if fields.view_custm then
mode = "owner_custm"
elseif fields.view_stock then
mode = "owner_stock"
else
return
end
minetest.show_formspec(player_name, "exchange_shop:shop_formspec",
get_exchange_shop_formspec(mode, pos, title))
end
end)
minetest.register_node(exchange_shop.shopname, {
description = "Exchange Shop",
tiles = {
"shop_top.png", "shop_top.png",
"shop_side.png","shop_side.png",
"shop_side.png", "shop_front.png"
},
paramtype2 = "facedir",
groups = {choppy=2, oddly_breakable_by_hand=2,
tubedevice=1, tubedevice_receiver=1},
tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
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)
end,
input_inventory = "custm",
connect_sides = {left=1, right=1, back=1, top=1, bottom=1}
},
sounds = default.node_sound_wood_defaults(),
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local owner = placer:get_player_name()
meta:set_string("owner", owner)
meta:set_string("infotext", "Exchange shop (owned by "
.. owner .. ")")
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Exchange shop (constructing)")
meta:set_string("owner", "")
local inv = meta:get_inventory()
inv:set_size("stock", exchange_shop.storage_size) -- needed stock for exchanges
inv:set_size("custm", exchange_shop.storage_size) -- stock of the customers exchanges
inv:set_size("custm_ej", 4) -- ejected items if shop has no inventory room
inv:set_size("cust_ow", 2*2) -- owner wants
inv:set_size("cust_og", 2*2) -- owner gives
inv:set_size("cust_ej", 4) -- ejected items if player has no inventory room
end,
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("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(),
"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 mode = "customer"
if exchange_shop.has_access(meta, player_name) and
not clicker:get_player_control().aux1 then
mode = "owner_custm"
end
shop_positions[player_name] = pos
minetest.show_formspec(player_name, "exchange_shop: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)
if exchange_shop.has_access(meta, player:get_player_name()) then
return count
end
return 0
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if player:get_player_name() == ":pipeworks" then
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.")
return 0
end
local meta = minetest.get_meta(pos)
if exchange_shop.has_access(meta, player:get_player_name())
and listname ~= "cust_ej"
and listname ~= "custm_ej" then
return stack:get_count()
end
return 0
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if player:get_player_name() == ":pipeworks" then
return stack:get_count()
end
local meta = minetest.get_meta(pos)
if exchange_shop.has_access(meta, player:get_player_name())
or listname == "cust_ej" then
return stack:get_count()
end
return 0
end,
})
minetest.register_craft({
output = "exchange_shop:shop",
recipe = {
{"default:sign_wall"},
{"default:chest_locked"},
}
})
minetest.register_on_leaveplayer(function(player)
shop_positions[player:get_player_name()] = nil
end)
if minetest.get_modpath("wrench") and wrench then
local STRING = wrench.META_TYPE_STRING
wrench:register_node("exchange_shop:shop", {
lists = {"stock", "custm", "custm_ej", "cust_ow", "cust_og", "cust_ej"},
metas = {
owner = STRING,
infotext = STRING,
title = STRING,
},
owned = true
})
end

162
shop_functions.lua Normal file
View File

@ -0,0 +1,162 @@
function exchange_shop.has_access(meta, player_name)
local owner = meta:get_string("owner")
if player_name == owner or owner == "" then
return true
end
local privs = minetest.get_player_privs(player_name)
return privs.server or privs.protection_bypass
end
-- Tool wear aware replacement for contains_item.
function exchange_shop.list_contains_item(inv, listname, stack)
local count = stack:get_count()
if count == 0 then
return true
end
local list = inv:get_list(listname)
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.
function exchange_shop.list_remove_item(inv, listname, stack)
local wanted = stack:get_count()
if wanted == 0 then
return stack
end
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
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
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
function exchange_shop.exchange_action(player_inv, shop_inv)
if not shop_inv:is_empty("cust_ej")
or not shop_inv:is_empty("custm_ej") then
return "One or multiple ejection fields are filled. "..
"Please empty them or contact the shop owner."
end
local owner_wants = shop_inv:get_list("cust_ow")
local owner_gives = shop_inv:get_list("cust_og")
-- Check validness of stack "owner wants"
for i1, item1 in pairs(owner_wants) do
local name1 = item1:get_name()
for i2, item2 in pairs(owner_wants) do
if name1 == "" then
break
end
if i1 ~= i2 and name1 == item2:get_name() then
return "The field 'Owner needs' can not contain multiple "..
"times the same items. Please contact the shop owner."
end
end
end
-- Check validness of stack "owner gives"
for i1, item1 in pairs(owner_gives) do
local name1 = item1:get_name()
for i2, item2 in pairs(owner_gives) do
if name1 == "" then
break
end
if i1 ~= i2 and name1 == item2:get_name() then
return "The field 'Owner gives' can not contain multiple "..
"times the same items. Please contact the shop owner."
end
end
end
-- Check for space in the shop
for i, item in pairs(owner_wants) do
if not shop_inv:room_for_item("custm", item) then
return "The stock in this shop is full. "..
"Please contact the shop owner."
end
end
local list_contains_item = exchange_shop.list_contains_item
-- Check availability of the shop's items
for i, item in pairs(owner_gives) do
if not list_contains_item(shop_inv, "stock", item) then
return "This shop is sold out."
end
end
-- Check for space in the player's inventory
for i, item in pairs(owner_gives) do
if not player_inv:room_for_item("main", item) then
return "You do not have enough space in your inventory."
end
end
-- Check availability of the player's items
for i, item in pairs(owner_wants) do
if not list_contains_item(player_inv, "main", item) then
return "You do not have the required items."
end
end
local list_remove_item = exchange_shop.list_remove_item
-- Conditions are ok: (try to) exchange now
local fully_exchanged = true
for i, item in pairs(owner_wants) do
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
-- Move to ejection field
shop_inv:add_item("custm_ej", stack)
fully_exchanged = false
end
end
for i, item in pairs(owner_gives) do
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
-- Move to ejection field
shop_inv:add_item("cust_ej", stack)
fully_exchanged = false
end
end
if not fully_exchanged then
return "Warning! Stacks are overflowing somewhere!"
end
end

BIN
textures/shop_front.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 B

BIN
textures/shop_side.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 672 B

BIN
textures/shop_top.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 652 B