2021-05-29 16:12:33 +02:00
local S = minetest.get_translator ( minetest.get_current_modname ( ) )
2022-08-29 04:18:47 +02:00
local F = minetest.formspec_escape
local C = minetest.colorize
2019-03-07 21:35:02 +01:00
2019-02-21 18:08:30 +01:00
local max_text_length = 4500 -- TODO: Increase to 12800 when scroll bar was added to written book
local max_title_length = 64
2022-09-01 12:10:12 +02:00
local bookshelf_inv = minetest.settings : get_bool ( " mcl_bookshelf_inventories " , true )
2020-03-10 05:01:49 +01:00
local header = " "
if minetest.get_modpath ( " mcl_init " ) then
header = " no_prepend[] " .. mcl_vars.gui_nonbg .. mcl_vars.gui_bg_color ..
" style_type[button;border=false;bgimg=mcl_books_button9.png;bgimg_pressed=mcl_books_button9_pressed.png;bgimg_middle=2,2] "
end
2017-02-01 23:15:23 +01:00
-- Book
minetest.register_craftitem ( " mcl_books:book " , {
2019-03-07 21:35:02 +01:00
description = S ( " Book " ) ,
_doc_items_longdesc = S ( " Books are used to make bookshelves and book and quills. " ) ,
2017-02-01 23:15:23 +01:00
inventory_image = " default_book.png " ,
stack_max = 64 ,
2020-11-25 12:47:27 +01:00
groups = { book = 1 , craftitem = 1 , enchantability = 1 } ,
_mcl_enchanting_enchanted_tool = " mcl_enchanting:book_enchanted " ,
2017-02-01 23:15:23 +01:00
} )
2017-07-26 19:16:32 +02:00
if minetest.get_modpath ( " mcl_core " ) and minetest.get_modpath ( " mcl_mobitems " ) then
minetest.register_craft ( {
2021-05-29 16:12:33 +02:00
type = " shapeless " ,
output = " mcl_books:book " ,
recipe = { " mcl_core:paper " , " mcl_core:paper " , " mcl_core:paper " , " mcl_mobitems:leather " , }
2017-07-26 19:16:32 +02:00
} )
end
2017-02-01 23:15:23 +01:00
2017-06-06 01:22:52 +02:00
-- Get the included text out of the book item
-- itemstack: Book item
-- meta: Meta of book (optional)
2021-05-28 00:34:58 +02:00
local function get_text ( itemstack )
2017-06-06 01:22:52 +02:00
-- Grab the text
local meta = itemstack : get_meta ( )
local text = meta : get_string ( " text " )
-- Backwards-compability with MCL2 0.21.0
-- Remember that get_metadata is deprecated since MT 0.4.16!
if text == nil or text == " " then
local meta_legacy = itemstack : get_metadata ( )
if itemstack : get_name ( ) == " mcl_books:written_book " then
local des = minetest.deserialize ( meta_legacy )
if des then
text = des.text
end
else
text = meta_legacy
end
end
-- Security check
if not text then
text = " "
end
return text
end
2021-05-28 00:34:58 +02:00
local function make_description ( title , author , generation )
2017-06-06 02:00:36 +02:00
local desc
if generation == 0 then
2019-03-07 21:35:02 +01:00
desc = S ( " “@1” " , title )
2017-06-06 02:00:36 +02:00
elseif generation == 1 then
2019-03-07 21:35:02 +01:00
desc = S ( " Copy of “@1” " , title )
2017-06-06 02:00:36 +02:00
elseif generation == 2 then
2019-03-07 21:35:02 +01:00
desc = S ( " Copy of Copy of “@1” " , title )
2017-06-06 02:00:36 +02:00
else
2019-03-07 21:35:02 +01:00
desc = S ( " Tattered Book " )
2017-06-06 02:00:36 +02:00
end
2021-04-19 15:19:59 +02:00
desc = desc .. " \n " .. minetest.colorize ( mcl_colors.GRAY , S ( " by @1 " , author ) )
2017-06-06 02:00:36 +02:00
return desc
end
2021-05-28 00:34:58 +02:00
local function cap_text_length ( text , max_length )
2019-02-21 18:08:30 +01:00
return string.sub ( text , 1 , max_length )
end
2021-05-28 00:34:58 +02:00
local function write ( itemstack , user , pointed_thing )
2017-03-02 15:44:31 +01:00
-- Call on_rightclick if the pointed node defines it
if pointed_thing.type == " node " then
local node = minetest.get_node ( pointed_thing.under )
if user and not user : get_player_control ( ) . sneak then
if minetest.registered_nodes [ node.name ] and minetest.registered_nodes [ node.name ] . on_rightclick then
return minetest.registered_nodes [ node.name ] . on_rightclick ( pointed_thing.under , node , user , itemstack ) or itemstack
end
end
end
2017-06-06 01:22:52 +02:00
local text = get_text ( itemstack )
2017-02-02 00:54:51 +01:00
local formspec = " size[8,9] " ..
2020-03-10 05:01:49 +01:00
header ..
2017-02-02 00:54:51 +01:00
" background[-0.5,-0.5;9,10;mcl_books_book_bg.png] " ..
2017-07-26 20:07:06 +02:00
" textarea[0.75,0.1;7.25,9;text;; " .. minetest.formspec_escape ( text ) .. " ] " ..
2019-03-07 21:35:02 +01:00
" button[0.75,7.95;3,1;sign; " .. minetest.formspec_escape ( S ( " Sign " ) ) .. " ] " ..
" button_exit[4.25,7.95;3,1;ok; " .. minetest.formspec_escape ( S ( " Done " ) ) .. " ] "
2017-02-02 00:54:51 +01:00
minetest.show_formspec ( user : get_player_name ( ) , " mcl_books:writable_book " , formspec )
end
2021-05-28 00:34:58 +02:00
local function read ( itemstack , user , pointed_thing )
2017-03-02 15:44:31 +01:00
-- Call on_rightclick if the pointed node defines it
if pointed_thing.type == " node " then
local node = minetest.get_node ( pointed_thing.under )
if user and not user : get_player_control ( ) . sneak then
if minetest.registered_nodes [ node.name ] and minetest.registered_nodes [ node.name ] . on_rightclick then
return minetest.registered_nodes [ node.name ] . on_rightclick ( pointed_thing.under , node , user , itemstack ) or itemstack
end
end
end
2017-06-06 01:22:52 +02:00
local text = get_text ( itemstack )
2017-02-01 23:36:53 +01:00
local formspec = " size[8,9] " ..
2020-03-10 05:01:49 +01:00
header ..
2017-02-01 23:36:53 +01:00
" background[-0.5,-0.5;9,10;mcl_books_book_bg.png] " ..
2020-03-10 05:01:49 +01:00
" textarea[0.75,0.1;7.25,9;; " .. minetest.formspec_escape ( text ) .. " ;] " ..
2019-03-07 21:35:02 +01:00
" button_exit[2.25,7.95;3,1;ok; " .. minetest.formspec_escape ( S ( " Done " ) ) .. " ] "
2017-02-02 00:54:51 +01:00
minetest.show_formspec ( user : get_player_name ( ) , " mcl_books:written_book " , formspec )
2017-02-01 23:36:53 +01:00
end
2017-02-01 23:15:23 +01:00
-- Book and Quill
minetest.register_craftitem ( " mcl_books:writable_book " , {
2019-03-14 06:20:05 +01:00
description = S ( " Book and Quill " ) ,
2020-02-19 04:54:17 +01:00
_tt_help = S ( " Write down some notes " ) ,
2019-03-07 21:35:02 +01:00
_doc_items_longdesc = S ( " This item can be used to write down some notes. " ) ,
_doc_items_usagehelp = S ( " Hold it in the hand, then rightclick to read the current notes and edit then. You can edit the text as often as you like. You can also sign the book which turns it into a written book which you can stack, but it can't be edited anymore. " ) .. " \n " ..
S ( " A book can hold up to 4500 characters. The title length is limited to 64 characters. " ) ,
2017-02-01 23:15:23 +01:00
inventory_image = " mcl_books_book_writable.png " ,
groups = { book = 1 } ,
stack_max = 1 ,
2017-02-01 23:36:53 +01:00
on_place = write ,
on_secondary_use = write ,
2017-02-01 23:15:23 +01:00
} )
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
if ( ( formname == " mcl_books:writable_book " ) and fields and fields.text ) then
local stack = player : get_wielded_item ( )
if ( stack : get_name ( ) and ( stack : get_name ( ) == " mcl_books:writable_book " ) ) then
2017-06-06 01:22:52 +02:00
local meta = stack : get_meta ( )
2019-02-21 18:08:30 +01:00
local text = cap_text_length ( fields.text , max_text_length )
2017-02-02 00:54:51 +01:00
if fields.ok then
2019-02-21 18:08:30 +01:00
meta : set_string ( " text " , text )
2017-06-06 01:22:52 +02:00
player : set_wielded_item ( stack )
2017-02-02 00:54:51 +01:00
elseif fields.sign then
2019-02-21 18:08:30 +01:00
meta : set_string ( " text " , text )
2017-06-06 01:22:52 +02:00
player : set_wielded_item ( stack )
2017-02-02 01:35:44 +01:00
2017-02-02 00:54:51 +01:00
local name = player : get_player_name ( )
local formspec = " size[8,9] " ..
2020-03-10 05:01:49 +01:00
header ..
2017-02-02 00:54:51 +01:00
" background[-0.5,-0.5;9,10;mcl_books_book_bg.png] " ..
2021-04-19 15:16:59 +02:00
" field[0.75,1;7.25,1;title; " .. minetest.formspec_escape ( minetest.colorize ( " #000000 " , S ( " Enter book title: " ) ) ) .. " ;] " ..
" label[0.75,1.5; " .. minetest.formspec_escape ( minetest.colorize ( " #404040 " , S ( " by @1 " , name ) ) ) .. " ] " ..
2019-03-07 21:35:02 +01:00
" button_exit[0.75,7.95;3,1;sign; " .. minetest.formspec_escape ( S ( " Sign and Close " ) ) .. " ] " ..
2019-03-25 12:31:22 +01:00
" tooltip[sign; " .. minetest.formspec_escape ( S ( " Note: The book will no longer be editable after signing " ) ) .. " ] " ..
2019-03-07 21:35:02 +01:00
" button[4.25,7.95;3,1;cancel; " .. minetest.formspec_escape ( S ( " Cancel " ) ) .. " ] "
2017-02-02 00:54:51 +01:00
minetest.show_formspec ( player : get_player_name ( ) , " mcl_books:signing " , formspec )
end
end
elseif ( ( formname == " mcl_books:signing " ) and fields and fields.sign and fields.title ) then
local newbook = ItemStack ( " mcl_books:written_book " )
local book = player : get_wielded_item ( )
local name = player : get_player_name ( )
if book : get_name ( ) == " mcl_books:writable_book " then
2019-02-21 18:08:30 +01:00
local title = fields.title
if string.len ( title ) == 0 then
2019-03-07 21:35:02 +01:00
title = S ( " Nameless Book " )
2017-02-02 00:54:51 +01:00
end
2019-02-21 18:08:30 +01:00
title = cap_text_length ( title , max_title_length )
2017-06-06 01:22:52 +02:00
local meta = newbook : get_meta ( )
2019-02-21 18:08:30 +01:00
local text = cap_text_length ( get_text ( book ) , max_text_length )
meta : set_string ( " title " , title )
2017-06-06 01:22:52 +02:00
meta : set_string ( " author " , name )
meta : set_string ( " text " , text )
2019-02-21 18:08:30 +01:00
meta : set_string ( " description " , make_description ( title , name , 0 ) )
2017-06-06 01:52:50 +02:00
-- The book copy counter. 0 = original, 1 = copy of original, 2 = copy of copy of original, …
meta : set_int ( " generation " , 0 )
2017-06-06 01:22:52 +02:00
2017-02-02 00:54:51 +01:00
player : set_wielded_item ( newbook )
else
minetest.log ( " error " , " [mcl_books] " .. name .. " failed to sign a book! " )
2017-02-01 23:15:23 +01:00
end
2017-02-02 01:21:35 +01:00
elseif ( ( formname == " mcl_books:signing " ) and fields and fields.cancel ) then
local book = player : get_wielded_item ( )
if book : get_name ( ) == " mcl_books:writable_book " then
2017-03-30 05:11:16 +02:00
write ( book , player , { type = " nothing " } )
2017-02-02 01:21:35 +01:00
end
2017-02-01 23:15:23 +01:00
end
end )
2017-07-26 19:16:32 +02:00
if minetest.get_modpath ( " mcl_dye " ) and minetest.get_modpath ( " mcl_mobitems " ) then
minetest.register_craft ( {
type = " shapeless " ,
output = " mcl_books:writable_book " ,
recipe = { " mcl_books:book " , " mcl_dye:black " , " mcl_mobitems:feather " } ,
} )
end
2017-02-01 23:15:23 +01:00
2017-02-02 00:54:51 +01:00
-- Written Book
minetest.register_craftitem ( " mcl_books:written_book " , {
2019-03-07 21:35:02 +01:00
description = S ( " Written Book " ) ,
_doc_items_longdesc = S ( " Written books contain some text written by someone. They can be read and copied, but not edited. " ) ,
_doc_items_usagehelp = S ( " Hold it in your hand, then rightclick to read the book. " ) .. " \n \n " ..
2017-06-06 02:54:40 +02:00
2019-03-07 21:35:02 +01:00
S ( " To copy the text of the written book, place it into the crafting grid together with a book and quill (or multiple of those) and craft. The written book will not be consumed. Copies of copies can not be copied. " ) ,
2017-02-02 00:54:51 +01:00
inventory_image = " mcl_books_book_written.png " ,
2018-02-02 05:13:26 +01:00
groups = { not_in_creative_inventory = 1 , book = 1 , no_rename = 1 } ,
2017-06-06 02:48:36 +02:00
stack_max = 16 ,
2017-02-02 00:54:51 +01:00
on_place = read ,
on_secondary_use = read
} )
2017-02-02 01:04:31 +01:00
-- Copy books
2017-06-06 02:48:36 +02:00
-- This adds 8 recipes
local baq = " mcl_books:writable_book "
local wb = " mcl_books:written_book "
local recipes = {
{ wb , baq } ,
{ baq , baq , wb } ,
{ baq , baq , wb , baq } ,
{ baq , baq , baq , baq , wb } ,
{ baq , baq , baq , baq , wb , baq } ,
{ baq , baq , baq , baq , wb , baq , baq } ,
{ baq , baq , baq , baq , wb , baq , baq , baq } ,
{ baq , baq , baq , baq , wb , baq , baq , baq , baq } ,
}
for r =# recipes , 1 , - 1 do
minetest.register_craft ( {
type = " shapeless " ,
output = " mcl_books:written_book " .. r ,
recipe = recipes [ r ] ,
} )
end
2017-02-02 01:04:31 +01:00
2017-06-06 02:25:22 +02:00
minetest.register_craft_predict ( function ( itemstack , player , old_craft_grid , craft_inv )
if itemstack : get_name ( ) ~= " mcl_books:written_book " then
return
end
local original
for i = 1 , player : get_inventory ( ) : get_size ( " craft " ) do
if old_craft_grid [ i ] : get_name ( ) == " mcl_books:written_book " then
original = old_craft_grid [ i ]
end
end
if not original then
return
end
local ometa = original : get_meta ( )
local generation = ometa : get_int ( " generation " )
2017-06-06 02:30:28 +02:00
2017-06-06 02:25:22 +02:00
-- Check generation, don't allow crafting with copy of copy of book
if generation >= 2 then
return ItemStack ( " " )
2017-06-06 02:30:28 +02:00
else
-- Valid copy. Let's update the description field of the result item
-- so it is properly displayed in the crafting grid.
local imeta = itemstack : get_meta ( )
2019-02-21 18:08:30 +01:00
local title = cap_text_length ( ometa : get_string ( " title " ) , max_title_length )
2017-06-06 02:30:28 +02:00
local author = ometa : get_string ( " author " )
-- Increase book generation and update description
generation = generation + 1
if generation < 1 then
generation = 1
end
local desc = make_description ( title , author , generation )
imeta : set_string ( " description " , desc )
return itemstack
2017-06-06 02:25:22 +02:00
end
end )
2017-02-02 01:04:31 +01:00
minetest.register_on_craft ( function ( itemstack , player , old_craft_grid , craft_inv )
if itemstack : get_name ( ) ~= " mcl_books:written_book " then
return
end
local original
local index
for i = 1 , player : get_inventory ( ) : get_size ( " craft " ) do
if old_craft_grid [ i ] : get_name ( ) == " mcl_books:written_book " then
original = old_craft_grid [ i ]
index = i
end
end
if not original then
return
end
2017-06-06 01:22:52 +02:00
-- copy of the book
local text = get_text ( original )
if not text or text == " " then
local copymeta = original : get_metadata ( )
itemstack : set_metadata ( copymeta )
else
local ometa = original : get_meta ( )
2017-06-06 02:25:22 +02:00
local generation = ometa : get_int ( " generation " )
-- No copy of copy of copy of book allowed
if generation >= 2 then
return ItemStack ( " " )
end
-- Copy metadata
2017-06-06 01:22:52 +02:00
local imeta = itemstack : get_meta ( )
2019-02-21 18:08:30 +01:00
local title = cap_text_length ( ometa : get_string ( " title " ) , max_title_length )
2017-06-06 01:52:50 +02:00
local author = ometa : get_string ( " author " )
imeta : set_string ( " title " , title )
imeta : set_string ( " author " , author )
2019-02-21 18:08:30 +01:00
imeta : set_string ( " text " , cap_text_length ( text , max_text_length ) )
2017-06-06 01:52:50 +02:00
-- Increase book generation and update description
generation = generation + 1
if generation < 1 then
generation = 1
end
2017-06-06 02:00:36 +02:00
local desc = make_description ( title , author , generation )
2017-06-06 01:52:50 +02:00
imeta : set_string ( " description " , desc )
imeta : set_int ( " generation " , generation )
2017-06-06 01:22:52 +02:00
end
2017-02-02 01:04:31 +01:00
-- put the book with metadata back in the craft grid
craft_inv : set_stack ( " craft " , index , original )
end )
2017-07-26 19:16:32 +02:00
local wood_sound
if minetest.get_modpath ( " mcl_sounds " ) then
wood_sound = mcl_sounds.node_sound_wood_defaults ( )
end
2022-08-29 04:18:47 +02:00
-- 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 )
2022-09-01 12:10:12 +02:00
if not bookshelf_inv then return end
2022-08-29 04:18:47 +02:00
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
2017-02-01 23:15:23 +01:00
-- Bookshelf
minetest.register_node ( " mcl_books:bookshelf " , {
2019-03-07 21:35:02 +01:00
description = S ( " Bookshelf " ) ,
_doc_items_longdesc = S ( " Bookshelves are used for decoration. " ) ,
2017-07-15 11:31:38 +02:00
tiles = { " mcl_books_bookshelf_top.png " , " mcl_books_bookshelf_top.png " , " default_bookshelf.png " } ,
2017-02-01 23:15:23 +01:00
stack_max = 64 ,
is_ground_content = false ,
2022-05-04 22:22:59 +02:00
groups = {
handy = 1 , axey = 1 , deco_block = 1 , material_wood = 1 ,
2022-08-31 01:41:52 +02:00
flammable = 3 , fire_encouragement = 30 , fire_flammability = 20 , container = 1
2022-05-04 22:22:59 +02:00
} ,
2017-02-01 23:15:23 +01:00
drop = " mcl_books:book 3 " ,
2017-07-26 19:16:32 +02:00
sounds = wood_sound ,
2020-04-17 21:40:13 +02:00
_mcl_blast_resistance = 1.5 ,
2017-02-27 01:13:03 +01:00
_mcl_hardness = 1.5 ,
2020-11-02 19:09:23 +01:00
_mcl_silk_touch_drop = true ,
2022-08-29 04:18:47 +02:00
o n_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 ,
2022-08-31 21:51:38 +02:00
on_destruct = close_forms ,
2017-02-01 23:15:23 +01:00
} )
minetest.register_craft ( {
2021-05-29 16:12:33 +02:00
output = " mcl_books:bookshelf " ,
2017-02-01 23:15:23 +01:00
recipe = {
2021-05-29 16:12:33 +02:00
{ " group:wood " , " group:wood " , " group:wood " } ,
{ " mcl_books:book " , " mcl_books:book " , " mcl_books:book " } ,
{ " group:wood " , " group:wood " , " group:wood " } ,
2017-02-01 23:15:23 +01:00
}
} )
minetest.register_craft ( {
type = " fuel " ,
2017-02-13 14:33:16 +01:00
recipe = " mcl_books:bookshelf " ,
2017-02-01 23:15:23 +01:00
burntime = 15 ,
} )