From 8bcf80422d11b9ed361fe2078aa9df06cd311418 Mon Sep 17 00:00:00 2001 From: PrairieWind Date: Sun, 28 Aug 2022 20:18:47 -0600 Subject: [PATCH 1/2] Added Formspec to Bookshelf --- mods/ITEMS/mcl_books/init.lua | 98 +++++++++++++++++++++++++++++++++++ mods/ITEMS/mcl_books/mod.conf | 1 + 2 files changed, 99 insertions(+) diff --git a/mods/ITEMS/mcl_books/init.lua b/mods/ITEMS/mcl_books/init.lua index 446be8cd66..f4e6ba4095 100644 --- a/mods/ITEMS/mcl_books/init.lua +++ b/mods/ITEMS/mcl_books/init.lua @@ -1,4 +1,6 @@ local S = minetest.get_translator(minetest.get_current_modname()) +local F = minetest.formspec_escape +local C = minetest.colorize local max_text_length = 4500 -- TODO: Increase to 12800 when scroll bar was added to written book local max_title_length = 64 @@ -331,6 +333,75 @@ if minetest.get_modpath("mcl_sounds") then wood_sound = mcl_sounds.node_sound_wood_defaults() end +-- Bookshelf GUI +local drop_content = mcl_util.drop_items_from_meta_container("main") + +local function on_blast(pos) + local node = minetest.get_node(pos) + drop_content(pos, node) + minetest.remove_node(pos) +end + +-- Simple protection checking functions +local function protection_check_move(pos, from_list, from_index, to_list, to_index, count, player) + local name = player:get_player_name() + if minetest.is_protected(pos, name) then + minetest.record_protection_violation(pos, name) + return 0 + else + return count + end +end + +local function protection_check_put_take(pos, listname, index, stack, player) + local name = player:get_player_name() + if minetest.is_protected(pos, name) then + minetest.record_protection_violation(pos, name) + return 0 + elseif minetest.get_item_group(stack:get_name(), "book") ~= 0 or stack:get_name() == "mcl_enchanting:book_enchanted" then + return stack:get_count() + else + return 0 + end +end + +local function bookshelf_gui(pos, node, clicker) + local name = minetest.get_meta(pos):get_string("name") + + if name == "" then + name = S("Bookshelf") + end + + local playername = clicker:get_player_name() + + minetest.show_formspec(playername, + "mcl_books:bookshelf_"..pos.x.."_"..pos.y.."_"..pos.z, + table.concat({ + "size[9,8.75]", + "label[0,0;"..F(C("#313131", name)).."]", + "list[nodemeta:"..pos.x..","..pos.y..","..pos.z..";main;0,0.5;9,3;]", + mcl_formspec.get_itemslot_bg(0, 0.5, 9, 3), + "label[0,4.0;"..F(C("#313131", S("Inventory"))).."]", + "list[current_player;main;0,4.5;9,3;9]", + mcl_formspec.get_itemslot_bg(0, 4.5, 9, 3), + "list[current_player;main;0,7.74;9,1;]", + mcl_formspec.get_itemslot_bg(0, 7.74, 9, 1), + "listring[nodemeta:"..pos.x..","..pos.y..","..pos.z..";main]", + "listring[current_player;main]", + }) + ) +end + +local function close_forms(pos) + local players = minetest.get_connected_players() + local formname = "mcl_books:bookshelf_"..pos.x.."_"..pos.y.."_"..pos.z + for p = 1, #players do + if vector.distance(players[p]:get_pos(), pos) <= 30 then + minetest.close_formspec(players[p]:get_player_name(), formname) + end + end +end + -- Bookshelf minetest.register_node("mcl_books:bookshelf", { description = S("Bookshelf"), @@ -347,6 +418,33 @@ minetest.register_node("mcl_books:bookshelf", { _mcl_blast_resistance = 1.5, _mcl_hardness = 1.5, _mcl_silk_touch_drop = true, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + inv:set_size("main", 9*3) + end, + after_place_node = function(pos, placer, itemstack, pointed_thing) + minetest.get_meta(pos):set_string("name", itemstack:get_meta():get_string("name")) + end, + allow_metadata_inventory_move = protection_check_move, + allow_metadata_inventory_take = protection_check_put_take, + allow_metadata_inventory_put = protection_check_put_take, + on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) + minetest.log("action", player:get_player_name().. + " moves stuff in bookshelf at "..minetest.pos_to_string(pos)) + end, + on_metadata_inventory_put = function(pos, listname, index, stack, player) + minetest.log("action", player:get_player_name().. + " moves stuff to bookshelf at "..minetest.pos_to_string(pos)) + end, + on_metadata_inventory_take = function(pos, listname, index, stack, player) + minetest.log("action", player:get_player_name().. + " takes stuff from bookshelf at "..minetest.pos_to_string(pos)) + end, + after_dig_node = drop_content, + on_blast = on_blast, + on_rightclick = bookshelf_gui, + on_destruct = close_gui, }) minetest.register_craft({ diff --git a/mods/ITEMS/mcl_books/mod.conf b/mods/ITEMS/mcl_books/mod.conf index cea9a5dd8d..cfa1f76229 100644 --- a/mods/ITEMS/mcl_books/mod.conf +++ b/mods/ITEMS/mcl_books/mod.conf @@ -1,4 +1,5 @@ name = mcl_books author = celeron55 description = Books mod for MCL2 +depends = mcl_util, mcl_formspec optional_depends = mcl_init, mcl_core, mcl_sounds, mcl_mobitems, mcl_dye, mcl_colors From 2eab947d84f7f4845ac5589c977a377dd93db312 Mon Sep 17 00:00:00 2001 From: PrairieWind Date: Tue, 30 Aug 2022 17:41:52 -0600 Subject: [PATCH 2/2] Added container group to Bookshelf --- mods/ITEMS/mcl_books/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ITEMS/mcl_books/init.lua b/mods/ITEMS/mcl_books/init.lua index f4e6ba4095..30fa3ea21c 100644 --- a/mods/ITEMS/mcl_books/init.lua +++ b/mods/ITEMS/mcl_books/init.lua @@ -411,7 +411,7 @@ minetest.register_node("mcl_books:bookshelf", { is_ground_content = false, groups = { handy=1, axey=1, deco_block=1, material_wood=1, - flammable=3, fire_encouragement=30, fire_flammability=20 + flammable=3, fire_encouragement=30, fire_flammability=20, container=1 }, drop = "mcl_books:book 3", sounds = wood_sound,