Skip to content

Commit f67fdfd

Browse files
committed
Replace hotbar_itemcount by hotbar_source
1 parent 4b85062 commit f67fdfd

File tree

23 files changed

+683
-189
lines changed

23 files changed

+683
-189
lines changed

doc/lua_api.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,10 @@ Displays a horizontal bar made up of half-images with an optional background.
18401840
* `direction`: Direction the list will be displayed in
18411841
* `offset`: offset in pixels from position.
18421842
* `alignment`: The alignment of the inventory.
1843+
* `world_pos`: Inventory of the hotbar depending on `hotbar_source`
1844+
`x` the index of the `hotbar_source` table to be used, if 0 use all next to each other
1845+
`y` an additional length to clamp the size, if 0 use whole length as defined in `hotbar_source`
1846+
`z` an additional offset to adjust where the displayed inventory starts
18431847

18441848
### `waypoint`
18451849

@@ -8270,7 +8274,7 @@ child will follow movement and rotation of that bone.
82708274
* `get_inventory()`: returns an `InvRef` for players, otherwise returns `nil`
82718275
* `get_wield_list()`: returns the name of the inventory list the wielded item
82728276
is in.
8273-
* `get_wield_index()`: returns the wield list index of the wielded item (starting with 1)
8277+
* `get_wield_index()`: returns the index of the wielded item in the wield list
82748278
* `get_wielded_item()`: returns a copy of the wielded item as an `ItemStack`
82758279
* `set_wielded_item(item)`: replaces the wielded item, returns `true` if
82768280
successful.
@@ -8632,8 +8636,16 @@ child will follow movement and rotation of that bone.
86328636
* `hud_set_hotbar_itemcount(count)`: sets number of items in builtin hotbar
86338637
* `count`: number of items, must be between `1` and `32`
86348638
* If `count` exceeds the `"main"` list size, the list size will be used instead.
8635-
* `hud_get_hotbar_itemcount()`: returns number of visible items
8636-
* This value is also clamped by the `"main"` list size.
8639+
* equal `set_hotbar_source({{list = "main", length = count}})`
8640+
* `hud_get_hotbar_itemcount()`: returns number of selectable items
8641+
* `get_hotbar_source()` returns used `hotbar_source`
8642+
* `set_hotbar_source({{list = "main", length = 6, offset = 24}, {list = "bag1", length = 4}, ...})`
8643+
* Sets inventory lists for the player to use in hotbar(s) and to select the wield item from.
8644+
`list` is a player inventory list
8645+
`length` is the amount of inventory slots
8646+
`offset` adjusts starting inventory position, 0 if not specified
8647+
* Note: Do not use this together with mods that relay on a fixed wield list and list size.
8648+
All mods should use `get_wield_list()` and `get_wield_index()` to get the wield position.
86378649
* `hud_set_hotbar_image(texturename)`
86388650
* sets background image for hotbar
86398651
* `hud_get_hotbar_image()`: returns texturename

games/devtest/mods/testhud/init.lua

Lines changed: 151 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -212,63 +212,182 @@ 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},
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+
},
219241
},
220242
{
221-
type = "hotbar",
222-
position = {x=0.2, y=0.5},
223-
direction = 2,
224-
alignment = {x=1, y=1},
225-
},
226-
{
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},
243+
hotbar_source = {
244+
{list = "craft", length = 4, offset = 3},
245+
{list = "main", length = 5, offset = 0},
246+
},
247+
hotbar_image = "default_stone.png^[opacity:150",
248+
{
249+
type = "hotbar",
250+
position = {x=0.5, y=0.2},
251+
world_pos = {x=1},
252+
},
253+
{
254+
type = "hotbar",
255+
position = {x=0.5, y=0.3},
256+
world_pos = {x=1, y=6},
257+
},
258+
{
259+
type = "hotbar",
260+
position = {x=0.5, y=0.4},
261+
world_pos = {x=1, z=5, y=3},
262+
},
263+
{
264+
type = "hotbar",
265+
position = {x=0.5, y=0.5},
266+
world_pos = {x=2},
267+
},
268+
{
269+
type = "hotbar",
270+
position = {x=0.5, y=0.6},
271+
world_pos = {x=2, z=1, y=3},
272+
},
273+
{
274+
type = "hotbar",
275+
position = {x=0.5, y=0.7},
276+
world_pos = {x=2},
277+
direction = 1,
278+
},
279+
{
280+
type = "hotbar",
281+
position = {x=0.5, y=0.8},
282+
world_pos = {x=2, z=1, y=3},
283+
direction = 1,
284+
},
285+
{
286+
type = "hotbar",
287+
position = {x=0.2, y=0.5},
288+
world_pos = {x=2, z=1, y=3},
289+
direction = 2,
290+
},
291+
{
292+
type = "hotbar",
293+
position = {x=0.8, y=0.5},
294+
world_pos = {x=2, z=1, y=3},
295+
direction = 3,
296+
},
232297
},
233298
{
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-
},
299+
hotbar_source = {
300+
{list = "craft", length = 4, offset = 3},
301+
{list = "main", length = 5, offset = 0},
302+
{list = "main", length = 2, offset = 4},
303+
{list = "craft", length = 4, offset = 3},
304+
},
305+
hotbar_image = "default_stone.png^[opacity:150",
306+
{
307+
type = "hotbar",
308+
position = {x=0.5, y=0.8},
309+
direction = 1,
310+
},
311+
{
312+
type = "hotbar",
313+
position = {x=0.5, y=0.7},
314+
world_pos = {z=1, y=10},
315+
direction = 1,
316+
},
317+
{
318+
type = "hotbar",
319+
position = {x=0.5, y=0.6},
320+
world_pos = {z=1, y=10},
321+
},
322+
{
323+
type = "hotbar",
324+
position = {x=0.5, y=0.5},
325+
world_pos = {z=17, y=5},
326+
},
327+
{
328+
type = "hotbar",
329+
position = {x=0.5, y=0.4},
330+
world_pos = {z=10, y=10},
331+
},
332+
{
333+
type = "hotbar",
334+
position = {x=0.2, y=0.5},
335+
direction = 2,
336+
},
337+
{
338+
type = "hotbar",
339+
position = {x=0.8, y=0.5},
340+
direction = 3,
341+
},
342+
}
240343
}
241344

242345

243346
local player_hud_hotbars= {}
244347
core.register_chatcommand("hudhotbars", {
245348
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)
349+
"(Cycles between: none, aligned using all direction and offset," ..
350+
"changed hotbar_source each only using a single inventory," ..
351+
"using multiple inventories (world_pos.x = 0))",
352+
func = function(name)
249353
local player = core.get_player_by_name(name)
250354
if not player then
251355
return false, "No player."
252356
end
253357

254358
local id_table = player_hud_hotbars[name]
255359
if not id_table then
256-
id_table = {}
360+
id_table = {mode = 0}
257361
player_hud_hotbars[name] = id_table
258362
end
259363

260-
if params == "remove" then
261-
for _, id in ipairs(id_table) do
262-
player:hud_remove(id)
263-
end
364+
id_table.mode = (id_table.mode + 1) % (#hud_hotbar_defs + 1)
365+
366+
-- Reset
367+
for _, id in ipairs(id_table) do
368+
player:hud_remove(id)
369+
end
370+
player:hud_set_hotbar_itemcount(8)
371+
player:hud_set_hotbar_image("")
372+
373+
if id_table.mode == 0 then
264374
return true, "Hotbars removed."
265375
end
266376

267-
-- params == "add" or default
268-
for _, def in ipairs(hud_hotbar_defs) do
377+
for _, def in ipairs(hud_hotbar_defs[id_table.mode]) do
269378
table.insert(id_table, player:hud_add(def))
270379
end
271-
return true, #hud_hotbar_defs .." Hotbars added."
380+
381+
if hud_hotbar_defs[id_table.mode].hotbar_source then
382+
player:set_hotbar_source(hud_hotbar_defs[id_table.mode].hotbar_source)
383+
end
384+
385+
if hud_hotbar_defs[id_table.mode].hotbar_image then
386+
player:hud_set_hotbar_image(hud_hotbar_defs[id_table.mode].hotbar_image)
387+
end
388+
389+
390+
return true, #hud_hotbar_defs[id_table.mode] .." Hotbars added."
272391
end
273392
})
274393

games/devtest/mods/unittests/player.lua

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,33 @@ 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+
checked[v] = true
202+
keys[k] = true
203+
end
204+
for k, _ in pairs(t2) do
205+
if not keys[k] then
206+
return false
207+
end
208+
end
209+
return true
210+
end
211+
212+
local function run_player_hotbar_source_tests(player)
189213
local inv = player:get_inventory()
190214
local old_inv_size = inv:get_size("main")
191215
local old_inv_list = inv:get_list("main") -- Avoid accidentally removing item
@@ -195,12 +219,22 @@ local function run_player_hotbar_clamp_tests(player)
195219

196220
player:hud_set_hotbar_itemcount(2)
197221
assert(player:hud_get_hotbar_itemcount() == 2)
222+
assert(table_equals(player:get_hotbar_source(), {{list = "main", length = 2, offset = 0}}))
198223

224+
-- Test clamp
199225
player:hud_set_hotbar_itemcount(6)
200226
assert(player:hud_get_hotbar_itemcount() == 5)
227+
assert(table_equals(player:get_hotbar_source(), {{list = "main", length = 5, offset = 0}}))
228+
229+
local hotbar_source = {{list = "test", length = 4, offset = 2}, {list = "myinv", length = 16, offset = 99}}
230+
print(dump(player:get_hotbar_source()))
231+
print(player:set_hotbar_source(hotbar_source))
232+
print(dump(player:get_hotbar_source()))
233+
assert(table_equals(player:get_hotbar_source(), hotbar_source))
234+
assert(player:hud_get_hotbar_itemcount() == 20)
201235

202236
inv:set_size("main", old_inv_size)
203237
inv:set_list("main", old_inv_list)
204238
player:hud_set_hotbar_itemcount(old_bar_size)
205239
end
206-
unittests.register("test_player_hotbar_clamp", run_player_hotbar_clamp_tests, {player=true})
240+
unittests.register("test_player_hotbar_source", run_player_hotbar_source_tests, {player=true})

src/client/game.cpp

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

19591959
*new_playeritem = player->getWieldIndex();
1960-
u16 max_item = player->getMaxHotbarItemcount();
1960+
u16 max_item = player->hotbar_source.getMaxLength();
19611961
if (max_item == 0)
19621962
return;
19631963
max_item -= 1;
@@ -2009,9 +2009,14 @@ void Game::dropSelectedItem(bool single_item)
20092009
IDropAction *a = new IDropAction();
20102010
a->count = single_item ? 1 : 0;
20112011
a->from_inv.setCurrentPlayer();
2012-
a->from_list = "main";
2013-
a->from_i = client->getEnv().getLocalPlayer()->getWieldIndex();
2014-
client->inventoryAction(a);
2012+
2013+
LocalPlayer* player = client->getEnv().getLocalPlayer();
2014+
u16 index;
2015+
if (player->hotbar_source.getInventoryFromWieldIndex(player->getWieldIndex(),
2016+
a->from_list, index)) {
2017+
a->from_i = index;
2018+
client->inventoryAction(a);
2019+
}
20152020
}
20162021

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

0 commit comments

Comments
 (0)