]> git.lizzy.rs Git - xdecor.git/blobdiff - enchanting.lua
More simplification
[xdecor.git] / enchanting.lua
index c70738d13a32353613b6395aa45ae77d2f362d18..75ae0ff6a20c201bcf3125db2fff90612c4e4674 100644 (file)
@@ -10,9 +10,9 @@ function enchanting.formspec(pos, tooltype)
                        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 sword kills faster]
-                       tooltip[durable;Your tool lasts longer]
-                       tooltip[fast;Your tool digs faster]
+                       tooltip[sharp;Your sword inflicts more damage]
+                       tooltip[durable;Your tool is more resistant]
+                       tooltip[fast;Your tool is more powerful]
                        tooltip[strong;Your armor is more resistant]
                        tooltip[speed;Your speed is increased] ]]
                        ..default.gui_slots..default.get_hotbar_bg(0.5,4.5)
@@ -20,39 +20,35 @@ function enchanting.formspec(pos, tooltype)
        if tooltype == "sword" then
                formspec = formspec.."image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]"
        elseif tooltype == "tool" then
-               formspec = formspec..
-                               [[ image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]
+               formspec = formspec..[[ 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] ]]
        elseif tooltype == "armor" then
                formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"
        elseif tooltype == "boots" then
-               formspec = formspec..
-                               [[ image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]
+               formspec = formspec..[[ 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] ]]
        end
 
        meta:set_string("formspec", formspec)
-       return formspec
 end
 
-function enchanting.on_put(pos, listname, _, stack, _)
-       local stn = stack:get_name()
-       local meta = minetest.get_meta(pos)
-
+function enchanting.on_put(pos, listname, _, stack)
        if listname == "tool" then
+               local stn = stack:get_name()
                if stn:find("pick") or stn:find("axe") or stn:find("shovel") then
-                       meta:set_string("formspec", enchanting.formspec(pos, "tool"))
+                       enchanting.formspec(pos, "tool")
                elseif stn:find("sword") then
-                       meta:set_string("formspec", enchanting.formspec(pos, "sword"))
+                       enchanting.formspec(pos, "sword")
                elseif stn:find("chestplate") or stn:find("leggings") or stn:find("helmet") then
-                       meta:set_string("formspec", enchanting.formspec(pos, "armor"))
+                       enchanting.formspec(pos, "armor")
                elseif stn:find("boots") then
-                       meta:set_string("formspec", enchanting.formspec(pos, "boots"))
+                       enchanting.formspec(pos, "boots")
                end
        end
 end
 
-function enchanting.fields(pos, _, fields, _)
+function enchanting.fields(pos, _, fields)
+       if fields.quit then return end
        local inv = minetest.get_meta(pos):get_inventory()
        local toolstack = inv:get_stack("tool", 1)
        local toolstack_name = toolstack:get_name()
@@ -60,12 +56,10 @@ function enchanting.fields(pos, _, fields, _)
        local mod, tool = toolstack_name:match("([%w_]+):([%w_]+)")
        local toolwear = toolstack:get_wear()
        local mese = mesestack:get_count()
-       local ench = dump(fields):match("%w+")
-       if ench == "quit" then return end
+       local ench = next(fields)
        local enchanted_tool = mod..":enchanted_"..tool.."_"..ench
 
-       if mese > 0 and fields[ench] and
-                       minetest.registered_tools[enchanted_tool] then
+       if mese > 0 and minetest.registered_tools[enchanted_tool] then
                toolstack:replace(enchanted_tool)
                toolstack:add_wear(toolwear)
                mesestack:take_item()
@@ -74,7 +68,7 @@ function enchanting.fields(pos, _, fields, _)
        end
 end
 
-function enchanting.dig(pos, _)
+function enchanting.dig(pos)
        local inv = minetest.get_meta(pos):get_inventory()
        return inv:is_empty("tool") and inv:is_empty("mese")
 end
@@ -85,11 +79,10 @@ local function allowed(tool)
                        return true
                end
        end
-
        return false
 end
 
-function enchanting.put(_, listname, _, stack, _)
+function enchanting.put(_, listname, _, stack)
        local toolstack = stack:get_name()
        local toolname = toolstack:match("[%w_]+:([%w_]+)")
 
@@ -102,6 +95,22 @@ function enchanting.put(_, listname, _, stack, _)
        return 0
 end
 
+function enchanting.on_take(pos, listname)
+       if listname == "tool" then
+               enchanting.formspec(pos, nil)
+       end
+end
+
+function enchanting.construct(pos)
+       local meta = minetest.get_meta(pos)
+       meta:set_string("infotext", "Enchantment Table")
+       enchanting.formspec(pos, nil)
+
+       local inv = meta:get_inventory()
+       inv:set_size("tool", 1)
+       inv:set_size("mese", 1)
+end
+
 xdecor.register("enchantment_table", {
        description = "Enchantment Table",
        tiles = {
@@ -113,25 +122,12 @@ xdecor.register("enchantment_table", {
        sounds = default.node_sound_stone_defaults(),
        on_rotate = screwdriver.rotate_simple,
        can_dig = enchanting.dig,
-       on_construct = function(pos)
-               local meta = minetest.get_meta(pos)
-               enchanting.formspec(pos, nil)
-               meta:set_string("infotext", "Enchantment Table")
-
-               local inv = meta:get_inventory()
-               inv:set_size("tool", 1)
-               inv:set_size("mese", 1)
-       end,
-       enchanting.formspec,
+       on_construct = enchanting.construct,
        on_receive_fields = enchanting.fields,
        on_metadata_inventory_put = enchanting.on_put,
+       on_metadata_inventory_take = enchanting.on_take,
        allow_metadata_inventory_put = enchanting.put,
-       allow_metadata_inventory_move = function() return 0 end,
-       on_metadata_inventory_take = function(pos, listname, _, _, _)
-               if listname == "tool" then
-                       enchanting.formspec(pos, nil)
-               end
-       end
+       allow_metadata_inventory_move = function() return 0 end
 })
 
 local function cap(str)
@@ -233,8 +229,6 @@ for _, ench in pairs(tooldef[3]) do
                        })
                end
        end
-       minetest.register_alias("xdecor:enchanted_"..tool.."_"..material.."_"..enchant, mod..
-                       ":enchanted_"..tool.."_"..material.."_"..enchant) -- legacy code
 end
 end
 end