Version 0.41.3
[MineClone/MineClone2/MineClone2-Fixes.git] / mods / HUD / mcl_inventory / init.lua
blob34a0e3f14bebe53b6d050253fe8330c4c8c1d87c
1 mcl_inventory = {}
3 local show_armor = false
4 if minetest.get_modpath("3d_armor") ~= nil then show_armor = true end
7 -- Returns a single itemstack in the given inventory to the main inventory, or drop it when there's no space left
8 local function return_item(itemstack, dropper, pos, inv)
9 if dropper:is_player() then
10 -- Return to main inventory
11 if inv:room_for_item("main", itemstack) then
12 inv:add_item("main", itemstack)
13 else
14 -- Drop item on the ground
15 local v = dropper:get_look_dir()
16 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
17 p.x = p.x+(math.random(1,3)*0.2)
18 p.z = p.z+(math.random(1,3)*0.2)
19 local obj = minetest.add_item(p, itemstack)
20 if obj then
21 v.x = v.x*4
22 v.y = v.y*4 + 2
23 v.z = v.z*4
24 obj:setvelocity(v)
25 obj:get_luaentity()._insta_collect = false
26 end
27 end
28 else
29 -- Fallback for unexpected cases
30 minetest.add_item(pos, itemstack)
31 end
32 return itemstack
33 end
35 -- Return items in the given inventory list (name) to the main inventory, or drop them if there is no space left
36 local function return_fields(player, name)
37 local inv = player:get_inventory()
38 for i,stack in ipairs(inv:get_list(name)) do
39 return_item(stack, player, player:getpos(), inv)
40 stack:clear()
41 inv:set_stack(name, i, stack)
42 end
43 end
45 local function set_inventory(player, armor_change_only)
46 if minetest.settings:get_bool("creative_mode") then
47 if armor_change_only then
48 -- Stay on survival inventory plage if only the armor has been changed
49 mcl_inventory.set_creative_formspec(player, 0, 0, nil, nil, "inv")
50 else
51 mcl_inventory.set_creative_formspec(player, 0, 1)
52 end
53 return
54 end
55 local inv = player:get_inventory()
56 inv:set_width("craft", 2)
57 inv:set_size("craft", 4)
59 local player_name = player:get_player_name()
61 -- Show armor and player image
62 local img = "player.png"
63 local player_preview = "image[0.6,0.2;2,4;"..img.."]"
64 if show_armor and armor.textures[player_name] and armor.textures[player_name].preview then
65 img = armor.textures[player_name].preview
66 local s1 = img:find("character_preview")
67 if s1 ~= nil then
68 s1 = img:sub(s1+21)
69 img = "player.png"..s1
70 end
71 player_preview = "image[1.1,0.2;2,4;"..img.."]"
72 end
74 local armor_slots = {"helmet", "chestplate", "leggings", "boots"}
75 local armor_slot_imgs = ""
76 for a=1,4 do
77 if inv:get_stack("armor", a+1):is_empty() then
78 armor_slot_imgs = armor_slot_imgs .. "image[0,"..(a-1)..";1,1;mcl_inventory_empty_armor_slot_"..armor_slots[a]..".png]"
79 end
80 end
82 local form = "size[9,8.75]"..
83 "background[-0.19,-0.25;9.41,9.49;crafting_formspec_bg.png]"..
84 mcl_vars.inventory_header..
85 player_preview..
86 --armor
87 "list[detached:"..player_name.."_armor;armor;0,0;1,1;1]"..
88 "list[detached:"..player_name.."_armor;armor;0,1;1,1;2]"..
89 "list[detached:"..player_name.."_armor;armor;0,2;1,1;3]"..
90 "list[detached:"..player_name.."_armor;armor;0,3;1,1;4]"..
91 armor_slot_imgs..
92 -- craft and inventory
93 "list[current_player;main;0,4.5;9,3;9]"..
94 "list[current_player;main;0,7.74;9,1;]"..
95 "list[current_player;craft;4,1;2,2]"..
96 "list[current_player;craftpreview;7,1.5;1,1;]"..
97 -- crafting guide button
98 "image_button[4.5,3;1,1;craftguide_book.png;__mcl_craftguide;]"..
99 "tooltip[__mcl_craftguide;Recipe book]"..
100 -- help button
101 "image_button[8,3;1,1;doc_button_icon_lores.png;__mcl_doc;]"..
102 "tooltip[__mcl_doc;Help]"..
103 -- achievements button
104 "image_button[7,3;1,1;mcl_achievements_button.png;__mcl_achievements;]"..
105 "tooltip[__mcl_achievements;Achievements]"..
106 -- for shortcuts
107 "listring[current_player;main]"..
108 "listring[current_player;craft]"..
109 "listring[current_player;main]"..
110 "listring[detached:"..player_name.."_armor;armor]"
112 player:set_inventory_formspec(form)
115 -- Drop items in craft grid and reset inventory on closing
116 minetest.register_on_player_receive_fields(function(player, formname, fields)
117 if fields.quit then
118 return_fields(player,"craft")
119 if not minetest.settings:get_bool("creative_mode") and (formname == "" or formname == "main") then
120 set_inventory(player)
123 end)
125 -- Drop crafting grid items on leaving
126 minetest.register_on_leaveplayer(function(player)
127 return_fields(player, "craft")
128 end)
130 minetest.register_on_joinplayer(function(player)
131 --init inventory
132 player:get_inventory():set_width("main", 9)
133 player:get_inventory():set_size("main", 36)
135 --set hotbar size
136 player:hud_set_hotbar_itemcount(9)
137 --add hotbar images
138 player:hud_set_hotbar_image("gui_hotbar.png")
139 player:hud_set_hotbar_selected_image("gui_hotbar_selected.png")
141 if show_armor then
142 local set_player_armor_original = armor.set_player_armor
143 local update_inventory_original = armor.update_inventory
144 armor.set_player_armor = function(self, player)
145 set_player_armor_original(self, player)
147 armor.update_inventory = function(self, player)
148 update_inventory_original(self, player)
149 set_inventory(player, true)
151 armor:set_player_armor(player)
152 armor:update_inventory(player)
155 -- In Creative Mode, the initial inventory setup is handled in creative.lua
156 if not minetest.settings:get_bool("creative_mode") then
157 set_inventory(player)
160 --[[ Make sure the crafting grid is empty. Why? Because the player might have
161 items remaining in the crafting grid from the previous join; this is likely
162 when the server has been shutdown and the server didn't clean up the player
163 inventories. ]]
164 return_fields(player, "craft")
165 end)
167 if minetest.settings:get_bool("creative_mode") then
168 dofile(minetest.get_modpath("mcl_inventory").."/creative.lua")