backpacks implementation. TODO hotkey to open equipped backpack, upgrades for backpack

This commit is contained in:
Kostinatyn Tsiupa 2023-10-04 18:55:51 +03:00
parent 480215407f
commit 03568560a7
13 changed files with 226 additions and 1 deletions

View File

@ -0,0 +1,2 @@
Copy of Iron Backpacks https://github.com/gr8pefish/IronBackpacks
Art is used from there under GNU General Public License v3.0

202
mods/AGE0/backpack/init.lua Normal file
View File

@ -0,0 +1,202 @@
local S = minetest.get_translator("backpack")
local function getBackpackSize(stack)
if not stack:get_definition().groups.backpack_size then
return 0
end
return stack:get_definition().groups.backpack_size
end
local function openBackpack(player, player_name, backapack_stack, context--[[opened equipped or inhand]])
--minetest.chat_send_all("Enter")
local inv_name = player_name .. "_backpack_tmp"
--Chaeck if something is broken. To prevent item voiding
local inv = minetest.get_inventory({type="detached", name=inv_name})
if inv ~= nil then
return false, "Backpack is already open!"
end
minetest.create_detached_inventory(player_name .. "_backpack_tmp", {
--Forbids puting backpacks into backpacks
allow_put = function(inv, listname, index, stack, player)
if minetest.get_item_group(stack:get_name(), "backpack_size") > 0 then
return 0
else
return stack:get_stack_max()
end
end,
}, player_name)
inv = minetest.get_inventory({type = "detached", name=inv_name})
inv:set_size("main", getBackpackSize(backapack_stack) * 9)
local meta = backapack_stack:get_meta()
--minetest.chat_send_all(dump(meta:to_table()))
local content = meta:get_string("backpack:content")
local list = content and minetest.deserialize(content) or {}
for i,stack in ipairs(list) do
inv:set_stack("main", i, stack)
--[[if stack ~= nil then
minetest.chat_send_all(dump(stack:to_string()))
end]]
end
local form = "size[9," .. (getBackpackSize(backapack_stack) + 6) .. "]"
.. mcl_formspec.get_itemslot_bg(0, 0, 9, getBackpackSize(backapack_stack))
.. "list[detached:".. inv_name .. ";main;0,0;9," .. getBackpackSize(backapack_stack) .. ";]"
.. mcl_formspec.get_itemslot_bg(0, getBackpackSize(backapack_stack) + 1.5, 9, 3)
.. "list[current_player;main;0," .. (getBackpackSize(backapack_stack) + 1.5) .. ";9,3;9]"
.. mcl_formspec.get_itemslot_bg(0, getBackpackSize(backapack_stack) + 5, 9, 1)
.. "list[current_player;main;0," .. (getBackpackSize(backapack_stack) + 5) .. ";9,1;]"
.. "listring[current_player;main]"
.. "listring[detached:".. inv_name .. ";main]"
.. "listring[current_player;main]"
minetest.show_formspec(player_name, "backpack:" .. context, form)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "backpack:equipped" and fields.quit then
--minetest.chat_send_all("Exit")
local player_inv = player:get_inventory()
local backapack_stack = player_inv:get_stack("backpack", 1)
local inv_name = player:get_player_name() .. "_backpack_tmp"
local inv = minetest.get_inventory({type="detached", name=inv_name})
local stacks = {}
for i = 0, inv:get_size("main"), 1 do
stacks[i] = inv:get_stack("main", i):to_string()
end
backapack_stack:get_meta():set_string("backpack:content", minetest.serialize(stacks))
player_inv:set_stack("backpack", 1, backapack_stack)
--minetest.chat_send_all(dump(meta:to_table()))
minetest.remove_detached_inventory(inv_name)
elseif formname == "backpack:inhand" and fields.quit then
local backapack_stack = player:get_wielded_item()
local inv_name = player:get_player_name() .. "_backpack_tmp"
local inv = minetest.get_inventory({type="detached", name=inv_name})
local stacks = {}
for i = 0, inv:get_size("main"), 1 do
stacks[i] = inv:get_stack("main", i):to_string()
end
backapack_stack:get_meta():set_string("backpack:content", minetest.serialize(stacks))
player:set_wielded_item(backapack_stack)
minetest.remove_detached_inventory(inv_name)
else
return
end
end)
--A chat command for now. Wonna change to hot-key later.
minetest.register_chatcommand("bag", {
func = function(player_name)
local player = minetest.get_player_by_name(player_name)
local player_inv = player:get_inventory()
local backapack_stack = player_inv:get_stack("backpack", 1)
if backapack_stack:is_empty() then
return false, "No backpack equiped!"
end
openBackpack(player, player_name, backapack_stack, "equipped")
return true, ""
end,
})
local function on_backpack_place(itemstack, user, pointed_thing)
openBackpack(user, user:get_player_name(), itemstack, "inhand")
return nil
end
minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
if action == "move" and inventory_info.to_list == "backpack" then
local itemstack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
if not (minetest.get_item_group(itemstack:get_name(), "backpack_size") > 0) then
return 0
else
return itemstack:get_stack_max()
end
end
end)
-- register backapack's
minetest.register_tool("backpack:leather", {
description = S("Leather backpack"),
inventory_image = "backpack_leather.png",
groups = {backpack_size = 1},
on_place = on_backpack_place,
on_secondary_use = on_backpack_place,
})
minetest.register_craftitem("backpack:iron", {
description = S("Iron backpack"),
inventory_image = "backpack_iron.png",
groups = {backpack_size = 2},
on_place = on_backpack_place,
on_secondary_use = on_backpack_place,
})
minetest.register_craftitem("backpack:gold", {
description = S("Gold backpack"),
inventory_image = "backpack_gold.png",
groups = {backpack_size = 3},
on_place = on_backpack_place,
on_secondary_use = on_backpack_place,
})
minetest.register_craftitem("backpack:diamond", {
description = S("Diamond backpack"),
inventory_image = "backpack_diamond.png",
groups = {backpack_size = 4},
on_place = on_backpack_place,
on_secondary_use = on_backpack_place,
})
-- register bag crafts
minetest.register_craft({
output = "backpack:leather",
recipe = {
{"mcl_mobitems:leather", "mcl_mobitems:leather", "mcl_mobitems:leather"},
{"mcl_mobitems:leather", "mcl_chests:chest", "mcl_mobitems:leather"},
{"mcl_mobitems:leather", "mcl_mobitems:leather", "mcl_mobitems:leather"}
}
})
minetest.register_craft({
output = "backpack:iron",
recipe = {
{"mcl_core:iron_ingot", "mcl_core:iron_ingot", "mcl_core:iron_ingot"},
{"mcl_core:iron_ingot", "backpack:leather", "mcl_core:iron_ingot"},
{"mcl_core:iron_ingot", "mcl_core:iron_ingot", "mcl_core:iron_ingot"}
}
})
minetest.register_craft({
output = "backpack:gold",
recipe = {
{"mcl_core:gold_ingot", "mcl_core:gold_ingot", "mcl_core:gold_ingot"},
{"mcl_core:gold_ingot", "backpack:iron", "mcl_core:gold_ingot"},
{"mcl_core:gold_ingot", "mcl_core:gold_ingot", "mcl_core:gold_ingot"}
}
})
minetest.register_craft({
output = "backpack:diamond",
recipe = {
{"mcl_core:diamond", "mcl_core:diamond", "mcl_core:diamond"},
{"mcl_core:diamond", "backpack:gold", "mcl_core:diamond"},
{"mcl_core:diamond", "mcl_core:diamond", "mcl_core:diamond"}
}
})
--transfer content of backpack on craft
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if minetest.get_item_group(itemstack:get_name(), "backpack_size") > 0 then
content = old_craft_grid[5]:get_meta():get_string("backpack:content")
itemstack:get_meta():set_string("backpack:content", content)
return itemstack
end
end)

View File

@ -0,0 +1,6 @@
# textdomain: backpack
Leather backpack=Шкіряний наплічник
Iron backpack=Залізний наплічник
Gold backpack=Золотий наплічник
Diamond backpack=Діамантовий наплічник

View File

@ -0,0 +1,3 @@
name = backpack
author = Morik666
description = Mode to add backpacks

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

2
mods/AGE0/modpack.conf Normal file
View File

@ -0,0 +1,2 @@
name = AGE0
description = Mode-pack for age 0

View File

@ -100,6 +100,7 @@ minetest.register_on_joinplayer(function(player)
inv:set_width("main", 9) inv:set_width("main", 9)
inv:set_size("main", 36) inv:set_size("main", 36)
inv:set_size("offhand", 1) inv:set_size("offhand", 1)
inv:set_size("backpack", 1)
--set hotbar size --set hotbar size
player:hud_set_hotbar_itemcount(9) player:hud_set_hotbar_itemcount(9)

View File

@ -100,6 +100,10 @@ local main_page_static = table.concat({
--Player model background --Player model background
"image[1.57,0.343;3.62,4.85;mcl_inventory_background9.png;2]", "image[1.57,0.343;3.62,4.85;mcl_inventory_background9.png;2]",
--Backpack
mcl_formspec.get_itemslot_bg_v4(5.375, 2.875, 1, 1),
"list[current_player;backpack;5.375,2.875;1,1]",
--Offhand --Offhand
mcl_formspec.get_itemslot_bg_v4(5.375, 4.125, 1, 1), mcl_formspec.get_itemslot_bg_v4(5.375, 4.125, 1, 1),
@ -158,9 +162,14 @@ mcl_inventory.register_survival_inventory_tab({
end end
end end
if inv:get_stack("backpack", 1):is_empty() then
armor_slot_imgs = armor_slot_imgs .. "image[5.375,2.875;1,1;ta_inventory_empty_armor_slot_backpack.png]"
end
if inv:get_stack("offhand", 1):is_empty() then if inv:get_stack("offhand", 1):is_empty() then
armor_slot_imgs = armor_slot_imgs .. "image[5.375,4.125;1,1;mcl_inventory_empty_armor_slot_shield.png]" armor_slot_imgs = armor_slot_imgs .. "image[5.375,4.125;1,1;mcl_inventory_empty_armor_slot_shield.png]"
end end
return main_page_static .. armor_slot_imgs .. mcl_player.get_player_formspec_model(player, 1.57, 0.4, 3.62, 4.85, "") return main_page_static .. armor_slot_imgs .. mcl_player.get_player_formspec_model(player, 1.57, 0.4, 3.62, 4.85, "")
end, end,
handle = function() end, handle = function() end,

View File

@ -60,7 +60,7 @@ end
minetest.register_chatcommand("gamemode", { minetest.register_chatcommand("gamemode", {
params = S("[<gamemode>] [<player>]"), params = S("[<gamemode>] [<player>]"),
description = S("Change gamemode (survival/creative) for yourself or player"), description = S("Change gamemode (survival/creative) for yourself or player"),
privs = { server = true }, --privs = { server = true },
func = function(n, param) func = function(n, param)
-- Full input validation ( just for @erlehmann <3 ) -- Full input validation ( just for @erlehmann <3 )
local p = minetest.get_player_by_name(n) local p = minetest.get_player_by_name(n)

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 B