2013-05-19 18:43:04 +02:00
|
|
|
-- mods/default/craftitems.lua
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:stick", {
|
|
|
|
description = "Stick",
|
|
|
|
inventory_image = "default_stick.png",
|
2016-10-24 20:34:00 +02:00
|
|
|
groups = {stick = 1, flammable = 2},
|
2013-05-19 18:43:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:paper", {
|
|
|
|
description = "Paper",
|
|
|
|
inventory_image = "default_paper.png",
|
2016-10-24 20:34:00 +02:00
|
|
|
groups = {flammable = 3},
|
2013-05-19 18:43:04 +02:00
|
|
|
})
|
|
|
|
|
2016-04-10 21:29:02 +02:00
|
|
|
local lpp = 14 -- Lines per book's page
|
2016-03-16 22:01:14 +01:00
|
|
|
local function book_on_use(itemstack, user)
|
2015-01-24 16:29:55 +01:00
|
|
|
local player_name = user:get_player_name()
|
2017-02-26 19:37:35 +01:00
|
|
|
local meta = itemstack:get_meta()
|
2016-06-26 13:34:14 +02:00
|
|
|
local title, text, owner = "", "", player_name
|
2016-04-10 21:29:02 +02:00
|
|
|
local page, page_max, lines, string = 1, 1, {}, ""
|
2016-03-16 22:01:14 +01:00
|
|
|
|
2017-02-26 19:37:35 +01:00
|
|
|
-- 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
|
2016-03-16 22:01:14 +01:00
|
|
|
title = data.title
|
|
|
|
text = data.text
|
|
|
|
owner = data.owner
|
2016-03-23 19:31:25 +01:00
|
|
|
|
2016-04-10 21:29:02 +02:00
|
|
|
for str in (text .. "\n"):gmatch("([^\n]*)[\n]") do
|
|
|
|
lines[#lines+1] = str
|
|
|
|
end
|
|
|
|
|
2016-03-23 19:31:25 +01:00
|
|
|
if data.page then
|
|
|
|
page = data.page
|
|
|
|
page_max = data.page_max
|
2016-04-10 21:29:02 +02:00
|
|
|
|
|
|
|
for i = ((lpp * page) - lpp) + 1, lpp * page do
|
|
|
|
if not lines[i] then break end
|
|
|
|
string = string .. lines[i] .. "\n"
|
|
|
|
end
|
2016-03-23 19:31:25 +01:00
|
|
|
end
|
2015-01-24 16:29:55 +01:00
|
|
|
end
|
2016-03-16 22:01:14 +01:00
|
|
|
|
2016-06-26 13:34:14 +02:00
|
|
|
local formspec
|
2015-01-24 16:29:55 +01:00
|
|
|
if owner == player_name then
|
2016-03-03 04:41:50 +01:00
|
|
|
formspec = "size[8,8]" .. default.gui_bg ..
|
|
|
|
default.gui_bg_img ..
|
|
|
|
"field[0.5,1;7.5,0;title;Title:;" ..
|
|
|
|
minetest.formspec_escape(title) .. "]" ..
|
|
|
|
"textarea[0.5,1.5;7.5,7;text;Contents:;" ..
|
|
|
|
minetest.formspec_escape(text) .. "]" ..
|
2015-01-24 16:29:55 +01:00
|
|
|
"button_exit[2.5,7.5;3,1;save;Save]"
|
|
|
|
else
|
2016-03-03 04:41:50 +01:00
|
|
|
formspec = "size[8,8]" .. default.gui_bg ..
|
|
|
|
default.gui_bg_img ..
|
|
|
|
"label[0.5,0.5;by " .. owner .. "]" ..
|
2016-03-16 22:01:14 +01:00
|
|
|
"tablecolumns[color;text]" ..
|
|
|
|
"tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
|
2016-03-23 19:31:25 +01:00
|
|
|
"table[0.4,0;7,0.5;title;#FFFF00," .. minetest.formspec_escape(title) .. "]" ..
|
2016-04-10 21:29:02 +02:00
|
|
|
"textarea[0.5,1.5;7.5,7;;" ..
|
|
|
|
minetest.formspec_escape(string ~= "" and string or text) .. ";]" ..
|
2016-03-21 20:37:35 +01:00
|
|
|
"button[2.4,7.6;0.8,0.8;book_prev;<]" ..
|
2016-03-23 19:31:25 +01:00
|
|
|
"label[3.2,7.7;Page " .. page .. " of " .. page_max .. "]" ..
|
2016-03-21 20:37:35 +01:00
|
|
|
"button[4.9,7.6;0.8,0.8;book_next;>]"
|
2015-01-24 16:29:55 +01:00
|
|
|
end
|
2016-03-16 22:01:14 +01:00
|
|
|
|
|
|
|
minetest.show_formspec(player_name, "default:book", formspec)
|
2017-03-14 06:28:03 +01:00
|
|
|
return itemstack
|
2015-01-24 16:29:55 +01:00
|
|
|
end
|
|
|
|
|
2016-03-16 22:01:14 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if formname ~= "default:book" then return end
|
2015-01-24 16:29:55 +01:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
local stack = player:get_wielded_item()
|
2016-03-16 22:01:14 +01:00
|
|
|
|
|
|
|
if fields.save and fields.title ~= "" and fields.text ~= "" then
|
|
|
|
local new_stack, data
|
|
|
|
if stack:get_name() ~= "default:book_written" then
|
|
|
|
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
|
2015-01-24 16:29:55 +01:00
|
|
|
else
|
2017-02-26 19:37:35 +01:00
|
|
|
data = stack:get_meta():to_table().fields
|
2015-01-24 16:29:55 +01:00
|
|
|
end
|
2016-03-16 22:01:14 +01:00
|
|
|
|
|
|
|
if not data then data = {} end
|
|
|
|
data.title = fields.title
|
2017-02-26 19:37:35 +01:00
|
|
|
data.owner = player:get_player_name()
|
|
|
|
data.description = "\""..fields.title.."\" by "..data.owner
|
2016-03-16 22:01:14 +01:00
|
|
|
data.text = fields.text
|
2016-04-10 21:29:02 +02:00
|
|
|
data.text_len = #data.text
|
2016-03-16 22:01:14 +01:00
|
|
|
data.page = 1
|
2016-04-10 21:29:02 +02:00
|
|
|
data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp)
|
2016-03-16 22:01:14 +01:00
|
|
|
|
|
|
|
if new_stack then
|
2017-02-26 19:37:35 +01:00
|
|
|
new_stack:get_meta():from_table({ fields = data })
|
2016-03-16 22:01:14 +01:00
|
|
|
if inv:room_for_item("main", new_stack) then
|
|
|
|
inv:add_item("main", new_stack)
|
|
|
|
else
|
|
|
|
minetest.add_item(player:getpos(), new_stack)
|
|
|
|
end
|
2015-01-24 16:29:55 +01:00
|
|
|
else
|
2017-02-26 19:37:35 +01:00
|
|
|
stack:get_meta():from_table({ fields = data })
|
2015-01-24 16:29:55 +01:00
|
|
|
end
|
2016-03-16 22:01:14 +01:00
|
|
|
|
|
|
|
elseif fields.book_next or fields.book_prev then
|
2017-02-26 19:37:35 +01:00
|
|
|
local data = stack:get_meta():to_table().fields
|
2016-10-04 19:49:06 +02:00
|
|
|
if not data or not data.page then
|
|
|
|
return
|
|
|
|
end
|
2016-03-23 19:31:25 +01:00
|
|
|
|
2017-02-26 19:37:35 +01:00
|
|
|
data.page = tonumber(data.page)
|
|
|
|
data.page_max = tonumber(data.page_max)
|
|
|
|
|
2016-03-16 22:01:14 +01:00
|
|
|
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
|
|
|
|
|
2017-03-14 06:28:03 +01:00
|
|
|
stack:get_meta():from_table(data)
|
|
|
|
stack = book_on_use(stack, player)
|
2015-01-24 16:29:55 +01:00
|
|
|
end
|
2016-04-10 21:29:02 +02:00
|
|
|
|
2017-03-14 06:28:03 +01:00
|
|
|
-- Update stack
|
2016-04-10 21:29:02 +02:00
|
|
|
player:set_wielded_item(stack)
|
2015-01-24 16:29:55 +01:00
|
|
|
end)
|
|
|
|
|
2013-05-19 18:43:04 +02:00
|
|
|
minetest.register_craftitem("default:book", {
|
|
|
|
description = "Book",
|
|
|
|
inventory_image = "default_book.png",
|
2016-10-24 20:34:00 +02:00
|
|
|
groups = {book = 1, flammable = 3},
|
2015-01-24 16:29:55 +01:00
|
|
|
on_use = book_on_use,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:book_written", {
|
|
|
|
description = "Book With Text",
|
2015-12-08 01:29:33 +01:00
|
|
|
inventory_image = "default_book_written.png",
|
2016-10-24 20:34:00 +02:00
|
|
|
groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
|
2015-01-24 16:29:55 +01:00
|
|
|
stack_max = 1,
|
|
|
|
on_use = book_on_use,
|
2013-05-19 18:43:04 +02:00
|
|
|
})
|
|
|
|
|
2015-12-15 06:42:09 +01:00
|
|
|
minetest.register_craft({
|
|
|
|
type = "shapeless",
|
|
|
|
output = "default:book_written",
|
2016-03-03 04:41:50 +01:00
|
|
|
recipe = {"default:book", "default:book_written"}
|
2015-12-15 06:42:09 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
|
|
|
|
if itemstack:get_name() ~= "default:book_written" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local original
|
|
|
|
local index
|
|
|
|
for i = 1, player:get_inventory():get_size("craft") do
|
|
|
|
if old_craft_grid[i]:get_name() == "default:book_written" then
|
|
|
|
original = old_craft_grid[i]
|
|
|
|
index = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not original then
|
|
|
|
return
|
|
|
|
end
|
2017-03-14 06:28:03 +01:00
|
|
|
local copymeta = original:get_meta():to_table()
|
2015-12-15 06:42:09 +01:00
|
|
|
-- copy of the book held by player's mouse cursor
|
2017-03-14 06:28:03 +01:00
|
|
|
itemstack:get_meta():from_table(copymeta)
|
2015-12-15 06:42:09 +01:00
|
|
|
-- put the book with metadata back in the craft grid
|
|
|
|
craft_inv:set_stack("craft", index, original)
|
|
|
|
end)
|
|
|
|
|
2013-05-19 18:43:04 +02:00
|
|
|
minetest.register_craftitem("default:coal_lump", {
|
|
|
|
description = "Coal Lump",
|
|
|
|
inventory_image = "default_coal_lump.png",
|
2016-10-24 20:34:00 +02:00
|
|
|
groups = {coal = 1, flammable = 1}
|
2013-05-19 18:43:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:iron_lump", {
|
|
|
|
description = "Iron Lump",
|
|
|
|
inventory_image = "default_iron_lump.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:copper_lump", {
|
|
|
|
description = "Copper Lump",
|
|
|
|
inventory_image = "default_copper_lump.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:mese_crystal", {
|
|
|
|
description = "Mese Crystal",
|
|
|
|
inventory_image = "default_mese_crystal.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:gold_lump", {
|
|
|
|
description = "Gold Lump",
|
|
|
|
inventory_image = "default_gold_lump.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:diamond", {
|
|
|
|
description = "Diamond",
|
|
|
|
inventory_image = "default_diamond.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:clay_lump", {
|
|
|
|
description = "Clay Lump",
|
|
|
|
inventory_image = "default_clay_lump.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:steel_ingot", {
|
|
|
|
description = "Steel Ingot",
|
|
|
|
inventory_image = "default_steel_ingot.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:copper_ingot", {
|
|
|
|
description = "Copper Ingot",
|
|
|
|
inventory_image = "default_copper_ingot.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:bronze_ingot", {
|
|
|
|
description = "Bronze Ingot",
|
|
|
|
inventory_image = "default_bronze_ingot.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:gold_ingot", {
|
|
|
|
description = "Gold Ingot",
|
|
|
|
inventory_image = "default_gold_ingot.png"
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:mese_crystal_fragment", {
|
|
|
|
description = "Mese Crystal Fragment",
|
|
|
|
inventory_image = "default_mese_crystal_fragment.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:clay_brick", {
|
|
|
|
description = "Clay Brick",
|
|
|
|
inventory_image = "default_clay_brick.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craftitem("default:obsidian_shard", {
|
|
|
|
description = "Obsidian Shard",
|
|
|
|
inventory_image = "default_obsidian_shard.png",
|
|
|
|
})
|
2016-03-11 18:27:22 +01:00
|
|
|
|
|
|
|
minetest.register_craftitem("default:flint", {
|
|
|
|
description = "Flint",
|
|
|
|
inventory_image = "default_flint.png"
|
|
|
|
})
|