/enchant working

This commit is contained in:
Lizzy Fleckenstein 2020-10-27 18:19:49 +01:00
parent 64e62486e2
commit 1cef707c6c
49 changed files with 81 additions and 47 deletions

View File

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

View File

@ -42,38 +42,43 @@ end)
--]] --]]
minetest.register_on_mods_loaded(function() minetest.register_on_mods_loaded(function()
local register_list = {}
for toolname, tooldef in pairs(minetest.registered_tools) do for toolname, tooldef in pairs(minetest.registered_tools) do
-- quick test if tooldef.groups.enchanted then
local has_enchantment = false break
end
local quick_test = false
for group, groupv in pairs(tooldef.groups) do for group, groupv in pairs(tooldef.groups) do
if groupv > 0 and mcl_enchanting.all_item_groups[group] then if groupv > 0 and mcl_enchanting.all_item_groups[group] then
has_enchantment = true quick_test = true
break break
end end
end end
if not has_enchantment then if quick_test then
break --print(toolname)
end local expensive_test = false
-- expensive test for enchantment in pairs(mcl_enchanting.enchantments) do
has_enchantment = false if mcl_enchanting.item_supports_enchantment(toolname, enchantment, true) then
for enchantment in pairs(mcl_enchanting.enchantments) do -- print("\tSupports " .. enchantment)
if mcl_enchanting.item_supports_enchantment(itemname, enchantment) then expensive_test = true
has_enchantment = true break
break end
end
if expensive_test then
local new_name = toolname .. "_enchanted"
minetest.override_item(toolname, {_mcl_enchanting_enchanted_tool = new_name})
local new_def = table.copy(tooldef)
new_def.inventory_image = tooldef.inventory_image .. "^[colorize:purple:50"
new_def.groups.not_in_creative_inventory = 1
new_def.groups.enchanted = 1
new_def.texture = tooldef.texture or toolname:gsub("%:", "_")
new_def._mcl_enchanting_enchanted_tool = new_name
register_list[":" .. new_name] = new_def
end end
end end
if not has_enchantment then end
break for new_name, new_def in pairs(register_list) do
end minetest.register_tool(new_name, new_def)
local new_name = toolname .. "_enchanted"
tooldef._mcl_enchanting_enchanted_tool = new_name
local new_def = table.copy(tooldef)
new_def.inventory_image = old_def.inventory_image .. "^[colorize:violet:50"
new_def.groups.not_in_creative_inventory = 1
new_def.groups.enchanted = 1
new_def.texture = old_def.texture or toolname:gsub("%:", "_")
new_def._mcl_enchanting_enchanted_tool = new_name
minetest.register_tool(":" .. new_name, new_def)
end end
end) end)
@ -91,15 +96,16 @@ end
function mcl_enchanting.get_enchantment_description(enchantment, level) function mcl_enchanting.get_enchantment_description(enchantment, level)
local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_def = mcl_enchanting.enchantments[enchantment]
return enchantment_def.name .. " " .. (enchantment_def.max_level == 1 and "" or mcl_enchanting.roman_numerals.toRoman(level)) return enchantment_def.name .. (enchantment_def.max_level == 1 and "" or " " .. mcl_enchanting.roman_numerals.toRoman(level))
end end
function mcl_enchanting.get_enchanted_itemstring(itemname) function mcl_enchanting.get_enchanted_itemstring(itemname)
return minetest.registered_items[itemname]._mcl_enchanting_enchanted_tool local def = minetest.registered_items[itemname]
return def and def._mcl_enchanting_enchanted_tool
end end
function mcl_enchanting.item_supports_enchantment(itemname, enchantment) function mcl_enchanting.item_supports_enchantment(itemname, enchantment, early)
if not mcl_enchanting.get_enchanted_itemstring(itemname) then if not early and not mcl_enchanting.get_enchanted_itemstring(itemname) then
return false return false
end end
local enchantment_def = mcl_enchanting.enchantments[enchantment] local enchantment_def = mcl_enchanting.enchantments[enchantment]
@ -109,7 +115,7 @@ function mcl_enchanting.item_supports_enchantment(itemname, enchantment)
end end
end end
for group in pairs(enchantment_def.all) do for group in pairs(enchantment_def.all) do
if minetest.get_item_group(itemname, group) then if minetest.get_item_group(itemname, group) > 0 then
return true return true
end end
end end
@ -124,7 +130,7 @@ function mcl_enchanting.can_enchant(itemstack, enchantment, level)
if itemstack:get_name() == "" then if itemstack:get_name() == "" then
return false, "item missing" return false, "item missing"
end end
if not mcl_enchanting.item_supports_enchantment(itemdef.name, enchantment) then if not mcl_enchanting.item_supports_enchantment(itemstack:get_name(), enchantment) then
return false, "item not supported" return false, "item not supported"
end end
if not level then if not level then
@ -146,19 +152,19 @@ function mcl_enchanting.can_enchant(itemstack, enchantment, level)
return false, "incompatible", mcl_enchanting.get_enchantment_description(incompatible, incompatible_level) return false, "incompatible", mcl_enchanting.get_enchantment_description(incompatible, incompatible_level)
end end
end end
return true
end end
function mcl_enchanting.enchant(itemstack, enchantment, level) function mcl_enchanting.enchant(itemstack, enchantment, level)
local enchanted_itemstack = ItemStack(mcl_enchanting.get_enchanted_itemstring(itemstack:get_name())) local enchanted_itemstack = ItemStack({name = mcl_enchanting.get_enchanted_itemstring(itemstack:get_name()), wear = itemstack:get_wear(), metadata = itemstack:get_metadata()})
enchanted_itemstack:add_wear(itemstack:get_wear())
enchanted_itemstack:set_meta(itemstack:get_meta())
local enchantments = mcl_enchanting.get_enchantments(enchanted_itemstack) local enchantments = mcl_enchanting.get_enchantments(enchanted_itemstack)
enchantments[enchantment] = level enchantments[enchantment] = level
mcl_enchanting.set_enchantments(enchanted_itemstack, enchantments) mcl_enchanting.set_enchantments(enchanted_itemstack, enchantments)
mcl_enchanting.reload_enchantments(enchanted_itemstack, enchantments) mcl_enchanting.reload_enchantments(enchanted_itemstack, enchantments)
return enchanted_itemstack
end end
function mcl_enchanting.reload_enchantments(itemstack, echantments) function mcl_enchanting.reload_enchantments(itemstack, enchantments)
local itemdef = itemstack:get_definition() local itemdef = itemstack:get_definition()
for enchantment, level in pairs(enchantments) do for enchantment, level in pairs(enchantments) do
local func = mcl_enchanting.features[enchantment] local func = mcl_enchanting.features[enchantment]

View File

@ -1,6 +1,6 @@
-- Taken from https://minecraft.gamepedia.com/Enchanting -- Taken from https://minecraft.gamepedia.com/Enchanting
mcl_enchantments.enchantments = { mcl_enchanting.enchantments = {
-- unimplemented -- unimplemented
aqua_affinity = { aqua_affinity = {
name = "Aqua Affinity", name = "Aqua Affinity",
@ -90,6 +90,17 @@ mcl_enchantments.enchantments = {
description = "Reduces fall damage." description = "Reduces fall damage."
}, },
-- unimplemented -- unimplemented
fire_aspect = {
name = "Fire Aspect",
max_level = 2,
primary = {sword = true},
secondary = {},
disallow = {},
incompatible = {},
weight = 2,
description = "Sets target on fire."
},
-- unimplemented
fire_protection = { fire_protection = {
name = "Fire Protection", name = "Fire Protection",
max_level = 4, max_level = 4,
@ -101,6 +112,17 @@ mcl_enchantments.enchantments = {
description = "Reduces fire damage." description = "Reduces fire damage."
}, },
-- unimplemented -- unimplemented
flame = {
name = "Flame",
max_level = 1,
primary = {bow = true},
secondary = {},
disallow = {},
incompatible = {},
weight = 2,
description = "Arrows set target on fire."
},
-- unimplemented
fortune = { fortune = {
name = "Fortune", name = "Fortune",
max_level = 4, max_level = 4,
@ -190,7 +212,7 @@ mcl_enchantments.enchantments = {
}, },
-- unimplemented -- unimplemented
power = { power = {
name = "Power", name = "Power",
max_level = 5, max_level = 5,
primary = {}, primary = {},
secondary = {bow = true}, secondary = {bow = true},
@ -267,12 +289,12 @@ mcl_enchantments.enchantments = {
}, },
-- unimplemented -- unimplemented
smite = { smite = {
name = "Sharpness", name = "Smite",
max_level = 5, max_level = 5,
primary = {sword = true}, primary = {sword = true},
secondary = {axe = true}, secondary = {axe = true},
disallow = {}, disallow = {},
incompatible = {sharpness = true, smite = true}, incompatible = {bane_of_anthropods = true, sharpness = true},
weight = 5, weight = 5,
description = "Increases damage to undead mobs." description = "Increases damage to undead mobs."
}, },
@ -292,7 +314,7 @@ mcl_enchantments.enchantments = {
name = "Sweeping Edge", name = "Sweeping Edge",
max_level = 3, max_level = 3,
primary = {sword = true}, primary = {sword = true},
secondary = {axe = true}, secondary = {},
disallow = {}, disallow = {},
incompatible = {}, incompatible = {},
weight = 2, weight = 2,

View File

@ -1,3 +1,6 @@
mcl_enchanting.features = {}
--[[ --[[
local pickaxes = {"mcl_tools:pick_wood", "mcl_tools:pick_stone", "mcl_tools:pick_gold", "mcl_tools:pick_iron", "mcl_tools:pick_diamond"} local pickaxes = {"mcl_tools:pick_wood", "mcl_tools:pick_stone", "mcl_tools:pick_gold", "mcl_tools:pick_iron", "mcl_tools:pick_diamond"}
local pickaxes_better_than_iron = {"mcl_tools:pick_iron", "mcl_tools:pick_diamond"} local pickaxes_better_than_iron = {"mcl_tools:pick_iron", "mcl_tools:pick_diamond"}
@ -118,3 +121,4 @@ end)
end end
}, },
},--]] },--]]

View File

@ -9,6 +9,7 @@ mcl_enchanting = {
dofile(modpath .. "/enchantments.lua") dofile(modpath .. "/enchantments.lua")
dofile(modpath .. "/features.lua") dofile(modpath .. "/features.lua")
dofile(modpath .. "/core.lua") dofile(modpath .. "/core.lua")
dofile(modpath .. "/command.lua")
-- dofile(modpath .. "/ui.lua") -- dofile(modpath .. "/ui.lua")
-- dofile(modpath .. "/fx.lua") -- dofile(modpath .. "/fx.lua")
-- dofile(modpath .. "/book.lua") -- dofile(modpath .. "/book.lua")

View File

@ -1,5 +1,5 @@
name = mcl_enchanting name = mcl_enchanting
description = The rewrite of the Enchanting mod for MineClone2 description = The rewrite of the Enchanting mod for MineClone2
depends = mcl_sounds, mcl_formspec, mcl_dye depends = mcl_formspec, _mcl_autogroup
optional_depends = screwdriver optional_depends = screwdriver
author = Fleckenstein author = Fleckenstein

View File

Before

Width:  |  Height:  |  Size: 875 B

After

Width:  |  Height:  |  Size: 875 B

View File

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

Before

Width:  |  Height:  |  Size: 875 B

After

Width:  |  Height:  |  Size: 875 B

View File

Before

Width:  |  Height:  |  Size: 876 B

After

Width:  |  Height:  |  Size: 876 B

View File

Before

Width:  |  Height:  |  Size: 161 B

After

Width:  |  Height:  |  Size: 161 B

View File

Before

Width:  |  Height:  |  Size: 157 B

After

Width:  |  Height:  |  Size: 157 B

View File

Before

Width:  |  Height:  |  Size: 158 B

After

Width:  |  Height:  |  Size: 158 B

View File

Before

Width:  |  Height:  |  Size: 156 B

After

Width:  |  Height:  |  Size: 156 B

View File

Before

Width:  |  Height:  |  Size: 159 B

After

Width:  |  Height:  |  Size: 159 B

View File

Before

Width:  |  Height:  |  Size: 106 B

After

Width:  |  Height:  |  Size: 106 B

View File

Before

Width:  |  Height:  |  Size: 160 B

After

Width:  |  Height:  |  Size: 160 B

View File

Before

Width:  |  Height:  |  Size: 166 B

After

Width:  |  Height:  |  Size: 166 B

View File

Before

Width:  |  Height:  |  Size: 145 B

After

Width:  |  Height:  |  Size: 145 B

View File

Before

Width:  |  Height:  |  Size: 161 B

After

Width:  |  Height:  |  Size: 161 B

View File

Before

Width:  |  Height:  |  Size: 159 B

After

Width:  |  Height:  |  Size: 159 B

View File

Before

Width:  |  Height:  |  Size: 159 B

After

Width:  |  Height:  |  Size: 159 B

View File

Before

Width:  |  Height:  |  Size: 105 B

After

Width:  |  Height:  |  Size: 105 B

View File

Before

Width:  |  Height:  |  Size: 161 B

After

Width:  |  Height:  |  Size: 161 B

View File

Before

Width:  |  Height:  |  Size: 159 B

After

Width:  |  Height:  |  Size: 159 B

View File

Before

Width:  |  Height:  |  Size: 157 B

After

Width:  |  Height:  |  Size: 157 B

View File

Before

Width:  |  Height:  |  Size: 158 B

After

Width:  |  Height:  |  Size: 158 B

View File

Before

Width:  |  Height:  |  Size: 102 B

After

Width:  |  Height:  |  Size: 102 B

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 298 B

After

Width:  |  Height:  |  Size: 298 B

View File

Before

Width:  |  Height:  |  Size: 315 B

After

Width:  |  Height:  |  Size: 315 B

View File

Before

Width:  |  Height:  |  Size: 340 B

After

Width:  |  Height:  |  Size: 340 B

View File

@ -156,7 +156,7 @@ for level=0, 2 do
wield_scale = { x = 1.8, y = 1.8, z = 1 }, wield_scale = { x = 1.8, y = 1.8, z = 1 },
stack_max = 1, stack_max = 1,
range = 0, -- Pointing range to 0 to prevent punching with bow :D range = 0, -- Pointing range to 0 to prevent punching with bow :D
groups = {not_in_creative_inventory=1, not_in_craft_guide=1}, groups = {not_in_creative_inventory=1, not_in_craft_guide=1, bow=1},
on_drop = function(itemstack, dropper, pos) on_drop = function(itemstack, dropper, pos)
reset_bow_state(dropper) reset_bow_state(dropper)
itemstack:set_name("mcl_bows:bow") itemstack:set_name("mcl_bows:bow")