Skip to content

Commit

Permalink
lord_inventory: add inventory.update() API; class for inv main fo…
Browse files Browse the repository at this point in the history
…rm. Relates to lord-server#1020
  • Loading branch information
alek13 committed Apr 21, 2023
1 parent 437b871 commit 1b8a20b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 44 deletions.
12 changes: 11 additions & 1 deletion mods/lord/Player/lord_inventory/src/inventory.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

require("inventory.main_form")
local main_form = require("inventory.main_form")
require("inventory.bags_form")


inventory = {}

--- @param player Player
inventory.update = function(player)
local name = player:get_player_name()
local formspec = main_form.get_spec(name)
player:set_inventory_formspec(formspec)
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ return {
on_put = function(inv, list_name, index, stack, player)
player:get_inventory():set_stack(list_name, index, stack)
armor:set_player_armor(player)
armor:update_inventory(player)
inventory.update(player)
lottachievements.equip(stack, player, 1)
end,
on_take = function(inv, list_name, index, stack, player)
player:get_inventory():set_stack(list_name, index, nil)
armor:set_player_armor(player)
armor:update_inventory(player)
inventory.update(player)
lottachievements.equip(stack, player, -1)
end,
allow_put = function(inv, list_name, index, stack, player)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ return {
on_put = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, stack)
clothing:set_player_clothing(player)
clothing:update_inventory(player)
inventory.update(player)
end,
on_take = function(inv, listname, index, stack, player)
player:get_inventory():set_stack(listname, index, nil)
clothing:set_player_clothing(player)
clothing:update_inventory(player)
inventory.update(player)
end,
allow_put = function(inv, listname, index, stack, player)
local group_name = "clothes_" .. SLOT_PURPOSE[index]
Expand Down
54 changes: 35 additions & 19 deletions mods/lord/Player/lord_inventory/src/inventory/main_form.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
local detached_inv_armor_slots = require("inventory.main.armor_slots")
local detached_inv_clothing_slots = require("inventory.main.clothing_slots")
local detached_inv_trash = minetest.create_detached_inventory("armor_trash", {
allow_put = function(inv, listname, index, stack, player)
return stack:get_count()
end,
on_put = function(inv, listname, index, stack, player)
inv:set_stack(listname, index, "")
end,
})
detached_inv_trash:set_size("main", 1)


local formspec_template = "size[8,8.5]"
-- Armor slots
.. "image[0,0;1,1;lottarmor_helmet.png]"
.. "image[0,1;1,1;lottarmor_chestplate.png]"
.. "image[0,2;1,1;lottarmor_leggings.png]"
.. "image[0,3;1,1;lottarmor_boots.png]"
.. "list[detached:player_name_armor;armor;0,0;1,4;]"
-- Clothes slots
.. "image[3,0;1,1;lottarmor_helmet.png]"
.. "image[3,1;1,1;lottarmor_shirt.png]"
.. "image[3,2;1,1;lottarmor_trousers.png]"
.. "image[3,3;1,1;lottarmor_shoes.png]"
.. "image[4,0;1,1;lottarmor_cloak.png]"
.. "list[detached:player_name_armor;armor;0,0;1,4;]"
.. "list[detached:player_name_armor;armor;2,2;1,1;4]"
.. "list[detached:player_name_clothing;clothing;3,0;1,4;]"
.. "image[4,0;1,1;lottarmor_cloak.png]"
.. "list[detached:player_name_clothing;clothing;4,0;1,1;4]"
-- Preview & Shield
.. "image[1.16,0.25;2,4;armor_preview]"
.. "image[2,2;1,1;lottarmor_shield.png]"
.. "list[current_player;main;0,4.25;8,1;]"
.. "list[current_player;main;0,5.5;8,3;8]"
.. "list[detached:player_name_armor;armor;2,2;1,1;4]"
-- Crafting
.. "image[5.05,0;3.5,1;lottarmor_crafting.png]"
.. "list[current_player;craft;4,1;3,3;]"
.. "list[current_player;craftpreview;7,2;1,1;]"
.. "listring[current_player;main]"
.. "listring[current_player;craft]"
-- Bags & Trash
.. "image_button[7,1;1,1;bags.png;bags;]"
.. "image[7,3;1,1;lottarmor_trash.png]"
.. "list[detached:armor_trash;main;7,3;1,1;]"
.. "image_button[7,1;1,1;bags.png;bags;]"
-- Player inventory content
.. "list[current_player;main;0,4.25;8,1;]"
.. "list[current_player;main;0,5.5;8,3;8]"
-- options
.. "listring[current_player;main]"
.. "listring[current_player;craft]"

---
--- @class inventory.main.Form
---
local Form = {

}

armor.get_armor_formspec = function(player_name)
Form.get_spec = function(player_name)
if not armor.textures[player_name] then
minetest.log("error", "lottarmor: Player texture[" .. player_name .. "] is nil [get_armor_formspec]")
return ""
Expand All @@ -43,16 +67,6 @@ armor.get_armor_formspec = function(player_name)
return formspec
end

--Trash
local trash = minetest.create_detached_inventory("armor_trash", {
allow_put = function(inv, listname, index, stack, player)
return stack:get_count()
end,
on_put = function(inv, listname, index, stack, player)
inv:set_stack(listname, index, "")
end,
})
trash:set_size("main", 1)

--- @param joined_player Player
minetest.register_on_joinplayer(function(joined_player, last_login)
Expand All @@ -75,3 +89,5 @@ minetest.register_on_joinplayer(function(joined_player, last_login)
clothing_inv:set_stack("clothing", i, stack)
end
end)

return Form
4 changes: 2 additions & 2 deletions mods/lord/bones/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ minetest.register_on_dieplayer(function(player)
clear_inventory_list(armor_inv, "armor")
end
armor:set_player_armor(player)
armor:update_inventory(player)
inventory.update(player)

return
end
Expand All @@ -210,7 +210,7 @@ minetest.register_on_dieplayer(function(player)
clear_inventory_list(armor_inv, "armor")
end
armor:set_player_armor(player)
armor:update_inventory(player)
inventory.update(player)

corpse_meta:set_string("formspec", bones_formspec)
if publish_after < 0 then -- все трупы - достояние общественности
Expand Down
14 changes: 2 additions & 12 deletions mods/lord/lottarmor/armor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ local handle_armor_wear = function(player)
minetest.chat_send_player(name, desc.." "..SL("got destroyed!"))
end
armor:set_player_armor(player)
armor:update_inventory(player)
inventory.update(player)
end
heal_max = heal_max + heal
end
Expand All @@ -161,16 +161,6 @@ local handle_armor_wear = function(player)
armor.player_hp[name] = hp
end

armor.update_inventory = function(self, player)
local name = armor.get_valid_player(player, "[set_player_armor]")
if not name then
return
end

local formspec = armor.get_armor_formspec(name)
player:set_inventory_formspec(formspec)
end

function armor.get_valid_player(player, msg)
msg = msg or ""
if not player then
Expand Down Expand Up @@ -222,7 +212,7 @@ races.register_init_callback(function(name, race, gender, skin, texture, face)

minetest.after(ARMOR_INIT_DELAY, function(player)
armor:set_player_armor(player)
armor:update_inventory(player)
inventory.update(player)
end, joined_player)
end)

Expand Down
7 changes: 1 addition & 6 deletions mods/lord/lottarmor/clothing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ clothing.set_player_clothing = function(self, player)
multiskin:update_player_visuals(player)
end

clothing.update_inventory = function(self, player)
local name = player:get_player_name()
local formspec = armor.get_armor_formspec(name)
player:set_inventory_formspec(formspec)
end


races.register_init_callback(function(name, race, gender, skin, texture, face)
Expand All @@ -47,6 +42,6 @@ races.register_init_callback(function(name, race, gender, skin, texture, face)

minetest.after(ARMOR_INIT_DELAY, function(player)
clothing:set_player_clothing(player)
clothing:update_inventory(player)
inventory.update(player)
end, joined_player)
end)

0 comments on commit 1b8a20b

Please sign in to comment.