From 691b93ac686fff0ae51d08e3354988b1263682bf Mon Sep 17 00:00:00 2001 From: AFCMS Date: Fri, 9 Sep 2022 18:59:12 +0200 Subject: [PATCH] Modern Survival Inventory (9 slice images, formspec v6) --- mods/HUD/mcl_formspec/init.lua | 21 +++- mods/HUD/mcl_inventory/init.lua | 143 +++++++++++++++---------- mods/PLAYER/mcl_player/init.lua | 35 ++++-- textures/mcl_inventory_background9.png | Bin 0 -> 5324 bytes 4 files changed, 126 insertions(+), 73 deletions(-) create mode 100644 textures/mcl_inventory_background9.png diff --git a/mods/HUD/mcl_formspec/init.lua b/mods/HUD/mcl_formspec/init.lua index 8ddc90c1d..e7817e639 100644 --- a/mods/HUD/mcl_formspec/init.lua +++ b/mods/HUD/mcl_formspec/init.lua @@ -15,20 +15,31 @@ function mcl_formspec.get_itemslot_bg(x, y, w, h) local out = "" for i = 0, w - 1, 1 do for j = 0, h - 1, 1 do - out = out .."image["..x+i..","..y+j..";1,1;mcl_formspec_itemslot.png]" + out = out .. "image[" .. x + i .. "," .. y + j .. ";1,1;mcl_formspec_itemslot.png]" end end return out end ---This function will replace mcl_formspec.get_itemslot_bg then every formspec will be upgrade to version 4 +---This function will replace mcl_formspec.get_itemslot_bg then every formspec will be upgrade to version 4 +---@param x number +---@param y number +---@param size number +---@return string local function get_slot(x, y, size) - local t = "image["..x-size..","..y-size..";".. 1+(size*2)..",".. 1+(size*2)..";mcl_formspec_itemslot.png]" + local t = "image[" .. x - size .. "," .. y - size .. ";" .. 1 + (size * 2) .. + "," .. 1 + (size * 2) .. ";mcl_formspec_itemslot.png]" return t end mcl_formspec.itemslot_border_size = 0.05 +---@param x number +---@param y number +---@param w integer +---@param h integer +---@param size? number +---@return string function mcl_formspec.get_itemslot_bg_v4(x, y, w, h, size) if not size then size = mcl_formspec.itemslot_border_size @@ -36,8 +47,8 @@ function mcl_formspec.get_itemslot_bg_v4(x, y, w, h, size) local out = "" for i = 0, w - 1, 1 do for j = 0, h - 1, 1 do - out = out .. get_slot(x+i+(i*0.25), y+j+(j*0.25), size) + out = out .. get_slot(x + i + (i * 0.25), y + j + (j * 0.25), size) end end return out -end \ No newline at end of file +end diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index a83425388..a46f2669f 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -6,8 +6,12 @@ mcl_inventory = {} --local mod_player = minetest.get_modpath("mcl_player") --local mod_craftguide = minetest.get_modpath("mcl_craftguide") --- Returns a single itemstack in the given inventory to the main inventory, or drop it when there's no space left -function return_item(itemstack, dropper, pos, inv) +---Returns a single itemstack in the given inventory to the main inventory, or drop it when there's no space left. +---@param itemstack ItemStack +---@param dropper ObjectRef +---@param pos Vector +---@param inv InvRef +local function return_item(itemstack, dropper, pos, inv) if dropper:is_player() then -- Return to main inventory if inv:room_for_item("main", itemstack) then @@ -15,7 +19,7 @@ function return_item(itemstack, dropper, pos, inv) 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 } + local p = vector.offset(pos, 0, 1.2, 0) 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) @@ -34,9 +38,14 @@ function return_item(itemstack, dropper, pos, inv) return itemstack end --- Return items in the given inventory list (name) to the main inventory, or drop them if there is no space left -function return_fields(player, name) +---Return items in the given inventory list (name) to the main inventory, or drop them if there is no space left. +---@param player ObjectRef +---@param name string +local function return_fields(player, name) local inv = player:get_inventory() + + ---@diagnostic disable need-check-nil + local list = inv:get_list(name) if not list then return end for i, stack in ipairs(list) do @@ -44,85 +53,102 @@ function return_fields(player, name) stack:clear() inv:set_stack(name, i, stack) end + + ---@diagnostic enable need-check-nil end +---@param player ObjectRef local function set_inventory(player) if minetest.is_creative_enabled(player:get_player_name()) then mcl_inventory.set_creative_formspec(player) return end + local inv = player:get_inventory() + + ---@diagnostic disable need-check-nil inv:set_width("craft", 2) inv:set_size("craft", 4) local armor_slots = { "helmet", "chestplate", "leggings", "boots" } local armor_slot_imgs = "" + for a = 1, 4 do if inv:get_stack("armor", a + 1):is_empty() then armor_slot_imgs = armor_slot_imgs .. - "image[0," .. (a - 1) .. ";1,1;mcl_inventory_empty_armor_slot_" .. armor_slots[a] .. ".png]" + "image[0.375," .. + (0.375 + (a - 1) * 1.25) .. ";1,1;mcl_inventory_empty_armor_slot_" .. armor_slots[a] .. ".png]" end end 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]" + armor_slot_imgs = armor_slot_imgs .. "image[5.375,4.125;1,1;mcl_inventory_empty_armor_slot_shield.png]" end - local form = "size[9,8.75]" .. - "background[-0.19,-0.25;9.41,9.49;crafting_formspec_bg.png]" .. - mcl_player.get_player_formspec_model(player, 1.0, 0.0, 2.25, 4.5, "") .. + ---@diagnostic enable need-check-nil - -- Armor - "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]" .. - 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) .. - "list[current_player;offhand;3,2;1,1]" .. - mcl_formspec.get_itemslot_bg(3, 2, 1, 1) .. - armor_slot_imgs .. + local form = table.concat({ + "formspec_version[6]", + "size[11.75,10.9]", - -- Craft and inventory - "label[0,4;" .. F(minetest.colorize("#313131", S("Inventory"))) .. "]" .. - "list[current_player;main;0,4.5;9,3;9]" .. - "list[current_player;main;0,7.74;9,1;]" .. - "label[4,0.5;" .. F(minetest.colorize("#313131", S("Crafting"))) .. "]" .. - "list[current_player;craft;4,1;2,2]" .. - "list[current_player;craftpreview;7,1.5;1,1;]" .. - 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) .. + --Armor slots + mcl_formspec.get_itemslot_bg_v4(0.375, 0.375, 1, 4), + "list[current_player;armor;0.375,0.375;1,1;1]", + "list[current_player;armor;0.375,1.625;1,1;2]", + "list[current_player;armor;0.375,2.875;1,1;3]", + "list[current_player;armor;0.375,4.125;1,1;4]", - -- Crafting guide button - "image_button[4.5,3;1,1;craftguide_book.png;__mcl_craftguide;]" .. - "tooltip[__mcl_craftguide;" .. F(S("Recipe book")) .. "]" .. + --Main inventory + mcl_formspec.get_itemslot_bg_v4(0.375, 5.575, 9, 3), + "list[current_player;main;0.375,5.575;9,3;9]", - -- Help button - "image_button[8,3;1,1;doc_button_icon_lores.png;__mcl_doc;]" .. - "tooltip[__mcl_doc;" .. F(S("Help")) .. "]" + --Hotbar + mcl_formspec.get_itemslot_bg_v4(0.375, 9.525, 9, 1), + "list[current_player;main;0.375,9.525;9,1;]", - -- Skins button - if minetest.global_exists("mcl_skins") then - form = form .. - "image_button[3,3;1,1;mcl_skins_button.png;__mcl_skins;]" .. - "tooltip[__mcl_skins;" .. F(S("Select player skin")) .. "]" - end + --Player model + "image[1.57,0.343;3.62,4.85;mcl_inventory_background9.png;2]", + mcl_player.get_player_formspec_model(player, 1.57, 0.4, 3.62, 4.85, ""), - form = form .. - -- Achievements button - "image_button[7,3;1,1;mcl_achievements_button.png;__mcl_achievements;]" .. - "tooltip[__mcl_achievements;" .. F(S("Advancements")) .. "]" .. + --Offhand + mcl_formspec.get_itemslot_bg_v4(5.375, 4.125, 1, 1), + "list[current_player;offhand;5.375,4.125;1,1]", - -- For shortcuts - "listring[current_player;main]" .. - "listring[current_player;armor]" .. - "listring[current_player;main]" .. - "listring[current_player;craft]" .. - "listring[current_player;main]" + armor_slot_imgs, + + --Craft grid + "label[6.61,0.5;" .. F(minetest.colorize(mcl_formspec.label_color, S("Crafting"))) .. "]", + mcl_formspec.get_itemslot_bg_v4(6.625, 0.875, 2, 2), + "list[current_player;craft;6.625,0.875;2,2]", + + "image[9.125,1.5;1,1;crafting_formspec_arrow.png]", + + mcl_formspec.get_itemslot_bg_v4(10.375, 1.5, 1, 1), + "list[current_player;craftpreview;10.375,1.5;1,1;]", + + --Crafting guide button + "image_button[6.575,4.075;1.1,1.1;craftguide_book.png;__mcl_craftguide;]", + "tooltip[__mcl_craftguide;" .. F(S("Recipe book")) .. "]", + + --Help button + "image_button[7.825,4.075;1.1,1.1;doc_button_icon_lores.png;__mcl_doc;]", + "tooltip[__mcl_doc;" .. F(S("Help")) .. "]", + + --Skins button + "image_button[9.075,4.075;1.1,1.1;mcl_skins_button.png;__mcl_skins;]", + "tooltip[__mcl_skins;" .. F(S("Select player skin")) .. "]", + + --Advancements button + "image_button[10.325,4.075;1.1,1.1;mcl_achievements_button.png;__mcl_achievements;]", + "tooltip[__mcl_achievements;" .. F(S("Advancements")) .. "]", + + --Listring + "listring[current_player;main]", + "listring[current_player;armor]", + "listring[current_player;main]", + "listring[current_player;craft]", + "listring[current_player;main]", + }) player:set_inventory_formspec(form) end @@ -151,9 +177,13 @@ end) minetest.register_on_joinplayer(function(player) --init inventory local inv = player:get_inventory() + + ---get_inventory can return nil if object isn't a player, but we are sure this is one :) + ---@diagnostic disable need-check-nil inv:set_width("main", 9) inv:set_size("main", 36) inv:set_size("offhand", 1) + ---@diagnostic enable need-check-nil --set hotbar size player:hud_set_hotbar_itemcount(9) @@ -175,9 +205,8 @@ minetest.register_on_joinplayer(function(player) return_fields(player, "enchanting_lapis") end) -dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.lua") -mcl_player.register_on_visual_change(mcl_inventory.update_inventory_formspec) +dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/creative.lua") local mt_is_creative_enabled = minetest.is_creative_enabled diff --git a/mods/PLAYER/mcl_player/init.lua b/mods/PLAYER/mcl_player/init.lua index 084fe48a1..520add947 100644 --- a/mods/PLAYER/mcl_player/init.lua +++ b/mods/PLAYER/mcl_player/init.lua @@ -11,15 +11,16 @@ local animation_blend = 0 local function get_mouse_button(player) local controls = player:get_player_control() local get_wielded_item_name = player:get_wielded_item():get_name() - if controls.RMB and not string.find(get_wielded_item_name, "mcl_bows:bow") and not string.find(get_wielded_item_name, "mcl_bows:crossbow") and - not mcl_shields.wielding_shield(player, 1) and not mcl_shields.wielding_shield(player, 2) or controls.LMB then + if controls.RMB and not string.find(get_wielded_item_name, "mcl_bows:bow") and + not string.find(get_wielded_item_name, "mcl_bows:crossbow") and + not mcl_shields.wielding_shield(player, 1) and not mcl_shields.wielding_shield(player, 2) or controls.LMB then return true else return false end end -mcl_player.registered_player_models = { } +mcl_player.registered_player_models = {} -- Local for speed. local models = mcl_player.registered_player_models @@ -49,7 +50,7 @@ function mcl_player.player_get_animation(player) model = player_model[name], textures = textures, animation = player_anim[name], - visibility = player_visibility[name] + visibility = player_visible[name] } end @@ -94,7 +95,7 @@ function mcl_player.player_set_model(player, model_name) player:set_properties({ mesh = model_name, visual = "mesh", - visual_size = model.visual_size or {x=1, y=1}, + visual_size = model.visual_size or { x = 1, y = 1 }, damage_texture_modifier = "^[colorize:red:130", }) update_player_textures(player) @@ -133,6 +134,14 @@ function mcl_player.player_set_armor(player, texture) update_player_textures(player) end +---comment +---@param player ObjectRef +---@param x number +---@param y number +---@param w number +---@param h number +---@param fsname string +---@return string function mcl_player.get_player_formspec_model(player, x, y, w, h, fsname) local name = player:get_player_name() local model = player_model[name] @@ -165,7 +174,8 @@ minetest.register_on_joinplayer(function(player) local name = player:get_player_name() mcl_player.player_attached[name] = false player_visible[name] = true - player_textures[name] = {"character.png", "blank.png", "blank.png"} + player_textures[name] = { "character.png", "blank.png", "blank.png" } + --player:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30) player:set_fov(86.1) -- see >>> end) @@ -221,11 +231,13 @@ minetest.register_globalstep(function(dtime) elseif mcl_playerplus.elytra[player] and mcl_playerplus.elytra[player].active then player_set_animation(player, "stand") elseif walking and velocity.x > 0.35 - or walking and velocity.x < -0.35 - or walking and velocity.z > 0.35 - or walking and velocity.z < -0.35 then + or walking and velocity.x < -0.35 + or walking and velocity.z > 0.35 + or walking and velocity.z < -0.35 then local wielded_itemname = player:get_wielded_item():get_name() - local no_arm_moving = string.find(wielded_itemname, "mcl_bows:bow") or mcl_shields.wielding_shield(player, 1) or mcl_shields.wielding_shield(player, 2) + local no_arm_moving = string.find(wielded_itemname, "mcl_bows:bow") or + mcl_shields.wielding_shield(player, 1) or + mcl_shields.wielding_shield(player, 2) if player_sneak[name] ~= controls.sneak then player_anim[name] = nil player_sneak[name] = controls.sneak @@ -234,7 +246,8 @@ minetest.register_globalstep(function(dtime) player_set_animation(player, "swim_walk_mine", animation_speed_mod) elseif not controls.sneak and head_in_water and is_sprinting == true then player_set_animation(player, "swim_walk", animation_speed_mod) - elseif no_arm_moving and controls.RMB and controls.sneak or string.find(wielded_itemname, "mcl_bows:crossbow_") and controls.sneak then + elseif no_arm_moving and controls.RMB and controls.sneak or + string.find(wielded_itemname, "mcl_bows:crossbow_") and controls.sneak then player_set_animation(player, "bow_sneak", animation_speed_mod) elseif no_arm_moving and controls.RMB or string.find(wielded_itemname, "mcl_bows:crossbow_") then player_set_animation(player, "bow_walk", animation_speed_mod) diff --git a/textures/mcl_inventory_background9.png b/textures/mcl_inventory_background9.png new file mode 100644 index 0000000000000000000000000000000000000000..d774fd31925f642e35f3fe4f7157bdf93dcf8bdd GIT binary patch literal 5324 zcmeH~X;2f{7RMVAlo&u@~DhOuZBVmzXAP_dAFepf;(}9+3CJh7xK|u!;7u<%& zAPQkn$87{08P|uRC^I^OB7&%+G75u;ilTzbs{}-R^=hj8FyA~?o$lOw&hOmwzo&0i z=Y#|a#$y~X004~l_v42F0N`r4MwfM#)L7AKHI#$UmLnm?XM}!vyd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