-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
596 lines (577 loc) · 20.4 KB
/
init.lua
File metadata and controls
596 lines (577 loc) · 20.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
dofile(core.get_modpath("schemlib") .. "/api.lua")
local path = core.get_worldpath() .. "/schemlib"
local dir_list = core.get_dir_list(core.get_worldpath(), true)
if not dir_list then
core.mkdir(path)
end
local exists = false
for i, dir in ipairs(dir_list) do
if dir == "schemlib" then
exists = true
core.log("action", "[schemlib] Found schemlib directory")
break
end
end
if not exists then
core.mkdir(path)
core.log("action", "[schemlib] Created schemlib directory")
end
-- schemlib compatibility layer...
schemlib.mapping = dofile(core.get_modpath("schemlib").."/compat/mapping.lua")
schemlib.node = dofile(core.get_modpath("schemlib").."/compat/node.lua")
schemlib.plan = dofile(core.get_modpath("schemlib").."/compat/plan.lua")
schemlib.plan_manager = dofile(core.get_modpath("schemlib").."/compat/plan_manager.lua")
core.register_craftitem("schemlib:wand", {
description = "Schematic Wand",
inventory_image = "default_stick.png^[colorize:#ff75ffbf",
wield_image = "default_stick.png^[colorize:#ff75ffbf",
groups = { not_in_creative_inventory = 1 },
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
schemlib.set_pos1(user, pos)
elseif pointed_thing.type == "object" then
local entity = pointed_thing.ref:get_luaentity()
if entity and entity.name == "schemlib:pos2" then
local name = user:get_player_name()
if entity:get_staticdata() == name then
schemlib.set_pos1(user, pointed_thing.ref:get_pos())
end
end
end
return itemstack
end,
on_place = function(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
schemlib.set_pos2(placer, pos)
end
end,
on_secondary_use = function(itemstack, user, pointed_thing)
if user == nil or (pointed_thing or {}).type ~= "object" then
return itemstack
end
local name = user:get_player_name()
local entity = pointed_thing.ref:get_luaentity()
if entity and entity.name == "schemlib:pos1" then
if entity:get_staticdata() == name then
schemlib.set_pos2(user, pointed_thing.ref:get_pos())
end
end
return itemstack
end,
})
core.register_entity("schemlib:pos1", {
initial_properties = {
visual = "cube",
visual_size = { x = 1.1, y = 1.1 },
textures = { "schemlib_pos1.png", "schemlib_pos1.png",
"schemlib_pos1.png", "schemlib_pos1.png",
"schemlib_pos1.png", "schemlib_pos1.png" },
collisionbox = { -0.55, -0.55, -0.55, 0.55, 0.55, 0.55 },
physical = false,
static_save = true,
glow = 14,
},
on_punch = function(self, puncher)
if puncher == nil then
return
end
local name = puncher:get_player_name()
if name == nil then
return
end
if self.player == name then
self.object:remove()
schemlib.set_pos1(puncher, nil)
end
end,
on_blast = function(self, damage)
return false, false, {}
end,
on_activate = function(self, staticdata)
if staticdata ~= nil and staticdata ~= "" then
local data = staticdata:split(';')
if data and data[1] then
self.player = data[1]
end
end
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
end
return
end
local extent = schemlib.get_selection(core.get_player_by_name(self.player))
if not extent or not extent.pos1 then
self.object:remove()
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
return
end
if vector.equals(self.object:get_pos(), extent.pos1) == false then
self.object:remove()
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
return
end
end,
on_deactivate = function(self)
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
end
return
end
end,
on_step = function(self, dtime)
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
end
return
end
local extent = schemlib.get_selection(core.get_player_by_name(self.player))
if not extent or not extent.pos1 then
self.object:remove()
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
return
end
if vector.equals(self.object:get_pos(), extent.pos1) == false then
self.object:remove()
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
return
end
end,
get_staticdata = function(self)
if self.player ~= nil then
return self.player
end
return ""
end,
})
core.register_entity("schemlib:pos2", {
initial_properties = {
visual = "cube",
visual_size = { x = 1.1, y = 1.1 },
textures = {
"schemlib_pos2.png", "schemlib_pos2.png",
"schemlib_pos2.png", "schemlib_pos2.png",
"schemlib_pos2.png", "schemlib_pos2.png"
},
collisionbox = { -0.55, -0.55, -0.55, 0.55, 0.55, 0.55 },
physical = false,
static_save = true,
glow = 14,
},
on_punch = function(self, puncher)
if puncher == nil then
return
end
local name = puncher:get_player_name()
if name == nil then
return
end
if self.player == name then
self.object:remove()
schemlib.set_pos2(puncher, nil)
end
end,
on_blast = function(self, damage)
return false, false, {}
end,
on_activate = function(self, staticdata)
if staticdata ~= nil and staticdata ~= "" then
local data = staticdata:split(';')
if data and data[1] then
self.player = data[1]
end
end
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
local extent = schemlib.get_selection(core.get_player_by_name(self.player))
if not extent or not extent.pos2 then
self.object:remove()
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
return
end
if vector.equals(self.object:get_pos(), extent.pos2) == false then
self.object:remove()
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
return
end
end,
on_deactivate = function(self)
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
end,
on_step = function(self, dtime)
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
local extent = schemlib.get_selection(core.get_player_by_name(self.player))
if not extent or not extent.pos2 then
self.object:remove()
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
return
end
if vector.equals(self.object:get_pos(), extent.pos2) == false then
self.object:remove()
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
return
end
end,
get_staticdata = function(self)
if self.player ~= nil then
return self.player
end
return ""
end,
})
core.register_entity("schemlib:cuboid", {
initial_properties = {
visual = "cube",
backface_culling = false,
visual_size = { x = 1, y = 1 },
textures = {
"schemlib_cuboid.png", "schemlib_cuboid.png",
"schemlib_cuboid.png", "schemlib_cuboid.png",
"schemlib_cuboid.png", "schemlib_cuboid.png"
},
collisionbox = { 0, 0, 0, 0, 0, 0 },
physical = false,
static_save = true,
use_texture_alpha = true,
glow = 14,
},
on_punch = function(self, puncher)
return
end,
on_blast = function(self, damage)
return false, false, {}
end,
on_activate = function(self, staticdata)
if staticdata ~= nil and staticdata ~= "" then
local data = staticdata:split(';')
if data and data[1] and data[2] and data[3] then
self.player = data[1]
self.pos1 = core.string_to_pos(data[2])
self.pos2 = core.string_to_pos(data[3])
end
end
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online or self.pos1 == nil or self.pos2 == nil then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
local extent = schemlib.get_selection(core.get_player_by_name(self.player))
if not extent or not extent.pos1 or not extent.pos2 then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
if vector.equals(self.pos1, extent.pos1) == false or vector.equals(self.pos2, extent.pos2) == false then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
-- calculate the size of the cuboid based on the two positions
local size = vector.add(vector.new(
math.abs(self.pos1.x - self.pos2.x),
math.abs(self.pos1.y - self.pos2.y),
math.abs(self.pos1.z - self.pos2.z)
), 1.1)
self.object:set_properties({
visual_size = size,
})
end,
on_deactivate = function(self)
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
end,
on_step = function(self, dtime)
local player_online = false
for _, player in ipairs(core.get_connected_players()) do
if player:get_player_name() == self.player then
player_online = true
break
end
end
if self.player == nil or not player_online then
self.object:remove()
if self.player ~= nil then
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
end
return
end
local extent = schemlib.get_selection(core.get_player_by_name(self.player))
if not extent or not extent.pos1 or not extent.pos2 then
self.object:remove()
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
return
end
if vector.equals(self.pos1, extent.pos1) == false or vector.equals(self.pos2, extent.pos2) == false then
self.object:remove()
schemlib.set_pos1(core.get_player_by_name(self.player), nil)
schemlib.set_pos2(core.get_player_by_name(self.player), nil)
return
end
end,
get_staticdata = function(self)
if self.player ~= nil and self.pos1 ~= nil and self.pos2 ~= nil then
return self.player .. ";" .. core.pos_to_string(self.pos1) .. ";" .. core.pos_to_string(self.pos2)
end
return ""
end,
})
core.register_privilege("schemlib", {
description = "Allow player to use schemlib",
give_to_singleplayer = false,
give_to_admin = true,
})
core.register_chatcommand("pos1", {
description = "Set position 1 to the current position",
privs = { schemlib = true },
func = function(name, param)
local player = core.get_player_by_name(name)
if player == nil then
return true, "This command can only be used by a player"
end
local pos = vector.round(player:get_pos())
pos.y = pos.y - 1
schemlib.set_pos1(player, pos)
return true
end,
})
core.register_chatcommand("pos2", {
description = "Set position 2 to the current position",
privs = { schemlib = true },
func = function(name, param)
local player = core.get_player_by_name(name)
if player == nil then
return true, "This command can only be used by a player"
end
local pos = vector.round(player:get_pos())
pos.y = pos.y - 1
schemlib.set_pos2(player, pos)
return true
end,
})
core.register_chatcommand("clear_selection", {
description = "Clear the current selection",
privs = { schemlib = true },
func = function(name, param)
local player = core.get_player_by_name(name)
if player == nil then
return true, "This command can only be used by a player"
end
schemlib.clear_selection(player)
return true
end,
})
core.register_chatcommand("schematic_wand", {
description = "Give the player a schematic wand",
privs = { schemlib = true },
func = function(name, param)
local player = core.get_player_by_name(name)
if player == nil then
return true, "This command can only be used by a player"
end
local has_wand = false
local inv = player:get_inventory()
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
if stack:get_name() == "schemlib:wand" then
has_wand = true
break
end
end
if not has_wand then
inv:add_item("main", "schemlib:wand")
end
return true, "You have been given a schematic wand"
end,
})
core.register_chatcommand("save_schematic", {
params = "<name> (force)",
description = "Save the current selection as a schematic",
privs = { schemlib = true },
func = function(name, param)
local player = core.get_player_by_name(name)
if player == nil then
return true, "This command can only be used by a player"
end
local selection = schemlib.get_selection(player)
if selection == nil then
return true, "No selection found"
end
if selection.pos1 == nil or selection.pos2 == nil then
return true, "Invalid selection"
end
local schem_name, force = string.match(param, "^(%S+)%s*(%S*)$")
if schem_name == nil then
return true, "Invalid arguments"
end
local schem_path = path .. "/" .. schem_name
if force == "force" then
force = true
else
force = false
end
core.chat_send_player(name, "Force: " .. tostring(force))
local file = io.open(schem_path .. ".mtx", "r")
if not force and file then
file:close()
return true, "Schematic already exists. Use 'force' to overwrite"
end
local min = vector.new(
math.min(selection.pos1.x, selection.pos2.x),
math.min(selection.pos1.y, selection.pos2.y),
math.min(selection.pos1.z, selection.pos2.z)
)
local max = vector.new(
math.max(selection.pos1.x, selection.pos2.x),
math.max(selection.pos1.y, selection.pos2.y),
math.max(selection.pos1.z, selection.pos2.z)
)
local pos = vector.round(player:get_pos())
pos.y = pos.y - 1
-- get the offset of the players postion from the min position
local offset = vector.subtract(pos, min)
local data = {
filename = schem_name,
min = min,
max = max,
origin = min,
offset = offset,
ttl = 0,
}
local flags = {
file_cache = true,
keep_inv = true,
keep_meta = true,
origin_clear = false,
keep_timers = true,
stop_timers = false
}
schemlib.emit(data, flags)
return true, "Schematic saved as " .. schem_name .. ".mtx"
end,
})
core.register_chatcommand("load_schematic", {
params = "<name>",
description = "Load a schematic into the world at the current position",
privs = { schemlib = true },
func = function(name, param)
local player = core.get_player_by_name(name)
if player == nil then
return true, "This command can only be used by a player"
end
local schem_name = param
local origin = vector.round(player:get_pos())
origin.y = origin.y - 1
local meta = schemlib.load_emitted({
filename = schem_name,
origin = origin,
emerge = true, -- false = disables write to world
})
if meta == nil then
return true, "Schematic " .. schem_name .. ".mtx not found"
end
return true, "Schematic " .. schem_name .. ".mtx loaded"
end,
})
core.register_on_leaveplayer(function(ObjectRef, timed_out)
schemlib.clear_selection(ObjectRef)
end)
core.register_on_shutdown(function()
schemlib.clear_selections()
end)