2020-10-27 17:37:25 +02:00
|
|
|
function mcl_enchanting.get_enchantments(itemstack)
|
|
|
|
return minetest.deserialize(itemstack:get_meta():get_string("mcl_enchanting:enchantments")) or {}
|
|
|
|
end
|
|
|
|
|
2020-10-27 20:53:49 +02:00
|
|
|
function mcl_enchanting.set_enchantments(itemstack, enchantments)
|
2020-11-01 16:15:44 +02:00
|
|
|
itemstack:get_meta():set_string("mcl_enchanting:enchantments", minetest.serialize(enchantments))
|
|
|
|
local itemdef = itemstack:get_definition()
|
2020-11-01 18:24:57 +02:00
|
|
|
if itemstack:get_name() ~= "mcl_enchanting:book_enchanted" then
|
2020-11-02 14:38:17 +02:00
|
|
|
if itemdef.tool_capabilities then
|
|
|
|
itemstack:get_meta():set_tool_capabilities(itemdef.tool_capabilities)
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
for enchantment, level in pairs(enchantments) do
|
|
|
|
local enchantment_def = mcl_enchanting.enchantments[enchantment]
|
|
|
|
if enchantment_def.on_enchant then
|
2020-11-02 18:58:13 +02:00
|
|
|
enchantment_def.on_enchant(itemstack, level)
|
2020-11-01 18:24:57 +02:00
|
|
|
end
|
2020-11-01 16:15:44 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
tt.reload_itemstack_description(itemstack)
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
|
2020-11-01 14:44:05 +02:00
|
|
|
function mcl_enchanting.get_enchantment(itemstack, enchantment)
|
2020-11-01 18:24:57 +02:00
|
|
|
if itemstack:get_name() == "mcl_enchanting:book_enchanted" then
|
|
|
|
return 0
|
|
|
|
end
|
2020-11-01 14:44:05 +02:00
|
|
|
return mcl_enchanting.get_enchantments(itemstack)[enchantment] or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_enchanting.has_enchantment(itemstack, enchantment)
|
|
|
|
return mcl_enchanting.get_enchantment(itemstack, enchantment) > 0
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_enchanting.get_enchantment_description(enchantment, level)
|
|
|
|
local enchantment_def = mcl_enchanting.enchantments[enchantment]
|
2020-10-27 19:19:49 +02:00
|
|
|
return enchantment_def.name .. (enchantment_def.max_level == 1 and "" or " " .. mcl_enchanting.roman_numerals.toRoman(level))
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
|
2020-11-01 14:44:05 +02:00
|
|
|
function mcl_enchanting.get_colorized_enchantment_description(enchantment, level)
|
|
|
|
return minetest.colorize(mcl_enchanting.enchantments[enchantment].curse and "#FC5454" or "#A8A8A8", mcl_enchanting.get_enchantment_description(enchantment, level))
|
|
|
|
end
|
|
|
|
|
2020-10-27 17:37:25 +02:00
|
|
|
function mcl_enchanting.get_enchanted_itemstring(itemname)
|
2020-11-01 14:44:05 +02:00
|
|
|
local def = minetest.registered_items[itemname]
|
2020-10-27 19:19:49 +02:00
|
|
|
return def and def._mcl_enchanting_enchanted_tool
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
|
2020-11-01 14:44:05 +02:00
|
|
|
function mcl_enchanting.is_enchanted_def(itemname)
|
|
|
|
return minetest.get_item_group(itemname, "enchanted") > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_enchanting.is_enchanted(itemstack)
|
|
|
|
return mcl_enchanting.is_enchanted_def(itemstack:get_name())
|
|
|
|
end
|
|
|
|
|
2020-10-27 19:19:49 +02:00
|
|
|
function mcl_enchanting.item_supports_enchantment(itemname, enchantment, early)
|
2020-11-01 18:24:57 +02:00
|
|
|
if itemname == "mcl_enchanting:book_enchanted" then
|
|
|
|
return true, true
|
|
|
|
end
|
2020-10-27 19:19:49 +02:00
|
|
|
if not early and not mcl_enchanting.get_enchanted_itemstring(itemname) then
|
2020-10-27 17:37:25 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
local enchantment_def = mcl_enchanting.enchantments[enchantment]
|
2020-11-01 16:15:44 +02:00
|
|
|
local itemdef = minetest.registered_items[itemname]
|
|
|
|
if itemdef.type ~= "tool" and enchantment_def.requires_tool then
|
2020-11-01 14:44:05 +02:00
|
|
|
return false
|
|
|
|
end
|
2020-10-27 17:37:25 +02:00
|
|
|
for disallow in pairs(enchantment_def.disallow) do
|
|
|
|
if minetest.get_item_group(itemname, disallow) > 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
for group in pairs(enchantment_def.primary) do
|
2020-10-27 19:19:49 +02:00
|
|
|
if minetest.get_item_group(itemname, group) > 0 then
|
2020-11-01 18:24:57 +02:00
|
|
|
return true, true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for group in pairs(enchantment_def.secondary) do
|
|
|
|
if minetest.get_item_group(itemname, group) > 0 then
|
|
|
|
return true, false
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_enchanting.can_enchant(itemstack, enchantment, level)
|
|
|
|
local enchantment_def = mcl_enchanting.enchantments[enchantment]
|
|
|
|
if not enchantment_def then
|
|
|
|
return false, "enchantment invalid"
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
local itemname = itemstack:get_name()
|
|
|
|
if itemname == "" then
|
2020-10-27 17:37:25 +02:00
|
|
|
return false, "item missing"
|
|
|
|
end
|
2020-10-27 19:19:49 +02:00
|
|
|
if not mcl_enchanting.item_supports_enchantment(itemstack:get_name(), enchantment) then
|
2020-10-27 17:37:25 +02:00
|
|
|
return false, "item not supported"
|
|
|
|
end
|
|
|
|
if not level then
|
|
|
|
return false, "level invalid"
|
|
|
|
end
|
|
|
|
if level > enchantment_def.max_level then
|
|
|
|
return false, "level too high", enchantment_def.max_level
|
|
|
|
elseif level < 1 then
|
|
|
|
return false, "level too small", 1
|
|
|
|
end
|
|
|
|
local item_enchantments = mcl_enchanting.get_enchantments(itemstack)
|
|
|
|
local enchantment_level = item_enchantments[enchantment]
|
|
|
|
if enchantment_level then
|
|
|
|
return false, "incompatible", mcl_enchanting.get_enchantment_description(enchantment, enchantment_level)
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
if itemname ~= "mcl_enchanting:book_enchanted" then
|
|
|
|
for incompatible in pairs(enchantment_def.incompatible) do
|
|
|
|
local incompatible_level = item_enchantments[incompatible]
|
|
|
|
if incompatible_level then
|
|
|
|
return false, "incompatible", mcl_enchanting.get_enchantment_description(incompatible, incompatible_level)
|
|
|
|
end
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
end
|
2020-10-27 19:19:49 +02:00
|
|
|
return true
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_enchanting.enchant(itemstack, enchantment, level)
|
2020-10-27 20:53:49 +02:00
|
|
|
itemstack:set_name(mcl_enchanting.get_enchanted_itemstring(itemstack:get_name()))
|
|
|
|
local enchantments = mcl_enchanting.get_enchantments(itemstack)
|
2020-10-27 17:37:25 +02:00
|
|
|
enchantments[enchantment] = level
|
2020-10-27 20:53:49 +02:00
|
|
|
mcl_enchanting.set_enchantments(itemstack, enchantments)
|
|
|
|
return itemstack
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
|
|
|
|
2020-11-01 16:15:44 +02:00
|
|
|
function mcl_enchanting.combine(itemstack, combine_with)
|
|
|
|
local itemname = itemstack:get_name()
|
2020-11-01 18:24:57 +02:00
|
|
|
local combine_name = combine_with:get_name()
|
2020-11-01 16:15:44 +02:00
|
|
|
local enchanted_itemname = mcl_enchanting.get_enchanted_itemstring(itemname)
|
2020-11-01 18:24:57 +02:00
|
|
|
if enchanted_itemname ~= mcl_enchanting.get_enchanted_itemstring(combine_name) and combine_name ~= "mcl_enchanting:book_enchanted" then
|
2020-11-01 16:15:44 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
local enchantments = mcl_enchanting.get_enchantments(itemstack)
|
|
|
|
for enchantment, combine_level in pairs(mcl_enchanting.get_enchantments(combine_with)) do
|
2020-11-01 14:44:05 +02:00
|
|
|
local enchantment_def = mcl_enchanting.enchantments[enchantment]
|
2020-11-01 18:24:57 +02:00
|
|
|
local enchantment_level = enchantments[enchantment]
|
2020-11-01 16:15:44 +02:00
|
|
|
if enchantment_level then
|
|
|
|
if enchantment_level == combine_level then
|
|
|
|
enchantment_level = math.min(enchantment_level + 1, enchantment_def.max_level)
|
2020-11-01 18:24:57 +02:00
|
|
|
else
|
|
|
|
enchantment_level = math.max(enchantment_level, combine_level)
|
2020-11-01 16:15:44 +02:00
|
|
|
end
|
|
|
|
elseif mcl_enchanting.item_supports_enchantment(itemname, enchantment) then
|
|
|
|
local supported = true
|
|
|
|
for incompatible in pairs(enchantment_def.incompatible) do
|
|
|
|
if enchantments[incompatible] then
|
|
|
|
supported = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if supported then
|
|
|
|
enchantment_level = combine_level
|
|
|
|
end
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
if enchantment_level and enchantment_level > 0 then
|
|
|
|
enchantments[enchantment] = enchantment_level
|
|
|
|
end
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
2020-11-01 16:15:44 +02:00
|
|
|
local any_enchantment = false
|
|
|
|
for enchantment, enchantment_level in pairs(enchantments) do
|
|
|
|
if enchantment_level > 0 then
|
|
|
|
any_enchantment = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if any_enchantment then
|
|
|
|
itemstack:set_name(enchanted_itemname)
|
|
|
|
end
|
|
|
|
mcl_enchanting.set_enchantments(itemstack, enchantments)
|
|
|
|
return true
|
2020-10-27 17:37:25 +02:00
|
|
|
end
|
2020-11-01 14:44:05 +02:00
|
|
|
|
|
|
|
function mcl_enchanting.initialize()
|
2020-11-01 18:24:57 +02:00
|
|
|
local all_groups = {}
|
2020-11-01 14:44:05 +02:00
|
|
|
for enchantment, enchantment_def in pairs(mcl_enchanting.enchantments) do
|
|
|
|
for primary in pairs(enchantment_def.primary) do
|
2020-11-01 18:24:57 +02:00
|
|
|
all_groups[primary] = true
|
2020-11-01 14:44:05 +02:00
|
|
|
end
|
|
|
|
for secondary in pairs(enchantment_def.secondary) do
|
2020-11-01 18:24:57 +02:00
|
|
|
all_groups[secondary] = true
|
2020-11-01 14:44:05 +02:00
|
|
|
end
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
local register_tool_list = {}
|
|
|
|
local register_item_list = {}
|
2020-11-01 14:44:05 +02:00
|
|
|
for itemname, itemdef in pairs(minetest.registered_items) do
|
|
|
|
if itemdef.groups.enchanted then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
local quick_test = false
|
|
|
|
for group, groupv in pairs(itemdef.groups) do
|
2020-11-01 18:24:57 +02:00
|
|
|
if groupv > 0 and all_groups[group] then
|
2020-11-01 14:44:05 +02:00
|
|
|
quick_test = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if quick_test then
|
|
|
|
if mcl_enchanting.debug then
|
|
|
|
print(itemname)
|
|
|
|
end
|
|
|
|
local expensive_test = false
|
|
|
|
for enchantment in pairs(mcl_enchanting.enchantments) do
|
|
|
|
if mcl_enchanting.item_supports_enchantment(itemname, enchantment, true) then
|
|
|
|
expensive_test = true
|
|
|
|
if mcl_enchanting.debug then
|
|
|
|
print("\tSupports " .. enchantment)
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if expensive_test then
|
|
|
|
local new_name = itemname .. "_enchanted"
|
|
|
|
minetest.override_item(itemname, {_mcl_enchanting_enchanted_tool = new_name})
|
|
|
|
local new_def = table.copy(itemdef)
|
2020-11-02 20:09:23 +02:00
|
|
|
new_def.inventory_image = itemdef.inventory_image .. "^[colorize:white:50^[colorize:purple:50"
|
2020-11-01 18:24:57 +02:00
|
|
|
if new_def.wield_image then
|
2020-11-02 20:09:23 +02:00
|
|
|
new_def.wield_image = new_def.wield_image .. "^[colorize:white:50^[colorize:purple:50"
|
2020-11-01 18:24:57 +02:00
|
|
|
end
|
2020-11-01 14:44:05 +02:00
|
|
|
new_def.groups.not_in_creative_inventory = 1
|
|
|
|
new_def.groups.enchanted = 1
|
|
|
|
new_def.texture = itemdef.texture or itemname:gsub("%:", "_")
|
|
|
|
new_def._mcl_enchanting_enchanted_tool = new_name
|
2020-11-01 18:24:57 +02:00
|
|
|
local register_list = register_item_list
|
2020-11-01 16:15:44 +02:00
|
|
|
if itemdef.type == "tool" then
|
2020-11-01 18:24:57 +02:00
|
|
|
register_list = register_tool_list
|
2020-11-01 14:44:05 +02:00
|
|
|
end
|
|
|
|
register_list[":" .. new_name] = new_def
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
for new_name, new_def in pairs(register_item_list) do
|
2020-11-01 14:44:05 +02:00
|
|
|
minetest.register_craftitem(new_name, new_def)
|
|
|
|
end
|
2020-11-01 18:24:57 +02:00
|
|
|
for new_name, new_def in pairs(register_tool_list) do
|
2020-11-01 14:44:05 +02:00
|
|
|
minetest.register_tool(new_name, new_def)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|