summaryrefslogtreecommitdiff
path: root/memorandum/init.lua
blob: 7542f13043358b1d8477690f5b9a084841b10e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
-----------------------------------------------------------------------------------------------
local title		= "Memorandum"
local version 	= "0.0.9"
local mname		= "memorandum"
-----------------------------------------------------------------------------------------------
--				{ left	, bottom , front  ,  right ,  top   ,  back  }
local sheet =	{ -1/2  , -1/2   , -1/2   , 1/2    , -7/16  ,  1/2  }
local info  =	'On this piece of paper is written: "'
local sign  =	'" Signed by '
			
minetest.register_craftitem(":default:paper", {
	description = "Paper",
	inventory_image = "default_paper.png",
	on_place = function(itemstack, placer, pointed_thing)
		local pt = pointed_thing
		local direction = minetest.dir_to_facedir(placer:get_look_dir())
		local there = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
		if minetest.get_node(there).name == "air" then
			minetest.add_node(there, {name="memorandum:letter_empty", param2=direction})
			itemstack:take_item()
			return itemstack
		end
	end,
})

minetest.register_node("memorandum:letter_empty", {
	drawtype = "nodebox",
	tiles = {"memorandum_letter_empty.png"},
	paramtype = "light",
	paramtype2 = "facedir",
	sunlight_propagates = true,
	walkable = false,
	node_box = {type = "fixed", fixed = sheet},
	groups = {snappy=3,dig_immediate=3,not_in_creative_inventory=1},
	sounds = default.node_sound_leaves_defaults(),
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string(
					"formspec", 
					"size[10,7]"..
					"field[1,1;8.5,1;text; Write a Letter;${text}]"..
					"field[1,3;4.25,1;signed; Sign Letter (optional);${signed}]"..
					"button_exit[0.75,5;4.25,1;text,signed;Done]"
				)
		meta:set_string("infotext", info..'"')
	end,
	on_receive_fields = function(pos, formname, fields, sender)
		local meta = minetest.get_meta(pos)
		fields.text = fields.text
		fields.signed = fields.signed
		--[[print((sender:get_player_name() or "").." wrote \""..fields.text..
				"\" to paper at "..minetest.pos_to_string(pos))]]
		local direction = minetest.get_node(pos).param2
		if fields.text ~= "" then
			minetest.add_node(pos, {name="memorandum:letter_written", param2=direction})
		end
		meta:set_string("text", fields.text)
		meta:set_string("signed", "")
		if fields.text then
			if fields.signed ~= "" then
				meta:set_string("signed", fields.signed)
				meta:set_string("infotext", info..fields.text..sign..fields.signed)
			else
				meta:set_string("infotext", info..fields.text..'" Unsigned')
			end
		else
			meta:set_string("infotext", "" )	
		end
	end,
	on_dig = function(pos, node, digger)
		if digger:is_player() and digger:get_inventory() then
			digger:get_inventory():add_item("main", {name="default:paper", count=1, wear=0, metadata=""})
		end
		minetest.remove_node(pos)
	end,
})

minetest.register_craftitem("memorandum:letter", {
	description = "Letter",
	inventory_image = "default_paper.png^memorandum_letters.png",
	stack_max = 1,
	groups = {not_in_creative_inventory=1},
	on_use = function(itemstack, user, pointed_thing)
		local player = user:get_player_name()
		local text = itemstack:get_metadata()
		local scnt = string.sub (text, -2, -1)
		if scnt == "00" then
			mssg = string.sub (text, 1, -3)
			sgnd = ""
		elseif tonumber(scnt) == nil then -- to support previous versions
			mssg = string.sub (text, 37, -1)
			sgnd = ""
		else
			mssg = string.sub (text, 1, -scnt -3)
			sgnd = string.sub (text, -scnt-2, -3)
		end
		if scnt == "00" or tonumber(scnt) == nil then
			minetest.chat_send_player(player, info..mssg..'" Unsigned', false)
		else
			minetest.chat_send_player(player, info..mssg..sign..sgnd, false)
		end
	end,
	on_place = function(itemstack, placer, pointed_thing)
		local pt = pointed_thing
		local there = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
		local direction = minetest.dir_to_facedir(placer:get_look_dir())
		local meta = minetest.get_meta(there)
		local text = itemstack:get_metadata()
		local scnt = string.sub (text, -2, -1)
		if scnt == "00" then
			mssg = string.sub (text, 1, -3)
			sgnd = ""
		elseif tonumber(scnt) == nil then -- to support previous versions
			mssg = string.sub (text, 37, -1)
			sgnd = ""
		else
			mssg = string.sub (text, 1, -scnt -3)
			sgnd = string.sub (text, -scnt-2, -3)
		end
		if minetest.get_node(there).name == "air" then
			minetest.add_node(there, {name="memorandum:letter_written", param2=direction})
			if scnt == "00" or tonumber(scnt) == nil then
				meta:set_string("infotext", info..mssg..'" Unsigned')
			else
				meta:set_string("infotext", info..mssg..sign..sgnd)
			end
			meta:set_string("text", mssg)
			meta:set_string("signed", sgnd)
			itemstack:take_item()
			return itemstack
		end
	end,
})

minetest.register_node("memorandum:letter_written", {
	drawtype = "nodebox",
	tiles = {"memorandum_letter_empty.png^memorandum_letter_text.png"},
	paramtype = "light",
	paramtype2 = "facedir",
	sunlight_propagates = true,
	walkable = false,
	node_box = {type = "fixed", fixed = sheet},
	groups = {snappy=3,dig_immediate=3,not_in_creative_inventory=1},
	sounds = default.node_sound_leaves_defaults(),
	on_receive_fields = function(pos, formname, fields, sender)
		local item = sender:get_wielded_item()
		if item:get_name() == "memorandum:eraser" then
			local meta = minetest.get_meta(pos)
			fields.text = fields.text
			fields.signed = fields.signed
			--[[print((sender:get_player_name() or "").." wrote \""..fields.text..
				"\" to paper at "..minetest.pos_to_string(pos))]]
			local direction = minetest.get_node(pos).param2
			if fields.text == "" then
				minetest.add_node(pos, {name="memorandum:letter_empty", param2=direction})
			end
			meta:set_string("text", fields.text)
			meta:set_string("signed", "")
			if fields.text then
				if fields.signed ~= "" then
					meta:set_string("signed", fields.signed)
					meta:set_string("infotext", info..fields.text..sign..fields.signed)
				else
					meta:set_string("infotext", info..fields.text..'" Unsigned')
				end
			else
				meta:set_string("infotext", "" )	
			end
		end
	end,
	on_dig = function(pos, node, digger)
		if digger:is_player() and digger:get_inventory() then
			local meta = minetest.get_meta(pos)
			local text = meta:get_string("text")
			local signed = meta:get_string("signed")
			local signcount = string.len(signed)
			local item = digger:get_wielded_item()
			local inv = digger:get_inventory()
			if string.len(signed) < 10 then
				signcount = "0"..string.len(signed)
			end
			if signed == '" Unsigned' then
				signcount = "00"
			end
			if item:get_name() == "vessels:glass_bottle" then
				inv:remove_item("main", "vessels:glass_bottle")
				inv:add_item("main", {name="memorandum:message", count=1, wear=0, metadata=text..signed..signcount})
			else
				inv:add_item("main", {name="memorandum:letter", count=1, wear=0, metadata=text..signed..signcount})
			end
		end
		minetest.remove_node(pos)
	end,
})

local function eraser_wear(itemstack, user, pointed_thing, uses)
	itemstack:add_wear(65535/(uses-1))
	return itemstack
end

minetest.register_tool("memorandum:eraser", {
	description = "Eraser",
	inventory_image = "memorandum_eraser.png",
	wield_image = "memorandum_eraser.png^[transformR90",--^[transformFX",
	wield_scale = {x = 0.5, y = 0.5, z = 1},
	on_use = function(itemstack, user, pointed_thing)
		local pt = pointed_thing
		if pt and pt.under then
			local node = minetest.get_node(pt.under)
			local meta = minetest.get_meta(pt.under)
			local player = user:get_player_name()
			local signer = meta:get_string("signed")
			if string.find(node.name, "memorandum:letter_written") then
				if signer == player or signer == "" then
					meta:set_string(
						"formspec", 
						"size[10,7]"..
						"field[1,1;8.5,1;text; Edit Text;${text}]"..
						"field[1,3;4.25,1;signed; Edit Signature;${signed}]"..
						"button_exit[0.75,5;4.25,1;text,signed;Done]"
					)
					if not minetest.setting_getbool("creative_mode") then
						return eraser_wear(itemstack, user, pointed_thing, 30)	
					else
						return {name="memorandum:eraser", count=1, wear=0, metadata=""}
					end
				end
			end
		end
	end,
})

minetest.register_node("memorandum:message", {
	description = "Message in a Bottle",
	drawtype = "plantlike",
	tiles = {"vessels_glass_bottle.png^memorandum_message.png"},
	inventory_image = "vessels_glass_bottle_inv.png^memorandum_message.png",
	wield_image = "vessels_glass_bottle.png^memorandum_message.png",
	paramtype = "light",
	selection_box = {
		type = "fixed",
		fixed = {-1/4, -1/2, -1/4, 1/4, 4/10, 1/4}
	},
	stack_max = 1,
	groups = {vessel=1,dig_immediate=3,attached_node=1,not_in_creative_inventory=1},
	--sounds = default.node_sound_glass_defaults(),
	on_use = function(itemstack, user, pointed_thing)
		local pt = pointed_thing
		if  pt.under then
			local there = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
			local meta = minetest.get_meta(there)
			local text = itemstack:get_metadata()
			local scnt = string.sub (text, -2, -1)
			if scnt == "00" then
				mssg = string.sub (text, 1, -3)
				sgnd = ""
			elseif tonumber(scnt) == nil then -- to support previous versions
				mssg = string.sub (text, 37, -1)
				sgnd = ""
			else
				mssg = string.sub (text, 1, -scnt -3)
				sgnd = string.sub (text, -scnt-2, -3)
			end
			if  minetest.get_node(there).name == "air" then
				minetest.add_node(there, {name="memorandum:letter_written", param2=math.random(0,3)})
				if scnt == "00" or tonumber(scnt) == nil then
					meta:set_string("infotext", info..mssg..'" Unsigned')
				else
					meta:set_string("infotext", info..mssg..sign..sgnd)
				end
				meta:set_string("text", mssg)
				meta:set_string("signed", sgnd)
				itemstack:take_item()
				user:get_inventory():add_item("main", {name="vessels:glass_bottle", count=1, wear=0, metadata=""})
				return itemstack
			end
		end
	end,
	on_place = function(itemstack, placer, pointed_thing)
		local pt = pointed_thing
		local there = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
		local meta = minetest.get_meta(there)
		local text = itemstack:get_metadata()
		local scnt = string.sub (text, -2, -1)
		if scnt == "00" then
			mssg = string.sub (text, 1, -3)
			sgnd = ""
		elseif tonumber(scnt) == nil then -- to support previous versions
			mssg = string.sub (text, 37, -1)
			sgnd = ""
		else
			mssg = string.sub (text, 1, -scnt -3)
			sgnd = string.sub (text, -scnt-2, -3)
		end
		if minetest.get_node(there).name == "air" then
			minetest.add_node(there, {name="memorandum:message"})
			meta:set_string("text", mssg)
			meta:set_string("signed", sgnd)
			itemstack:take_item()
			return itemstack
		end
	end,
	on_dig = function(pos, node, digger)
		if digger:is_player() and digger:get_inventory() then
			local meta = minetest.get_meta(pos)
			local text = meta:get_string("text")
			local signed = meta:get_string("signed")
			local signcount = string.len(signed)
			local item = digger:get_wielded_item()
			if string.len(signed) < 10 then
				signcount = "0"..string.len(signed)
			end
			if signed == '" Unsigned' then
				signcount = "00"
			end
			digger:get_inventory():add_item("main", {name="memorandum:message", count=1, wear=0, metadata=text..signed..signcount})
		end
		minetest.remove_node(pos)
	end,
})

if minetest.get_modpath("farming") ~= nil then
minetest.register_craft({
	type = "shapeless",
	output = "memorandum:eraser",
	recipe = {"farming:bread"},
})
end
if minetest.get_modpath("candles") ~= nil then
minetest.register_craft({
	type = "shapeless",
	output = "memorandum:eraser",
	recipe = {"candles:wax"},
})
end
if minetest.get_modpath("bees") ~= nil then
minetest.register_craft({
	type = "shapeless",
	output = "memorandum:eraser",
	recipe = {"bees:honey_comb"},
})
end
if minetest.get_modpath("technic") ~= nil then
minetest.register_craft({
	type = "shapeless",
	output = "memorandum:eraser",
	recipe = {"technic:raw_latex"},
})
end

--[[minetest.register_craft({
	type = "shapeless",
	output = "default:paper",
	recipe = {"memorandum:letter","memorandum:eraser"},
	replacements = {
		{"memorandum:eraser", "memorandum:eraser"}
	},
})]]
-----------------------------------------------------------------------------------------------
print("[Mod] "..title.." ["..version.."] ["..mname.."] Loaded...")
-----------------------------------------------------------------------------------------------