Refactor enchanting table code
This commit is contained in:
parent
fdf186c275
commit
24c173792b
148
enchanting.lua
148
enchanting.lua
@ -1,4 +1,6 @@
|
|||||||
local function enchconstruct(pos)
|
local enchanting = {}
|
||||||
|
|
||||||
|
function enchanting.construct(pos)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("formspec", "size[8,7;]"..xdecor.fancy_gui..
|
meta:set_string("formspec", "size[8,7;]"..xdecor.fancy_gui..
|
||||||
"label[0.85,-0.15;Enchant]"..
|
"label[0.85,-0.15;Enchant]"..
|
||||||
@ -16,7 +18,7 @@ local function enchconstruct(pos)
|
|||||||
inv:set_size("mese", 1)
|
inv:set_size("mese", 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function is_allowed_tool(toolname)
|
function enchanting.is_allowed_tool(toolname)
|
||||||
local tdef = minetest.registered_tools[toolname]
|
local tdef = minetest.registered_tools[toolname]
|
||||||
if tdef and string.find(toolname, "default:") and not
|
if tdef and string.find(toolname, "default:") and not
|
||||||
string.find(toolname, "sword") and not
|
string.find(toolname, "sword") and not
|
||||||
@ -26,7 +28,7 @@ local function is_allowed_tool(toolname)
|
|||||||
else return 0 end
|
else return 0 end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function enchfields(pos, formname, fields, sender)
|
function enchanting.fields(pos, formname, fields, sender)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
local toolstack = inv:get_stack("tool", 1)
|
local toolstack = inv:get_stack("tool", 1)
|
||||||
@ -36,7 +38,7 @@ local function enchfields(pos, formname, fields, sender)
|
|||||||
local enchs = {"durable", "fast"}
|
local enchs = {"durable", "fast"}
|
||||||
|
|
||||||
for _, e in pairs(enchs) do
|
for _, e in pairs(enchs) do
|
||||||
if is_allowed_tool(toolname) ~= 0 and mese > 0 and fields[e] then
|
if enchanting.is_allowed_tool(toolname) ~= 0 and mese > 0 and fields[e] then
|
||||||
toolstack:replace("xdecor:enchanted_"..string.sub(toolname, 9).."_"..e)
|
toolstack:replace("xdecor:enchanted_"..string.sub(toolname, 9).."_"..e)
|
||||||
mesestack:take_item()
|
mesestack:take_item()
|
||||||
inv:set_stack("mese", 1, mesestack)
|
inv:set_stack("mese", 1, mesestack)
|
||||||
@ -45,7 +47,7 @@ local function enchfields(pos, formname, fields, sender)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function enchdig(pos, player)
|
function enchanting.dig(pos, player)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
|
|
||||||
@ -55,7 +57,7 @@ local function enchdig(pos, player)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function enchput(pos, listname, index, stack, player)
|
function enchanting.put(pos, listname, index, stack, player)
|
||||||
local toolname = stack:get_name()
|
local toolname = stack:get_name()
|
||||||
local count = stack:get_count()
|
local count = stack:get_count()
|
||||||
|
|
||||||
@ -64,7 +66,7 @@ local function enchput(pos, listname, index, stack, player)
|
|||||||
else return 0 end
|
else return 0 end
|
||||||
end
|
end
|
||||||
if listname == "tool" then
|
if listname == "tool" then
|
||||||
return is_allowed_tool(toolname)
|
return enchanting.is_allowed_tool(toolname)
|
||||||
end
|
end
|
||||||
return count
|
return count
|
||||||
end
|
end
|
||||||
@ -78,12 +80,39 @@ xdecor.register("enchantment_table", {
|
|||||||
},
|
},
|
||||||
groups = {cracky=1},
|
groups = {cracky=1},
|
||||||
sounds = xdecor.stone,
|
sounds = xdecor.stone,
|
||||||
on_construct = enchconstruct,
|
on_construct = enchanting.construct,
|
||||||
can_dig = enchdig,
|
can_dig = enchanting.dig,
|
||||||
allow_metadata_inventory_put = enchput,
|
allow_metadata_inventory_put = enchanting.put,
|
||||||
on_receive_fields = enchfields
|
on_receive_fields = enchanting.fields
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function enchanting.register_enchtools(init, m, def)
|
||||||
|
local faster, longer = {}, {}
|
||||||
|
longer = init["uses"] * 1.2 -- Wearing factor for enchanted tools (higher number = longer use).
|
||||||
|
for i = 1, 3 do
|
||||||
|
faster[i] = init["times"][i] - 0.1 -- Digging factor for enchanted tools (higher number = faster dig).
|
||||||
|
end
|
||||||
|
|
||||||
|
local enchtools = {
|
||||||
|
{"axe", "durable", {choppy = {times=def.times, uses=longer, maxlevel=def.maxlvl}}},
|
||||||
|
{"axe", "fast", {choppy = {times=faster, uses=def.uses, maxlevel=def.maxlvl}}},
|
||||||
|
{"pick", "durable", {cracky = {times=def.times, uses=longer, maxlevel=def.maxlvl}}},
|
||||||
|
{"pick", "fast", {cracky = {times=faster, uses=def.uses, maxlevel=def.maxlvl}}},
|
||||||
|
{"shovel", "durable", {crumbly = {times=def.times, uses=longer, maxlevel=def.maxlvl}}},
|
||||||
|
{"shovel", "fast", {crumbly = {times=faster, uses=def.uses, maxlevel=def.maxlvl}}}
|
||||||
|
}
|
||||||
|
for _, x in pairs(enchtools) do
|
||||||
|
local tool, ench, grp = x[1], x[2], x[3]
|
||||||
|
minetest.register_tool("xdecor:enchanted_"..tool.."_"..m.."_"..ench, {
|
||||||
|
description = "Enchanted "..string.gsub(m, "%l", string.upper, 1)..
|
||||||
|
" "..string.gsub(tool, "%l", string.upper, 1).." ("..string.gsub(ench, "%l", string.upper, 1)..")",
|
||||||
|
inventory_image = minetest.registered_tools["default:"..tool.."_"..m]["inventory_image"],
|
||||||
|
groups = {not_in_creative_inventory=1},
|
||||||
|
tool_capabilities = {groupcaps = grp, damage_groups = def.dmg}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local tools = {
|
local tools = {
|
||||||
{"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"}
|
{"axe", "choppy"}, {"pick", "cracky"}, {"shovel", "crumbly"}
|
||||||
}
|
}
|
||||||
@ -93,97 +122,14 @@ for _, t in pairs(tools) do
|
|||||||
for _, m in pairs(materials) do
|
for _, m in pairs(materials) do
|
||||||
local tool, group = t[1], t[2]
|
local tool, group = t[1], t[2]
|
||||||
local toolname = tool.."_"..m
|
local toolname = tool.."_"..m
|
||||||
|
local init_def = minetest.registered_tools["default:"..toolname]["tool_capabilities"]["groupcaps"][group]
|
||||||
|
|
||||||
local registered_tool = {}
|
local tooldef = {
|
||||||
registered_tool = minetest.registered_tools["default:"..toolname]["tool_capabilities"]["groupcaps"][group]
|
times = init_def["times"],
|
||||||
|
uses = init_def["uses"],
|
||||||
local times = registered_tool["times"]
|
dmg = init_def["damage_groups"],
|
||||||
local uses = registered_tool["uses"]
|
maxlvl = init_def["maxlevel"]
|
||||||
local dmg = registered_tool["damage_groups"]
|
|
||||||
local maxlvl = registered_tool["maxlevel"]
|
|
||||||
|
|
||||||
local dig_faster, use_longer = {}, {}
|
|
||||||
use_longer = registered_tool["uses"] * 1.1 -- Wearing factor for enchanted tools (higher number = longer use).
|
|
||||||
for i = 1, 3 do
|
|
||||||
dig_faster[i] = registered_tool["times"][i] - 0.1 -- Digging factor for enchanted tools (lower number = faster dig).
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Pickaxes ---
|
|
||||||
|
|
||||||
minetest.register_tool("xdecor:enchanted_pick_"..m.."_durable", {
|
|
||||||
description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Pickaxe (Durable)",
|
|
||||||
inventory_image = minetest.registered_tools["default:pick_"..m]["inventory_image"],
|
|
||||||
groups = {not_in_creative_inventory=1},
|
|
||||||
tool_capabilities = {
|
|
||||||
groupcaps = {
|
|
||||||
cracky = {times=times, uses=use_longer, maxlevel=maxlvl}
|
|
||||||
},
|
|
||||||
damage_groups = dmg
|
|
||||||
}
|
}
|
||||||
})
|
enchanting.register_enchtools(init_def, m, tooldef)
|
||||||
|
|
||||||
minetest.register_tool("xdecor:enchanted_pick_"..m.."_fast", {
|
|
||||||
description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Pickaxe (Fast)",
|
|
||||||
inventory_image = minetest.registered_tools["default:pick_"..m]["inventory_image"],
|
|
||||||
groups = {not_in_creative_inventory=1},
|
|
||||||
tool_capabilities = {
|
|
||||||
groupcaps = {
|
|
||||||
cracky = {times=dig_faster, uses=uses, maxlevel=maxlvl}
|
|
||||||
},
|
|
||||||
damage_groups = dmg
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
--- Axes ---
|
|
||||||
|
|
||||||
minetest.register_tool("xdecor:enchanted_axe_"..m.."_durable", {
|
|
||||||
description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Axe (Durable)",
|
|
||||||
inventory_image = minetest.registered_tools["default:axe_"..m]["inventory_image"],
|
|
||||||
groups = {not_in_creative_inventory=1},
|
|
||||||
tool_capabilities = {
|
|
||||||
groupcaps = {
|
|
||||||
choppy = {times=times, uses=use_longer, maxlevel=maxlvl}
|
|
||||||
},
|
|
||||||
damage_groups = dmg
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_tool("xdecor:enchanted_axe_"..m.."_fast", {
|
|
||||||
description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Axe (Fast)",
|
|
||||||
inventory_image = minetest.registered_tools["default:axe_"..m]["inventory_image"],
|
|
||||||
groups = {not_in_creative_inventory=1},
|
|
||||||
tool_capabilities = {
|
|
||||||
groupcaps = {
|
|
||||||
choppy = {times=dig_faster, uses=uses, maxlevel=maxlvl}
|
|
||||||
},
|
|
||||||
damage_groups = dmg
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
--- Shovels ---
|
|
||||||
|
|
||||||
minetest.register_tool("xdecor:enchanted_shovel_"..m.."_durable", {
|
|
||||||
description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Shovel (Durable)",
|
|
||||||
inventory_image = minetest.registered_tools["default:shovel_"..m]["inventory_image"],
|
|
||||||
groups = {not_in_creative_inventory=1},
|
|
||||||
tool_capabilities = {
|
|
||||||
groupcaps = {
|
|
||||||
crumbly = {times=times, uses=use_longer, maxlevel=maxlvl}
|
|
||||||
},
|
|
||||||
damage_groups = dmg
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_tool("xdecor:enchanted_shovel_"..m.."_fast", {
|
|
||||||
description = "Enchanted "..string.sub(string.upper(m), 0, 1)..string.sub(m, 2).." Shovel (Fast)",
|
|
||||||
inventory_image = minetest.registered_tools["default:shovel_"..m]["inventory_image"],
|
|
||||||
groups = {not_in_creative_inventory=1},
|
|
||||||
tool_capabilities = {
|
|
||||||
groupcaps = {
|
|
||||||
crumbly = {times=dig_faster, uses=uses, maxlevel=maxlvl}
|
|
||||||
},
|
|
||||||
damage_groups = dmg
|
|
||||||
}
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user