2021-10-21 02:08:59 +02:00
|
|
|
|
|
|
|
|
2022-01-08 09:20:01 +01:00
|
|
|
-- Copyright (C) 2021 2022 GNU Hacker
|
2021-10-21 02:08:59 +02:00
|
|
|
|
|
|
|
-- This file is part of Emeraldbank Minetest Mod.
|
|
|
|
|
|
|
|
-- Emeraldbank is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
|
|
|
|
-- Emeraldbank is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with Emeraldbank. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local S = core.get_translator(core.get_current_modname())
|
|
|
|
|
2021-12-16 18:21:56 +01:00
|
|
|
local shopcraft = core.settings:get_bool("emeraldbank.shop_craft") or true
|
|
|
|
|
2021-12-16 19:41:21 +01:00
|
|
|
local shop_timer = 5
|
|
|
|
|
2021-10-21 02:08:59 +02:00
|
|
|
local stock_h = 3
|
|
|
|
local stock_w = 5
|
|
|
|
|
|
|
|
local formspec_prefix = "emeraldbank:shop_formspec"
|
|
|
|
|
|
|
|
emeraldbank.player_inv =
|
|
|
|
"list[current_player;main;0,4.5;9,3;9]"..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,4.5,9,3)..
|
|
|
|
"list[current_player;main;0,7.74;9,1;]"..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,7.74,9,1)..
|
|
|
|
"listring[current_player;main]"
|
|
|
|
|
|
|
|
|
|
|
|
function emeraldbank.get_shop_fs(pos, clicker)
|
|
|
|
local meta = core.get_meta(pos)
|
2021-12-16 18:21:56 +01:00
|
|
|
local count = meta:get_int("count")
|
2021-10-21 02:08:59 +02:00
|
|
|
local price = meta:get_int("price")
|
|
|
|
local shop_item = meta:get_string("shop_item")
|
|
|
|
local list_name = "nodemeta:"..pos.x..','..pos.y..','..pos.z
|
|
|
|
local pname = clicker:get_player_name()
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
local player_press_key = clicker:get_player_control().aux1
|
|
|
|
local shop_fs = ""
|
2021-12-16 18:44:20 +01:00
|
|
|
local is_admin = core.check_player_privs(pname, {server=true})
|
|
|
|
if (pname == owner or is_admin) and not player_press_key then
|
2021-10-21 02:08:59 +02:00
|
|
|
shop_fs = "size[9,8.75]"..
|
|
|
|
"label[0,0;"..S("Your stock:").."]"..
|
|
|
|
"list["..list_name..";stock;0,0.5;"..stock_w..","..stock_h..";]"..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,0.5,stock_w,stock_h)..
|
2021-12-16 18:21:56 +01:00
|
|
|
"field[6.5,2;2,1;count;"..S("Count")..";"..count.."]"..
|
2021-10-21 02:08:59 +02:00
|
|
|
"field[6.5,3;2,1;price;"..S("Price")..";"..price.."]"..
|
|
|
|
"label[6,0;"..S("In exchange, you give:").."]"..
|
|
|
|
"item_image[6.5,0.5;1,1;"..shop_item.."]"..
|
|
|
|
mcl_formspec.get_itemslot_bg(6.5,0.5,1,1)..
|
|
|
|
"label[0,4;"..S("Owner, Use (special key + right mouse button) for customer interface").."]"..
|
|
|
|
emeraldbank.player_inv
|
|
|
|
else
|
|
|
|
shop_fs = "size[9,8.75]"..
|
|
|
|
"label[3.7,0;"..S("Owner gives:").."]"..
|
|
|
|
"item_image[4,1;1,1;"..shop_item.."]"..
|
|
|
|
mcl_formspec.get_itemslot_bg(4,1,1,1)..
|
2021-12-16 18:21:56 +01:00
|
|
|
"label[5,1.5;"..S("x @1", count).."]"..
|
2021-10-21 02:08:59 +02:00
|
|
|
"label[4,2;"..S("Price: @1", price).."]"..
|
|
|
|
"button[3.5,3;2,1;exchange;"..S("Exchange").."]"..
|
|
|
|
emeraldbank.player_inv
|
|
|
|
end
|
|
|
|
|
|
|
|
return shop_fs
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function set_item(pos, listname, index, stack, player)
|
|
|
|
local meta = core.get_meta(pos)
|
|
|
|
local itemname = stack:get_name()
|
|
|
|
local itemcount = stack:get_count()
|
|
|
|
meta:set_string("shop_item", itemname)
|
|
|
|
core.show_formspec(player:get_player_name(), formspec_prefix..core.pos_to_string(pos), emeraldbank.get_shop_fs(pos, player) )
|
|
|
|
end
|
|
|
|
|
2021-10-21 19:06:05 +02:00
|
|
|
local function check_empty(pos, listname, index, stack, player)
|
2021-10-21 02:08:59 +02:00
|
|
|
local meta = core.get_meta(pos)
|
2021-10-21 19:06:05 +02:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
if inv:is_empty("stock") then
|
|
|
|
meta:set_string("shop_item", "")
|
|
|
|
end
|
|
|
|
core.show_formspec(player:get_player_name(), formspec_prefix..core.pos_to_string(pos), emeraldbank.get_shop_fs(pos, player) )
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_stonks(pos, player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local meta = core.get_meta(pos)
|
|
|
|
local owner = meta:get_string("owner")
|
2021-10-21 02:08:59 +02:00
|
|
|
local stonks = meta:get_int("stonks")
|
|
|
|
if name == owner and stonks > 0 then
|
2021-12-16 20:19:08 +01:00
|
|
|
core.sound_play("cash", {
|
|
|
|
to_player = name,
|
|
|
|
gain = 1.0,
|
|
|
|
fade = 0.0,
|
|
|
|
pitch = 1.0,
|
|
|
|
})
|
2021-10-21 19:06:05 +02:00
|
|
|
emeraldbank.add_emeralds(player, stonks)
|
2021-10-21 02:08:59 +02:00
|
|
|
meta:set_int("stonks", 0)
|
2021-12-16 20:26:54 +01:00
|
|
|
core.chat_send_player(name, S("You've earned @1 Emeralds with your shops.", stonks) )
|
2021-10-21 02:08:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- register shop node
|
|
|
|
core.register_node("emeraldbank:shop", {
|
|
|
|
description = S("Shop"),
|
|
|
|
_doc_items_longdesc = S("A shop to sell your items with emeralds."),
|
|
|
|
is_ground_content = false,
|
|
|
|
tiles = {
|
|
|
|
"default_tree.png",
|
|
|
|
"default_tree.png",
|
|
|
|
"default_tree.png^mcl_core_emerald.png"
|
|
|
|
},
|
|
|
|
stack_max = 64,
|
|
|
|
groups = {axey=1, handy=1, building_block=1, enderman_takable=1},
|
|
|
|
sounds = mcl_sounds.node_sound_wood_defaults(),
|
|
|
|
_mcl_blast_resistance = 5,
|
|
|
|
_mcl_hardness = 1,
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer, itemstack)
|
|
|
|
local owner = placer:get_player_name()
|
|
|
|
local meta = core.get_meta(pos)
|
|
|
|
meta:set_string("infotext", S("Exchange shop (owned by @1)", owner))
|
|
|
|
meta:set_string("owner", owner)
|
2021-12-16 19:47:35 +01:00
|
|
|
meta:set_int("count", 10) -- default count
|
|
|
|
meta:set_int("price", 5) -- default price
|
2021-12-16 19:41:21 +01:00
|
|
|
core.get_node_timer(pos):start(shop_timer)
|
2021-10-21 02:08:59 +02:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size("stock", stock_w*stock_h)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_rightclick = function(pos, node, clicker, itemstack)
|
2021-12-16 18:44:20 +01:00
|
|
|
local pname = clicker:get_player_name()
|
2021-10-21 02:08:59 +02:00
|
|
|
get_stonks(pos, clicker)
|
2021-12-16 18:44:20 +01:00
|
|
|
core.show_formspec(pname, formspec_prefix..core.pos_to_string(pos), emeraldbank.get_shop_fs(pos, clicker) )
|
2021-10-21 02:08:59 +02:00
|
|
|
end,
|
2021-10-21 19:06:05 +02:00
|
|
|
|
|
|
|
on_punch = function(pos, node, puncher, pointed_thing)
|
|
|
|
get_stonks(pos, puncher)
|
|
|
|
end,
|
2021-10-21 02:08:59 +02:00
|
|
|
|
|
|
|
on_metadata_inventory_put = set_item,
|
2021-10-21 19:06:05 +02:00
|
|
|
|
|
|
|
on_metadata_inventory_take = check_empty,
|
2021-12-16 19:41:21 +01:00
|
|
|
|
|
|
|
on_timer = function(pos, elapsed)
|
|
|
|
local meta = core.get_meta(pos)
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
local is_online = core.player_exists(owner)
|
2021-12-16 20:24:40 +01:00
|
|
|
core.get_node_timer(pos):start(shop_timer)
|
2021-12-16 19:41:21 +01:00
|
|
|
if is_online then
|
|
|
|
local player = core.get_player_by_name(owner)
|
|
|
|
if not player or player.is_fake_player then return end
|
|
|
|
get_stonks(pos, player)
|
|
|
|
end
|
|
|
|
end,
|
2021-10-21 02:08:59 +02:00
|
|
|
|
2021-10-21 19:06:05 +02:00
|
|
|
can_dig = function(pos, player)
|
2021-12-16 18:47:43 +01:00
|
|
|
local pname = player:get_player_name()
|
|
|
|
local is_admin = core.check_player_privs(pname, {server=true})
|
2021-10-21 02:08:59 +02:00
|
|
|
local meta = core.get_meta(pos)
|
2021-10-21 19:06:05 +02:00
|
|
|
local owner = meta:get_string("owner")
|
2021-10-21 02:08:59 +02:00
|
|
|
local inv = meta:get_inventory()
|
2021-12-16 18:47:43 +01:00
|
|
|
if inv:is_empty("stock") and (pname == owner or is_admin) then
|
2021-10-21 19:06:05 +02:00
|
|
|
return true
|
2021-10-21 02:08:59 +02:00
|
|
|
end
|
2021-10-21 19:06:05 +02:00
|
|
|
end
|
2021-10-21 02:08:59 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
core.register_on_player_receive_fields(function(sender, formname, fields)
|
|
|
|
local prefix_len = string.len(formspec_prefix)
|
|
|
|
if formname:sub(1,prefix_len) == formspec_prefix then
|
|
|
|
|
|
|
|
local pos_string = formname:sub(prefix_len+1)
|
|
|
|
local pos = core.string_to_pos(pos_string)
|
|
|
|
local meta = core.get_meta(pos)
|
|
|
|
local name = sender:get_player_name()
|
|
|
|
local playermeta = sender:get_meta()
|
|
|
|
local player_pos = sender:get_pos()
|
2021-12-16 18:21:56 +01:00
|
|
|
local old_count = meta:get_int("count")
|
|
|
|
local new_count = tonumber(fields.count)
|
2021-10-21 02:08:59 +02:00
|
|
|
local old_price = meta:get_int("price")
|
|
|
|
local new_price = tonumber(fields.price)
|
|
|
|
local shop_item = meta:get_string("shop_item")
|
|
|
|
local minv = meta:get_inventory()
|
|
|
|
local pinv = sender:get_inventory()
|
|
|
|
local bankemeralds = playermeta:get_int("emeraldbank:emerald")
|
2021-12-16 19:41:21 +01:00
|
|
|
|
|
|
|
-- set or reset timer
|
|
|
|
core.get_node_timer(pos):start(shop_timer)
|
2021-10-21 02:08:59 +02:00
|
|
|
|
2021-12-16 18:21:56 +01:00
|
|
|
if fields.count and string.find(fields.count, "^[0-9]+$") then
|
|
|
|
if new_count >= 1 and new_count <= 64 and new_count ~= meta:get_int("count") then
|
|
|
|
meta:set_int("count", new_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-21 02:08:59 +02:00
|
|
|
if fields.price and string.find(fields.price, "^[0-9]+$") then
|
|
|
|
if new_price >= 1 and new_price <= 10000 and new_price ~= meta:get_int("price") then
|
|
|
|
meta:set_int("price", new_price)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.exchange ~= nil and fields.exchange ~= "" then
|
|
|
|
|
|
|
|
if meta:get_string("owner") == name then
|
|
|
|
|
|
|
|
core.chat_send_player(name, S("This is your own shop, you can't exchange to yourself!"))
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
local can_exchange = true
|
|
|
|
|
|
|
|
if bankemeralds < old_price then
|
|
|
|
can_exchange = false
|
|
|
|
core.chat_send_player(name, S("Not enough Emeralds in your account") )
|
|
|
|
end
|
|
|
|
|
2021-12-16 18:21:56 +01:00
|
|
|
if not minv:contains_item("stock", shop_item.." "..old_count, true) then
|
2021-10-21 02:08:59 +02:00
|
|
|
can_exchange = false
|
|
|
|
core.chat_send_player(name, S("Out of Stock!") )
|
|
|
|
end
|
|
|
|
|
|
|
|
if can_exchange then
|
2021-12-16 18:21:56 +01:00
|
|
|
minv:remove_item("stock", shop_item.." "..old_count)
|
|
|
|
core.add_item(player_pos, shop_item.." "..old_count)
|
2021-10-21 02:08:59 +02:00
|
|
|
emeraldbank.add_emeralds(sender, -old_price)
|
|
|
|
meta:set_int("stonks", meta:get_int("stonks")+old_price)
|
|
|
|
core.chat_send_player(name, S("Exchanged!"))
|
|
|
|
core.show_formspec(sender:get_player_name(), formspec_prefix..core.pos_to_string(pos), emeraldbank.get_shop_fs(pos, sender) )
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end)
|
2021-12-16 18:21:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
if shopcraft then
|
|
|
|
core.register_craft({
|
|
|
|
output = "emeraldbank:shop 3",
|
|
|
|
recipe = {
|
|
|
|
{"mcl_core:emerald", "mcl_core:emerald", "mcl_core:emerald"},
|
|
|
|
{"mcl_core:emerald", "mcl_core:tree", "mcl_core:emerald"},
|
|
|
|
{"mcl_core:emerald", "mcl_core:emerald", "mcl_core:emerald"},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|