1
0
Fork 0

Trading: Hide left/right button if end of list

objects
Wuzzy 2018-06-10 13:06:28 +02:00
parent cca841e459
commit 3e8ca5a1ca
1 changed files with 43 additions and 30 deletions

View File

@ -329,6 +329,21 @@ for id, _ in pairs(professions) do
table.insert(profession_names, id) table.insert(profession_names, id)
end end
local update_max_tradenum = function(self)
if not self._trades then
return
end
local trades = minetest.deserialize(self._trades)
for t=1, #trades do
local trade = trades[t]
if trade.tier > self._max_trade_tier then
self._max_tradenum = t - 1
return
end
end
self._max_tradenum = #trades
end
local init_profession = function(self) local init_profession = function(self)
if not self._profession then if not self._profession then
-- Select random profession from all professions with matching clothing -- Select random profession from all professions with matching clothing
@ -397,20 +412,16 @@ local set_trade = function(trader, player, inv, concrete_tradenum)
return return
end end
end end
local name = player:get_player_name()
if concrete_tradenum > #trades then -- Stop tradenum from advancing into locked tiers or out-of-range areas
concrete_tradenum = 1 if concrete_tradenum > trader._max_tradenum then
player_tradenum[player:get_player_name()] = concrete_tradenum concrete_tradenum = trader._max_tradenum
elseif concrete_tradenum < 1 then elseif concrete_tradenum < 1 then
concrete_tradenum = #trades
player_tradenum[player:get_player_name()] = concrete_tradenum
end
local trade = trades[concrete_tradenum]
if trader._max_trade_tier < trade.tier then
concrete_tradenum = 1 concrete_tradenum = 1
player_tradenum[player:get_player_name()] = concrete_tradenum
trade = trades[concrete_tradenum]
end end
player_tradenum[name] = concrete_tradenum
local trade = trades[concrete_tradenum]
inv:set_stack("wanted", 1, ItemStack(trade.wanted[1])) inv:set_stack("wanted", 1, ItemStack(trade.wanted[1]))
inv:set_stack("offered", 1, ItemStack(trade.offered)) inv:set_stack("offered", 1, ItemStack(trade.offered))
if trade.wanted[2] then if trade.wanted[2] then
@ -439,6 +450,17 @@ local function show_trade_formspec(playername, trader, tradenum)
end end
local tradeinv_name = "mobs_mc:trade_"..playername local tradeinv_name = "mobs_mc:trade_"..playername
local tradeinv = minetest.formspec_escape("detached:"..tradeinv_name) local tradeinv = minetest.formspec_escape("detached:"..tradeinv_name)
local b_prev, b_next = "", ""
if #trades > 1 then
if tradenum > 1 then
b_prev = "button[1,1;0.5,1;prev_trade;<]"
end
if tradenum < trader._max_tradenum then
b_next = "button[7.26,1;0.5,1;next_trade;>]"
end
end
local formspec = local formspec =
"size[9,8.75]" "size[9,8.75]"
.."background[-0.19,-0.25;9.41,9.49;mobs_mc_trading_formspec_bg.png]" .."background[-0.19,-0.25;9.41,9.49;mobs_mc_trading_formspec_bg.png]"
@ -447,8 +469,7 @@ local function show_trade_formspec(playername, trader, tradenum)
.."label[4,0;"..minetest.formspec_escape(profession).."]" .."label[4,0;"..minetest.formspec_escape(profession).."]"
.."list[current_player;main;0,4.5;9,3;9]" .."list[current_player;main;0,4.5;9,3;9]"
.."list[current_player;main;0,7.74;9,1;]" .."list[current_player;main;0,7.74;9,1;]"
.."button[1,1;0.5,1;prev_trade;<]" ..b_prev..b_next
.."button[7.26,1;0.5,1;next_trade;>]"
.."list["..tradeinv..";wanted;2,1;2,1;]" .."list["..tradeinv..";wanted;2,1;2,1;]"
.."list["..tradeinv..";offered;5.76,1;1,1;]" .."list["..tradeinv..";offered;5.76,1;1,1;]"
.."list["..tradeinv..";input;2,2.5;2,1;]" .."list["..tradeinv..";input;2,2.5;2,1;]"
@ -602,6 +623,7 @@ mobs:register_mob("mobs_mc:villager", {
if self._trades == nil then if self._trades == nil then
init_trades(self) init_trades(self)
end end
update_max_tradenum(self)
if self._trades == false then if self._trades == false then
-- Villager has no trades, rightclick is a no-op -- Villager has no trades, rightclick is a no-op
return return
@ -611,8 +633,7 @@ mobs:register_mob("mobs_mc:villager", {
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name}) local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
player_tradenum[name] = 1 set_trade(self, clicker, inv, 1)
set_trade(self, clicker, inv, player_tradenum[name], player_tradenum[name])
show_trade_formspec(name, self) show_trade_formspec(name, self)
end, end,
@ -669,7 +690,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.quit then if fields.quit then
return_fields(player) return_fields(player)
player_trading_with[name] = nil player_trading_with[name] = nil
elseif fields.next_trade then elseif fields.next_trade or fields.prev_trade then
local trader = player_trading_with[name] local trader = player_trading_with[name]
if not trader or not trader.object:get_luaentity() then if not trader or not trader.object:get_luaentity() then
return return
@ -678,23 +699,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if not trades then if not trades then
return return
end end
player_tradenum[name] = player_tradenum[name] + 1 local dir = 1
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name}) if fields.prev_trade then
set_trade(trader, player, inv, player_tradenum[name]) dir = -1
update_offer(inv, player, false)
show_trade_formspec(name, trader, player_tradenum[name])
elseif fields.prev_trade then
local trader = player_trading_with[name]
if not trader or not trader.object:get_luaentity() then
return
end end
local trades = trader._trades local tradenum = player_tradenum[name] + dir
if not trades then
return
end
player_tradenum[name] = player_tradenum[name] - 1
local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name}) local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
set_trade(trader, player, inv, player_tradenum[name]) set_trade(trader, player, inv, tradenum)
update_offer(inv, player, false) update_offer(inv, player, false)
show_trade_formspec(name, trader, player_tradenum[name]) show_trade_formspec(name, trader, player_tradenum[name])
end end
@ -835,6 +846,8 @@ local trade_inventory = {
-- First-time trade unlock all trades and unlock next trade tier -- First-time trade unlock all trades and unlock next trade tier
if trade.tier + 1 > trader._max_trade_tier then if trade.tier + 1 > trader._max_trade_tier then
trader._max_trade_tier = trader._max_trade_tier + 1 trader._max_trade_tier = trader._max_trade_tier + 1
update_max_tradenum(trader)
show_trade_formspec(name, trader, tradenum)
end end
for t=1, #trades do for t=1, #trades do
trades[t].locked = false trades[t].locked = false