Add API to register custom tool enchantments
This commit is contained in:
parent
32188e3eba
commit
f1cf8c3fb0
@ -21,6 +21,27 @@ local function to_percent(orig_value, final_value)
|
|||||||
return abs(ceil(((final_value - orig_value) / orig_value) * 100))
|
return abs(ceil(((final_value - orig_value) / orig_value) * 100))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function enchanting:get_tooltip_raw(enchant, percent)
|
||||||
|
local specs = {
|
||||||
|
durable = "#00baff",
|
||||||
|
fast = "#74ff49",
|
||||||
|
sharp = "#ffff00",
|
||||||
|
}
|
||||||
|
local enchant_loc = {
|
||||||
|
fast = S("Efficiency"),
|
||||||
|
durable = S("Durability"),
|
||||||
|
sharp = S("Sharpness"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if minetest.colorize then
|
||||||
|
--~ Tooltip in format "<enchantment name> (+<bonus>%)", e.g. "Efficiency (+5%)"
|
||||||
|
return minetest.colorize(specs[enchant], S("@1 (+@2%)", enchant_loc[enchant], percent))
|
||||||
|
else
|
||||||
|
return S("@1 (+@2%", enchant_loc[enchant], percent)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
function enchanting:get_tooltip(enchant, orig_caps, fleshy, bonus_defs)
|
function enchanting:get_tooltip(enchant, orig_caps, fleshy, bonus_defs)
|
||||||
local bonus = {durable = 0, efficiency = 0, damages = 0}
|
local bonus = {durable = 0, efficiency = 0, damages = 0}
|
||||||
|
|
||||||
@ -40,23 +61,12 @@ function enchanting:get_tooltip(enchant, orig_caps, fleshy, bonus_defs)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local specs = {
|
local specs = {
|
||||||
durable = {"#00baff", bonus.durable},
|
durable = bonus.durable,
|
||||||
fast = {"#74ff49", bonus.efficiency},
|
fast = bonus.efficiency,
|
||||||
sharp = {"#ffff00", bonus.damages},
|
sharp = bonus.damages,
|
||||||
}
|
}
|
||||||
|
local percent = specs[enchant]
|
||||||
local enchant_loc = {
|
return enchanting:get_tooltip_raw(enchant, percent)
|
||||||
fast = S("Efficiency"),
|
|
||||||
durable = S("Durability"),
|
|
||||||
sharp = S("Sharpness"),
|
|
||||||
}
|
|
||||||
|
|
||||||
if minetest.colorize then
|
|
||||||
--~ Tooltip in format "<enchantment name> (+<bonus>%)", e.g. "Efficiency (+5%)"
|
|
||||||
return minetest.colorize(specs[enchant][1], S("@1 (+@2%)", enchant_loc[enchant], specs[enchant][2]))
|
|
||||||
else
|
|
||||||
return S("@1 (+@2%", enchant_loc[enchant], specs[enchant][2])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local enchant_buttons = {
|
local enchant_buttons = {
|
||||||
@ -279,6 +289,14 @@ minetest.register_lbm({
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function enchanting:enchant_texture(img)
|
||||||
|
if img == nil or img == "" or type(img) ~= "string" then
|
||||||
|
return "no_texture.png"
|
||||||
|
else
|
||||||
|
return "("..img.. ")^[colorize:violet:50"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function enchanting:register_tool(original_tool_name, def)
|
function enchanting:register_tool(original_tool_name, def)
|
||||||
local original_tool = reg_tools[original_tool_name]
|
local original_tool = reg_tools[original_tool_name]
|
||||||
if not original_tool then
|
if not original_tool then
|
||||||
@ -330,11 +348,7 @@ function enchanting:register_tool(original_tool_name, def)
|
|||||||
local enchantedTool = original_tool.mod_origin .. ":enchanted_" .. original_basename .. "_" .. enchant
|
local enchantedTool = original_tool.mod_origin .. ":enchanted_" .. original_basename .. "_" .. enchant
|
||||||
|
|
||||||
local invimg = original_tool.inventory_image
|
local invimg = original_tool.inventory_image
|
||||||
if invimg == nil or invimg == "" then
|
invimg = enchanting:enchant_texture(invimg)
|
||||||
invimg = "no_texture.png"
|
|
||||||
else
|
|
||||||
invimg = "("..invimg.. ")^[colorize:violet:50"
|
|
||||||
end
|
|
||||||
local wieldimg = original_tool.wield_image
|
local wieldimg = original_tool.wield_image
|
||||||
if wieldimg == nil or wieldimg == "" then
|
if wieldimg == nil or wieldimg == "" then
|
||||||
wieldimg = invimg
|
wieldimg = invimg
|
||||||
@ -361,6 +375,16 @@ function enchanting:register_tool(original_tool_name, def)
|
|||||||
reg_enchantable_tools[original_tool_name] = true
|
reg_enchantable_tools[original_tool_name] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function enchanting:register_custom_tool(original_tool_name, enchanted_tools)
|
||||||
|
if not available_tool_enchants[original_tool_name] then
|
||||||
|
available_tool_enchants[original_tool_name] = {}
|
||||||
|
end
|
||||||
|
for enchant, v in pairs(enchanted_tools) do
|
||||||
|
table.insert(available_tool_enchants[original_tool_name], enchant)
|
||||||
|
end
|
||||||
|
reg_enchantable_tools[original_tool_name] = true
|
||||||
|
end
|
||||||
|
|
||||||
-- Register enchantments for default tools from Minetest Game
|
-- Register enchantments for default tools from Minetest Game
|
||||||
local materials = {"steel", "bronze", "mese", "diamond"}
|
local materials = {"steel", "bronze", "mese", "diamond"}
|
||||||
local tooltypes = {
|
local tooltypes = {
|
||||||
@ -435,3 +459,52 @@ Arguments:
|
|||||||
xdecor.register_enchantable_tool = function(toolname, def)
|
xdecor.register_enchantable_tool = function(toolname, def)
|
||||||
enchanting:register_tool(toolname, def)
|
enchanting:register_tool(toolname, def)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[ Registers a custom tool enchantment.
|
||||||
|
Here, you are fully free to design the tool yourself.
|
||||||
|
|
||||||
|
The enchanted tools should follow these guidelines:
|
||||||
|
|
||||||
|
1) Use xdecor.enchant_description to generate the description and short_description
|
||||||
|
2) Use xdecor.enchant_texture to generate the inventory_image and wield_image
|
||||||
|
3) Set groups to { not_in_creative_inventory = 1 }
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
* toolname: Itemstring of original tool to enchant
|
||||||
|
* enchanted_tools: Table of enchanted tools.
|
||||||
|
* The keys are enchantment names from "enchants" in xdecor.register_enchantable_tool
|
||||||
|
* The values are the itemstrings of the enchanted tools for those
|
||||||
|
enchantments
|
||||||
|
]]
|
||||||
|
xdecor.register_custom_enchantable_tool = function(toolname, enchanted_tools)
|
||||||
|
enchanting:register_custom_tool(toolname, enchanted_tools)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Takes a texture (string) and applies an "enchanting" modifier on it.
|
||||||
|
-- Useful when you want to register custom tool enchantments.
|
||||||
|
xdecor.enchant_texture = function(texture)
|
||||||
|
return enchanting:enchant_texture(texture)
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Takes a description of a normal tool and modifies it for the enchanted tool variant.
|
||||||
|
Arguments:
|
||||||
|
* description: Original description to modify
|
||||||
|
* enchant: Enchantment type. One of the enchantment names from "enchants" in xdecor.register_enchantable_tool
|
||||||
|
* percent: Percentage to display
|
||||||
|
|
||||||
|
Returns: <description>, <short_description>
|
||||||
|
|
||||||
|
-- Useful when you want to register custom tool enchantments.
|
||||||
|
]]
|
||||||
|
xdecor.enchant_description = function(description, enchant, percent)
|
||||||
|
local append = enchanting:get_tooltip_raw(enchant, percent)
|
||||||
|
local desc = S("Enchanted @1\n@2", description, append)
|
||||||
|
local short_desc S("Enchanted @1", description)
|
||||||
|
return desc, short_desc
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--[[ END OF API ]]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user