From 18f8768909849bbe631c0b5be147360ea2cc3a15 Mon Sep 17 00:00:00 2001 From: Johannes Fritz Date: Thu, 15 Sep 2022 12:02:28 -0500 Subject: [PATCH] Fix invisibility potion when changing skin --- mods/HUD/mcl_inventory/init.lua | 3 +- mods/HUD/mcl_inventory/mod.conf | 4 +- mods/ITEMS/mcl_potions/functions.lua | 59 ++++++++--------------- mods/PLAYER/mcl_meshhand/init.lua | 2 +- mods/PLAYER/mcl_meshhand/mod.conf | 3 +- mods/PLAYER/mcl_player/init.lua | 72 ++++++++++++++++++++++++---- mods/PLAYER/mcl_skins/README.md | 4 -- mods/PLAYER/mcl_skins/edit_skin.lua | 12 ----- mods/PLAYER/mcl_skins/mod.conf | 2 +- 9 files changed, 89 insertions(+), 72 deletions(-) diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index c8caaab2c..3b52685d4 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -189,9 +189,10 @@ 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) + local mt_is_creative_enabled = minetest.is_creative_enabled function minetest.is_creative_enabled(name) diff --git a/mods/HUD/mcl_inventory/mod.conf b/mods/HUD/mcl_inventory/mod.conf index 10e669265..e42213d4f 100644 --- a/mods/HUD/mcl_inventory/mod.conf +++ b/mods/HUD/mcl_inventory/mod.conf @@ -1,5 +1,5 @@ name = mcl_inventory author = BlockMen description = Adds the player inventory and creative inventory. -depends = mcl_init, mcl_formspec, mcl_enchanting -optional_depends = mcl_armor, mcl_brewing, mcl_potions, mcl_enchanting, mcl_craftguide, mcl_player +depends = mcl_init, mcl_formspec, mcl_enchanting, mcl_player +optional_depends = mcl_armor, mcl_brewing, mcl_potions, mcl_enchanting, mcl_craftguide diff --git a/mods/ITEMS/mcl_potions/functions.lua b/mods/ITEMS/mcl_potions/functions.lua index b7102d8a4..bb1474696 100644 --- a/mods/ITEMS/mcl_potions/functions.lua +++ b/mods/ITEMS/mcl_potions/functions.lua @@ -405,11 +405,11 @@ function mcl_potions._reset_player_effects(player, set_hud) mcl_weather.skycolor.update_sky_color({player}) + mcl_potions._clear_cached_player_data(player) + if set_hud ~= false then potions_set_hud(player) end - - mcl_potions._clear_cached_player_data(player) end function mcl_potions._save_player_effects(player) @@ -561,45 +561,24 @@ function mcl_potions.is_obj_hit(self, pos) end -function mcl_potions.make_invisible(player, toggle) - - if not player then - return false +function mcl_potions.make_invisible(obj_ref, hide) + if obj_ref:is_player() then + if hide then + mcl_player.player_set_visibility(obj_ref, false) + obj_ref:set_nametag_attributes({ color = { a = 0 } }) + else + mcl_player.player_set_visibility(obj_ref, true) + obj_ref:set_nametag_attributes({ color = { r = 255, g = 255, b = 255, a = 255 } }) + end + else + if hide then + local luaentity = obj_ref:get_luaentity() + EF.invisible[obj_ref].old_size = luaentity.visual_size + obj_ref:set_properties({ visual_size = { x = 0, y = 0 } }) + else + obj_ref:set_properties({ visual_size = EF.invisible[obj_ref].old_size }) + end end - - local is_player = player:is_player() - local entity = player:get_luaentity() - --local playername = player:get_player_name() - local skin_file - - if toggle then -- hide player - - skin_file = "mobs_mc_empty.png" - - if entity then - EF.invisible[player].old_size = entity.visual_size - elseif not is_player then -- if not a player or entity, do nothing - return - end - - if is_player then - mcl_player.player_set_skin(player, skin_file) - elseif not is_player then - player:set_properties({visual_size = {x = 0, y = 0}}) - end - player:set_nametag_attributes({color = {a = 0}}) - - elseif EF.invisible[player] then -- show player - - if is_player then - mcl_skins.update_player_skin(player) - elseif not is_player then - player:set_properties({visual_size = EF.invisible[player].old_size}) - end - player:set_nametag_attributes({color = {r = 255, g = 255, b = 255, a = 255}}) - - end - end diff --git a/mods/PLAYER/mcl_meshhand/init.lua b/mods/PLAYER/mcl_meshhand/init.lua index 96086e4f2..0d4bf091d 100644 --- a/mods/PLAYER/mcl_meshhand/init.lua +++ b/mods/PLAYER/mcl_meshhand/init.lua @@ -48,7 +48,7 @@ end if mcl_skins_enabled then -- Change the player's hand to their skin - mcl_skins.register_on_set_skin(function(player) + mcl_player.register_on_visual_change(function(player) local node_id = mcl_skins.get_node_id_by_player(player) player:get_inventory():set_stack("hand", 1, "mcl_meshhand:" .. node_id) end) diff --git a/mods/PLAYER/mcl_meshhand/mod.conf b/mods/PLAYER/mcl_meshhand/mod.conf index 6a988417f..a95edb858 100644 --- a/mods/PLAYER/mcl_meshhand/mod.conf +++ b/mods/PLAYER/mcl_meshhand/mod.conf @@ -1,6 +1,5 @@ name = mcl_meshhand author = jordan4ibanez description = Applies the player skin texture to the hand. -depends = mcl_tools +depends = mcl_tools, mcl_player optional_depends = mcl_skins - diff --git a/mods/PLAYER/mcl_player/init.lua b/mods/PLAYER/mcl_player/init.lua index ccd5d5e0e..e915efbe2 100644 --- a/mods/PLAYER/mcl_player/init.lua +++ b/mods/PLAYER/mcl_player/init.lua @@ -1,3 +1,5 @@ +local sf = string.format + -- Minetest 0.4 mod: player -- See README.txt for licensing and other information. mcl_player = {} @@ -53,17 +55,55 @@ local player_model = {} local player_textures = {} local player_anim = {} local player_sneak = {} +local player_visible = {} mcl_player.player_attached = {} function mcl_player.player_get_animation(player) local name = player:get_player_name() + local textures = player_textures[name] + + if not player_visible[name] then + textures = table.copy(textures) + textures[1] = "blank.png" + end + return { model = player_model[name], - textures = player_textures[name], + textures = textures, animation = player_anim[name], + visibility = player_visibility[name] } end +local registered_on_visual_change = {} + +function mcl_player.register_on_visual_change(func) + table.insert(registered_on_visual_change, func) +end + +local function update_player_textures(player) + local name = player:get_player_name() + local textures = player_textures[name] + + if not player_visible[name] then + textures = table.copy(textures) + textures[1] = "blank.png" + end + + player:set_properties({ textures = textures }) + + -- Delay calling the callbacks because mods (including mcl_player) + -- need to fully initialize player data from minetest.register_on_joinplayer + -- before callbacks run + minetest.after(0.1, function() + if player:is_player() then + for i, func in ipairs(registered_on_visual_change) do + func(player) + end + end + end) +end + -- Called when a player's appearance needs to be updated function mcl_player.player_set_model(player, model_name) local name = player:get_player_name() @@ -74,11 +114,11 @@ function mcl_player.player_set_model(player, model_name) end player:set_properties({ mesh = model_name, - textures = player_textures[name] or model.textures, visual = "mesh", visual_size = model.visual_size or {x=1, y=1}, damage_texture_modifier = "^[colorize:red:130", }) + update_player_textures(player) mcl_player.player_set_animation(player, "stand") else player:set_properties({ @@ -89,25 +129,36 @@ function mcl_player.player_set_model(player, model_name) player_model[name] = model_name end -local function set_texture(player, index, texture) - local textures = player_textures[player:get_player_name()] - textures[index] = texture - player:set_properties({textures = textures}) +function mcl_player.player_set_visibility(player, visible) + local name = player:get_player_name() + if player_visible[name] == visible then return end + player_visible[name] = visible + update_player_textures(player) end function mcl_player.player_set_skin(player, texture) - set_texture(player, 1, texture) + local name = player:get_player_name() + player_textures[name][1] = texture + update_player_textures(player) end function mcl_player.player_set_armor(player, texture) - set_texture(player, 2, texture) + local name = player:get_player_name() + player_textures[name][2] = texture + update_player_textures(player) end function mcl_player.get_player_formspec_model(player, x, y, w, h, fsname) local name = player:get_player_name() local model = player_model[name] local anim = models[model].animations[player_anim[name]] - return "model["..x..","..y..";"..w..","..h..";"..fsname..";"..model..";"..table.concat(player_textures[name], ",")..";0,".. 180 ..";false;false;"..anim.x..","..anim.y.."]" + local textures = player_textures[name] + if not player_visible[name] then + textures = table.copy(textures) + textures[1] = "blank.png" + end + return sf("model[%s,%s;%s,%s;%s;%s;%s;0,180;false;false;%s,%s]", x, y, w, h, fsname, model, + table.concat(textures, ","), anim.x, anim.y) end function mcl_player.player_set_animation(player, anim_name, speed) @@ -128,6 +179,7 @@ end minetest.register_on_joinplayer(function(player) local name = player:get_player_name() mcl_player.player_attached[name] = false + player_visible[name] = true mcl_player.player_set_model(player, "character.b3d") 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) @@ -139,6 +191,8 @@ minetest.register_on_leaveplayer(function(player) player_model[name] = nil player_anim[name] = nil player_textures[name] = nil + player_sneak[name] = nil + player_visible[name] = nil end) -- Localize for better performance. diff --git a/mods/PLAYER/mcl_skins/README.md b/mods/PLAYER/mcl_skins/README.md index a6ad02f30..d05bf9a2e 100644 --- a/mods/PLAYER/mcl_skins/README.md +++ b/mods/PLAYER/mcl_skins/README.md @@ -44,10 +44,6 @@ Can be one of: "arm", "base", "footwear", "eye", "mouth", "bottom", "top", "hair `page_num` The page number to display of there are multiple pages of items. This parameter is optional. Must be a number. If it is not a valid page number the closest page number will be shown. -### `mcl_skins.register_on_set_skin(func)` -Register a function to be called whenever a player skin changes. -The function will be given a player ObjectRef as a parameter. - ### `mcl_skins.get_skin_list()` This function is used by mods that want a list of skins to register nodes that use the player skin as a texture. Returns an array of tables containing information about each skin. diff --git a/mods/PLAYER/mcl_skins/edit_skin.lua b/mods/PLAYER/mcl_skins/edit_skin.lua index eea6e5e87..1de500f36 100644 --- a/mods/PLAYER/mcl_skins/edit_skin.lua +++ b/mods/PLAYER/mcl_skins/edit_skin.lua @@ -127,12 +127,6 @@ function mcl_skins.update_player_skin(player) end local model = slim_arms and "mcl_armor_character_female.b3d" or "mcl_armor_character.b3d" mcl_player.player_set_model(player, model) - - mcl_inventory.update_inventory_formspec(player) - - for i=1, #mcl_skins.registered_on_set_skins do - mcl_skins.registered_on_set_skins[i](player) - end end -- Load player skin on join @@ -170,12 +164,6 @@ minetest.register_on_leaveplayer(function(player) mcl_skins.players[player] = nil end) -mcl_skins.registered_on_set_skins = {} - -function mcl_skins.register_on_set_skin(func) - table.insert(mcl_skins.registered_on_set_skins, func) -end - function mcl_skins.show_formspec(player, active_tab, page_num) local skin = mcl_skins.players[player] local default = #mcl_skins.simple_skins > 0 and "skin" or "template" diff --git a/mods/PLAYER/mcl_skins/mod.conf b/mods/PLAYER/mcl_skins/mod.conf index f631b76dc..bd2191097 100644 --- a/mods/PLAYER/mcl_skins/mod.conf +++ b/mods/PLAYER/mcl_skins/mod.conf @@ -1,4 +1,4 @@ name = mcl_skins author = MrRar description = Advanced player skin customization. -depends = mcl_player,mcl_inventory +depends = mcl_player