forked from thunderdog1138/star_wars
340 lines
9.1 KiB
Lua
340 lines
9.1 KiB
Lua
-- mods/default/craftitems.lua
|
|
|
|
-- support for MT game translation.
|
|
local S = default.get_translator
|
|
|
|
minetest.register_craftitem("default:stick", {
|
|
description = S("Stick"),
|
|
inventory_image = "stick.png",
|
|
groups = {stick = 1, flammable = 2},
|
|
})
|
|
|
|
minetest.register_craftitem("default:paper", {
|
|
description = S("Paper"),
|
|
inventory_image = "paper.png",
|
|
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 meta = itemstack:get_meta()
|
|
local title, text, owner = "", "", player_name
|
|
local page, page_max, lines, string = 1, 1, {}, ""
|
|
|
|
-- 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
|
|
|
|
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 esc = minetest.formspec_escape
|
|
if owner == player_name then
|
|
formspec = "size[8,8]" ..
|
|
"field[0.5,1;7.5,0;title;" .. esc(S("Title:")) .. ";" ..
|
|
esc(title) .. "]" ..
|
|
"textarea[0.5,1.5;7.5,7;text;" .. esc(S("Contents:")) .. ";" ..
|
|
esc(text) .. "]" ..
|
|
"button_exit[2.5,7.5;3,1;save;" .. esc(S("Save")) .. "]"
|
|
else
|
|
formspec = "size[8,8]" ..
|
|
"label[0.5,0.5;" .. esc(S("by @1", owner)) .. "]" ..
|
|
"tablecolumns[color;text]" ..
|
|
"tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
|
|
"table[0.4,0;7,0.5;title;#FFFF00," .. esc(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;" .. esc(S("Page @1 of @2", page, page_max)) .. "]" ..
|
|
"button[4.9,7.6;0.8,0.8;book_next;>]"
|
|
end
|
|
|
|
minetest.show_formspec(player_name, "default:book", formspec)
|
|
return itemstack
|
|
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
|
|
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
|
|
else
|
|
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: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 = S("\"@1\" by @2", short_title, data.owner)
|
|
data.text = fields.text:sub(1, max_text_size)
|
|
data.text = data.text:gsub("\r\n", "\n"):gsub("\r", "\n")
|
|
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:get_pos(), new_stack)
|
|
end
|
|
else
|
|
stack:get_meta():from_table({ fields = data })
|
|
end
|
|
|
|
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
|
|
|
|
-- Update stack
|
|
player:set_wielded_item(stack)
|
|
end)
|
|
|
|
minetest.register_craftitem("default:book", {
|
|
description = S("Book"),
|
|
inventory_image = "book.png",
|
|
groups = {book = 1, flammable = 3},
|
|
on_use = book_on_use,
|
|
})
|
|
|
|
minetest.register_craftitem("default:book_written", {
|
|
description = S("Book with Text"),
|
|
inventory_image = "book_written.png",
|
|
groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
|
|
stack_max = 1,
|
|
on_use = book_on_use,
|
|
})
|
|
|
|
minetest.register_craft({
|
|
type = "shapeless",
|
|
output = "default:book_written",
|
|
recipe = {"default:book", "default:book_written"}
|
|
})
|
|
|
|
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
|
|
local copymeta = original:get_meta():to_table()
|
|
-- copy of the book held by player's mouse cursor
|
|
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_keycard", {
|
|
description = S("Skeleton Keycard"),
|
|
inventory_image = "keycard_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:keycard")
|
|
local meta = new_stack:get_meta()
|
|
meta:set_string("secret", secret)
|
|
meta:set_string("description", S("Key to @1's @2", user:get_player_name(),
|
|
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:get_pos(), new_stack)
|
|
end -- else: added to inventory successfully
|
|
end
|
|
|
|
return itemstack
|
|
end
|
|
end
|
|
})
|
|
|
|
minetest.register_craftitem("default:coal_lump", {
|
|
description = S("Coal Lump"),
|
|
inventory_image = "coal_lump.png",
|
|
groups = {coal = 1, flammable = 1}
|
|
})
|
|
|
|
minetest.register_craftitem("default:zersium_lump", {
|
|
description = S("Zersium Lump"),
|
|
inventory_image = "zersium_lump.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:copper_lump", {
|
|
description = S("Copper Lump"),
|
|
inventory_image = "copper_lump.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:tin_lump", {
|
|
description = S("Tin Lump"),
|
|
inventory_image = "tin_lump.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:gold_lump", {
|
|
description = S("Gold Lump"),
|
|
inventory_image = "gold_lump.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:beskar_lump", {
|
|
description = S("Beskar Lump"),
|
|
inventory_image = "beskar_lump.png",
|
|
})
|
|
|
|
minetest.register_craftitem("default:clay_lump", {
|
|
description = S("Clay Lump"),
|
|
inventory_image = "clay_lump.png",
|
|
})
|
|
|
|
minetest.register_craftitem("default:durasteel_ingot", {
|
|
description = S("Durasteel Ingot"),
|
|
inventory_image = "durasteel_ingot.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:copper_ingot", {
|
|
description = S("Copper Ingot"),
|
|
inventory_image = "copper_ingot.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:tin_ingot", {
|
|
description = S("Tin Ingot"),
|
|
inventory_image = "tin_ingot.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:bronze_ingot", {
|
|
description = S("Bronze Ingot"),
|
|
inventory_image = "bronze_ingot.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:gold_ingot", {
|
|
description = S("Gold Ingot"),
|
|
inventory_image = "gold_ingot.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:beskar_ingot", {
|
|
description = S("Beskar Ingot"),
|
|
inventory_image = "beskar_ingot.png"
|
|
})
|
|
|
|
minetest.register_craftitem("default:clay_brick", {
|
|
description = S("Clay Brick"),
|
|
inventory_image = "clay_brick.png",
|
|
})
|
|
|
|
minetest.register_craftitem("default:obsidian_shard", {
|
|
description = S("Obsidian Shard"),
|
|
inventory_image = "obsidian_shard.png",
|
|
})
|
|
|
|
minetest.register_craftitem("default:flint", {
|
|
description = S("Flint"),
|
|
inventory_image = "flint.png"
|
|
}) |