2 local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots
4 function enchanting.tools_fs()
5 return "size[8,7;]"..xbg..
6 "label[0.85,-0.15;Enchant]image[0.6,0.2;2,2;xdecor_enchbook.png]list[current_name;tool;0.5,2;1,1;]list[current_name;mese;1.5,2;1,1;]image[1.5,2;1,1;mese_layout.png]image[3,-0.15;5.7,3.8;ench_ui.png]list[current_player;main;0,3.3;8,4;]"..
7 "image_button[3.35,0.2;4,0.8;bg_btn.png;fast;Efficiency]"..
8 "image_button[3.35,1.2;4,0.8;bg_btn.png;durable;Durability]"
11 function enchanting.swords_fs()
12 return "size[8,7;]"..xbg..
13 "label[0.85,-0.15;Enchant]image[0.6,0.2;2,2;xdecor_enchbook.png]list[current_name;tool;0.5,2;1,1;]list[current_name;mese;1.5,2;1,1;]image[1.5,2;1,1;mese_layout.png]image[3,-0.15;5.7,3.8;ench_ui.png]list[current_player;main;0,3.3;8,4;]"..
14 "image_button[3.35,2.2;4,0.8;bg_btn.png;sharp;Sharpness]"
17 function enchanting.default_fs(pos)
18 local meta = minetest.get_meta(pos)
19 local formspec = "size[8,7;]"..xbg..
20 "label[0.85,-0.15;Enchant]image[0.6,0.2;2,2;xdecor_enchbook.png]list[current_name;tool;0.5,2;1,1;]list[current_name;mese;1.5,2;1,1;]image[1.5,2;1,1;mese_layout.png]image[3,-0.15;5.7,3.8;ench_ui.png]list[current_player;main;0,3.3;8,4;]"..
21 "image_button[3.35,0.2;4,0.8;bg_btn.png;fast;Efficiency]"..
22 "image_button[3.35,1.2;4,0.8;bg_btn.png;durable;Durability]"..
23 "image_button[3.35,2.2;4,0.8;bg_btn.png;sharp;Sharpness]"
25 meta:set_string("formspec", formspec)
26 meta:set_string("infotext", "Enchantment Table")
28 local inv = meta:get_inventory()
29 inv:set_size("tool", 1)
30 inv:set_size("mese", 1)
33 function enchanting.on_put(pos, listname, _, stack, _)
34 local stn = stack:get_name()
35 local meta = minetest.get_meta(pos)
37 if listname == "tool" then
38 if stn:find("sword") then
39 meta:set_string("formspec", enchanting.swords_fs())
41 meta:set_string("formspec", enchanting.tools_fs())
46 function enchanting.is_allowed(toolname)
47 local tdef = minetest.registered_tools[toolname]
48 if tdef and toolname:find("default:") and not toolname:find("stone") and not
49 toolname:find("wood") then
54 function enchanting.fields(pos, _, fields, _)
55 local inv = minetest.get_meta(pos):get_inventory()
56 local toolstack = inv:get_stack("tool", 1)
57 local mesestack = inv:get_stack("mese", 1)
58 local toolname = toolstack:get_name()
59 local toolwear = toolstack:get_wear()
60 local mese = mesestack:get_count()
61 local ench = dump(fields):match("%w+")
63 if enchanting.is_allowed(toolname) ~= 0 and mese > 0 and
64 fields[ench] and ench ~= "quit" then
65 toolstack:replace("xdecor:enchanted_"..toolname:sub(9).."_"..ench)
66 toolstack:add_wear(toolwear)
68 inv:set_stack("mese", 1, mesestack)
69 inv:set_stack("tool", 1, toolstack)
73 function enchanting.dig(pos, _)
74 local inv = minetest.get_meta(pos):get_inventory()
75 if not inv:is_empty("tool") or not inv:is_empty("mese") then
81 function enchanting.put(_, listname, _, stack, _)
82 local toolname = stack:get_name()
83 local count = stack:get_count()
85 if listname == "mese" then
86 if toolname == "default:mese_crystal" then return count
89 if listname == "tool" then return enchanting.is_allowed(toolname) end
93 xdecor.register("enchantment_table", {
94 description = "Enchantment Table",
96 "xdecor_enchantment_top.png", "xdecor_enchantment_bottom.png",
97 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png",
98 "xdecor_enchantment_side.png", "xdecor_enchantment_side.png"
101 sounds = default.node_sound_stone_defaults(),
102 can_dig = enchanting.dig,
103 on_construct = enchanting.default_fs,
104 on_receive_fields = enchanting.fields,
105 on_metadata_inventory_put = enchanting.on_put,
106 allow_metadata_inventory_put = enchanting.put,
107 allow_metadata_inventory_move = function(...) return 0 end,
108 on_metadata_inventory_take = function(pos, listname, _, _, _)
109 if listname == "tool" then enchanting.default_fs(pos) end
113 local function cap(str) return str:gsub("^%l", string.upper) end
115 -- Higher number = stronger enchant.
116 local use_factor = 1.2
117 local times_subtractor = 0.1
118 local sharp_factor = 1
120 function enchanting.register_enchtools()
121 local materials = {"steel", "bronze", "mese", "diamond"}
122 local tools = { {"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"}, {"sword", "fleshy"} }
123 local chants = {"durable", "fast", "sharp"}
125 for _, m in pairs(materials) do
126 for k, t in pairs(tools) do
127 for _, c in pairs(chants) do
128 local original_tool = minetest.registered_tools["default:"..t[1].."_"..m]
129 local original_damage_groups = original_tool.tool_capabilities.damage_groups
130 local original_groupcaps = original_tool.tool_capabilities.groupcaps
131 local groupcaps = table.copy(original_groupcaps)
134 if c == "durable" and k <= 3 then
135 groupcaps[t[2]].uses = original_groupcaps[t[2]].uses * use_factor
136 elseif c == "fast" and k <= 3 then
138 groupcaps[t[2]].times[i] = original_groupcaps[t[2]].times[i] - times_subtractor
140 elseif c == "sharp" and k == 4 then
141 fleshy = original_damage_groups.fleshy + sharp_factor
144 minetest.register_tool(string.format("xdecor:enchanted_%s_%s_%s", t[1], m, c), {
145 description = string.format("Enchanted %s %s (%s)", cap(m), cap(t[1]), cap(c)),
146 inventory_image = original_tool.inventory_image,
147 wield_image = original_tool.wield_image,
148 groups = {not_in_creative_inventory=1},
149 tool_capabilities = {groupcaps = groupcaps, damage_groups = fleshy}
156 enchanting.register_enchtools()