1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-11-13 14:00:32 +01:00

[default & nether] Update book handling functions

- In sight of MinetestForFun/server-minetestforfun-skyblock#217
 - default/craftitems.lua : Merge changes made to the book and how it is handled ; updates
 - nether/nether/guide.lua : I don't remember ever modifying it but it was there and modified and it worked so wowie
This commit is contained in:
LeMagnesium 2017-09-06 21:48:27 +02:00
parent e13fb99494
commit 27a7195c52
No known key found for this signature in database
GPG Key ID: A54DDB5272C51E8B
2 changed files with 100 additions and 21 deletions

View File

@ -13,14 +13,23 @@ minetest.register_craftitem("default:paper", {
groups = {flammable = 3},
})
local lpp = 14 -- Lines per book's page
local function book_on_use(itemstack, user)
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 page, page_max, lines, string = 1, 1, {}, ""
if data then
-- Backwards compatibility
local old_data = minetest.deserialize(itemstack:get_metadata())
if old_data then
meta:from_table({ fields = old_data })
end
local data = meta:to_table().fields
if data.owner then
title = data.title
text = data.text
owner = data.owner
@ -66,12 +75,16 @@ local function book_on_use(itemstack, user)
minetest.show_formspec(player_name, "default:book", formspec)
end
local max_text_size = 10000
local max_title_size = 80
local short_title_size = 35
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "default:book" then return end
local inv = player:get_inventory()
local stack = player:get_wielded_item()
if fields.save and fields.title ~= "" and fields.text ~= "" then
if fields.save and fields.title and fields.text
and fields.title ~= "" and fields.text ~= "" then
local new_stack, data
if stack:get_name() ~= "default:book_written" then
local count = stack:get_count()
@ -82,35 +95,46 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
new_stack = ItemStack("default:book_written")
end
else
data = minetest.deserialize(stack:get_metadata())
data = stack:get_meta():to_table().fields
end
if data and data.owner and data.owner ~= player:get_player_name() then
return
end
if not data then data = {} end
data.title = fields.title
data.text = fields.text
data.text_len = #data.text
data.title = fields.title:sub(1, max_title_size)
data.owner = player:get_player_name()
local short_title = data.title
-- Don't bother triming the title if the trailing dots would make it longer
if #short_title > short_title_size + 3 then
short_title = short_title:sub(1, short_title_size) .. "..."
end
data.description = "\""..short_title.."\" by "..data.owner
data.text = fields.text:sub(1, max_text_size)
data.page = 1
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
data.owner = player:get_player_name()
local data_str = minetest.serialize(data)
if new_stack then
new_stack:set_metadata(data_str)
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
stack:set_metadata(data_str)
stack:get_meta():from_table({ fields = data })
end
elseif fields.book_next or fields.book_prev then
local data = minetest.deserialize(stack:get_metadata())
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
@ -123,11 +147,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end
local data_str = minetest.serialize(data)
stack:set_metadata(data_str)
book_on_use(stack, player)
stack:get_meta():from_table({fields = data})
stack = book_on_use(stack, player)
end
-- Update stack
player:set_wielded_item(stack)
end)
@ -168,13 +192,69 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv
if not original then
return
end
local copymeta = original:get_metadata()
local copymeta = original:get_meta():to_table()
-- 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
craft_inv:set_stack("craft", index, original)
end)
minetest.register_craftitem("default:skeleton_key", {
description = "Skeleton Key",
inventory_image = "default_key_skeleton.png",
groups = {key = 1},
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
if not node then
return itemstack
end
local on_skeleton_key_use = minetest.registered_nodes[node.name].on_skeleton_key_use
if not on_skeleton_key_use then
return itemstack
end
-- make a new key secret in case the node callback needs it
local random = math.random
local newsecret = string.format(
"%04x%04x%04x%04x",
random(2^16) - 1, random(2^16) - 1,
random(2^16) - 1, random(2^16) - 1)
local secret, _, _ = on_skeleton_key_use(pos, user, newsecret)
if secret then
local inv = minetest.get_inventory({type="player", name=user:get_player_name()})
-- update original itemstack
itemstack:take_item()
-- finish and return the new key
local new_stack = ItemStack("default:key")
local meta = new_stack:get_meta()
meta:set_string("secret", secret)
meta:set_string("description", "Key to "..user:get_player_name().."'s "
..minetest.registered_nodes[node.name].description)
if itemstack:get_count() == 0 then
itemstack = new_stack
else
if inv:add_item("main", new_stack):get_count() > 0 then
minetest.add_item(user:getpos(), new_stack)
end -- else: added to inventory successfully
end
return itemstack -- FIXME: See if this return is a problem
end
end
})
minetest.register_craftitem("default:coal_lump", {
description = "Coal Lump",
inventory_image = "default_coal_lump.png",
@ -293,4 +373,3 @@ minetest.register_craftitem("default:flint", {
description = "Flint",
inventory_image = "default_flint.png"
})

View File

@ -315,13 +315,13 @@ for n,data in ipairs(guide_infos) do
elseif typ == "x" then
x = math.max(x, content)
elseif typ == "text" then
local tab = minetest.splittext(content, guide_size.fx)
--local tab = minetest.splittext(content, guide_size.fx)
local l = guide_size.cx
for _,str in ipairs(tab) do
--[[for _,str in ipairs(tab) do
form = form.."label["..guide_size.cx ..","..guide_size.cy+y..";"..str.."]"
y = y+guide_size.fy
l = math.max(l, #str)
end
end]]
x = math.max(x, l/font_size)
elseif typ == "image" then
local w, h, texture_name, px, py = unpack(content)