-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
1667 lines (1275 loc) · 41.3 KB
/
main.lua
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-------------------------------------------------------------------------------
PLUGIN = nil -- Plugin object handler
MsgSuffix = "Private: "
private_db = nil -- SQLite file object handler for in disk private area data
-- g_private_db_tmp = nil -- SQLite in memory copy of in disk private area data (speed up)
g_tmp_db = nil -- SQLite in memmory temp database
g_player_data = nil -- In memory player database
g_area_size_min = 16 -- Minimun area size. Read from config
g_area_size_max = 500 -- Maximum area size. Read from config
g_DEFAULT_MAX_AREA_COUNT = 1 -- Like Constans for default config
g_MAX_TOTAL_AREA_SIZE = 500 -- Like Constant for default config
g_MAX_Y = 255
g_MIN_Y = 0
-------------------------------------------------------------------------------
function Initialize(Plugin)
Plugin:SetName("Private")
Plugin:SetVersion(0)
PLUGIN = Plugin
LOG(MsgSuffix .. Plugin:GetName() .. " initialize...")
-- Setup hooks
cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_LEFT_CLICK, MyOnPlayerLeftClick)
cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_RIGHT_CLICK, MyOnPlayerRightClick)
-- Load the InfoReg shared library:
dofile(cPluginManager:GetPluginsPath() .. "/InfoReg.lua")
-- Bind all the console commands:
RegisterPluginInfoConsoleCommands()
-- Bind all the commands (userspace):
RegisterPluginInfoCommands()
-- Create or open database
LOG(MsgSuffix .. "Open database private.sqlite3...")
private_db = sqlite3.open(PLUGIN:GetLocalFolder() .. "/private.sqlite3")
LOG(MsgSuffix .. "Create database if not exists")
create_database()
-- Create in memory database for player data save
LOG(MsgSuffix .. "Create in-memory temp database")
if not create_temp_db() then
LOG(MsgSuffix .. ">> Can\'t create temp database")
return false
end
-- Create in-memory private database
-- Future request
-- LGO(MsgSuffix .."Create in-memmory private temp database")
-- if not private_db_tmp_create() then
-- LOG(MsgSuffix .. ">> Can\'t create in-memory private temp database")
-- return false
-- end
-- Nice message :)
LOG(MsgSuffix .. "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion())
return true
end
-------------------------------------------------------------------------------
function create_temp_db()
-- Create in memmory database for temporary data holding and manipulation
g_tmp_db = sqlite3.open_memory()
if not g_tmp_db then
return false
end
--
-- Create tables
--
local sql = [=[
CREATE TABLE IF NOT EXISTS data(
world text, uuid text, mark integer,
x1 integer, y1 integer, z1 integer,
x2 integer, y2 integer, z2 integer,
x01 integer, y01 integer, z01 integer,
x02 integer, y02 integer, z02 integer,
x03 integer, y03 integer, z03 integer,
x04 integer, y04 integer, z04 integer);
CREATE INDEX IF NOT EXISTS data_world_uuid on data(world, uuid);
CREATE INDEX IF NOT EXISTS data_world_uuid_mark on data(world, uuid, mark);
]=]
-- Execute SQL statement
if g_tmp_db:exec(sql) ~= sqlite3.OK then
console_log("Can\'t create tables into in memory DB")
return false
end
return true
end
-------------------------------------------------------------------------------
function create_database()
-- Create DB if not exists
local sql =[=[
CREATE TABLE IF NOT EXISTS area(
world text, uuid text,
x1 integer, y1 integer, z1 integer,
x2 integer, y2 integer, z2 integer,
x3 integer, y3 integer, z3 integer,
x4 integer, y4 integer, z4 integer,
area_size integer, area_name text
);
CREATE TABLE IF NOT EXISTS config(
world text, uuid text,
areas_count integer,
areas_total_size integer
);
CREATE INDEX IF NOT EXISTS config_world_uuid on config(world, uuid);
]=]
if private_db:exec(sql) ~= sqlite3.OK then
console_log("Error. create_database() -> private_db:exec(sql)")
return false
end
-- Write down defaul data into config table
create_default_config()
return true
end
-------------------------------------------------------------------------------
function CommandMark(Split, Player)
-- Begin mark square.
if not Player then
return false
end
local ret = nil
-- Check number of available areas
-- if available_area_count() < 1 then
-- Player:SendMessage("You do not have more free areas.")
-- return true
-- end
-- Check available area size
-- if available_area_size() < g_area_size_min then
-- Player:SendMessage("You do not have free area")
-- return true
-- end
--
-- Does we have the record (player run command some time)
--
if is_mark_exists(Player) then
Player:SendMessageInfo("Marker already activated")
return true
end
-- All right - create record in temp DB
-- g_tmp_db:exec()
local stmt = g_tmp_db:prepare("INSERT INTO data(world, uuid, mark) VALUES(?, ?, ?);")
if not stmt then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Can\'t prepare SQL statement to create marked record")
return true
end
--
-- Bind values for SQL statements
--
if stmt:bind(1, Player:GetWorld():GetName()) ~= sqlite3.OK then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Can\'t bind value #1 for SQL statement to create marked record")
return true
end
if stmt:bind(2, Player:GetUUID()) ~= sqlite3.OK then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Can\'t bind value #2 for SQL statement to create marked record")
return true
end
if stmt:bind(3, 1) ~= sqlite3.OK then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Can\'t bind value #3 for SQL statement to create marked record")
return true
end
--
-- Try to execute SQL statement
--
ret = stmt:step()
if ret ~= sqlite3.DONE then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Can\'t execute stmt:step() for SQL statement to create marked record. Error code: " .. ret)
return true
end
if stmt:finalize() ~= sqlite3.OK then -- Finish him!
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Can\'t stmt:finalize() for SQL statement to create marked record")
return true
end
-- Talk with player
Player:SendMessageSuccess("Private marker activated")
return true
end
-------------------------------------------------------------------------------
function CommandCancel(Split, Player)
-- Cancel mark operation
if not Player then
return false
end
if not clean_player_data(Player) then
player_message_error(Player, "Some error on canceled oparation")
else
Player:SendMessageSuccess("Private marker canceled")
end
return true
end -- CommandCancel
-------------------------------------------------------------------------------
function CommandSave(Split, Player)
-- Save merked square
if not Player then
console_log("Error. CommandSave() -> Player is nil")
return false
end
-- Is it coordinates set and mark activated
if not is_area_selected(Player) then
player_message_error(Player, "Area not selected")
return true
end
-- Get player config
local player_area_max_count, player_area_max_size = player_get_config(Player)
-- Calculate area size
local area_size = area_size_calculate(Player)
if not area_size then
console_log("Error. CommandSave() -> area_size_calculate(Player)")
player_message_error(Player)
return false
end
-- Check minimum area size
if area_size < g_area_size_min then
player_message_error(Player, "Area too small. Minimum area size is ".. g_area_size_min ..", your area size is ".. area_size)
return true
end
-- Check maximum area size
if area_size > player_area_max_size then
player_message_error(Player, "Area too big. Maximum area size is ".. player_area_max_size ..". Your area size is ".. area_size)
return true
end
-- Check player area count
if player_areas_count(Player) >= player_area_max_count then
player_message_error(Player, "No more free areas for you")
return true
end
-- Calculate area corners
if not area_corners_calculate(Player) then
console_log("Error. area_corners_calculate return false")
player_message_error(Player)
return true
end
-- Check is it area has owner
local owner_exists = is_area_has_owner(Player)
if owner_exists == 1 then
player_message_error(Player, "Area has owner. Select another region")
-- Delete user data
-- player_clean_data(Player)
return true
elseif owner_exists < 0 then -- Some error in function
player_message_error(Player)
return true
end
-- Get the area name
local area_name = tostring(Split[3] or math.random(1, 100))
-- All right. Try to save data
if not player_save_area(Player, area_name) then
player_message_error(Player)
return true
end
-- Talk with player
Player:SendMessageSuccess("Area saved as ".. area_name ..", area size is ".. area_size)
-- Clean marker
clean_player_data(Player)
return true
end -- CommandSave
-------------------------------------------------------------------------------
function on_player_click(Player, BlockX, BlockY, BlockZ, BlockFace, Action, ClickedButton)
-- ClickedButton - 1 -left click, 2 - right click
-- Is it Player object exists. If not - go away
if not Player then
player_message_error(Player)
console_log("Error. on_player_click() -> empty Player")
return false
end
-- Button was clicked
if ClickedButton ~= 1 and ClickedButton ~= 2 then
player_message_error(Player)
console_log("Error. on_player_click() -> Unknown ClickedButton = ".. ClickedButton)
return false
end
-- Check is it area (corner) beasy (other owner)
-- Check, is it marked action
-- if not is_mark_exists(Player) then
-- return true
-- end
-- All right - save point
if not save_corner_position(Player, ClickedButton, math.floor(BlockX), math.floor(BlockY), math.floor(BlockZ)) then
return false
end
-- Show user information
Player:SendMessageSuccess("Point coordinates is (".. math.floor(BlockX) ..", ".. math.floor(BlockY) ..", ".. math.floor(BlockZ) ..")")
-- Inform player for next step
Player:SendMessageSuccess("Click another button for second point or \"/private save\" to save private area")
return true
end -- on_player_click
-------------------------------------------------------------------------------
function MyOnPlayerLeftClick(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
-- Is it try to create area?
if not is_mark_exists(Player) then
if check_block_access(Player, BlockX, BlockY, BlockZ) == 0 then -- 1 - player has access, 0 - no access
return true
end
return false
end
if not on_player_click(Player, BlockX, BlockY, BlockZ, BlockFace, Action, 1) then
player_message_error(Player)
console_log("Error. MyOnPlayerLeftClick()")
else
return true
end
-- Do not block access to object
return false
end
-------------------------------------------------------------------------------
function MyOnPlayerRightClick(Player, BlockX, BlockY, BlockZ, BlockFace, Action)
if not is_mark_exists(Player) then
if check_block_access(Player, BlockX, BlockY, BlockZ) == 0 then -- 1 - player has access, 0 - no access
return true
end
return false
end
if not on_player_click(Player, BlockX, BlockY, BlockZ, BlockFace, Action, 2) then
player_message_error(Player)
console_log("Error. MyOnPlayerRightClick()")
else
return true
end
return false
end
-------------------------------------------------------------------------------
function area_size_calculate(Player)
-- Calculate area size
-- Cordinates may be <0 and >0.
local x1, z1, x2, z2 = area_corners_2v(Player)
-- calculate square border lingth
local x_length = 0
local z_length = 0
x_length = distance_2v(x1, x2)
if not x_length then
console_log("Error. area_size_calculate() -> x_length")
return false
end
z_length = distance_2v(z1, z2)
if not z_length then
console_log("Error. area_size_calculate() -> z_length")
return false
end
return (x_length * z_length)
end -- area_size_calculate
-------------------------------------------------------------------------------
function distance_2v(a_p1, a_p2)
--
if a_p1 == nil or a_p2 == nil then
return false
end
if a_p1 >= 0 and a_p2 >= 0 then
return math.abs(a_p1 - a_p2)
elseif a_p1 < 0 and a_p2 >= 0 then
return (a_p2 - a_p1)
elseif a_p1 < 0 and a_p2 <= 0 then
return math.abs(a_p1 - a_p2)
elseif a_p1 >= 0 and a_p2 < 0 then
return (a_p1 - a_p2)
end
return false
end -- distance_2v
-------------------------------------------------------------------------------
function player_clean_data(Player)
-- Remove all player relative data
g_player_data[Player:GetWorld():GetName()][Player:GetUUID()] = nil
return true
end
-------------------------------------------------------------------------------
function player_save_area(Player, area_name)
-- Write down data into database
-- return false on error
-- x1, z1 - corner A, x3, z3 - corner C
if area_name == nil then
console_log("Error. player_save_area() -> area_name == nil")
return false
end
-- Get corners A and C from g_tmp_db
local x1, z1, x3, z3 = area_get_main_corners(Player)
-- Recalculate selected area size
local area_size = area_size_calculate(Player)
-- Prepare insert operator
local sql = [=[
INSERT INTO area(world, uuid, x1, z1, x3, z3, area_size, area_name)
VALUES(:world, :uuid, :x1, :z1, :x3, :z3, :area_size, :area_name);
]=]
local stmt = private_db:prepare(sql)
if not stmt then
console_log("Error. player_save_area() -> private_db:prepare(".. sql ..")")
return false
end
local ret = stmt:bind_names(
{
world = Player:GetWorld():GetName(),
uuid = Player:GetUUID(),
x1 = x1,
z1 = z1,
x3 = x3,
z3 = z3,
area_size = area_size,
area_name = area_name
})
if ret ~= sqlite3.OK then
console_log("Error. player_save_area() -> stmt:bind_names")
return false
end
-- Create next step for data write
ret = stmt:step()
if ret ~= sqlite3.OK and ret ~= sqlite3.DONE then
console_log("Error. player_save_area() -> stmt:step() code = ".. ret)
return false
end
-- Finish him!
if stmt:finalize() ~= sqlite3.OK then
console_log("Error. player_save_area() -> stmt:finalize()")
return false
end
return true
end
-------------------------------------------------------------------------------
function console_log(a_msg)
LOG(MsgSuffix .. a_msg)
end
-------------------------------------------------------------------------------
function area_corners_calculate(Player)
-- Calculate all four corners for area
-- Area have 4 corners (x, y) from left to right, from up to down.
--[[
A ---> B
|
|
v
D <--- C
--]]
-- Check Player object
if not Player then
return false
end
local x1, z1, x2, z2 = area_corners_2v(Player)
local min_x = math.min(x1, x2)
local max_x = math.max(x1, x2)
local min_z = math.min(z1, z2)
local max_z = math.max(z1, z2)
-- Point A
local x01 = min_x
local z01 = max_z
-- Point B
local x02 = max_x
local z02 = max_y
-- Point C
local x03 = max_x
local z03 = min_z
-- Point D
local x04 = min_z
local z04 = min_z
--
-- Update records into temp DB
--
local sql = [=[
UPDATE data
SET x01 = :x01, z01 = :z01,
x02 = :x02, z02 = :z02,
x03 = :x03, z03 = :z03,
x04 = :x04, z04 = :z04
WHERE world = :world AND uuid = :uuid and mark=1;
]=]
local stmt = g_tmp_db:prepare(sql)
if not stmt then
console_log("Error. area_corners_calculate() -> g_tmp_db:prepare()")
return false
end
-- Bind values
local ret = stmt:bind_names(
{
world = Player:GetWorld():GetName(),
uuid = Player:GetUUID(),
x01 = x01,
z01 = z01,
x02 = x02,
z02 = z02,
x03 = x03,
z03 = z03,
x04 = x04,
z04 = z04
})
if ret ~= sqlite3.OK then
console_log("Error. area_corners_calculate() -> stmt:bind_names")
return false
end
-- Execute statement
ret = stmt:step()
if ret ~= sqlite3.DONE and ret ~= sqlite3.ROW then
console_log("Error. area_corners_calculate() - > stmt:step(). Error code: " .. ret)
return false
end
if stmt:finalize() ~= sqlite3.OK then -- Finish him!
console_log("Error. area_corners_calculate() -> stmt:finalize()")
return false
end
return true
end -- area_corners_calculate
-------------------------------------------------------------------------------
function show_database()
-- Show database record
local out = {}
local n = 1
-- local ret_rows_count = " LIMIT " .. math.floor(tonumber(Split[3]) or 30)
local ret_rows_count = 0
-- Display the database
for row in g_tmp_db:nrows("SELECT * from data;") do
out[n] = row.world .. " | " .. row.uuid .. " | " .. row.mark .. " | ".. row.x1 .." | " .. row.y1 .." | " .. row.z1 .." | ".. row.x2 .." | " .. row.y2 .." | " .. row.z2
n = n + 1
end
return true, table.concat(out, "\n")
end
-------------------------------------------------------------------------------
function is_mark_exists(Player)
-- Check is it already activated mark action
-- Is it mark exists
local records_count = 0 -- save rows count into database
local stmt = g_tmp_db:prepare("SELECT count(*) from data WHERE world=? and uuid=? and mark=1;")
if not stmt then
console_log("Error. is_mark_exists(). Into prepere")
player_message_error(Player)
return false
end
--
-- Bind values for SQL statements
--
if stmt:bind(1, Player:GetWorld():GetName()) ~= sqlite3.OK then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Error. is_mark_exists. Can\'t bind value #1 for SQL statement")
return false
end
if stmt:bind(2, Player:GetUUID()) ~= sqlite3.OK then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Error. is_mark_exists. Can\'t bind value #2 for SQL statement")
return false
end
--
-- Try to execute SQL statement
--
local ret = stmt:step()
if ret ~= sqlite3.DONE and ret ~= sqlite3.ROW then
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Error. is_mark_exists. Can\'t execute stmt:step() for SQL statement. Error code: " .. ret)
return false
end
records_count = stmt:get_value(0)
if stmt:finalize() ~= sqlite3.OK then -- Finish him!
Player:SendMessage("Some error. Can\'t mark area. Talk admin")
console_log("Error. is_mark_exists. Can\'t stmt:finalize() for SQL statement")
return false
end
if records_count == 0 then
return false
end
-- All right - record exists
return true
end
-------------------------------------------------------------------------------
function clean_player_data(Player)
-- Delete record from memmory database
if not Player then
console_log("Error. clean_player_data() -> Player is nil")
return false
end
local stmt = g_tmp_db:prepare("DELETE FROM data WHERE world = :world AND uuid = :uuid;")
if not stmt then
console_log("Error. clean_player_data() -> prepere")
player_message_error(Player)
return false
end
local ret = stmt:bind_names(
{
world = Player:GetWorld():GetName(),
uuid = Player:GetUUID()
})
if ret ~= sqlite3.OK then
console_log("Error. clean_player_data(). Can\'t bind values")
player_message_error(Player)
return false
end
--
-- Try to execute SQL statement
--
local ret = stmt:step()
if ret ~= sqlite3.DONE and ret ~= sqlite3.ROW and ret ~= sqlite3.OK then
player_message_error(Player)
console_log("Error. clean_player_data() -> stmt:step()")
return false
end
-- Clean statement
if stmt:finalize() ~= sqlite3.OK then
console_log("Error. clean_player_data() -> stmt:finalize()")
player_message_error(Player)
return false
end
return true
end
-------------------------------------------------------------------------------
function player_message_error(Player, msg)
-- Send player internal error message
if msg == nil or msg == nil then
Player:SendMessageInfo("Some error. Say to admin")
return true
else
Player:SendMessageInfo(msg)
end
return true
end
-------------------------------------------------------------------------------
function save_corner_position(Player, corner_id, BlockX, BlockY, BlockZ)
-- Write down user selected point
-- corner_id - 1 or 2
-- Check empty Player
if not Player then
player_message_error(Player)
console_log("Error. save_corner_position() -> empty Player")
return false
end
-- Unknown corner selected :>
if corner_id ~= 1 and corner_id ~= 2 then
player_message_error(Player)
console_log("Error. save_corner_position() -> corner_id != 1 or corner_id != 2. corner_id = ".. corner_id)
return false
end
-- If y > 255 or y < 0 - error
if BlockY > 255 or BlockY < 0 then
console_log("Warning: save_corner_position() -> BlockY > 255 or BlockY < 0")
return false
end
-- Prepare statement
local sql = "UPDATE data "
sql = sql .. "SET x".. corner_id .."=:x, y".. corner_id .."=:y, z".. corner_id .."=:z "
sql = sql .. "WHERE world=:world AND uuid=:uuid AND mark=1;"
local stmt = g_tmp_db:prepare(sql)
if not stmt then
player_message_error(Player)
console_log("Error. save_corner_position() -> prepare(".. sql ..")")
return false
end
-- Execute with data
local ret = stmt:bind_names(
{
world=Player:GetWorld():GetName(),
uuid=Player:GetUUID(),
x=BlockX,
y=BlockY,
z=BlockZ
})
if ret ~= sqlite3.OK then
player_message_error(Player)
console_log("Error. save_corner_position() -> virtual step")
return false
end
stmt:step()
stmt:finalize()
return true
end
-------------------------------------------------------------------------------
function area_corners_2v(Player)
-- Return player selected corners
local sql = [=[
SELECT x1, z1, x2, z2
FROM data
WHERE world = :world AND uuid = :uuid AND mark = 1;
]=]
local stmt = g_tmp_db:prepare(sql)
if not stmt then
console_log("Error. area_corners_2v() -> g_tmp_db:prepare(sql)")
return false
end
local ret = stmt:bind_names(
{
world = Player:GetWorld():GetName(),
uuid = Player:GetUUID()
})
if ret ~= sqlite3.OK then
console_log("Error. area_corners_2v() -> stmt:bind_names")
return false
end
local ret = stmt:step()
if ret ~= sqlite3.DONE and ret ~= sqlite3.ROW then
console_log("Error. area_corners_2v() -> stmt:step(). Code".. ret)
return false
end
local x1 = stmt:get_value(0)
local z1 = stmt:get_value(1)
local x2 = stmt:get_value(2)
local z2 = stmt:get_value(3)
if stmt:finalize() ~= sqlite3.OK then -- Finish him!
console_log("Error. area_corners_2v() -> stmt:finalize()")
return false
end
return x1, z1, x2, z2
end -- area_corners_2v
-------------------------------------------------------------------------------
function is_area_selected(Player)
-- Return true if 2 point of area selected
-- Prepera statement
local sql =[=[
SELECT count(*)
FROM data
WHERE world = :world AND uuid = :uuid AND mark = 1
AND x1 IS NOT NULL AND y1 IS NOT NULL AND z1 IS NOT NULL
AND x2 IS NOT NULL AND y2 IS NOT NULL AND z2 IS NOT NULL;
]=]
local stmt = g_tmp_db:prepare(sql)
if not stmt then
console_log("Error. is_area_selected() -> g_tmp_db:prepare()")
player_message_error(Player)
return false
end
-- Bind values
local ret = stmt:bind_names(
{
world = Player:GetWorld():GetName(),
uuid = Player:GetUUID()
})
if ret ~= sqlite3.OK then
console_log("Error. is_area_selected() -> stmt:bind_names")
player_message_error(Player)
return false
end
-- Execute statement
ret = stmt:step()
if ret ~= sqlite3.DONE and ret ~= sqlite3.ROW then
console_log("Error. is_area_selected() - > stmt:step(). Error code: " .. ret)
player_message_error(Player)
return false
end
if stmt:get_value(0) ~= 1 then
return false
end
return true
end
-------------------------------------------------------------------------------
function area_get_main_corners(Player)
-- Return main coreners coordinates
-- Main corners is A and C corner
local sql = [=[
SELECT x01, z01, x03, z03
FROM data
WHERE world = :world AND uuid = :uuid AND mark=1;
]=]
local stmt = g_tmp_db:prepare(sql)
if not stmt then
console_log("Error. area_get_main_corners() -> g_tmp_db:prepare(".. sql ..")")
return false
end
local ret = stmt:bind_names(
{
world = Player:GetWorld():GetName(),
uuid = Player:GetUUID()
})
if ret ~= sqlite3.OK then
console_log("Error. area_get_main_corners() -> stmt:bind_names")
return false
end
ret = stmt:step()
if ret ~= sqlite3.DONE and ret ~= sqlite3.ROW then
console_log("Error. area_get_main_corners() -> stmt:step()")
return false
end