1
0
Fork 0
MineClone2/mods/CORE/_mcl_enchanting/command.lua

42 lines
1.9 KiB
Lua
Raw Normal View History

2020-10-27 15:37:25 +00:00
minetest.register_chatcommand("enchant", {
2020-10-27 17:19:49 +00:00
description = "Enchant an item.",
2020-10-27 15:37:25 +00:00
params = "<player> <enchantment> [<level>]",
privs = {give = true},
func = function(_, param)
local sparam = param:split(" ")
local target_name = sparam[1]
local enchantment = sparam[2]
local level_str = sparam[3]
local level = tonumber(level_str or "1")
2020-10-27 17:19:49 +00:00
if not target_name or not enchantment then
2020-10-27 15:37:25 +00:00
return false, "Usage: /enchant <player> <enchantment> [<level>]"
end
2020-10-27 17:19:49 +00:00
local target = minetest.get_player_by_name(target_name)
2020-10-27 15:37:25 +00:00
if not target then
return false, "Player '" .. target_name .. "' cannot be found"
end
local itemstack = target:get_wielded_item()
local can_enchant, errorstring, extra_info = mcl_enchanting.can_enchant(itemstack, enchantment, level)
if not can_enchant then
if errorstring == "enchantment invalid" then
return false, "There is no such enchantment '" .. enchantment .. "'"
elseif errorstring == "item missing" then
return false, "The target doesn't hold an item"
elseif errorstring == "item not supported" then
return false, "The selected enchantment can't be added to the target item"
elseif errorstring == "level invalid" then
return false, "'" .. level_str .. "' is not a valid number"
2020-10-27 17:19:49 +00:00
elseif errorstring == "level too high" then
2020-10-27 15:37:25 +00:00
return false, "The number you have entered (" .. level_str .. ") is too big, it must be at most " .. extra_info
2020-10-27 17:19:49 +00:00
elseif errorstring == "level too small" then
2020-10-27 15:37:25 +00:00
return false, "The number you have entered (" .. level_str .. ") is too small, it must be at least " .. extra_info
elseif errorstring == "incompatible" then
return false, mcl_enchanting.get_enchantment_description(enchantment, level) .. " can't be combined with " .. extra_info
end
else
target:set_wielded_item(mcl_enchanting.enchant(itemstack, enchantment, level))
2020-10-27 17:19:49 +00:00
return true, "Enchanting succeded"
2020-10-27 15:37:25 +00:00
end
end
})