Updated Books Mod
I need the page system added to my books, Hopefully books that are already made with work alright with this.
This commit is contained in:
parent
d6e57ac4e0
commit
88430ec55e
@ -11,13 +11,41 @@ minetest.register_craftitem("default:paper", {
|
|||||||
inventory_image = "default_paper.png",
|
inventory_image = "default_paper.png",
|
||||||
})
|
})
|
||||||
|
|
||||||
local function book_on_use(itemstack, user, pointed_thing)
|
local lpp = 14 -- Lines per book's page
|
||||||
|
local function book_on_use(itemstack, user)
|
||||||
local player_name = user:get_player_name()
|
local player_name = user:get_player_name()
|
||||||
local data = minetest.deserialize(itemstack:get_metadata())
|
local meta = itemstack:get_meta()
|
||||||
local title, text, owner = "", "", player_name
|
local title, text, owner = "", "", player_name
|
||||||
if data then
|
local page, page_max, lines, string = 1, 1, {}, ""
|
||||||
title, text, owner = data.title, data.text, data.owner
|
|
||||||
|
-- Backwards compatibility
|
||||||
|
local old_data = minetest.deserialize(itemstack:get_metadata())
|
||||||
|
if old_data then
|
||||||
|
meta:from_table({ fields = old_data })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local data = meta:to_table().fields
|
||||||
|
|
||||||
|
if data.owner then
|
||||||
|
title = data.title
|
||||||
|
text = data.text
|
||||||
|
owner = data.owner
|
||||||
|
|
||||||
|
for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
|
||||||
|
lines[#lines+1] = str
|
||||||
|
end
|
||||||
|
|
||||||
|
if data.page then
|
||||||
|
page = data.page
|
||||||
|
page_max = data.page_max
|
||||||
|
|
||||||
|
for i = ((lpp * page) - lpp) + 1, lpp * page do
|
||||||
|
if not lines[i] then break end
|
||||||
|
string = string .. lines[i] .. "\n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local formspec
|
local formspec
|
||||||
if owner == player_name then
|
if owner == player_name then
|
||||||
formspec = "size[8,8]" .. default.gui_bg ..
|
formspec = "size[8,8]" .. default.gui_bg ..
|
||||||
@ -31,61 +59,103 @@ local function book_on_use(itemstack, user, pointed_thing)
|
|||||||
formspec = "size[8,8]" .. default.gui_bg ..
|
formspec = "size[8,8]" .. default.gui_bg ..
|
||||||
default.gui_bg_img ..
|
default.gui_bg_img ..
|
||||||
"label[0.5,0.5;by " .. owner .. "]" ..
|
"label[0.5,0.5;by " .. owner .. "]" ..
|
||||||
"label[0.5,0;" .. minetest.formspec_escape(title) .. "]" ..
|
"tablecolumns[color;text]" ..
|
||||||
"textarea[0.5,1.5;7.5,7;text;;" ..
|
"tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
|
||||||
minetest.formspec_escape(text) .. "]"
|
"table[0.4,0;7,0.5;title;#FFFF00," .. minetest.formspec_escape(title) .. "]" ..
|
||||||
|
"textarea[0.5,1.5;7.5,7;;" ..
|
||||||
|
minetest.formspec_escape(string ~= "" and string or text) .. ";]" ..
|
||||||
|
"button[2.4,7.6;0.8,0.8;book_prev;<]" ..
|
||||||
|
"label[3.2,7.7;Page " .. page .. " of " .. page_max .. "]" ..
|
||||||
|
"button[4.9,7.6;0.8,0.8;book_next;>]"
|
||||||
end
|
end
|
||||||
minetest.show_formspec(user:get_player_name(), "default:book", formspec)
|
|
||||||
|
minetest.show_formspec(player_name, "default:book", formspec)
|
||||||
|
return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, form_name, fields)
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
if form_name ~= "default:book" or not fields.save or
|
if formname ~= "default:book" then return end
|
||||||
fields.title == "" or fields.text == "" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local inv = player:get_inventory()
|
local inv = player:get_inventory()
|
||||||
local stack = player:get_wielded_item()
|
local stack = player:get_wielded_item()
|
||||||
local new_stack, data
|
|
||||||
if stack:get_name() ~= "default:book_written" then
|
if fields.save and fields.title ~= "" and fields.text ~= "" then
|
||||||
local count = stack:get_count()
|
local new_stack, data
|
||||||
if count == 1 then
|
if stack:get_name() ~= "default:book_written" then
|
||||||
stack:set_name("default:book_written")
|
local count = stack:get_count()
|
||||||
|
if count == 1 then
|
||||||
|
stack:set_name("default:book_written")
|
||||||
|
else
|
||||||
|
stack:set_count(count - 1)
|
||||||
|
new_stack = ItemStack("default:book_written")
|
||||||
|
end
|
||||||
else
|
else
|
||||||
stack:set_count(count - 1)
|
data = stack:get_meta():to_table().fields
|
||||||
new_stack = ItemStack("default:book_written")
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
data = minetest.deserialize(stack:get_metadata())
|
if data and data.owner and data.owner ~= player:get_player_name() then
|
||||||
end
|
return
|
||||||
if not data then data = {} end
|
end
|
||||||
data.title = fields.title
|
|
||||||
data.text = fields.text
|
if not data then data = {} end
|
||||||
data.owner = player:get_player_name()
|
data.title = fields.title
|
||||||
local data_str = minetest.serialize(data)
|
data.owner = player:get_player_name()
|
||||||
if new_stack then
|
data.description = "\""..fields.title.."\" by "..data.owner
|
||||||
new_stack:set_metadata(data_str)
|
data.text = fields.text
|
||||||
if inv:room_for_item("main", new_stack) then
|
data.text_len = #data.text
|
||||||
inv:add_item("main", new_stack)
|
data.page = 1
|
||||||
|
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
|
||||||
|
|
||||||
|
if new_stack then
|
||||||
|
new_stack:get_meta():from_table({ fields = data })
|
||||||
|
if inv:room_for_item("main", new_stack) then
|
||||||
|
inv:add_item("main", new_stack)
|
||||||
|
else
|
||||||
|
minetest.add_item(player:getpos(), new_stack)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
minetest.add_item(player:getpos(), new_stack)
|
stack:get_meta():from_table({ fields = data })
|
||||||
end
|
end
|
||||||
else
|
|
||||||
stack:set_metadata(data_str)
|
elseif fields.book_next or fields.book_prev then
|
||||||
|
local data = stack:get_meta():to_table().fields
|
||||||
|
if not data or not data.page then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
data.page = tonumber(data.page)
|
||||||
|
data.page_max = tonumber(data.page_max)
|
||||||
|
|
||||||
|
if fields.book_next then
|
||||||
|
data.page = data.page + 1
|
||||||
|
if data.page > data.page_max then
|
||||||
|
data.page = 1
|
||||||
|
end
|
||||||
|
else
|
||||||
|
data.page = data.page - 1
|
||||||
|
if data.page == 0 then
|
||||||
|
data.page = data.page_max
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
stack:get_meta():from_table({fields = data})
|
||||||
|
stack = book_on_use(stack, player)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Update stack
|
||||||
player:set_wielded_item(stack)
|
player:set_wielded_item(stack)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_craftitem("default:book", {
|
minetest.register_craftitem("default:book", {
|
||||||
description = "Book",
|
description = "Book",
|
||||||
inventory_image = "default_book.png",
|
inventory_image = "default_book.png",
|
||||||
groups = {book = 1},
|
groups = {book = 1, flammable = 3},
|
||||||
on_use = book_on_use,
|
on_use = book_on_use,
|
||||||
})
|
})
|
||||||
|
|
||||||
minetest.register_craftitem("default:book_written", {
|
minetest.register_craftitem("default:book_written", {
|
||||||
description = "Book With Text",
|
description = "Book With Text",
|
||||||
inventory_image = "default_book_written.png",
|
inventory_image = "default_book_written.png",
|
||||||
groups = {book = 1, not_in_creative_inventory = 1},
|
groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
|
||||||
stack_max = 1,
|
stack_max = 1,
|
||||||
on_use = book_on_use,
|
on_use = book_on_use,
|
||||||
})
|
})
|
||||||
@ -101,7 +171,6 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local copy = ItemStack("default:book_written")
|
|
||||||
local original
|
local original
|
||||||
local index
|
local index
|
||||||
for i = 1, player:get_inventory():get_size("craft") do
|
for i = 1, player:get_inventory():get_size("craft") do
|
||||||
@ -113,9 +182,9 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
|
|||||||
if not original then
|
if not original then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local copymeta = original:get_metadata()
|
local copymeta = original:get_meta():to_table()
|
||||||
-- copy of the book held by player's mouse cursor
|
-- copy of the book held by player's mouse cursor
|
||||||
itemstack:set_metadata(copymeta)
|
itemstack:get_meta():from_table(copymeta)
|
||||||
-- put the book with metadata back in the craft grid
|
-- put the book with metadata back in the craft grid
|
||||||
craft_inv:set_stack("craft", index, original)
|
craft_inv:set_stack("craft", index, original)
|
||||||
end)
|
end)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user