local enchanting = {}
screwdriver = screwdriver or {}
-function enchanting.formspec(pos, tooltype)
- local meta = minetest.get_meta(pos)
- local formspec = "size[9,9;]"..default.gui_slots..default.get_hotbar_bg(0.5,4.5)..
- "bgcolor[#080808BB;true]background[0,0;9,9;ench_ui.png]list[context;tool;0.9,2.9;1,1;]list[context;mese;2,2.9;1,1;]image[2,2.9;1,1;mese_layout.png]list[current_player;main;0.5,4.5;8,4;]"
-
- if tooltype == "sword" then
- formspec = formspec.."image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]"
- elseif tooltype == "tool" then
- formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability]"
- elseif tooltype == "armor" then
- formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"
- elseif tooltype == "boots" then
- formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed]"
+function enchanting.formspec(pos, tool)
+ local formspec = [[ size[9,9;]
+ bgcolor[#080808BB;true]
+ background[0,0;9,9;ench_ui.png]
+ list[context;tool;0.9,2.9;1,1;]
+ list[context;mese;2,2.9;1,1;]
+ list[current_player;main;0.5,4.5;8,4;]
+ image[2,2.9;1,1;mese_layout.png]
+ tooltip[sharp;Your sword inflicts more damage]
+ tooltip[durable;Your tool is more resistant]
+ tooltip[fast;Your tool is more powerful]
+ tooltip[strong;Your armor is more resistant]
+ tooltip[speed;Your speed is increased] ]]
+ ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
+
+ local tool_fs = {
+ ["tool"] = [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
+ image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
+ ["armor"] = "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
+ ["sword"] = "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
+ ["boots"] = [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
+ image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]] }
+
+ for cat in pairs(tool_fs) do
+ if tool == cat then
+ formspec = formspec..tool_fs[cat]
+ end
end
- meta:set_string("formspec", formspec)
- return formspec
+ minetest.get_meta(pos):set_string("formspec", formspec)
end
-function enchanting.on_put(pos, listname, _, stack, _)
- local stn = stack:get_name()
- local meta = minetest.get_meta(pos)
-
+function enchanting.on_put(pos, listname, _, stack)
if listname == "tool" then
- if stn:find("pick") or stn:find("axe") or stn:find("shovel") then
- meta:set_string("formspec", enchanting.formspec(pos, "tool"))
- elseif stn:find("sword") then
- meta:set_string("formspec", enchanting.formspec(pos, "sword"))
- elseif stn:find("chestplate") or stn:find("leggings") or stn:find("helmet") then
- meta:set_string("formspec", enchanting.formspec(pos, "armor"))
- elseif stn:find("boots") then
- meta:set_string("formspec", enchanting.formspec(pos, "boots"))
+ local tools_cat = {
+ ["tool"] = {"pick", "axe", "shovel"},
+ ["armor"] = {"chestplate", "leggings", "helmet"},
+ ["sword"] = {"sword"}, ["boots"] = {"boots"} }
+
+ for cat, name in pairs(tools_cat) do
+ for _, n in pairs(name) do
+ if stack:get_name():find(n) then
+ enchanting.formspec(pos, cat)
+ end
+ end
end
end
end
-function enchanting.fields(pos, _, fields, _)
+function enchanting.fields(pos, _, fields)
+ if fields.quit then return end
local inv = minetest.get_meta(pos):get_inventory()
- local toolstack = inv:get_stack("tool", 1)
- local toolstack_name = toolstack:get_name()
- local mesestack = inv:get_stack("mese", 1)
- local modname, toolname = toolstack_name:match("([%w_]+):([%w_]+)")
- local toolwear = toolstack:get_wear()
- local mese = mesestack:get_count()
- local ench = dump(fields):match("%w+")
- if ench == "quit" then return end
-
- if mese > 0 and fields[ench] then
- local enchanted_tool = modname..":enchanted_"..toolname.."_"..ench
- local tdef = minetest.registered_tools[enchanted_tool]
-
- if tdef then
- toolstack:replace(enchanted_tool)
- toolstack:add_wear(toolwear)
- mesestack:take_item()
- inv:set_stack("mese", 1, mesestack)
- inv:set_stack("tool", 1, toolstack)
- end
+ local tool = inv:get_stack("tool", 1)
+ local mese = inv:get_stack("mese", 1)
+ local orig_wear = tool:get_wear()
+ local mod, name = tool:get_name():match("([%w_]+):([%w_]+)")
+ local enchanted_tool = mod..":enchanted_"..name.."_"..next(fields)
+
+ if mese:get_count() > 0 and minetest.registered_tools[enchanted_tool] then
+ tool:replace(enchanted_tool)
+ tool:add_wear(orig_wear)
+ mese:take_item()
+ inv:set_stack("mese", 1, mese)
+ inv:set_stack("tool", 1, tool)
end
end
-function enchanting.dig(pos, _)
+function enchanting.dig(pos)
local inv = minetest.get_meta(pos):get_inventory()
return inv:is_empty("tool") and inv:is_empty("mese")
end
-function enchanting.put(_, listname, _, stack, _)
- local toolstack = stack:get_name()
- local count = stack:get_count()
+local function allowed(tool)
+ for item in pairs(minetest.registered_tools) do
+ if item:match("enchanted_"..tool) then
+ return true
+ end
+ end
+ return false
+end
+
+function enchanting.put(_, listname, _, stack)
+ local item = stack:get_name():match("[^:]+$")
+ if listname == "mese" and item == "mese_crystal" then
+ return stack:get_count()
+ elseif listname == "tool" and allowed(item) then
+ return 1
+ end
+
+ return 0
+end
- if listname == "mese" and
- toolstack ~= "default:mese_crystal" then return 0
- elseif listname == "tool" and not
- minetest.registered_tools[toolstack] then return 0
+function enchanting.on_take(pos, listname)
+ if listname == "tool" then
+ enchanting.formspec(pos, nil)
end
- return count
+end
+
+function enchanting.construct(pos)
+ local meta = minetest.get_meta(pos)
+ meta:set_string("infotext", "Enchantment Table")
+ enchanting.formspec(pos, nil)
+
+ local inv = meta:get_inventory()
+ inv:set_size("tool", 1)
+ inv:set_size("mese", 1)
end
xdecor.register("enchantment_table", {
"xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
"xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
},
- groups = {cracky=1, oddly_breakable_by_hand=1, level=2},
+ groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
sounds = default.node_sound_stone_defaults(),
on_rotate = screwdriver.rotate_simple,
can_dig = enchanting.dig,
- on_construct = function(pos)
- local meta = minetest.get_meta(pos)
- enchanting.formspec(pos, nil)
- meta:set_string("infotext", "Enchantment Table")
-
- local inv = meta:get_inventory()
- inv:set_size("tool", 1)
- inv:set_size("mese", 1)
- end,
- enchanting.formspec,
+ on_construct = enchanting.construct,
on_receive_fields = enchanting.fields,
on_metadata_inventory_put = enchanting.on_put,
+ on_metadata_inventory_take = enchanting.on_take,
allow_metadata_inventory_put = enchanting.put,
- allow_metadata_inventory_move = function(...) return 0 end,
- on_metadata_inventory_take = function(pos, listname, _, _, _)
- if listname == "tool" then enchanting.formspec(pos, nil) end
- end
+ allow_metadata_inventory_move = function() return 0 end
})
-local function cap(str) return str:gsub("^%l", string.upper) end
+local function cap(str)
+ return str:gsub("^%l", string.upper)
+end
-- Higher number = stronger enchant.
local use_factor = 1.2
local times_subtractor = 0.1
local damage_adder = 1
-local strenght_factor = 1.2
+local strength_factor = 1.2
+local speed_factor = 0.2
+local jump_factor = 0.2
local tools = {
--[[ Registration format :
end
minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
- description = string.format("Enchanted %s %s (%s)", cap(material), cap(tool), cap(enchant)),
+ description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
wield_image = original_tool.wield_image,
groups = {not_in_creative_inventory=1},
for armor_group, value in pairs(original_armor_groups) do
if enchant == "strong" then
- armorcaps[armor_group] = math.ceil(value * 1.2)
+ armorcaps[armor_group] = math.ceil(value * strength_factor)
elseif enchant == "speed" then
armorcaps[armor_group] = value
- armorcaps.physics_speed = 0.3
- armorcaps.physics_jump = 0.2
+ armorcaps.physics_speed = speed_factor
+ armorcaps.physics_jump = jump_factor
end
end
minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
- description = string.format("Enchanted %s %s (%s)", cap(material), cap(tool), cap(enchant)),
- inventory_image = original_tool.inventory_image.."^[colorize:blue:20",
+ description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
+ inventory_image = original_tool.inventory_image,
+ texture = "3d_armor_"..tool.."_"..material,
wield_image = original_tool.wield_image,
groups = armorcaps,
wear = 0
})
end
end
- minetest.register_alias("xdecor:enchanted_"..tool.."_"..material.."_"..enchant, mod..":enchanted_"..tool.."_"..material.."_"..enchant)
- minetest.register_alias(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, mod..":enchanted_"..tool.."_"..material.."_"..enchant)
end
end
end