2 screwdriver = screwdriver or {}
4 -- Cost in Mese crystal(s) for enchanting.
7 -- Force of the enchantments.
8 enchanting.uses = 1.2 -- Durability
9 enchanting.times = 0.1 -- Efficiency
10 enchanting.damages = 1 -- Sharpness
11 enchanting.strength = 1.2 -- Armor strength (3d_armor only)
12 enchanting.speed = 0.2 -- Player speed (3d_armor only)
13 enchanting.jump = 0.2 -- Player jumping (3d_armor only)
15 function enchanting.formspec(pos, num)
16 local meta = minetest.get_meta(pos)
17 local formspec = [[ size[9,9;]
18 bgcolor[#080808BB;true]
19 background[0,0;9,9;ench_ui.png]
20 list[context;tool;0.9,2.9;1,1;]
21 list[context;mese;2,2.9;1,1;]
22 list[current_player;main;0.5,4.5;8,4;]
23 image[2,2.9;1,1;mese_layout.png]
24 tooltip[sharp;Your weapon inflicts more damages]
25 tooltip[durable;Your tool last longer]
26 tooltip[fast;Your tool digs faster]
27 tooltip[strong;Your armor is more resistant]
28 tooltip[speed;Your speed is increased] ]]
29 ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
31 local enchant_buttons = {
32 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
33 image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability] ]],
34 "image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]",
35 "image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]",
36 [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
37 image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]]
40 formspec = formspec..(enchant_buttons[num] or "")
41 meta:set_string("formspec", formspec)
44 function enchanting.on_put(pos, listname, _, stack)
45 if listname == "tool" then
46 local stackname = stack:get_name()
49 "chestplate, leggings, helmet",
53 for idx, tools in pairs(tool_groups) do
54 if tools:find(stackname:match(":(%w+)")) then
55 enchanting.formspec(pos, idx)
61 function enchanting.fields(pos, _, fields, sender)
62 if not next(fields) or fields.quit then
65 local inv = minetest.get_meta(pos):get_inventory()
66 local tool = inv:get_stack("tool", 1)
67 local mese = inv:get_stack("mese", 1)
68 local orig_wear = tool:get_wear()
69 local mod, name = tool:get_name():match("(.*):(.*)")
70 local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
72 if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
73 minetest.sound_play("xdecor_enchanting", {to_player=sender:get_player_name(), gain=0.8})
74 tool:replace(enchanted_tool)
75 tool:add_wear(orig_wear)
76 mese:take_item(mese_cost)
77 inv:set_stack("mese", 1, mese)
78 inv:set_stack("tool", 1, tool)
82 function enchanting.dig(pos)
83 local inv = minetest.get_meta(pos):get_inventory()
84 return inv:is_empty("tool") and inv:is_empty("mese")
87 local function allowed(tool)
91 for item in pairs(minetest.registered_tools) do
92 if item:find("enchanted_"..tool) then return true end
97 function enchanting.put(_, listname, _, stack)
98 local item = stack:get_name():match("[^:]+$")
99 if listname == "mese" and item == "mese_crystal" then
100 return stack:get_count()
101 elseif listname == "tool" and allowed(item) then
107 function enchanting.on_take(pos, listname)
108 if listname == "tool" then enchanting.formspec(pos, nil) end
111 function enchanting.construct(pos)
112 local meta = minetest.get_meta(pos)
113 meta:set_string("infotext", "Enchantment Table")
114 enchanting.formspec(pos, nil)
116 local inv = meta:get_inventory()
117 inv:set_size("tool", 1)
118 inv:set_size("mese", 1)
120 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
121 local timer = minetest.get_node_timer(pos)
125 function enchanting.destruct(pos)
126 for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
127 if obj and obj:get_luaentity() and
128 obj:get_luaentity().name == "xdecor:book_open" then
134 function enchanting.timer(pos)
135 local num = #minetest.get_objects_inside_radius(pos, 0.9)
138 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
141 local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
142 local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
143 local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
144 if #bookshelves == 0 then return true end
146 local bookshelf_pos = bookshelves[math.random(1, #bookshelves)]
147 local x = pos.x - bookshelf_pos.x
148 local y = bookshelf_pos.y - pos.y
149 local z = pos.z - bookshelf_pos.z
151 if tostring(x..z):find(2) then
152 minetest.add_particle({
154 velocity = {x=x, y=2-y, z=z},
155 acceleration = {x=0, y=-2.2, z=0},
158 texture = "xdecor_glyph"..math.random(1,18)..".png"
164 xdecor.register("enchantment_table", {
165 description = "Enchantment Table",
166 tiles = {"xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
167 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
168 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"},
169 groups = {cracky=1, level=1},
170 sounds = default.node_sound_stone_defaults(),
171 on_rotate = screwdriver.rotate_simple,
172 can_dig = enchanting.dig,
173 on_timer = enchanting.timer,
174 on_construct = enchanting.construct,
175 on_destruct = enchanting.destruct,
176 on_receive_fields = enchanting.fields,
177 on_metadata_inventory_put = enchanting.on_put,
178 on_metadata_inventory_take = enchanting.on_take,
179 allow_metadata_inventory_put = enchanting.put,
180 allow_metadata_inventory_move = function() return 0 end
183 minetest.register_entity("xdecor:book_open", {
185 visual_size = {x=0.75, y=0.75},
188 textures = {"xdecor_book_open.png"},
189 on_activate = function(self)
190 local pos = self.object:getpos()
191 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
193 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
199 local function cap(S) return S:gsub("^%l", string.upper) end
201 function enchanting:register_tools(mod, def)
202 for tool in pairs(def.tools) do
203 for material in def.materials:gmatch("[%w_]+") do
204 for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
205 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
206 if not original_tool then break end
208 if original_tool.tool_capabilities then
209 local original_damage_groups = original_tool.tool_capabilities.damage_groups
210 local original_groupcaps = original_tool.tool_capabilities.groupcaps
211 local groupcaps = table.copy(original_groupcaps)
212 local fleshy = original_damage_groups.fleshy
213 local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
214 local max_drop_level = original_tool.tool_capabilities.max_drop_level
215 local group = next(original_groupcaps)
217 if enchant == "durable" then
218 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
219 elseif enchant == "fast" then
220 for i, time in pairs(original_groupcaps[group].times) do
221 groupcaps[group].times[i] = time - enchanting.times
223 elseif enchant == "sharp" then
224 fleshy = fleshy + enchanting.damages
227 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
228 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
229 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
230 wield_image = original_tool.wield_image,
231 groups = {not_in_creative_inventory=1},
232 tool_capabilities = {
233 groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
234 full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
239 if mod == "3d_armor" then
240 local original_armor_groups = original_tool.groups
242 armorcaps.not_in_creative_inventory = 1
244 for armor_group, value in pairs(original_armor_groups) do
245 if enchant == "strong" then
246 armorcaps[armor_group] = math.ceil(value * enchanting.strength)
247 elseif enchant == "speed" then
248 armorcaps[armor_group] = value
249 armorcaps.physics_speed = enchanting.speed
250 armorcaps.physics_jump = enchanting.jump
254 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
255 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
256 inventory_image = original_tool.inventory_image,
257 texture = "3d_armor_"..tool.."_"..material,
258 wield_image = original_tool.wield_image,
268 enchanting:register_tools("default", {
269 materials = "steel, bronze, mese, diamond",
271 axe = {enchants = "durable, fast"},
272 pick = {enchants = "durable, fast"},
273 shovel = {enchants = "durable, fast"},
274 sword = {enchants = "sharp"}
278 enchanting:register_tools("3d_armor", {
279 materials = "steel, bronze, gold, diamond",
281 boots = {enchants = "strong, speed"},
282 chestplate = {enchants = "strong"},
283 helmet = {enchants = "strong"},
284 leggings = {enchants = "strong"}