From 040115397db85881feb2ff580689c072f0ddc025 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 3 Feb 2020 19:31:18 +0100 Subject: [PATCH] Initial commit --- API.md | 31 ++++++++++++ README.md | 12 +++++ init.lua | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ locale/template.txt | 23 +++++++++ locale/tt.de.tr | 23 +++++++++ mod.conf | 2 + 6 files changed, 232 insertions(+) create mode 100644 API.md create mode 100644 README.md create mode 100644 init.lua create mode 100644 locale/template.txt create mode 100644 locale/tt.de.tr create mode 100644 mod.conf diff --git a/API.md b/API.md new file mode 100644 index 0000000..4616fdc --- /dev/null +++ b/API.md @@ -0,0 +1,31 @@ +# Tooltip API +This API explains how to handle the extended item tooltips (`description` field). + +## Fields + +Add these to the item definition. + +* `_tt_ignore`: If `true`, the `description` of this item won't be altered at all +* `_tt_help`: Custom help text +* `_tt_food`: If `true`, item is a food item that can be consumed by the player +* `_tt_food_hp`: Health increase (in HP) for player when consuming food item +* `_tt_food_satiation`: Satiation increase for player when consuming food item (note: the meaning of satiation is depending on the game being used; some games might not have a satiation mechanic at all, in which case you can skip this field) + +Once this mod had overwritten the `description` field of an item was overwritten, it will save the original (unaltered) `description` in the `_tt_original_description` field. + +## `tt.register_snippet(func)` + +Register a custom snippet function. +`func` is a function of the form `func(itemstring)`. +It will be called for (nearly) every itemstring and it must return a string you want to append to this item or `nil` if nothing shall be appended. +You can optionally return the text color in `"#RRGGBB"` format as the second return value. + +Example: + +``` +tt.register_snippet(function(itemstring) + if minetest.get_item_group(itemstring, "magic") == 1 then + return "This item is magic" + end +end) +``` diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca76902 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Extended Tooltip (`tt`) +This mod extends the tooltip of items to add more informative texts. + +It displays the following useful information: +* Weapon damage and speed +* Tool properties +* Noteworthy block properties +* Food satiation +* Custom help text (added by mods) + +## License +MIT License. diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..041c5d2 --- /dev/null +++ b/init.lua @@ -0,0 +1,141 @@ +local S = minetest.get_translator("tt") +local COLOR_DEFAULT = "#d0ffd0" +local COLOR_DANGER = "#ffff00" +local COLOR_GOOD = "#00ff00" + +tt = {} +tt.registered_snippets = {} + +local function append_descs() + for itemstring, def in pairs(minetest.registered_items) do + if itemstring ~= "" and itemstring ~= "air" and itemstring ~= "ignore" and itemstring ~= "unknown" and def ~= nil and def.description ~= nil and def.description ~= "" and def._tt_ignore ~= true then + local desc = def.description + local orig_desc = desc + -- Custom text + if def._tt_help then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, def._tt_help) + end + -- Tool info + if def.tool_capabilities then + -- Digging stats + if def.tool_capabilities.groupcaps then + -- TODO: Add more detail (such as digging speed) + --local groups = {} + --for group, caps in pairs(def.tool_capabilities.groupcaps) do + -- table.insert(groups, group) + --end + --desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Digs: @1", table.concat(groups, ", "))) + end + -- Weapon stats + if def.tool_capabilities.damage_groups then + for group, damage in pairs(def.tool_capabilities.damage_groups) do + local msg + if group == "fleshy" then + if damage >= 0 then + msg = S("Damage: @1", damage) + else + msg = S("Healing: @1", math.abs(damage)) + end + else + if damage >= 0 then + msg = S("Damage (@1): @2", group, damage) + else + msg = S("Healing (@1): @2", group, math.abs(damage)) + end + end + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, msg) + end + local full_punch_interval = def.tool_capabilities.full_punch_interval + if not full_punch_interval then + full_punch_interval = 1 + end + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Full punch interval: @1s", full_punch_interval)) + end + end + -- Food + if def._tt_food then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Food item")) + if def._tt_food_hp then + local msg = S("+@1 food points", def._tt_food_hp) + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, msg) + end + -- NOTE: This is unused atm + --[[if def._tt_food_satiation then + if def._tt_food_satiation >= 0 then + msg = S("+@1 satiation", def._tt_food_satiation) + else + msg = S("@1 satiation", def._tt_food_satiation) + end + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, msg) + end]] + end + -- Node info + -- Damage-related + do + if def.damage_per_second then + if def.damage_per_second > 0 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DANGER, S("Contact damage: @1 per second", def.damage_per_second)) + elseif def.damage_per_second < 0 then + desc = desc .. "\n" .. minetest.colorize(COLOR_GOOD, S("Contact healing: @1 per second", math.abs(def.damage_per_second))) + end + end + if def.drowning and def.drowning ~= 0 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DANGER, S("Drowning damage: @1", def.drowning)) + end + local tmp = minetest.get_item_group(itemstring, "fall_damage_add_percent") + if tmp > 0 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DANGER, S("Fall damage: +@1%", tmp)) + elseif tmp == -100 then + desc = desc .. "\n" .. minetest.colorize(COLOR_GOOD, S("No fall damage")) + elseif tmp < 0 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Fall damage: @1%", tmp)) + end + end + -- Movement-related node facts + if minetest.get_item_group(itemstring, "disable_jump") == 1 and not def.climbable then + if def.liquidtype == "none" then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("No jumping")) + else + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("No swimming upwards")) + end + end + if def.climbable then + if minetest.get_item_group(itemstring, "disable_jump") == 1 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Climbable (only downwards)")) + else + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Climbable")) + end + end + if minetest.get_item_group(itemstring, "slippery") >= 1 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Slippery")) + end + local tmp = minetest.get_item_group(itemstring, "bouncy") + if tmp >= 1 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Bouncy (@1%)", tmp)) + end + -- Node appearance + tmp = def.light_source + if tmp and tmp >= 1 then + desc = desc .. "\n" .. minetest.colorize(COLOR_DEFAULT, S("Luminance: @1", tmp)) + end + -- Custom functions + for s=1, #tt.registered_snippets do + local str, snippet_color = tt.registered_snippets[s](itemstring) + if not snippet_color then + snippet_color = COLOR_DEFAULT + end + if str then + desc = desc .. "\n" .. minetest.colorize(snippet_color, str) + end + end + + minetest.override_item(itemstring, { description = desc, _tt_original_description = orig_desc }) + end + end +end + +tt.register_snippet = function(func) + table.insert(tt.registered_snippets, func) +end + +minetest.register_on_mods_loaded(append_descs) diff --git a/locale/template.txt b/locale/template.txt new file mode 100644 index 0000000..02cce0c --- /dev/null +++ b/locale/template.txt @@ -0,0 +1,23 @@ +# textdomain:tt +Damage: @1= +Damage (@1): @2= +Healing: @1= +Healing (@1): @2= +Full punch interval: @1s= +Food item= ++@1 satiation= +@1 satiation= ++@1 food points= +Contact damage: @1 per second= +Contact healing: @1 per second= +Drowning damage: @1= +Bouncy (@1%)= +Luminance: @1= +Slippery= +Climbable= +Climbable (only downwards)= +No jumping= +No swimming upwards= +Fall damage: @1%= +Fall damage: +@1%= +No fall damage= diff --git a/locale/tt.de.tr b/locale/tt.de.tr new file mode 100644 index 0000000..10a59a9 --- /dev/null +++ b/locale/tt.de.tr @@ -0,0 +1,23 @@ +# textdomain:tt +Damage: @1=Schaden: @1 +Damage (@1): @2=Schaden (@1): @2 +Healing: @1=Heilung: @1 +Healing (@1): @2=Heilung (@1): @2 +Full punch interval: @1s=Zeit zum Ausholen: @1s +Food item=Lebensmittel ++@1 satiation=+@1 Sättigung +@1 satiation=@1 Sättigung ++@1 food points=+@1 Nahrungspunkte +Contact damage: @1 per second=Kontaktschaden: @1 pro Sekunde +Contact healing: @1 per second=Kontaktheilung: @1 pro Sekunde +Drowning damage: @1=Ertrinkensschaden: @1 +Bouncy (@1%)=Sprunghaft (@1%) +Luminance: @1=Lichtstärke: @1 +Slippery=Rutschig +Climbable=Erkletterbar +Climbable (only downwards)=Erkletterbar (nur nach unten) +No jumping=Kein Springen +No swimming upwards=Kein nach oben schwimmen +Fall damage: @1%=Fallschaden: @1% +Fall damage: +@1%=Fallschaden: +@1% +No fall damage=Kein Fallschaden diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..aee1572 --- /dev/null +++ b/mod.conf @@ -0,0 +1,2 @@ +name = tt +description = Appends a helpful tooltip to the item description -- 2.11.4.GIT