2 screwdriver = screwdriver or {}
4 function enchanting.formspec(pos, num)
5 local formspec = [[ size[9,9;]
6 bgcolor[#080808BB;true]
7 background[0,0;9,9;ench_ui.png]
8 list[context;tool;0.9,2.9;1,1;]
9 list[context;mese;2,2.9;1,1;]
10 list[current_player;main;0.5,4.5;8,4;]
11 image[2,2.9;1,1;mese_layout.png]
12 tooltip[sharp;Your sword inflicts more damage]
13 tooltip[durable;Your tool is more resistant]
14 tooltip[fast;Your tool is more powerful]
15 tooltip[strong;Your armor is more resistant]
16 tooltip[speed;Your speed is increased] ]]
17 ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
20 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
21 image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
22 "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
23 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
24 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
25 image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]] }
27 formspec = formspec..(tool_enchs[num] or "")
28 minetest.get_meta(pos):set_string("formspec", formspec)
31 function enchanting.on_put(pos, listname, _, stack)
32 if listname == "tool" then
33 for k, v in pairs({"axe, pick, shovel",
34 "chestplate, leggings, helmet",
36 if v:match(stack:get_name():match(":(.-)%_")) then
37 enchanting.formspec(pos, k)
43 function enchanting.fields(pos, _, fields)
44 if fields.quit then return end
45 local inv = minetest.get_meta(pos):get_inventory()
46 local tool = inv:get_stack("tool", 1)
47 local mese = inv:get_stack("mese", 1)
48 local orig_wear = tool:get_wear()
49 local mod, name = tool:get_name():match("(.*):(.*)")
50 local enchanted_tool = mod..":enchanted_"..name.."_"..next(fields)
52 if mese:get_count() > 0 and minetest.registered_tools[enchanted_tool] then
53 tool:replace(enchanted_tool)
54 tool:add_wear(orig_wear)
56 inv:set_stack("mese", 1, mese)
57 inv:set_stack("tool", 1, tool)
61 function enchanting.dig(pos)
62 local inv = minetest.get_meta(pos):get_inventory()
63 return inv:is_empty("tool") and inv:is_empty("mese")
66 local function allowed(tool)
67 for item in pairs(minetest.registered_tools) do
68 if item:match("enchanted_"..tool) then return true end
73 function enchanting.put(_, listname, _, stack)
74 local item = stack:get_name():match("[^:]+$")
75 if listname == "mese" and item == "mese_crystal" then
76 return stack:get_count()
77 elseif listname == "tool" and allowed(item) then
83 function enchanting.on_take(pos, listname)
84 if listname == "tool" then
85 enchanting.formspec(pos, nil)
89 function enchanting.construct(pos)
90 local meta = minetest.get_meta(pos)
91 meta:set_string("infotext", "Enchantment Table")
92 enchanting.formspec(pos, nil)
94 local inv = meta:get_inventory()
95 inv:set_size("tool", 1)
96 inv:set_size("mese", 1)
99 xdecor.register("enchantment_table", {
100 description = "Enchantment Table",
102 "xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
103 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
104 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
106 groups = {cracky=1, oddly_breakable_by_hand=1, level=1},
107 sounds = default.node_sound_stone_defaults(),
108 on_rotate = screwdriver.rotate_simple,
109 can_dig = enchanting.dig,
110 on_construct = enchanting.construct,
111 on_receive_fields = enchanting.fields,
112 on_metadata_inventory_put = enchanting.on_put,
113 on_metadata_inventory_take = enchanting.on_take,
114 allow_metadata_inventory_put = enchanting.put,
115 allow_metadata_inventory_move = function() return 0 end
118 local function cap(str)
119 return str:gsub("^%l", string.upper)
122 -- Higher number = stronger enchant.
123 enchanting.uses = 1.2
124 enchanting.times = 0.1
125 enchanting.damages = 1
126 enchanting.strength = 1.2
127 enchanting.speed = 0.2
128 enchanting.jump = 0.2
131 --[[ Registration format:
134 {tool name, tool group, enchantments}
138 "steel, bronze, mese, diamond",
139 {"axe", "choppy", "durable, fast"},
140 {"pick", "cracky", "durable, fast"},
141 {"shovel", "crumbly", "durable, fast"},
142 {"sword", "fleshy", "sharp"}
145 "steel, bronze, gold, diamond",
146 {"boots", nil, "strong, speed"},
147 {"chestplate", nil, "strong"},
148 {"helmet", nil, "strong"},
149 {"leggings", nil, "strong"}
153 for mod, defs in pairs(tools) do
154 for material in defs[1]:gmatch("[%w_]+") do
155 for _, tooldef in next, defs, 1 do
156 for enchant in tooldef[3]:gmatch("[%w_]+") do
157 local tool, group = tooldef[1], tooldef[2]
158 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
160 if original_tool then
161 if mod == "default" then
162 local original_damage_groups = original_tool.tool_capabilities.damage_groups
163 local original_groupcaps = original_tool.tool_capabilities.groupcaps
164 local groupcaps = table.copy(original_groupcaps)
165 local fleshy = original_damage_groups.fleshy
166 local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
167 local max_drop_level = original_tool.tool_capabilities.max_drop_level
169 if enchant == "durable" then
170 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
171 elseif enchant == "fast" then
173 groupcaps[group].times[i] = original_groupcaps[group].times[i] - enchanting.times
175 elseif enchant == "sharp" then
176 fleshy = fleshy + enchanting.damages
179 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
180 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
181 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
182 wield_image = original_tool.wield_image,
183 groups = {not_in_creative_inventory=1},
184 tool_capabilities = {
185 groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
186 full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
191 if mod == "3d_armor" then
192 local original_armor_groups = original_tool.groups
193 local armorcaps = table.copy(original_armor_groups)
195 armorcaps.not_in_creative_inventory = 1
197 for armor_group, value in pairs(original_armor_groups) do
198 if enchant == "strong" then
199 armorcaps[armor_group] = math.ceil(value * enchanting.strength)
200 elseif enchant == "speed" then
201 armorcaps[armor_group] = value
202 armorcaps.physics_speed = enchanting.speed
203 armorcaps.physics_jump = enchanting.jump
207 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
208 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
209 inventory_image = original_tool.inventory_image,
210 texture = "3d_armor_"..tool.."_"..material,
211 wield_image = original_tool.wield_image,