From da24f4fbf642066730e42685ec539ffa8e2bc76a Mon Sep 17 00:00:00 2001 From: Louis <55180044+louisroyer@users.noreply.github.com> Date: Tue, 11 Aug 2020 03:34:24 +0200 Subject: [PATCH] Fix #2 --- .luacheckrc | 17 +++++++++++ README.md | 20 +++++++++++-- init.lua | 10 ++++--- settingtypes.txt | 3 +- tool_types.lua | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 tool_types.lua diff --git a/.luacheckrc b/.luacheckrc index 7a8b68e..0f767c3 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -28,3 +28,20 @@ read_globals = { "toolranks", "farming", } + +files["tool_types.lua"] = { + read_globals = { + toolranks = { + fields = { + get_tool_type = { + read_only = false, + other_fields = false + }, + create_description = { + read_only = false, + otherfields = false + }, + } + } + } +} diff --git a/README.md b/README.md index 4fcdd4d..93005fa 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,30 @@ # minetest-toolranks-extras [![Build Status](https://travis-ci.org/louisroyer/minetest-toolranks-extras.svg?branch=master)](https://travis-ci.org/louisroyer/minetest-toolranks-extras) -This minetest mod adds [toolranks](https://github.com/lisacvuk/minetest-toolranks) support for `minetest-games`’s mods (except `default`). +This minetest mod extends [toolranks](https://github.com/lisacvuk/minetest-toolranks). It adds support for `minetest-games`’s mods (except `default`). This mod currently adds support for hoes from `farming` mod. This mod is compatible with [farming redo](https://notabug.org/tenplus1/farming) (nothing breaks if you have both installed). +This mod also adds an API to register new types of tools for toolranks. + +## API + +```lua +--[[ +-- `keyword` is a string to be detected in tool description +-- `tool_type` is a localized tool type (if nil, then unregister the keyword) +--]] +toolranks_extras.register_tool_type(keyword, tool_type) + +-- example +toolranks_extras.register_tool_type("battle_axe", S("battle axe")) +``` + ![Screenshot](screenshot.png) ## License - CC0-1.0, Louis Royer 2020 ## Settings -Settings are available to disable support of some mods (currently only `farming`). This is useful if you want to reimplement minetest-games modules +- Settings are available to disable support of some mods (currently only `farming`). This is useful if you want to reimplement minetest-games modules with a `toolranks` support. It is also possible to ask me to automatically disabling support if your mod is detected (like it is done with farming redo). +- Extras tool types registering can be disabled (registering function will still be available, but will do nothing). diff --git a/init.lua b/init.lua index 954188a..f5db140 100644 --- a/init.lua +++ b/init.lua @@ -3,11 +3,13 @@ local MP = minetest.get_modpath("toolranks_extras") toolranks_extras = {} -- mod information -toolranks_extras.mod = {version = "1.3.3", author = "Louis Royer"} +toolranks_extras.mod = {version = "1.4.0", author = "Louis Royer"} -- settings -toolranks_extras.settings = - {enable_farming_tools = minetest.settings:get_bool("toolranks_extra.farming", true)} +toolranks_extras.settings = { + enable_farming_tools = minetest.settings:get_bool("toolranks_extras.farming", true), + enable_tool_types = minetest.settings:get_bool("toolranks_extras.tool_types", true), +} -- XXX: when https://github.com/minetest/minetest/pull/7377 -- is merged, we can remove this function @@ -33,7 +35,7 @@ if toolranks.add_tool == nil then .." toolranks (at least version 1.2).") end - +dofile(MP.."/tool_types.lua") if use_farming and (not use_farming_redo) and toolranks_extras.settings.enable_farming_tools then dofile(MP.."/hoe.lua") diff --git a/settingtypes.txt b/settingtypes.txt index 0a354a4..6455612 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -1 +1,2 @@ -toolranks_extra.farming (Enable toolranks on farming tools) bool true +toolranks_extras.farming (Enable toolranks on farming tools) bool true +toolranks_extras.tool_types (Enable registering of extras tool types) bool true diff --git a/tool_types.lua b/tool_types.lua new file mode 100644 index 0000000..3fde758 --- /dev/null +++ b/tool_types.lua @@ -0,0 +1,74 @@ +toolranks_extras.registered_tool_types = {} +--[[ +-- `keyword` is a string to be detected in tool description +-- `tool_type` is a localized tool type (if nil, then unregister the keyword) +-- example +-- `toolranks_extras.register_tool_type("battle_axe", S("battle axe"))` +--]] +toolranks_extras.register_tool_type = function(keyword, tool_type) + toolranks_extras.registered_tool_types[string.lower(keyword)] = tool_type +end + +toolranks_extras.ignored_toolranks_tool_types = {} +--[[ +-- `keyword` is a string to be detected in tool description +-- `tool_type` is a localized tool type +-- +-- example +-- `toolranks_extras.ignore_toolranks_tool_type("pickaxe")` +-- This only works with tool types built in toolranks default get_tool_types function. +--]] +toolranks_extras.ignore_toolranks_tool_type = function(keyword) + table.insert(toolranks_extras.ignored_toolranks_tool_types, string.lower(keyword)) +end +toolranks_extras.ignore_toolranks_tool_type("tool") + +if toolranks_extras.settings.enable_tool_types then + local TR = minetest.get_translator("toolranks") + local origin_get_tool_type = toolranks.get_tool_type + --[[ + -- Returns nil if the keyword is ignored, else returns the keyword. + --]] + local function check_ignore(keyword) + for _, k in ipairs(toolranks_extras.ignored_toolranks_tool_types) do + if keyword == k then + return + end + end + return keyword + end + + -- overwrite of toolranks.get_tool_type(description) + toolranks.get_tool_type = function(description) + -- execute original function + local tool_type = check_ignore(origin_get_tool_type(description)) + if tool_type ~= nil then + return TR(tool_type) + end + -- search for registered tool types + local d = string.lower(description) + for k, desc in pairs(toolranks_extras.registered_tool_types) do + if string.find(d, k) then + return desc + end + end + -- fallback + return TR("tool") + end + + -- overwrite of toolranks.create_description(name, uses) + toolranks.create_description = function(name, uses) + local tooltype = toolranks.get_tool_type(name) + local newdesc = TR( + "@1@2\n@3Level @4 @5\n@6@Node dug: @7", + toolranks.colors.green, + name, + toolranks.colors.gold, + toolranks.get_level(uses), + tooltype, + toolranks.colors.grey, + (type(uses) == "number" and uses or 0) + ) + return newdesc + end +end