Skip to content

Commit 23773c4

Browse files
committed
Replace hotbar_itemcount by hotbar_source
1 parent 298a27c commit 23773c4

25 files changed

+576
-195
lines changed

doc/lua_api.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8494,7 +8494,7 @@ child will follow movement and rotation of that bone.
84948494
* `get_inventory()`: returns an `InvRef` for players, otherwise returns `nil`
84958495
* `get_wield_list()`: returns the name of the inventory list the wielded item
84968496
is in.
8497-
* `get_wield_index()`: returns the wield list index of the wielded item (starting with 1)
8497+
* `get_wield_index()`: returns the index of the wielded item in the wield list
84988498
* `get_wielded_item()`: returns a copy of the wielded item as an `ItemStack`
84998499
* `set_wielded_item(item)`: replaces the wielded item, returns `true` if
85008500
successful.
@@ -8870,8 +8870,16 @@ child will follow movement and rotation of that bone.
88708870
* `hud_set_hotbar_itemcount(count)`: sets number of items in builtin hotbar
88718871
* `count`: number of items, must be between `1` and `32`
88728872
* If `count` exceeds the `"main"` list size, the list size will be used instead.
8873-
* `hud_get_hotbar_itemcount()`: returns number of visible items
8874-
* This value is also clamped by the `"main"` list size.
8873+
* equal `set_hotbar_source({{list = "main", length = count}})`
8874+
* `hud_get_hotbar_itemcount()`: returns number of selectable items
8875+
* `get_hotbar_source()` returns used `hotbar_source`
8876+
* `set_hotbar_source({{list = "main", length = 6, offset = 24}, {list = "bag1", length = 4}, ...})`
8877+
* Sets inventory lists for the player to use in hotbar(s) and to select the wield item from.
8878+
`list` is a player inventory list
8879+
`length` is the amount of inventory slots
8880+
`offset` adjusts starting inventory position, 0 if not specified
8881+
* Note: Do not use this together with mods that rely on a fixed wield list and list size.
8882+
All mods should use `get_wield_list()` and `get_wield_index()` to get the wield position.
88758883
* `hud_set_hotbar_image(texturename)`
88768884
* sets background image for hotbar
88778885
* `hud_get_hotbar_image()`: returns texturename

games/devtest/mods/testhud/init.lua

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -212,63 +212,104 @@ core.register_chatcommand("zoomfov", {
212212

213213
local hud_hotbar_defs = {
214214
{
215-
type = "hotbar",
216-
position = {x=0.2, y=0.5},
217-
direction = 0,
218-
alignment = {x=1, y=-1},
219-
},
220-
{
221-
type = "hotbar",
222-
position = {x=0.2, y=0.5},
223-
direction = 2,
224-
alignment = {x=1, y=1},
215+
{
216+
type = "hotbar",
217+
position = {x=0.2, y=0.5},
218+
direction = 1,
219+
alignment = {x=1, y=-1},
220+
},
221+
{
222+
type = "hotbar",
223+
position = {x=0.2, y=0.5},
224+
direction = 3,
225+
alignment = {x=1, y=1},
226+
},
227+
{
228+
type = "hotbar",
229+
position = {x=0.7, y=0.5},
230+
direction = 0,
231+
offset = {x=140, y=20},
232+
alignment = {x=-1, y=-1},
233+
},
234+
{
235+
type = "hotbar",
236+
position = {x=0.7, y=0.5},
237+
direction = 2,
238+
offset = {x=140, y=20},
239+
alignment = {x=-1, y=1},
240+
},
225241
},
226242
{
227-
type = "hotbar",
228-
position = {x=0.7, y=0.5},
229-
direction = 0,
230-
offset = {x=140, y=20},
231-
alignment = {x=-1, y=-1},
232-
},
233-
{
234-
type = "hotbar",
235-
position = {x=0.7, y=0.5},
236-
direction = 2,
237-
offset = {x=140, y=20},
238-
alignment = {x=-1, y=1},
239-
},
243+
hotbar_source = {
244+
{list = "craft", length = 4, offset = 3},
245+
{list = "main", length = 5, offset = 0},
246+
{list = "main", length = 2, offset = 4},
247+
{list = "craft", length = 4, offset = 3},
248+
},
249+
hotbar_image = "default_stone.png^[opacity:150",
250+
{
251+
type = "hotbar",
252+
position = {x=0.5, y=0.8},
253+
direction = 1,
254+
},
255+
{
256+
type = "hotbar",
257+
position = {x=0.2, y=0.5},
258+
direction = 2,
259+
},
260+
{
261+
type = "hotbar",
262+
position = {x=0.8, y=0.5},
263+
direction = 3,
264+
},
265+
}
240266
}
241267

242268

243269
local player_hud_hotbars= {}
244270
core.register_chatcommand("hudhotbars", {
245271
description = "Shows some test Lua HUD elements of type hotbar. " ..
246-
"(add: Adds elements (default). remove: Removes elements)",
247-
params = "[ add | remove ]",
248-
func = function(name, params)
272+
"(Cycles between tests for: none, aligned all direction and offset," ..
273+
"changed hotbar_source all direction and hotbar image)",
274+
func = function(name)
249275
local player = core.get_player_by_name(name)
250276
if not player then
251277
return false, "No player."
252278
end
253279

254280
local id_table = player_hud_hotbars[name]
255281
if not id_table then
256-
id_table = {}
282+
id_table = {mode = 0}
257283
player_hud_hotbars[name] = id_table
258284
end
259285

260-
if params == "remove" then
261-
for _, id in ipairs(id_table) do
262-
player:hud_remove(id)
263-
end
286+
id_table.mode = (id_table.mode + 1) % (#hud_hotbar_defs + 1)
287+
288+
-- Reset
289+
for _, id in ipairs(id_table) do
290+
player:hud_remove(id)
291+
end
292+
player:hud_set_hotbar_itemcount(8)
293+
player:hud_set_hotbar_image("")
294+
295+
if id_table.mode == 0 then
264296
return true, "Hotbars removed."
265297
end
266298

267-
-- params == "add" or default
268-
for _, def in ipairs(hud_hotbar_defs) do
299+
for _, def in ipairs(hud_hotbar_defs[id_table.mode]) do
269300
table.insert(id_table, player:hud_add(def))
270301
end
271-
return true, #hud_hotbar_defs .." Hotbars added."
302+
303+
if hud_hotbar_defs[id_table.mode].hotbar_source then
304+
player:set_hotbar_source(hud_hotbar_defs[id_table.mode].hotbar_source)
305+
end
306+
307+
if hud_hotbar_defs[id_table.mode].hotbar_image then
308+
player:hud_set_hotbar_image(hud_hotbar_defs[id_table.mode].hotbar_image)
309+
end
310+
311+
312+
return true, #hud_hotbar_defs[id_table.mode] .." Hotbars added."
272313
end
273314
})
274315

games/devtest/mods/unittests/player.lua

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,32 @@ end
183183
unittests.register("test_player_add_pos", run_player_add_pos_tests, {player=true})
184184

185185
--
186-
-- Hotbar selection clamp
186+
-- Hotbar Source
187187
--
188-
local function run_player_hotbar_clamp_tests(player)
188+
189+
local function table_equals(t1, t2)
190+
local keys = {}
191+
local checked = {}
192+
for k, v in pairs(t1) do
193+
if type(v) == "table" then
194+
if not (checked[v] or table_equals(v, t2[k])) then
195+
return false
196+
end
197+
checked[v] = true
198+
elseif t2[k] ~= v then
199+
return false
200+
end
201+
keys[k] = true
202+
end
203+
for k, _ in pairs(t2) do
204+
if not keys[k] then
205+
return false
206+
end
207+
end
208+
return true
209+
end
210+
211+
local function run_player_hotbar_source_tests(player)
189212
local inv = player:get_inventory()
190213
local old_inv_size = inv:get_size("main")
191214
local old_inv_list = inv:get_list("main") -- Avoid accidentally removing item
@@ -195,15 +218,23 @@ local function run_player_hotbar_clamp_tests(player)
195218

196219
player:hud_set_hotbar_itemcount(2)
197220
assert(player:hud_get_hotbar_itemcount() == 2)
221+
assert(table_equals(player:get_hotbar_source(), {{list = "main", length = 2, offset = 0}}))
198222

223+
-- Test clamp
199224
player:hud_set_hotbar_itemcount(6)
200225
assert(player:hud_get_hotbar_itemcount() == 5)
226+
assert(table_equals(player:get_hotbar_source(), {{list = "main", length = 5, offset = 0}}))
227+
228+
local hotbar_source = {{list = "test", length = 4, offset = 2}, {list = "myinv", length = 16, offset = 99}}
229+
player:set_hotbar_source(hotbar_source)
230+
assert(table_equals(player:get_hotbar_source(), hotbar_source))
231+
assert(player:hud_get_hotbar_itemcount() == 20)
201232

202233
inv:set_size("main", old_inv_size)
203234
inv:set_list("main", old_inv_list)
204235
player:hud_set_hotbar_itemcount(old_bar_size)
205236
end
206-
unittests.register("test_player_hotbar_clamp", run_player_hotbar_clamp_tests, {player=true})
237+
unittests.register("test_player_hotbar_source", run_player_hotbar_source_tests, {player=true})
207238

208239
--
209240
-- Player GUID

src/client/game.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ void Game::processItemSelection(u16 *new_playeritem)
16371637
LocalPlayer *player = client->getEnv().getLocalPlayer();
16381638

16391639
*new_playeritem = player->getWieldIndex();
1640-
u16 max_item = player->getMaxHotbarItemcount();
1640+
u16 max_item = player->hotbar_source.getMaxLength();
16411641
if (max_item == 0)
16421642
return;
16431643
max_item -= 1;
@@ -1689,9 +1689,14 @@ void Game::dropSelectedItem(bool single_item)
16891689
IDropAction *a = new IDropAction();
16901690
a->count = single_item ? 1 : 0;
16911691
a->from_inv.setCurrentPlayer();
1692-
a->from_list = "main";
1693-
a->from_i = client->getEnv().getLocalPlayer()->getWieldIndex();
1694-
client->inventoryAction(a);
1692+
1693+
LocalPlayer *player = client->getEnv().getLocalPlayer();
1694+
u16 index;
1695+
if (player->hotbar_source.getInventoryFromWieldIndex(player->getWieldIndex(),
1696+
a->from_list, index)) {
1697+
a->from_i = index;
1698+
client->inventoryAction(a);
1699+
}
16951700
}
16961701

16971702
void Game::openConsole(float scale, const wchar_t *line)

0 commit comments

Comments
 (0)