From f1e9c3b5632bbcde1900deb05165fa22adc1a2b7 Mon Sep 17 00:00:00 2001 From: the-real-herowl Date: Fri, 27 Oct 2023 05:02:43 +0200 Subject: [PATCH] Translatable effect names and cleanup -added translatable effect names to the API -made /effect utilize those descriptions -cleaned up variables/constants to go with further API changes --- mods/ITEMS/mcl_potions/commands.lua | 14 ++++---- mods/ITEMS/mcl_potions/functions.lua | 51 +++++++++++++++++----------- mods/ITEMS/mcl_potions/init.lua | 11 +++--- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/mods/ITEMS/mcl_potions/commands.lua b/mods/ITEMS/mcl_potions/commands.lua index a04f8f079..53245f4fc 100644 --- a/mods/ITEMS/mcl_potions/commands.lua +++ b/mods/ITEMS/mcl_potions/commands.lua @@ -24,9 +24,8 @@ minetest.register_chatcommand("effect",{ if not P[1] then return false, S("Missing effect parameter!") elseif P[1] == "list" then - local regs = mcl_potions.get_registered_effects() local effects = "heal" - for name, _ in pairs(regs) do + for name, _ in pairs(mcl_potions.registered_effects) do effects = effects .. ", " .. name end return true, effects @@ -57,20 +56,21 @@ minetest.register_chatcommand("effect",{ P[3] = 1 end - if mcl_potions.is_effect_registered(P[1]) then + local def = mcl_potions.registered_effects[P[1]] + if def then if P[3] == "F" then local given = mcl_potions.give_effect(P[1], minetest.get_player_by_name(name), tonumber(P[4]), tonumber(P[2])) if given then - return true, S("@1 effect given to player @2 for @3 seconds with factor of @4.", P[1], name, P[2], P[4]) + return true, S("@1 effect given to player @2 for @3 seconds with factor of @4.", def.description, name, P[2], P[4]) else - return false, S("Giving effect @1 to player @2 failed.", P[1], name) + return false, S("Giving effect @1 to player @2 failed.", def.description, name) end else local given = mcl_potions.give_effect_by_level(P[1], minetest.get_player_by_name(name), tonumber(P[3]), tonumber(P[2])) if given then - return true, S("@1 effect on level @2 given to player @3 for @4 seconds.", P[1], P[3], name, P[2]) + return true, S("@1 effect on level @2 given to player @3 for @4 seconds.", def.description, P[3], name, P[2]) else - return false, S("Giving effect @1 to player @2 failed.", P[1], name) + return false, S("Giving effect @1 to player @2 failed.", def.description, name) end end else diff --git a/mods/ITEMS/mcl_potions/functions.lua b/mods/ITEMS/mcl_potions/functions.lua index a5d05e8f9..8aa01c569 100644 --- a/mods/ITEMS/mcl_potions/functions.lua +++ b/mods/ITEMS/mcl_potions/functions.lua @@ -1,5 +1,8 @@ +local S = minetest.get_translator(minetest.get_current_modname()) + local EF = {} -local registered_effects = {} +mcl_potions.registered_effects = {} +local registered_effects = mcl_potions.registered_effects -- shorthand ref local EFFECT_TYPES = 0 minetest.register_on_mods_loaded(function() @@ -8,7 +11,6 @@ minetest.register_on_mods_loaded(function() end end) - -- ██████╗░███████╗░██████╗░██╗░██████╗████████╗███████╗██████╗ -- ██╔══██╗██╔════╝██╔════╝░██║██╔════╝╚══██╔══╝██╔════╝██╔══██╗ -- ██████╔╝█████╗░░██║░░██╗░██║╚█████╗░░░░██║░░░█████╗░░██████╔╝ @@ -51,7 +53,9 @@ end -- API - registers an effect -- required parameters in def: -- name - string - effect name in code +-- description - translated string - actual effect name in game -- optional parameters in def: +-- get_tt - function(factor) - returns tooltip description text for use with potions -- icon - string - file name of the effect icon in HUD - defaults to one based on name -- res_condition - function(object) - returning true if target is to be resistant to the effect -- on_start - function(object, factor) - called when dealing the effect @@ -70,25 +74,33 @@ end -- modifier_priority - integer - priority passed when registering damage_modifier - defaults to -50 function mcl_potions.register_effect(def) local modname = minetest.get_current_modname() - if def.name == nil then + local name = def.name + if name == nil then error("Unable to register effect: name is nil") end - if def.name == "list" then + if type(name) ~= "string" then + error("Unable to register effect: name is not a string") + end + if name == "list" then error("Unable to register effect: list is a reserved word") end - if def.name == "heal" then + if name == "heal" then error("Unable to register effect: heal is a reserved word") end if registered_effects[name] then error("Effect named "..name.." already registered!") end - local name = def.name + if not def.description or type(def.description) ~= "string" then + error("Unable to register effect: description is not a string") + end local pdef = {} + pdef.description = def.description if not def.icon then pdef.icon = modname.."_effect_"..name..".png" else pdef.icon = def.icon end + pdef.get_tt = def.get_tt pdef.res_condition = def.res_condition pdef.on_start = def.on_start pdef.on_load = def.on_load @@ -134,20 +146,9 @@ function mcl_potions.register_effect(def) EF[name] = {} end -function mcl_potions.get_registered_effects() - return table.copy(registered_effects) -end - -function mcl_potions.is_effect_registered(name) - if registered_effects[name] then - return true - else - return false - end -end - mcl_potions.register_effect({ name = "invisibility", + description = S("Invisiblity"), on_start = function(object, factor) mcl_potions.make_invisible(object, true) end, @@ -163,6 +164,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "poison", + description = S("Poison"), res_condition = function(object) local entity = object:get_luaentity() return (entity and (entity.harmed_by_heal or string.find(entity.name, "spider"))) @@ -181,6 +183,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "regeneration", + description = S("Regeneration"), res_condition = function(object) local entity = object:get_luaentity() return (entity and entity.harmed_by_heal) @@ -202,6 +205,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "strength", + description = S("Strength"), res_condition = function(object) return (not object:is_player()) end, @@ -210,6 +214,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "weakness", + description = S("Weakness"), res_condition = function(object) return (not object:is_player()) end, @@ -218,6 +223,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "water_breathing", + description = S("Water Breathing"), on_step = function(dtime, object, factor, duration) if not object:is_player() then return end if object:get_breath() then @@ -231,6 +237,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "leaping", + description = S("Leaping"), res_condition = function(object) return (not object:is_player()) end, @@ -248,6 +255,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "swiftness", + description = S("Swiftness"), res_condition = function(object) return (not object:is_player()) end, @@ -265,6 +273,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "slowness", + description = S("Slowness"), res_condition = function(object) return (not object:is_player()) end, @@ -282,6 +291,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "night_vision", + description = S("Night Vision"), res_condition = function(object) return (not object:is_player()) end, @@ -303,6 +313,7 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "fire_resistance", + description = S("Fire Resistance"), res_condition = function(object) return (not object:is_player()) end, @@ -313,11 +324,13 @@ mcl_potions.register_effect({ mcl_potions.register_effect({ name = "bad_omen", + description = S("Bad Omen"), particle_color = "#0b6138", }) mcl_potions.register_effect({ name = "withering", + description = S("Withering"), res_condition = function(object) local entity = object:get_luaentity() return (entity and string.find(entity.name, "wither")) @@ -696,7 +709,7 @@ function mcl_potions.make_invisible(obj_ref, hide) end -function mcl_potions._use_potion(item, obj, color) +function mcl_potions._use_potion(obj, color) local d = 0.1 local pos = obj:get_pos() minetest.sound_play("mcl_potions_drinking", {pos = pos, max_hear_distance = 6, gain = 1}) diff --git a/mods/ITEMS/mcl_potions/init.lua b/mods/ITEMS/mcl_potions/init.lua index 8c308f1fb..6df03db9d 100644 --- a/mods/ITEMS/mcl_potions/init.lua +++ b/mods/ITEMS/mcl_potions/init.lua @@ -8,14 +8,17 @@ mcl_potions = {} -- duration effects of glowstone are a time factor of 1/2 -- splash potion duration effects are reduced by a factor of 3/4 -mcl_potions.II_FACTOR = 2 +mcl_potions.POTENT_FACTOR = 2 mcl_potions.PLUS_FACTOR = 8/3 +mcl_potions.INV_FACTOR = 0.50 mcl_potions.DURATION = 180 -mcl_potions.DURATION_PLUS = mcl_potions.DURATION * mcl_potions.PLUS_FACTOR -mcl_potions.DURATION_2 = mcl_potions.DURATION / mcl_potions.II_FACTOR +mcl_potions.DURATION_INV = mcl_potions.DURATION * mcl_potions.INV_FACTOR + +mcl_potions.II_FACTOR = mcl_potions.POTENT_FACTOR -- TODO remove at some point +mcl_potions.DURATION_PLUS = mcl_potions.DURATION * mcl_potions.PLUS_FACTOR -- TODO remove at some point +mcl_potions.DURATION_2 = mcl_potions.DURATION / mcl_potions.II_FACTOR -- TODO remove at some point -mcl_potions.INV_FACTOR = 0.50 mcl_potions.SPLASH_FACTOR = 0.75 mcl_potions.LINGERING_FACTOR = 0.25