diff --git a/mods/HUD/mcl_inventory/depends.txt b/mods/HUD/mcl_inventory/depends.txt index 27231e5dff..cf04c75efd 100644 --- a/mods/HUD/mcl_inventory/depends.txt +++ b/mods/HUD/mcl_inventory/depends.txt @@ -5,4 +5,3 @@ _mcl_autogroup? mcl_armor? mcl_brewing? mcl_potions? -mcl_enchanting diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index a4bd0e167f..c6f97185d9 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -141,7 +141,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return_fields(player,"craft") return_fields(player,"enchanting_lapis") return_fields(player,"enchanting_item") - mcl_enchanting.reload_inventory(player) if not minetest.is_creative_enabled(player:get_player_name()) and (formname == "" or formname == "main") then set_inventory(player) end diff --git a/mods/ITEMS/mcl_enchanting/engine.lua b/mods/ITEMS/mcl_enchanting/engine.lua index ac12857b97..043d11860c 100644 --- a/mods/ITEMS/mcl_enchanting/engine.lua +++ b/mods/ITEMS/mcl_enchanting/engine.lua @@ -393,15 +393,19 @@ function mcl_enchanting.show_enchanting_formspec(player) .. "formspec_version[3]" .. "label[0,0;" .. C("#313131") .. F(table_name) .. "]" .. mcl_formspec.get_itemslot_bg(0.2, 2.4, 1, 1) - .. "list[detached:" .. name .. "_enchanting;enchanting;0.2,2.4;1,1;1]" + .. "list[current_player;enchanting_item;0.2,2.4;1,1]" .. mcl_formspec.get_itemslot_bg(1.1, 2.4, 1, 1) .. "image[1.1,2.4;1,1;mcl_enchanting_lapis_background.png]" - .. "list[detached:" .. name .. "_enchanting;enchanting;1.1,2.4;1,1;2]" + .. "list[current_player;enchanting_lapis;1.1,2.4;1,1]" .. "label[0,4;" .. C("#313131") .. F(S("Inventory")).."]" .. mcl_formspec.get_itemslot_bg(0, 4.5, 9, 3) .. mcl_formspec.get_itemslot_bg(0, 7.74, 9, 1) .. "list[current_player;main;0,4.5;9,3;9]" - .. "listring[detached:" .. name .. "_enchanting;enchanting]" + .. "listring[current_player;enchanting_item]" + .. "listring[current_player;main]" + .. "listring[current_player;enchanting]" + .. "listring[current_player;main]" + .. "listring[current_player;enchanting_lapis]" .. "listring[current_player;main]" .. "list[current_player;main;0,7.74;9,1;]" .. "real_coordinates[true]" @@ -420,7 +424,7 @@ function mcl_enchanting.show_enchanting_formspec(player) local hover_ending = (can_enchant and "_hovered" or "_off") formspec = formspec .. "container[3.2," .. y .. "]" - .. (slot and "tooltip[button_" .. i .. ";" .. C("#818181") .. F(slot.description) .. " " .. C("#FFFFFF") .. " . . . ?\n\n" .. (enough_levels and C(enough_lapis and "#818181" or "#FC5454") .. F(S("@1 × Lapis Lazuli", i)) .. "\n" .. C("#818181") .. F(S("Enchantment levels: @1", i)) or C("#FC5454") .. F(S("Level requirement: @1", slot.level_requirement))) .. "]" or "") + .. (slot and "tooltip[button_" .. i .. ";" .. C("#818181") .. F(slot.description) .. " " .. C("#FFFFFF") .. " . . . ?\n\n" .. (enough_levels and C(enough_lapis and "#818181" or "#FC5454") .. F(S("@1 Lapis Lazuli", i)) .. "\n" .. C("#818181") .. F(S("@1 Enchantment Levels", i)) or C("#FC5454") .. F(S("Level requirement: @1", slot.level_requirement))) .. "]" or "") .. "style[button_" .. i .. ";bgimg=mcl_enchanting_button" .. ending .. ".png;bgimg_hovered=mcl_enchanting_button" .. hover_ending .. ".png;bgimg_pressed=mcl_enchanting_button" .. hover_ending .. ".png]" .. "button[0,0;7.5,1.3;button_" .. i .. ";]" .. (slot and "image[0,0;1.3,1.3;mcl_enchanting_number_" .. i .. ending .. ".png]" or "") @@ -474,74 +478,62 @@ function mcl_enchanting.handle_formspec_fields(player, formname, fields) end function mcl_enchanting.initialize_player(player) - local player_inv = player:get_inventory() - player_inv:set_size("enchanting_lapis", 1) - player_inv:set_size("enchanting_item", 1) - local name = player:get_player_name() - local detached_inv = minetest.create_detached_inventory(name .. "_enchanting", { - allow_put = function(inv, listname, index, stack, player) - if player:get_player_name() ~= name then - return 0 - end - if stack:get_name() == "mcl_dye:blue" and index ~= 2 then - return math.min(inv:get_stack(listname, 3):get_free_space(), stack:get_count()) - elseif index ~= 3 and inv:get_stack(listname, 2):get_count() == 0 then + local inv = player:get_inventory() + inv:set_size("enchanting", 1) + inv:set_size("enchanting_item", 1) + inv:set_size("enchanting_item", 1) +end + +function mcl_enchanting.is_enchanting_inventory_action(action, inventory, inventory_info) + if inventory:get_location().type == "player" then + local enchanting_lists = mcl_enchanting.enchanting_lists + if action == "move" then + local is_from = table.indexof(enchanting_lists, inventory_info.from_list) ~= -1 + local is_to = table.indexof(enchanting_lists, inventory_info.to_list) ~= -1 + return is_from or is_to, is_to + elseif (action == "put" or action == "take") and table.indexof(enchanting_lists, inventory_info.listname) ~= -1 then + return true + end + else + return false + end +end + +function mcl_enchanting.allow_inventory_action(player, action, inventory, inventory_info) + local is_enchanting_action, do_limit = mcl_enchanting.is_enchanting_inventory_action(action, inventory, inventory_info) + if is_enchanting_action and do_limit then + if action == "move" then + local listname = inventory_info.to_list + local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index) + if stack:get_name() == "mcl_dye:blue" and listname ~= "enchanting_item" then + return math.min(inventory:get_stack("enchanting_lapis", 1):get_free_space(), stack:get_count()) + elseif inventory:get_stack("enchanting_item", 1):get_count() == 0 and listname ~= "enchanting_lapis" then return 1 else return 0 end - end, - allow_take = function(inv, listname, index, stack, player) - if player:get_player_name() ~= name or index == 1 then - return 0 - end - return stack:get_count() - end, - allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) + else return 0 - end, - on_put = function(inv, listname, index, stack, player) - local result_list - if index == 1 then - if stack:get_name() == "mcl_dye:blue" then - result_list = "lapis" - stack:add_item(inv:get_stack(listname, 3)) - inv:set_stack(listname, 1, nil) - inv:set_stack(listname, 3, stack) - else - result_list = "item" - inv:set_stack(listname, 1, nil) - inv:set_stack(listname, 2, stack) - end - elseif index == 2 then - result_list = "item" - elseif index == 3 then - result_list = "lapis" - stack = inv:get_stack(listname, 3) - end - player:get_inventory():set_stack("enchanting_" .. result_list, 1, stack) - mcl_enchanting.show_enchanting_formspec(player) - end, - on_take = function(inv, listname, index, stack, player) - local result_list - if index == 2 then - result_list = "item" - elseif index == 3 then - result_list = "lapis" - end - player:get_inventory():set_stack("enchanting_" .. result_list, 1, nil) - mcl_enchanting.show_enchanting_formspec(player) end - }, name) - detached_inv:set_size("enchanting", 3) - mcl_enchanting.reload_inventory(player) + end end -function mcl_enchanting.reload_inventory(player) - local player_inv = player:get_inventory() - local detached_inv = minetest.get_inventory({type = "detached", name = player:get_player_name() .. "_enchanting"}) - detached_inv:set_stack("enchanting", 2, player_inv:get_stack("enchanting_item", 1)) - detached_inv:set_stack("enchanting", 3, player_inv:get_stack("enchanting_lapis", 1)) +function mcl_enchanting.on_inventory_action(player, action, inventory, inventory_info) + if mcl_enchanting.is_enchanting_inventory_action(action, inventory, inventory_info) then + if action == "move" and inventory_info.to_list == "enchanting" then + local stack = inventory:get_stack("enchanting", 1) + local result_list + if stack:get_name() == "mcl_dye:blue" then + result_list = "enchanting_lapis" + stack:add_item(inventory:get_stack("enchanting_lapis", 1)) + else + result_list = "enchanting_item" + end + inventory:set_stack(result_list, 1, stack) + inventory:set_stack("enchanting", 1, nil) + end + mcl_enchanting.show_enchanting_formspec(player) + end end function mcl_enchanting.schedule_book_animation(self, anim) diff --git a/mods/ITEMS/mcl_enchanting/init.lua b/mods/ITEMS/mcl_enchanting/init.lua index 78c5b7f930..8e72cf7824 100644 --- a/mods/ITEMS/mcl_enchanting/init.lua +++ b/mods/ITEMS/mcl_enchanting/init.lua @@ -9,6 +9,7 @@ mcl_enchanting = { roman_numerals = dofile(modpath .. "/roman_numerals.lua"), -- https://exercism.io/tracks/lua/exercises/roman-numerals/solutions/73c2fb7521e347209312d115f872fa49 enchantments = {}, overlay = "^[colorize:white:50^[colorize:purple:50", + enchanting_lists = {"enchanting", "enchanting_item", "enchanting_lapis"}, bookshelf_positions = { {x = -2, y = 0, z = -2}, {x = -2, y = 1, z = -2}, {x = -1, y = 0, z = -2}, {x = -1, y = 1, z = -2}, @@ -324,4 +325,6 @@ minetest.register_lbm({ minetest.register_on_mods_loaded(mcl_enchanting.initialize) minetest.register_on_joinplayer(mcl_enchanting.initialize_player) minetest.register_on_player_receive_fields(mcl_enchanting.handle_formspec_fields) +minetest.register_allow_player_inventory_action(mcl_enchanting.allow_inventory_action) +minetest.register_on_player_inventory_action(mcl_enchanting.on_inventory_action) table.insert(tt.registered_snippets, 1, mcl_enchanting.enchantments_snippet)