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 fields.quit then return end
63 local inv = minetest.get_meta(pos):get_inventory()
64 local tool = inv:get_stack("tool", 1)
65 local mese = inv:get_stack("mese", 1)
66 local orig_wear = tool:get_wear()
67 local mod, name = tool:get_name():match("(.*):(.*)")
68 local enchanted_tool = (mod or "")..":enchanted_"..(name or "").."_"..next(fields)
70 if mese:get_count() >= mese_cost and minetest.registered_tools[enchanted_tool] then
71 minetest.sound_play("xdecor_enchanting", {to_player=sender:get_player_name(), gain=0.8})
72 tool:replace(enchanted_tool)
73 tool:add_wear(orig_wear)
74 mese:take_item(mese_cost)
75 inv:set_stack("mese", 1, mese)
76 inv:set_stack("tool", 1, tool)
80 function enchanting.dig(pos)
81 local inv = minetest.get_meta(pos):get_inventory()
82 return inv:is_empty("tool") and inv:is_empty("mese")
85 local function allowed(tool)
86 for item in pairs(minetest.registered_tools) do
87 if item:find("enchanted_"..tool) then return true end
92 function enchanting.put(_, listname, _, stack)
93 local item = stack:get_name():match("[^:]+$")
94 if listname == "mese" and item == "mese_crystal" then
95 return stack:get_count()
96 elseif listname == "tool" and allowed(item) then
102 function enchanting.on_take(pos, listname)
103 if listname == "tool" then enchanting.formspec(pos, nil) end
106 function enchanting.construct(pos)
107 local meta = minetest.get_meta(pos)
108 meta:set_string("infotext", "Enchantment Table")
109 enchanting.formspec(pos, nil)
111 local inv = meta:get_inventory()
112 inv:set_size("tool", 1)
113 inv:set_size("mese", 1)
115 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
116 local timer = minetest.get_node_timer(pos)
120 function enchanting.destruct(pos)
121 for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.9)) do
122 if obj and obj:get_luaentity() and
123 obj:get_luaentity().name == "xdecor:book_open" then
129 function enchanting.timer(pos)
130 local node = minetest.get_node(pos)
131 local num = #minetest.get_objects_inside_radius(pos, 0.9)
134 minetest.add_entity({x=pos.x, y=pos.y+0.85, z=pos.z}, "xdecor:book_open")
137 local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
138 local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
139 local bookshelves = minetest.find_nodes_in_area(minp, maxp, "default:bookshelf")
140 if #bookshelves == 0 then return true end
142 local bookshelf_pos = bookshelves[math.random(1, #bookshelves)]
143 local x = pos.x - bookshelf_pos.x
144 local y = bookshelf_pos.y - pos.y
145 local z = pos.z - bookshelf_pos.z
147 if tostring(x..z):find(2) then
148 minetest.add_particle({
150 velocity = {x=x, y=2-y, z=z},
151 acceleration = {x=0, y=-2.2, z=0},
154 texture = "xdecor_glyph"..math.random(1,18)..".png"
160 xdecor.register("enchantment_table", {
161 description = "Enchantment Table",
162 tiles = {"xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
163 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
164 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"},
165 groups = {cracky=1, level=1},
166 sounds = default.node_sound_stone_defaults(),
167 on_rotate = screwdriver.rotate_simple,
168 can_dig = enchanting.dig,
169 on_timer = enchanting.timer,
170 on_construct = enchanting.construct,
171 on_destruct = enchanting.destruct,
172 on_receive_fields = enchanting.fields,
173 on_metadata_inventory_put = enchanting.on_put,
174 on_metadata_inventory_take = enchanting.on_take,
175 allow_metadata_inventory_put = enchanting.put,
176 allow_metadata_inventory_move = function() return 0 end
179 minetest.register_entity("xdecor:book_open", {
181 visual_size = {x=0.75, y=0.75},
184 textures = {"xdecor_book_open.png"},
185 on_activate = function(self)
186 local pos = self.object:getpos()
187 local pos_under = {x=pos.x, y=pos.y-1, z=pos.z}
189 if minetest.get_node(pos_under).name ~= "xdecor:enchantment_table" then
195 local function cap(S) return S:gsub("^%l", string.upper) end
197 function enchanting:register_tools(mod, def)
198 for tool in pairs(def.tools) do
199 for material in def.materials:gmatch("[%w_]+") do
200 for enchant in def.tools[tool].enchants:gmatch("[%w_]+") do
201 local original_tool = minetest.registered_tools[mod..":"..tool.."_"..material]
202 if not original_tool then break end
204 if original_tool.tool_capabilities then
205 local original_damage_groups = original_tool.tool_capabilities.damage_groups
206 local original_groupcaps = original_tool.tool_capabilities.groupcaps
207 local groupcaps = table.copy(original_groupcaps)
208 local fleshy = original_damage_groups.fleshy
209 local full_punch_interval = original_tool.tool_capabilities.full_punch_interval
210 local max_drop_level = original_tool.tool_capabilities.max_drop_level
211 local group = next(original_groupcaps)
213 if enchant == "durable" then
214 groupcaps[group].uses = math.ceil(original_groupcaps[group].uses * enchanting.uses)
215 elseif enchant == "fast" then
216 for i, time in pairs(original_groupcaps[group].times) do
217 groupcaps[group].times[i] = time - enchanting.times
219 elseif enchant == "sharp" then
220 fleshy = fleshy + enchanting.damages
223 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
224 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
225 inventory_image = original_tool.inventory_image.."^[colorize:violet:50",
226 wield_image = original_tool.wield_image,
227 groups = {not_in_creative_inventory=1},
228 tool_capabilities = {
229 groupcaps = groupcaps, damage_groups = {fleshy = fleshy},
230 full_punch_interval = full_punch_interval, max_drop_level = max_drop_level
235 if mod == "3d_armor" then
236 local original_armor_groups = original_tool.groups
238 armorcaps.not_in_creative_inventory = 1
240 for armor_group, value in pairs(original_armor_groups) do
241 if enchant == "strong" then
242 armorcaps[armor_group] = math.ceil(value * enchanting.strength)
243 elseif enchant == "speed" then
244 armorcaps[armor_group] = value
245 armorcaps.physics_speed = enchanting.speed
246 armorcaps.physics_jump = enchanting.jump
250 minetest.register_tool(":"..mod..":enchanted_"..tool.."_"..material.."_"..enchant, {
251 description = "Enchanted "..cap(material).." "..cap(tool).." ("..cap(enchant)..")",
252 inventory_image = original_tool.inventory_image,
253 texture = "3d_armor_"..tool.."_"..material,
254 wield_image = original_tool.wield_image,
264 enchanting:register_tools("default", {
265 materials = "steel, bronze, mese, diamond",
267 axe = {enchants = "durable, fast"},
268 pick = {enchants = "durable, fast"},
269 shovel = {enchants = "durable, fast"},
270 sword = {enchants = "sharp"}
274 enchanting:register_tools("3d_armor", {
275 materials = "steel, bronze, gold, diamond",
277 boots = {enchants = "strong, speed"},
278 chestplate = {enchants = "strong"},
279 helmet = {enchants = "strong"},
280 leggings = {enchants = "strong"}