Skip to content

Instantly share code, notes, and snippets.

@ashtrayoz
Created November 27, 2017 10:57
Show Gist options
  • Save ashtrayoz/aad4a50d31b5dc8d0e0c0f9992d8e178 to your computer and use it in GitHub Desktop.
Save ashtrayoz/aad4a50d31b5dc8d0e0c0f9992d8e178 to your computer and use it in GitHub Desktop.
An example of preserving node metadata in minetest.
--() { cat >>/dev/null <<"SHELL_ARCHIVE_AND_LUA_SCRIPT"
--
-- A block that shows text, and holds its text when it is dug up or falls, by ashtrayoz.
--
-- This is an example to test some new callback stuff.
-- If you want to use it in a game, you should probably read through it and figure out
-- what is not finished first ;-)
-- Code is licensed under the same terms as MinetestGame (basically, LGPL 2.1 or later).
--
-- To install:
-- 1. Make an empty directory in your "worldmods" directory. It can have any name you want.
-- 2. Copy this file into that directory, giving it the name "init.lua".
-- 3. cd into that directory.
-- 4. execute the command "bash init.lua".
--
-- If you do not like running shell scripts from strange people on the interwebs, you can extract
-- the image file at the end by hand, and install it and this file in any way you like.
-- This file needs to be renamed to "init.lua", but it does not need to be editted.
local modname = minetest.get_current_modname()
local itemname = modname..":engraved_stone"
local short_prefix = "An engraved stone block with the words: "
local long_prefix = "An engraved stone block. The words begin: "
local too_long = 40
minetest.register_node(itemname, {
description = "Engraved Stone",
drawtype = "nodebox",
tiles = {"engraved_stone.png"},
paramtype = "light",
sunlight_propagates = false,
is_ground_content = false,
walkable = true,
groups = { cracky = 3, attached_node = 1, },
sounds = default.node_sound_stone_defaults(),
-- Preserve our text when we are dug up or fall off.
preserve_metadata = function(pos, node, oldmeta, drops)
local text = oldmeta.text
if text and type(text) == "string" and string.gsub(text, "%s+", "") ~= "" then
if drops[1] and drops[1]:get_name() == itemname then -- In case somebody modifies our drop table.
local meta = drops[1]:get_meta()
meta:set_string("text", text)
if string.len(text) >= too_long then
meta:set_string("description", long_prefix..string.sub(text, 1, too_long-1).." ...")
else
meta:set_string("description", short_prefix..text)
end
end
end
end,
-- Put our text back, if we have any.
after_place_node = function(pos, placer, itemstack, pointed_thing)
local item_meta = itemstack:get_meta()
local text = item_meta:get_string("text")
if text and string.gsub(text, "%s+", "") ~= "" then
local player_name = placer:get_player_name()
minetest.log('action', (player_name or '') .. ' placed a stone saying "' ..
text .. '" at ' .. minetest.pos_to_string(pos))
local node_meta = minetest.get_meta(pos)
node_meta:set_string("text", text)
node_meta:set_string('infotext', '"' .. text .. '"')
end
end,
-- The following is shamelessly stolen from default:wall_sign_wood.
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "field[text;;${text}]")
end,
on_receive_fields = function(pos, formname, fields, sender)
local player_name = sender:get_player_name()
if minetest.is_protected(pos, player_name) then
minetest.record_protection_violation(pos, player_name)
return
end
local meta = minetest.get_meta(pos)
if not fields.text then return end
minetest.log("action", (player_name or "") .. " engraved \"" ..
fields.text .. "\" on a stone at " .. minetest.pos_to_string(pos))
meta:set_string("text", fields.text)
meta:set_string("infotext", '"' .. fields.text .. '"')
end,
})
minetest.register_craft({
output = itemname.." 3",
recipe = {
{"default:stone", "default:stone", "default:stone"},
{"default:stone", "default:stone", "default:stone"},
{"", "group:stick", ""}, -- Hey, it is actually a sign :-)
}
})
--[[
SHELL_ARCHIVE_AND_LUA_SCRIPT
}
# This is the texture file.
# License is (CC BY-SA 3.0).
# It is partly derived from graphics in MinetestGame by celeron55 and others.
mkdir textures
cd textures
uudecode <<"END_OF_IMAGE"
begin 644 engraved_stone.png.gz
M'XL(`('H&UH"`^L,\'/GY9+B8F!@X/7T<`D"T@(@S,(,)-_>?0024`SP"7%-
MC(L-"PG.S\E.BH]+B(G.R\X*#0KT\_:JJ2@/\/7)2$ENXBT1!"KF+/"(+&9@
MX!8&84:&67,D@(+L)9Z^KNP/N:6YI'FM13;^!`IE>;HXAG!<U^T]R'O8@(?9
MLCOT0/UIX1,\6QR:WCSD.N%RT[@HJJ[2)BGC.9?7[DJ9L`/=^UQ9)-[)^:7L
MW>^SZ9OANQ<30]:$GGE55_=Q<P[;/^9=Q7&A"9O/<?@F:B>MDCVTT$=T5D#(
CS[B>I14,\4N4"]\V79H`M)7!T]7/99U30A,``.1>B_@`````
`
end
END_OF_IMAGE
gunzip "engraved_stone.png".gz
cat >>/dev/null <<---END_OF_FILE
--]]
minetest.log(modname.." Loaded")
--END_OF_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment