2021-05-29 16:12:33 +02:00
|
|
|
local S = minetest.get_translator(minetest.get_current_modname())
|
2019-03-14 01:50:29 +01:00
|
|
|
local F = minetest.formspec_escape
|
2019-03-07 21:10:39 +01:00
|
|
|
|
2017-03-09 17:16:50 +01:00
|
|
|
mcl_inventory = {}
|
|
|
|
|
2021-05-29 16:12:33 +02:00
|
|
|
--local mod_player = minetest.get_modpath("mcl_player")
|
|
|
|
--local mod_craftguide = minetest.get_modpath("mcl_craftguide")
|
2017-08-03 19:21:31 +02:00
|
|
|
|
2017-07-25 18:00:49 +02: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 12:47:27 +01:00
|
|
|
function return_item(itemstack, dropper, pos, inv)
|
2017-01-05 23:37:40 +01:00
|
|
|
if dropper:is_player() then
|
2017-07-25 18:00:49 +02: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 04:38:57 +01:00
|
|
|
obj:set_velocity(v)
|
2017-07-25 18:00:49 +02:00
|
|
|
obj:get_luaentity()._insta_collect = false
|
|
|
|
end
|
2017-01-05 23:37:40 +01:00
|
|
|
end
|
|
|
|
else
|
2017-07-25 18:00:49 +02:00
|
|
|
-- Fallback for unexpected cases
|
2017-01-05 23:37:40 +01:00
|
|
|
minetest.add_item(pos, itemstack)
|
|
|
|
end
|
|
|
|
return itemstack
|
|
|
|
end
|
|
|
|
|
2017-07-25 18:00:49 +02: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 12:47:27 +01:00
|
|
|
function return_fields(player, name)
|
2017-01-05 23:37:40 +01:00
|
|
|
local inv = player:get_inventory()
|
2020-11-25 12:47:27 +01:00
|
|
|
local list = inv:get_list(name)
|
|
|
|
if not list then return end
|
|
|
|
for i,stack in ipairs(list) do
|
2019-02-01 06:33:07 +01:00
|
|
|
return_item(stack, player, player:get_pos(), inv)
|
2017-01-05 23:37:40 +01:00
|
|
|
stack:clear()
|
|
|
|
inv:set_stack(name, i, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-09 18:58:35 +01:00
|
|
|
local function set_inventory(player, armor_change_only)
|
2020-07-10 16:08:40 +02:00
|
|
|
if minetest.is_creative_enabled(player:get_player_name()) then
|
2017-03-09 18:58:35 +01:00
|
|
|
if armor_change_only then
|
|
|
|
-- Stay on survival inventory plage if only the armor has been changed
|
2017-06-02 22:46:39 +02:00
|
|
|
mcl_inventory.set_creative_formspec(player, 0, 0, nil, nil, "inv")
|
2017-03-09 18:58:35 +01:00
|
|
|
else
|
|
|
|
mcl_inventory.set_creative_formspec(player, 0, 1)
|
|
|
|
end
|
2017-01-06 01:25:49 +01:00
|
|
|
return
|
2017-01-05 23:37:40 +01:00
|
|
|
end
|
2017-03-09 05:24:50 +01:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
inv:set_width("craft", 2)
|
|
|
|
inv:set_size("craft", 4)
|
2017-01-05 23:37:40 +01:00
|
|
|
|
2017-04-01 05:41:53 +02:00
|
|
|
local armor_slots = {"helmet", "chestplate", "leggings", "boots"}
|
2017-03-09 05:24:50 +01:00
|
|
|
local armor_slot_imgs = ""
|
|
|
|
for a=1,4 do
|
|
|
|
if inv:get_stack("armor", a+1):is_empty() then
|
2017-04-01 05:41:53 +02: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 05:24:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-15 15:58:27 +01:00
|
|
|
if inv:get_stack("offhand", 1):is_empty() then
|
|
|
|
armor_slot_imgs = armor_slot_imgs .. "image[3,2;1,1;mcl_inventory_empty_armor_slot_shield.png]"
|
|
|
|
end
|
|
|
|
|
2017-01-05 23:37:40 +01:00
|
|
|
local form = "size[9,8.75]"..
|
2017-07-08 16:40:09 +02:00
|
|
|
"background[-0.19,-0.25;9.41,9.49;crafting_formspec_bg.png]"..
|
2022-05-02 10:18:25 +02:00
|
|
|
mcl_player.get_player_formspec_model(player, 1.0, 0.0, 2.25, 4.5, "")..
|
2017-01-05 23:37:40 +01:00
|
|
|
--armor
|
2021-04-14 15:46:52 +02:00
|
|
|
"list[current_player;armor;0,0;1,1;1]"..
|
|
|
|
"list[current_player;armor;0,1;1,1;2]"..
|
|
|
|
"list[current_player;armor;0,2;1,1;3]"..
|
|
|
|
"list[current_player;armor;0,3;1,1;4]"..
|
2020-03-24 18:48:14 +01: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)..
|
2022-03-15 15:58:27 +01:00
|
|
|
"list[current_player;offhand;3,2;1,1]"..
|
|
|
|
mcl_formspec.get_itemslot_bg(3,2,1,1)..
|
2017-03-09 05:24:50 +01:00
|
|
|
armor_slot_imgs..
|
2017-01-06 00:36:58 +01:00
|
|
|
-- craft and inventory
|
2021-04-19 14:44:20 +02:00
|
|
|
"label[0,4;"..F(minetest.colorize("#313131", S("Inventory"))).."]"..
|
2017-01-05 23:37:40 +01:00
|
|
|
"list[current_player;main;0,4.5;9,3;9]"..
|
|
|
|
"list[current_player;main;0,7.74;9,1;]"..
|
2021-04-19 14:44:20 +02:00
|
|
|
"label[4,0.5;"..F(minetest.colorize("#313131", S("Crafting"))).."]"..
|
2017-01-06 01:18:02 +01:00
|
|
|
"list[current_player;craft;4,1;2,2]"..
|
2017-01-05 23:37:40 +01:00
|
|
|
"list[current_player;craftpreview;7,1.5;1,1;]"..
|
2020-03-24 18:48:14 +01: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-05 22:23:44 +01:00
|
|
|
-- crafting guide button
|
2017-06-08 21:09:23 +02:00
|
|
|
"image_button[4.5,3;1,1;craftguide_book.png;__mcl_craftguide;]"..
|
2019-03-14 01:50:29 +01:00
|
|
|
"tooltip[__mcl_craftguide;"..F(S("Recipe book")).."]"..
|
2017-03-18 03:27:36 +01:00
|
|
|
-- help button
|
2017-06-08 21:09:23 +02:00
|
|
|
"image_button[8,3;1,1;doc_button_icon_lores.png;__mcl_doc;]"..
|
2019-03-14 01:50:29 +01:00
|
|
|
"tooltip[__mcl_doc;"..F(S("Help")).."]"..
|
2019-03-05 13:50:35 +01:00
|
|
|
-- skins button
|
|
|
|
"image_button[3,3;1,1;mcl_skins_button.png;__mcl_skins;]"..
|
2019-03-14 01:50:29 +01:00
|
|
|
"tooltip[__mcl_skins;"..F(S("Select player skin")).."]"..
|
2017-03-04 01:57:10 +01:00
|
|
|
-- achievements button
|
2017-06-08 21:09:23 +02:00
|
|
|
"image_button[7,3;1,1;mcl_achievements_button.png;__mcl_achievements;]"..
|
2019-03-14 01:50:29 +01:00
|
|
|
"tooltip[__mcl_achievements;"..F(S("Achievements")).."]"..
|
2017-01-06 00:36:58 +01:00
|
|
|
-- for shortcuts
|
|
|
|
"listring[current_player;main]"..
|
2021-04-14 15:46:52 +02:00
|
|
|
"listring[current_player;armor]"..
|
|
|
|
"listring[current_player;main]" ..
|
|
|
|
"listring[current_player;craft]" ..
|
|
|
|
"listring[current_player;main]"
|
2017-01-05 23:37:40 +01:00
|
|
|
player:set_inventory_formspec(form)
|
|
|
|
end
|
|
|
|
|
2017-06-01 22:57:13 +02:00
|
|
|
-- Drop items in craft grid and reset inventory on closing
|
2017-01-05 23:37:40 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if fields.quit then
|
2017-07-25 18:00:49 +02:00
|
|
|
return_fields(player,"craft")
|
2020-11-25 12:47:27 +01:00
|
|
|
return_fields(player,"enchanting_lapis")
|
|
|
|
return_fields(player,"enchanting_item")
|
2020-07-10 16:08:40 +02:00
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) and (formname == "" or formname == "main") then
|
2017-01-09 03:29:52 +01:00
|
|
|
set_inventory(player)
|
2017-01-05 23:37:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2020-07-10 16:08:40 +02:00
|
|
|
if not minetest.is_creative_enabled("") then
|
2021-05-28 00:34:58 +02:00
|
|
|
function mcl_inventory.update_inventory_formspec(player)
|
2019-03-05 10:29:49 +01:00
|
|
|
set_inventory(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-01 23:34:48 +01:00
|
|
|
-- Drop crafting grid items on leaving
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2017-07-25 18:00:49 +02:00
|
|
|
return_fields(player, "craft")
|
2020-11-25 12:47:27 +01:00
|
|
|
return_fields(player, "enchanting_lapis")
|
|
|
|
return_fields(player, "enchanting_item")
|
2017-03-01 23:34:48 +01:00
|
|
|
end)
|
|
|
|
|
2017-01-05 23:37:40 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
--init inventory
|
2022-03-15 15:58:27 +01:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
inv:set_width("main", 9)
|
|
|
|
inv:set_size("main", 36)
|
|
|
|
inv:set_size("offhand", 1)
|
2017-01-06 01:18:02 +01:00
|
|
|
|
2017-01-05 23:37:40 +01:00
|
|
|
--set hotbar size
|
2017-01-06 01:25:49 +01:00
|
|
|
player:hud_set_hotbar_itemcount(9)
|
2017-01-05 23:37:40 +01:00
|
|
|
--add hotbar images
|
2019-02-04 12:21:58 +01:00
|
|
|
player:hud_set_hotbar_image("mcl_inventory_hotbar.png")
|
|
|
|
player:hud_set_hotbar_selected_image("mcl_inventory_hotbar_selected.png")
|
2017-01-06 01:25:49 +01:00
|
|
|
|
2021-04-14 15:46:52 +02:00
|
|
|
local old_update_player = mcl_armor.update_player
|
2021-05-28 00:34:58 +02:00
|
|
|
function mcl_armor.update_player(player, info)
|
2021-04-14 15:46:52 +02:00
|
|
|
old_update_player(player, info)
|
|
|
|
set_inventory(player, true)
|
2017-01-06 01:25:49 +01:00
|
|
|
end
|
2017-03-01 23:34:48 +01:00
|
|
|
|
2017-06-02 23:43:39 +02:00
|
|
|
-- In Creative Mode, the initial inventory setup is handled in creative.lua
|
2020-07-10 16:08:40 +02:00
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) then
|
2017-06-02 23:43:39 +02:00
|
|
|
set_inventory(player)
|
|
|
|
end
|
2017-03-09 19:15:09 +01:00
|
|
|
|
2017-03-01 23:34:48 +01: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 18:00:49 +02:00
|
|
|
return_fields(player, "craft")
|
2020-11-25 12:47:27 +01:00
|
|
|
return_fields(player, "enchanting_item")
|
|
|
|
return_fields(player, "enchanting_lapis")
|
2017-01-05 23:37:40 +01:00
|
|
|
end)
|
|
|
|
|
2022-05-24 17:41:18 +02:00
|
|
|
|
|
|
|
dofile(minetest.get_modpath(minetest.get_current_modname()).."/creative.lua")
|
|
|
|
|
|
|
|
local mt_is_creative_enabled = minetest.is_creative_enabled
|
|
|
|
|
|
|
|
function minetest.is_creative_enabled(name)
|
|
|
|
if mt_is_creative_enabled(name) then return true end
|
|
|
|
local p = minetest.get_player_by_name(name)
|
|
|
|
if p then
|
2022-05-24 19:50:22 +02:00
|
|
|
return p:get_meta():get_string("gamemode") == "creative"
|
2022-05-24 17:41:18 +02:00
|
|
|
end
|
|
|
|
return false
|
2017-01-24 21:18:42 +01:00
|
|
|
end
|
2017-08-03 19:21:31 +02:00
|
|
|
|
2022-05-24 19:50:22 +02:00
|
|
|
local function in_table(n,h)
|
|
|
|
for k,v in pairs(h) do
|
|
|
|
if v == n then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function str_split(s,d)
|
|
|
|
if d == nil then d = "%s" end
|
|
|
|
local t={}
|
|
|
|
for v in string.gmatch(s, "([^"..d.."]+)") do
|
|
|
|
table.insert(t, v)
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
local gamemodes = {
|
|
|
|
"survival",
|
|
|
|
"creative"
|
|
|
|
}
|
|
|
|
|
|
|
|
local function player_set_gamemode(p,g)
|
|
|
|
local m = p:get_meta()
|
|
|
|
m:set_string("gamemode",g)
|
|
|
|
set_inventory(p)
|
|
|
|
end
|
|
|
|
|
2022-05-24 17:41:18 +02:00
|
|
|
minetest.register_chatcommand("gamemode",{
|
|
|
|
privs = { server = true },
|
|
|
|
func = function(n,param)
|
2022-05-24 19:50:22 +02:00
|
|
|
-- Full input validation ( just for @erlehmann <3 )
|
2022-05-24 17:41:18 +02:00
|
|
|
local p = minetest.get_player_by_name(n)
|
2022-05-24 19:50:22 +02:00
|
|
|
local args = str_split(param)
|
|
|
|
if args[2] ~= nil then
|
|
|
|
p = minetest.get_player_by_name(args[2])
|
|
|
|
end
|
|
|
|
if not p then
|
|
|
|
minetest.chat_send_player(n,S("Player not online"))
|
|
|
|
return
|
2022-05-24 17:41:18 +02:00
|
|
|
end
|
2022-05-24 19:50:22 +02:00
|
|
|
if args[1] ~= nil and not in_table(args[1],gamemodes) then
|
|
|
|
minetest.chat_send_player(n,S("Gamemode "..tostring(args[1]).." does not exist." ))
|
|
|
|
return
|
|
|
|
elseif args[1] ~= nil then
|
|
|
|
player_set_gamemode(p,args[1])
|
|
|
|
end
|
|
|
|
--Result message - show effective game mode
|
|
|
|
local gm = p:get_meta():get_string("gamemode")
|
|
|
|
if gm == "" then gm = gamemodes[1] end
|
2022-05-24 17:41:18 +02:00
|
|
|
minetest.chat_send_player(n,S("Gamemode for player ")..n..S(": "..gm))
|
|
|
|
end
|
|
|
|
})
|