2019-03-07 23:10:39 +03:00
|
|
|
local S = minetest.get_translator("mcl_inventory")
|
2019-03-14 03:50:29 +03:00
|
|
|
local F = minetest.formspec_escape
|
2019-03-07 23:10:39 +03:00
|
|
|
|
2017-03-09 19:16:50 +03:00
|
|
|
mcl_inventory = {}
|
|
|
|
|
2020-02-18 17:44:54 +03:00
|
|
|
local show_armor = minetest.get_modpath("mcl_armor") ~= nil
|
2019-03-05 03:50:51 +03:00
|
|
|
local mod_player = minetest.get_modpath("mcl_player") ~= nil
|
2019-03-13 00:03:21 +03:00
|
|
|
local mod_craftguide = minetest.get_modpath("mcl_craftguide") ~= nil
|
2017-08-03 20:21:31 +03:00
|
|
|
|
2017-07-25 19:00:49 +03:00
|
|
|
-- Returns a single itemstack in the given inventory to the main inventory, or drop it when there's no space left
|
2020-11-25 14:47:27 +03:00
|
|
|
function return_item(itemstack, dropper, pos, inv)
|
2017-01-06 01:37:40 +03:00
|
|
|
if dropper:is_player() then
|
2017-07-25 19:00:49 +03:00
|
|
|
-- Return to main inventory
|
|
|
|
if inv:room_for_item("main", itemstack) then
|
|
|
|
inv:add_item("main", itemstack)
|
|
|
|
else
|
|
|
|
-- Drop item on the ground
|
|
|
|
local v = dropper:get_look_dir()
|
|
|
|
local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
|
|
|
|
p.x = p.x+(math.random(1,3)*0.2)
|
|
|
|
p.z = p.z+(math.random(1,3)*0.2)
|
|
|
|
local obj = minetest.add_item(p, itemstack)
|
|
|
|
if obj then
|
|
|
|
v.x = v.x*4
|
|
|
|
v.y = v.y*4 + 2
|
|
|
|
v.z = v.z*4
|
2019-03-06 06:38:57 +03:00
|
|
|
obj:set_velocity(v)
|
2017-07-25 19:00:49 +03:00
|
|
|
obj:get_luaentity()._insta_collect = false
|
|
|
|
end
|
2017-01-06 01:37:40 +03:00
|
|
|
end
|
|
|
|
else
|
2017-07-25 19:00:49 +03:00
|
|
|
-- Fallback for unexpected cases
|
2017-01-06 01:37:40 +03:00
|
|
|
minetest.add_item(pos, itemstack)
|
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
2017-07-25 19:00:49 +03:00
|
|
|
-- Return items in the given inventory list (name) to the main inventory, or drop them if there is no space left
|
2020-11-25 14:47:27 +03:00
|
|
|
function return_fields(player, name)
|
2017-01-06 01:37:40 +03:00
|
|
|
local inv = player:get_inventory()
|
2020-11-25 14:47:27 +03:00
|
|
|
local list = inv:get_list(name)
|
|
|
|
if not list then return end
|
|
|
|
for i,stack in ipairs(list) do
|
2019-02-01 08:33:07 +03:00
|
|
|
return_item(stack, player, player:get_pos(), inv)
|
2017-01-06 01:37:40 +03:00
|
|
|
stack:clear()
|
|
|
|
inv:set_stack(name, i, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-09 20:58:35 +03:00
|
|
|
local function set_inventory(player, armor_change_only)
|
2020-07-10 17:08:40 +03:00
|
|
|
if minetest.is_creative_enabled(player:get_player_name()) then
|
2017-03-09 20:58:35 +03:00
|
|
|
if armor_change_only then
|
|
|
|
-- Stay on survival inventory plage if only the armor has been changed
|
2017-06-02 23:46:39 +03:00
|
|
|
mcl_inventory.set_creative_formspec(player, 0, 0, nil, nil, "inv")
|
2017-03-09 20:58:35 +03:00
|
|
|
else
|
|
|
|
mcl_inventory.set_creative_formspec(player, 0, 1)
|
|
|
|
end
|
2017-01-06 03:25:49 +03:00
|
|
|
return
|
2017-01-06 01:37:40 +03:00
|
|
|
end
|
2017-03-09 07:24:50 +03:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
inv:set_width("craft", 2)
|
|
|
|
inv:set_size("craft", 4)
|
2017-01-06 01:37:40 +03:00
|
|
|
|
|
|
|
local player_name = player:get_player_name()
|
2017-03-09 20:26:16 +03:00
|
|
|
|
|
|
|
-- Show armor and player image
|
2021-02-08 22:13:08 +03:00
|
|
|
local player_preview
|
2021-02-25 11:52:08 +03:00
|
|
|
if minetest.settings:get_bool("3d_player_preview", true) then
|
2021-02-08 22:13:08 +03:00
|
|
|
player_preview = mcl_player.get_player_formspec_model(player, 1.0, 0.0, 2.25, 4.5, "")
|
2019-03-05 03:50:51 +03:00
|
|
|
else
|
2021-02-08 22:13:08 +03:00
|
|
|
local img, img_player
|
|
|
|
if mod_player then
|
|
|
|
img_player = mcl_player.player_get_preview(player)
|
|
|
|
else
|
|
|
|
img_player = "player.png"
|
|
|
|
end
|
|
|
|
img = img_player
|
|
|
|
player_preview = "image[0.6,0.2;2,4;"..img.."]"
|
|
|
|
if show_armor and armor.textures[player_name] and armor.textures[player_name].preview then
|
|
|
|
img = armor.textures[player_name].preview
|
|
|
|
local s1 = img:find("character_preview")
|
|
|
|
if s1 ~= nil then
|
|
|
|
s1 = img:sub(s1+21)
|
|
|
|
img = img_player..s1
|
|
|
|
end
|
|
|
|
player_preview = "image[1.1,0.2;2,4;"..img.."]"
|
2017-01-06 01:37:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-01 06:41:53 +03:00
|
|
|
local armor_slots = {"helmet", "chestplate", "leggings", "boots"}
|
2017-03-09 07:24:50 +03:00
|
|
|
local armor_slot_imgs = ""
|
|
|
|
for a=1,4 do
|
|
|
|
if inv:get_stack("armor", a+1):is_empty() then
|
2017-04-01 06:41:53 +03:00
|
|
|
armor_slot_imgs = armor_slot_imgs .. "image[0,"..(a-1)..";1,1;mcl_inventory_empty_armor_slot_"..armor_slots[a]..".png]"
|
2017-03-09 07:24:50 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-06 01:37:40 +03:00
|
|
|
local form = "size[9,8.75]"..
|
2017-07-08 17:40:09 +03:00
|
|
|
"background[-0.19,-0.25;9.41,9.49;crafting_formspec_bg.png]"..
|
2017-03-09 20:26:16 +03:00
|
|
|
player_preview..
|
2017-01-06 01:37:40 +03:00
|
|
|
--armor
|
2017-01-06 02:36:58 +03:00
|
|
|
"list[detached:"..player_name.."_armor;armor;0,0;1,1;1]"..
|
|
|
|
"list[detached:"..player_name.."_armor;armor;0,1;1,1;2]"..
|
|
|
|
"list[detached:"..player_name.."_armor;armor;0,2;1,1;3]"..
|
|
|
|
"list[detached:"..player_name.."_armor;armor;0,3;1,1;4]"..
|
2020-03-24 20:48:14 +03:00
|
|
|
mcl_formspec.get_itemslot_bg(0,0,1,1)..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,1,1,1)..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,2,1,1)..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,3,1,1)..
|
2017-03-09 07:24:50 +03:00
|
|
|
armor_slot_imgs..
|
2017-01-06 02:36:58 +03:00
|
|
|
-- craft and inventory
|
2021-03-30 01:01:29 +03:00
|
|
|
"label[0,4;"..F(minetest.colorize(mcl_colors.DARK_GRAY, S("Inventory"))).."]"..
|
2017-01-06 01:37:40 +03:00
|
|
|
"list[current_player;main;0,4.5;9,3;9]"..
|
|
|
|
"list[current_player;main;0,7.74;9,1;]"..
|
2021-03-30 01:01:29 +03:00
|
|
|
"label[4,0.5;"..F(minetest.colorize(mcl_colors.DARK_GRAY, S("Crafting"))).."]"..
|
2017-01-06 03:18:02 +03:00
|
|
|
"list[current_player;craft;4,1;2,2]"..
|
2017-01-06 01:37:40 +03:00
|
|
|
"list[current_player;craftpreview;7,1.5;1,1;]"..
|
2020-03-24 20:48:14 +03:00
|
|
|
mcl_formspec.get_itemslot_bg(0,4.5,9,3)..
|
|
|
|
mcl_formspec.get_itemslot_bg(0,7.74,9,1)..
|
|
|
|
mcl_formspec.get_itemslot_bg(4,1,2,2)..
|
|
|
|
mcl_formspec.get_itemslot_bg(7,1.5,1,1)..
|
2017-02-06 00:23:44 +03:00
|
|
|
-- crafting guide button
|
2017-06-08 22:09:23 +03:00
|
|
|
"image_button[4.5,3;1,1;craftguide_book.png;__mcl_craftguide;]"..
|
2019-03-14 03:50:29 +03:00
|
|
|
"tooltip[__mcl_craftguide;"..F(S("Recipe book")).."]"..
|
2017-03-18 05:27:36 +03:00
|
|
|
-- help button
|
2017-06-08 22:09:23 +03:00
|
|
|
"image_button[8,3;1,1;doc_button_icon_lores.png;__mcl_doc;]"..
|
2019-03-14 03:50:29 +03:00
|
|
|
"tooltip[__mcl_doc;"..F(S("Help")).."]"..
|
2019-03-05 15:50:35 +03:00
|
|
|
-- skins button
|
|
|
|
"image_button[3,3;1,1;mcl_skins_button.png;__mcl_skins;]"..
|
2019-03-14 03:50:29 +03:00
|
|
|
"tooltip[__mcl_skins;"..F(S("Select player skin")).."]"..
|
2017-03-04 03:57:10 +03:00
|
|
|
-- achievements button
|
2017-06-08 22:09:23 +03:00
|
|
|
"image_button[7,3;1,1;mcl_achievements_button.png;__mcl_achievements;]"..
|
2019-03-14 03:50:29 +03:00
|
|
|
"tooltip[__mcl_achievements;"..F(S("Achievements")).."]"..
|
2017-01-06 02:36:58 +03:00
|
|
|
-- for shortcuts
|
|
|
|
"listring[current_player;main]"..
|
|
|
|
"listring[current_player;craft]"..
|
|
|
|
"listring[current_player;main]"..
|
2017-06-01 23:57:13 +03:00
|
|
|
"listring[detached:"..player_name.."_armor;armor]"
|
|
|
|
|
2017-01-06 01:37:40 +03:00
|
|
|
player:set_inventory_formspec(form)
|
|
|
|
end
|
|
|
|
|
2017-06-01 23:57:13 +03:00
|
|
|
-- Drop items in craft grid and reset inventory on closing
|
2017-01-06 01:37:40 +03:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if fields.quit then
|
2017-07-25 19:00:49 +03:00
|
|
|
return_fields(player,"craft")
|
2020-11-25 14:47:27 +03:00
|
|
|
return_fields(player,"enchanting_lapis")
|
|
|
|
return_fields(player,"enchanting_item")
|
2020-07-10 17:08:40 +03:00
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) and (formname == "" or formname == "main") then
|
2017-01-09 05:29:52 +03:00
|
|
|
set_inventory(player)
|
2017-01-06 01:37:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2020-07-10 17:08:40 +03:00
|
|
|
if not minetest.is_creative_enabled("") then
|
2019-03-05 12:29:49 +03:00
|
|
|
mcl_inventory.update_inventory_formspec = function(player)
|
|
|
|
set_inventory(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-02 01:34:48 +03:00
|
|
|
-- Drop crafting grid items on leaving
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2017-07-25 19:00:49 +03:00
|
|
|
return_fields(player, "craft")
|
2020-11-25 14:47:27 +03:00
|
|
|
return_fields(player, "enchanting_lapis")
|
|
|
|
return_fields(player, "enchanting_item")
|
2017-03-02 01:34:48 +03:00
|
|
|
end)
|
|
|
|
|
2017-01-06 01:37:40 +03:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
--init inventory
|
2017-01-06 03:18:02 +03:00
|
|
|
player:get_inventory():set_width("main", 9)
|
|
|
|
player:get_inventory():set_size("main", 36)
|
|
|
|
|
2017-01-06 01:37:40 +03:00
|
|
|
--set hotbar size
|
2017-01-06 03:25:49 +03:00
|
|
|
player:hud_set_hotbar_itemcount(9)
|
2017-01-06 01:37:40 +03:00
|
|
|
--add hotbar images
|
2019-02-04 14:21:58 +03:00
|
|
|
player:hud_set_hotbar_image("mcl_inventory_hotbar.png")
|
|
|
|
player:hud_set_hotbar_selected_image("mcl_inventory_hotbar_selected.png")
|
2017-01-06 03:25:49 +03:00
|
|
|
|
|
|
|
if show_armor then
|
2017-03-09 21:15:09 +03:00
|
|
|
local set_player_armor_original = armor.set_player_armor
|
|
|
|
local update_inventory_original = armor.update_inventory
|
2017-01-06 03:25:49 +03:00
|
|
|
armor.set_player_armor = function(self, player)
|
2017-03-09 21:15:09 +03:00
|
|
|
set_player_armor_original(self, player)
|
|
|
|
end
|
|
|
|
armor.update_inventory = function(self, player)
|
|
|
|
update_inventory_original(self, player)
|
2017-06-03 01:51:28 +03:00
|
|
|
set_inventory(player, true)
|
2017-01-06 01:37:40 +03:00
|
|
|
end
|
2017-03-09 21:19:53 +03:00
|
|
|
armor:set_player_armor(player)
|
2017-06-10 18:52:21 +03:00
|
|
|
armor:update_inventory(player)
|
2017-01-06 03:25:49 +03:00
|
|
|
end
|
2017-03-02 01:34:48 +03:00
|
|
|
|
2017-06-03 00:43:39 +03:00
|
|
|
-- In Creative Mode, the initial inventory setup is handled in creative.lua
|
2020-07-10 17:08:40 +03:00
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) then
|
2017-06-03 00:43:39 +03:00
|
|
|
set_inventory(player)
|
|
|
|
end
|
2017-03-09 21:15:09 +03:00
|
|
|
|
2017-03-02 01:34:48 +03:00
|
|
|
--[[ Make sure the crafting grid is empty. Why? Because the player might have
|
|
|
|
items remaining in the crafting grid from the previous join; this is likely
|
|
|
|
when the server has been shutdown and the server didn't clean up the player
|
|
|
|
inventories. ]]
|
2017-07-25 19:00:49 +03:00
|
|
|
return_fields(player, "craft")
|
2020-11-25 14:47:27 +03:00
|
|
|
return_fields(player, "enchanting_item")
|
|
|
|
return_fields(player, "enchanting_lapis")
|
2017-01-06 01:37:40 +03:00
|
|
|
end)
|
|
|
|
|
2020-07-10 17:08:40 +03:00
|
|
|
if minetest.is_creative_enabled("") then
|
2017-03-09 19:16:50 +03:00
|
|
|
dofile(minetest.get_modpath("mcl_inventory").."/creative.lua")
|
2017-01-24 23:18:42 +03:00
|
|
|
end
|
2017-08-03 20:21:31 +03:00
|
|
|
|