2015-09-01 17:15:24 +02:00
|
|
|
--
|
|
|
|
-- Editable books
|
|
|
|
-- Code based off the books_plus mod
|
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("book")
|
|
|
|
local F = minetest.formspec_escape
|
|
|
|
|
2019-08-29 05:23:23 +02:00
|
|
|
local BOOK_MAX_TITLE_LENGTH = 64
|
|
|
|
local BOOK_MAX_TEXT_LENGTH = 4500
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_craftitem(
|
2015-09-01 17:15:24 +02:00
|
|
|
":default:book",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("Unnamed Book"),
|
2015-09-01 17:15:24 +02:00
|
|
|
inventory_image = "default_book.png",
|
|
|
|
stack_max = 1,
|
2017-07-04 20:42:22 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
on_use = function(itemstack, player, pointed_thing)
|
2017-07-04 20:42:22 +02:00
|
|
|
local name = player:get_player_name()
|
2019-08-28 22:11:16 +02:00
|
|
|
local data = itemstack:get_meta()
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-07-04 20:42:22 +02:00
|
|
|
local title = ""
|
|
|
|
local text = ""
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-07-04 20:42:22 +02:00
|
|
|
if data then
|
2019-08-28 22:11:16 +02:00
|
|
|
text = data:get_string("book:text")
|
|
|
|
title = data:get_string("book:title")
|
2017-07-04 20:42:22 +02:00
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-07-04 20:42:22 +02:00
|
|
|
local form = default.ui.get_page("default:notabs")
|
2019-08-28 17:31:41 +02:00
|
|
|
form = form .. "field[0.5,1.25;8,0;title;"..F(S("Title:"))..";"..F(title).."]"
|
|
|
|
form = form .. "textarea[0.5,1.75;8,6.75;text;"..F(S("Contents:"))..";"..F(text).."]"
|
|
|
|
form = form .. default.ui.button_exit(2.75, 7.75, 3, 1, "write", S("Write"))
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-07-04 20:42:22 +02:00
|
|
|
minetest.show_formspec(name, "book:book", form)
|
|
|
|
end,
|
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_player_receive_fields(
|
2015-09-01 17:15:24 +02:00
|
|
|
function(player, form_name, fields)
|
|
|
|
if form_name ~= "book:book" or not fields.write then return end
|
|
|
|
|
|
|
|
local itemstack = player:get_wielded_item()
|
|
|
|
|
2017-07-04 20:42:22 +02:00
|
|
|
local meta = itemstack:get_meta()
|
|
|
|
|
2019-08-29 05:23:23 +02:00
|
|
|
-- Limit title and text length
|
|
|
|
if string.len(fields.title) > BOOK_MAX_TITLE_LENGTH then
|
|
|
|
fields.title = string.sub(fields.title, 1, BOOK_MAX_TITLE_LENGTH)
|
|
|
|
end
|
|
|
|
if string.len(fields.text) > BOOK_MAX_TEXT_LENGTH then
|
|
|
|
fields.text= string.sub(fields.text, 1, BOOK_MAX_TEXT_LENGTH)
|
|
|
|
end
|
|
|
|
|
2019-08-28 22:13:07 +02:00
|
|
|
meta:set_string("description", S("Book: “@1”", minetest.colorize("#ffff00", fields.title))) -- Set the item description
|
2017-07-04 20:42:22 +02:00
|
|
|
|
|
|
|
meta:set_string("book:title", fields.title)
|
|
|
|
meta:set_string("book:text", fields.text)
|
2017-05-13 20:50:15 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
player:set_wielded_item(itemstack)
|
2017-07-04 20:42:22 +02:00
|
|
|
end)
|
2015-10-25 22:18:54 +01:00
|
|
|
|
|
|
|
-- Achievements
|
|
|
|
|
|
|
|
achievements.register_achievement(
|
|
|
|
"scribe",
|
|
|
|
{
|
2019-08-28 17:31:41 +02:00
|
|
|
title = S("Scribe"),
|
|
|
|
description = S("Craft a book"),
|
2015-10-25 22:18:54 +01:00
|
|
|
times = 1,
|
2015-11-11 21:36:58 +01:00
|
|
|
craftitem = "default:book",
|
2017-07-04 20:42:22 +02:00
|
|
|
})
|
2015-10-25 22:18:54 +01:00
|
|
|
|
2017-05-13 20:50:15 +02:00
|
|
|
default.log("mod:book", "loaded")
|