Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8494,7 +8494,7 @@ child will follow movement and rotation of that bone.
* `get_inventory()`: returns an `InvRef` for players, otherwise returns `nil`
* `get_wield_list()`: returns the name of the inventory list the wielded item
is in.
* `get_wield_index()`: returns the wield list index of the wielded item (starting with 1)
* `get_wield_index()`: returns the index of the wielded item in the wield list
* `get_wielded_item()`: returns a copy of the wielded item as an `ItemStack`
* `set_wielded_item(item)`: replaces the wielded item, returns `true` if
successful.
Expand Down Expand Up @@ -8870,8 +8870,16 @@ child will follow movement and rotation of that bone.
* `hud_set_hotbar_itemcount(count)`: sets number of items in builtin hotbar
* `count`: number of items, must be between `1` and `32`
* If `count` exceeds the `"main"` list size, the list size will be used instead.
* `hud_get_hotbar_itemcount()`: returns number of visible items
* This value is also clamped by the `"main"` list size.
* equal `set_hotbar_source({{list = "main", length = count}})`
* `hud_get_hotbar_itemcount()`: returns number of selectable items
* `get_hotbar_source()` returns used `hotbar_source`
* `set_hotbar_source({{list = "main", length = 6, offset = 24}, {list = "bag1", length = 4}, ...})`
* Sets inventory lists for the player to use in hotbar(s) and to select the wield item from.
`list` is a player inventory list
`length` is the amount of inventory slots
`offset` adjusts starting inventory position, 0 if not specified
* Note: Do not use this together with mods that rely on a fixed wield list and list size.
All mods should use `get_wield_list()` and `get_wield_index()` to get the wield position.
* `hud_set_hotbar_image(texturename)`
* sets background image for hotbar
* `hud_get_hotbar_image()`: returns texturename
Expand Down
109 changes: 75 additions & 34 deletions games/devtest/mods/testhud/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,63 +212,104 @@ core.register_chatcommand("zoomfov", {

local hud_hotbar_defs = {
{
type = "hotbar",
position = {x=0.2, y=0.5},
direction = 0,
alignment = {x=1, y=-1},
},
{
type = "hotbar",
position = {x=0.2, y=0.5},
direction = 2,
alignment = {x=1, y=1},
{
type = "hotbar",
position = {x=0.2, y=0.5},
direction = 1,
alignment = {x=1, y=-1},
},
{
type = "hotbar",
position = {x=0.2, y=0.5},
direction = 3,
alignment = {x=1, y=1},
},
{
type = "hotbar",
position = {x=0.7, y=0.5},
direction = 0,
offset = {x=140, y=20},
alignment = {x=-1, y=-1},
},
{
type = "hotbar",
position = {x=0.7, y=0.5},
direction = 2,
offset = {x=140, y=20},
alignment = {x=-1, y=1},
},
},
{
type = "hotbar",
position = {x=0.7, y=0.5},
direction = 0,
offset = {x=140, y=20},
alignment = {x=-1, y=-1},
},
{
type = "hotbar",
position = {x=0.7, y=0.5},
direction = 2,
offset = {x=140, y=20},
alignment = {x=-1, y=1},
},
hotbar_source = {
{list = "craft", length = 4, offset = 3},
{list = "main", length = 5, offset = 0},
{list = "main", length = 2, offset = 4},
{list = "craft", length = 4, offset = 3},
},
hotbar_image = "default_stone.png^[opacity:150",
{
type = "hotbar",
position = {x=0.5, y=0.8},
direction = 1,
},
{
type = "hotbar",
position = {x=0.2, y=0.5},
direction = 2,
},
{
type = "hotbar",
position = {x=0.8, y=0.5},
direction = 3,
},
}
}


local player_hud_hotbars= {}
core.register_chatcommand("hudhotbars", {
description = "Shows some test Lua HUD elements of type hotbar. " ..
"(add: Adds elements (default). remove: Removes elements)",
params = "[ add | remove ]",
func = function(name, params)
"(Cycles between tests for: none, aligned all direction and offset," ..
"changed hotbar_source all direction and hotbar image)",
func = function(name)
local player = core.get_player_by_name(name)
if not player then
return false, "No player."
end

local id_table = player_hud_hotbars[name]
if not id_table then
id_table = {}
id_table = {mode = 0}
player_hud_hotbars[name] = id_table
end

if params == "remove" then
for _, id in ipairs(id_table) do
player:hud_remove(id)
end
id_table.mode = (id_table.mode + 1) % (#hud_hotbar_defs + 1)

-- Reset
for _, id in ipairs(id_table) do
player:hud_remove(id)
end
player:hud_set_hotbar_itemcount(8)
player:hud_set_hotbar_image("")

if id_table.mode == 0 then
return true, "Hotbars removed."
end

-- params == "add" or default
for _, def in ipairs(hud_hotbar_defs) do
for _, def in ipairs(hud_hotbar_defs[id_table.mode]) do
table.insert(id_table, player:hud_add(def))
end
return true, #hud_hotbar_defs .." Hotbars added."

if hud_hotbar_defs[id_table.mode].hotbar_source then
player:set_hotbar_source(hud_hotbar_defs[id_table.mode].hotbar_source)
end

if hud_hotbar_defs[id_table.mode].hotbar_image then
player:hud_set_hotbar_image(hud_hotbar_defs[id_table.mode].hotbar_image)
end


return true, #hud_hotbar_defs[id_table.mode] .." Hotbars added."
end
})

Expand Down
37 changes: 34 additions & 3 deletions games/devtest/mods/unittests/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,32 @@ end
unittests.register("test_player_add_pos", run_player_add_pos_tests, {player=true})

--
-- Hotbar selection clamp
-- Hotbar Source
--
local function run_player_hotbar_clamp_tests(player)

local function table_equals(t1, t2)
local keys = {}
local checked = {}
for k, v in pairs(t1) do
if type(v) == "table" then
if not (checked[v] or table_equals(v, t2[k])) then
return false
end
checked[v] = true
elseif t2[k] ~= v then
return false
end
keys[k] = true
end
for k, _ in pairs(t2) do
if not keys[k] then
return false
end
end
return true
end

local function run_player_hotbar_source_tests(player)
local inv = player:get_inventory()
local old_inv_size = inv:get_size("main")
local old_inv_list = inv:get_list("main") -- Avoid accidentally removing item
Expand All @@ -195,15 +218,23 @@ local function run_player_hotbar_clamp_tests(player)

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

-- Test clamp
player:hud_set_hotbar_itemcount(6)
assert(player:hud_get_hotbar_itemcount() == 5)
assert(table_equals(player:get_hotbar_source(), {{list = "main", length = 5, offset = 0}}))

local hotbar_source = {{list = "test", length = 4, offset = 2}, {list = "myinv", length = 16, offset = 99}}
player:set_hotbar_source(hotbar_source)
assert(table_equals(player:get_hotbar_source(), hotbar_source))
assert(player:hud_get_hotbar_itemcount() == 20)

inv:set_size("main", old_inv_size)
inv:set_list("main", old_inv_list)
player:hud_set_hotbar_itemcount(old_bar_size)
end
unittests.register("test_player_hotbar_clamp", run_player_hotbar_clamp_tests, {player=true})
unittests.register("test_player_hotbar_source", run_player_hotbar_source_tests, {player=true})

--
-- Player GUID
Expand Down
13 changes: 9 additions & 4 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,7 @@ void Game::processItemSelection(u16 *new_playeritem)
LocalPlayer *player = client->getEnv().getLocalPlayer();

*new_playeritem = player->getWieldIndex();
u16 max_item = player->getMaxHotbarItemcount();
u16 max_item = player->hotbar_source.getMaxLength();
if (max_item == 0)
return;
max_item -= 1;
Expand Down Expand Up @@ -1689,9 +1689,14 @@ void Game::dropSelectedItem(bool single_item)
IDropAction *a = new IDropAction();
a->count = single_item ? 1 : 0;
a->from_inv.setCurrentPlayer();
a->from_list = "main";
a->from_i = client->getEnv().getLocalPlayer()->getWieldIndex();
client->inventoryAction(a);

LocalPlayer *player = client->getEnv().getLocalPlayer();
u16 index;
if (player->hotbar_source.getInventoryFromWieldIndex(player->getWieldIndex(),
a->from_list, index)) {
a->from_i = index;
client->inventoryAction(a);
}
}

void Game::openConsole(float scale, const wchar_t *line)
Expand Down
Loading
Loading