+screwdriver = screwdriver or {}
+
+-- Cost in Mese crystal(s) for enchanting.
+local mese_cost = 1
+
+-- Force of the enchantments.
+enchanting.uses = 1.2 -- Durability
+enchanting.times = 0.1 -- Efficiency
+enchanting.damages = 1 -- Sharpness
+enchanting.strength = 1.2 -- Armor strength (3d_armor only)
+enchanting.speed = 0.2 -- Player speed (3d_armor only)
+enchanting.jump = 0.2 -- Player jumping (3d_armor only)
+
+function enchanting.formspec(pos, num)
+ local meta = minetest.get_meta(pos)
+ 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 weapon inflicts more damages]
+ tooltip[durable;Your tool last longer]
+ tooltip[fast;Your tool digs faster]
+ 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 enchant_buttons = {
+ [[ 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] ]],
+ "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
+ "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
+ [[ 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] ]]
+ }
+
+ formspec = formspec..(enchant_buttons[num] or "")
+ meta:set_string("formspec", formspec)
+end
+
+function enchanting.on_put(pos, listname, _, stack)
+ if listname == "tool" then
+ local stackname = stack:get_name()
+ local tool_groups = {
+ "axe, pick, shovel",
+ "chestplate, leggings, helmet",
+ "sword", "boots"
+ }
+
+ for idx, tools in pairs(tool_groups) do
+ if tools:find(stackname:match(":(%w+)")) then
+ enchanting.formspec(pos, idx)
+ end
+ end
+ end
+end
+
+function enchanting.fields(pos, _, fields)
+ if fields.quit then return end
+ local inv = minetest.get_meta(pos):get_inventory()
+ 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("(.*):(.*)")
+ local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
+
+ if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
+ tool:replace(enchanted_tool)
+ tool:add_wear(orig_wear)
+ mese:take_item(mese_cost)
+ inv:set_stack("mese", 1, mese)
+ inv:set_stack("tool", 1, tool)
+ end
+end
+
+function enchanting.dig(pos)
+ local inv = minetest.get_meta(pos):get_inventory()
+ return inv:is_empty("tool") and inv:is_empty("mese")
+end
+
+local function allowed(tool)
+ for item in pairs(minetest.registered_tools) do
+ if item:find("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
+
+function enchanting.on_take(pos, listname)
+ if listname == "tool" then enchanting.formspec(pos, nil) end
+end