-
Notifications
You must be signed in to change notification settings - Fork 17
/
PhysDB.java
1829 lines (1533 loc) · 53.4 KB
/
PhysDB.java
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
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.sql;
import com.griefcraft.cache.ProtectionCache;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.Flag;
import com.griefcraft.model.History;
import com.griefcraft.model.Permission;
import com.griefcraft.model.Protection;
import org.bukkit.entity.Player;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class PhysDB extends Database {
/**
* The JSON Parser object
*/
private final JSONParser jsonParser = new JSONParser();
/**
* The database version
*/
private int databaseVersion = 0;
/**
* The number of protections that should exist
*/
private int protectionCount = 0;
public PhysDB() {
super();
}
public PhysDB(Type currentType) {
super(currentType);
}
/**
* Decrement the known protection counter
*/
public void decrementProtectionCount() {
protectionCount --;
}
/**
* Check if the protection cache has all of the known protections cached
*
* @return
*/
public boolean hasAllProtectionsCached() {
ProtectionCache cache = LWC.getInstance().getProtectionCache();
return cache.size() >= protectionCount;
}
/**
* Fetch an object from the sql database
*
* @param sql
* @param column
* @return
*/
private Object fetch(String sql, String column, Object... toBind) {
PreparedStatement statement = null;
try {
int index = 1;
statement = prepare(sql);
for (Object bind : toBind) {
statement.setObject(index, bind);
index++;
}
ResultSet set = statement.executeQuery();
if (set.next()) {
Object object = set.getObject(column);
set.close();
return object;
}
set.close();
} catch (Exception e) {
printException(e);
} finally {
safeClose(statement);
}
return null;
}
/**
* Get the total amount of protections
* @return the number of protections
*/
public int getProtectionCount() {
return Integer.decode(fetch("SELECT COUNT(*) AS count FROM " + prefix + "protections", "count").toString());
}
/**
* Get the amount of protections for the given protection type
* @param type
* @return the number of protected chests
*/
public int getProtectionCount(Protection.Type type) {
return Integer.decode(fetch("SELECT COUNT(*) AS count FROM " + prefix + "protections WHERE type = " + type.ordinal(), "count").toString());
}
/**
* @return the number of history items stored
*/
public int getHistoryCount() {
return Integer.decode(fetch("SELECT COUNT(*) AS count FROM " + prefix + "history", "count").toString());
}
/**
* Get the amount of protections a player has
*
* @param player
* @return the amount of protections they have
*/
public int getProtectionCount(String player) {
int count = 0;
PreparedStatement statement = null;
try {
statement = prepare("SELECT COUNT(*) as count FROM " + prefix + "protections WHERE owner = ?");
statement.setString(1, player);
ResultSet set = statement.executeQuery();
if (set.next()) {
count = set.getInt("count");
}
set.close();
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return count;
}
/**
* Get the amount of protections a player has
*
* @param player
* @return the amount of protections they have
*/
public int getHistoryCount(String player) {
int count = 0;
PreparedStatement statement = null;
try {
statement = prepare("SELECT COUNT(*) AS count FROM " + prefix + "history WHERE LOWER(player) = LOWER(?)");
statement.setString(1, player);
ResultSet set = statement.executeQuery();
if (set.next()) {
count = set.getInt("count");
}
set.close();
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return count;
}
/**
* Get the amount of chests a player has of a specific block id
*
* @param player
* @return the amount of protections they have of blockId
*/
public int getProtectionCount(String player, int blockId) {
int count = 0;
PreparedStatement statement = null;
try {
statement = prepare("SELECT COUNT(*) AS count FROM " + prefix + "protections WHERE owner = ? AND blockId = ?");
statement.setString(1, player);
statement.setInt(2, blockId);
ResultSet set = statement.executeQuery();
if (set.next()) {
count = set.getInt("count");
}
set.close();
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return count;
}
/**
* Get the menu style for a player
*
* @param player
* @return
* @deprecated
*/
public String getMenuStyle(String player) {
return "basic";
}
/**
* Load the database and do any updating required or create the tables
*/
@Override
public void load() {
if (loaded) {
return;
}
Column column;
Table protections = new Table(this, "protections");
{
column = new Column("id");
column.setType("INTEGER");
column.setPrimary(true);
protections.add(column);
column = new Column("owner");
column.setType("VARCHAR(255)");
protections.add(column);
column = new Column("type");
column.setType("INTEGER");
protections.add(column);
column = new Column("x");
column.setType("INTEGER");
protections.add(column);
column = new Column("y");
column.setType("INTEGER");
protections.add(column);
column = new Column("z");
column.setType("INTEGER");
protections.add(column);
column = new Column("flags");
column.setType("INTEGER");
protections.add(column);
column = new Column("data");
column.setType("TEXT");
protections.add(column);
column = new Column("blockId");
column.setType("INTEGER");
protections.add(column);
column = new Column("world");
column.setType("VARCHAR(255)");
protections.add(column);
column = new Column("password");
column.setType("VARCHAR(255)");
protections.add(column);
column = new Column("date");
column.setType("VARCHAR(255)");
protections.add(column);
column = new Column("last_accessed");
column.setType("INTEGER");
protections.add(column);
}
Table history = new Table(this, "history");
{
column = new Column("id");
column.setType("INTEGER");
column.setPrimary(true);
history.add(column);
column = new Column("protectionId");
column.setType("INTEGER");
history.add(column);
column = new Column("player");
column.setType("VARCHAR(255)");
history.add(column);
column = new Column("x");
column.setType("INTEGER");
history.add(column);
column = new Column("y");
column.setType("INTEGER");
history.add(column);
column = new Column("z");
column.setType("INTEGER");
history.add(column);
column = new Column("type");
column.setType("INTEGER");
history.add(column);
column = new Column("status");
column.setType("INTEGER");
history.add(column);
column = new Column("metadata");
column.setType("VARCHAR(255)");
history.add(column);
column = new Column("timestamp");
column.setType("long");
history.add(column);
}
Table internal = new Table(this, "internal");
{
column = new Column("name");
column.setType("VARCHAR(40)");
column.setPrimary(true);
column.setAutoIncrement(false);
internal.add(column);
column = new Column("value");
column.setType("VARCHAR(40)");
internal.add(column);
}
protections.execute();
history.execute();
internal.execute();
// Load the database version
loadDatabaseVersion();
// perform database upgrades
performDatabaseUpdates();
// get the amount of protections
protectionCount = getProtectionCount();
loaded = true;
}
/**
* Perform any database updates
*/
public void performDatabaseUpdates() {
// Indexes
if (databaseVersion == 0) {
// Drop old, old indexes
log("Dropping old indexes (One time, may take a while!)");
dropIndex("protections", "in1");
dropIndex("protections", "in6");
dropIndex("protections", "in7");
dropIndex("history", "in8");
dropIndex("history", "in9");
dropIndex("protections", "in10");
dropIndex("history", "in12");
dropIndex("history", "in13");
dropIndex("history", "in14");
// Create our updated (good) indexes
log("Creating new indexes (One time, may take a while!)");
createIndex("protections", "protections_main", "x, y, z, world");
createIndex("protections", "protections_utility", "owner");
createIndex("history", "history_main", "protectionId");
createIndex("history", "history_utility", "player");
createIndex("history", "history_utility2", "x, y, z");
// increment the database version
incrementDatabaseVersion();
}
if (databaseVersion == 1) {
log("Creating index on internal");
createIndex("internal", "internal_main", "name");
incrementDatabaseVersion();
}
if (databaseVersion == 2) {
incrementDatabaseVersion();
}
if (databaseVersion == 3) {
createIndex("protections", "protections_type", "type");
incrementDatabaseVersion();
}
}
/**
* Increment the database version
*/
public void incrementDatabaseVersion() {
setDatabaseVersion(++databaseVersion);
}
/**
* Set the database version and sync it to the database
*
* @param databaseVersion
*/
public void setDatabaseVersion(int databaseVersion) {
// set it locally
this.databaseVersion = databaseVersion;
// ship it to the database
PreparedStatement statement = null;
try {
statement = prepare("UPDATE " + prefix + "internal SET value = ? WHERE name = ?");
statement.setInt(1, databaseVersion);
statement.setString(2, "version");
// ok
statement.executeUpdate();
} catch (SQLException e) {
} finally {
safeClose(statement);
}
}
/**
* Get a value in the internal table
*
* @param key
* @return the value found, otherwise NULL if none exists
*/
public String getInternal(String key) {
PreparedStatement statement = null;
try {
statement = prepare("SELECT value FROM " + prefix + "internal WHERE name = ?");
statement.setString(1, key);
ResultSet set = statement.executeQuery();
if (set.next()) {
String value = set.getString("value");
set.close();
return value;
}
set.close();
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return null;
}
/**
* Set a value in the internal table
*
* @param key
* @param value
*/
public void setInternal(String key, String value) {
PreparedStatement statement = null;
try {
statement = prepare("INSERT INTO " + prefix +"internal (name, value) VALUES (?, ?)");
statement.setString(1, key);
statement.setString(2, value);
statement.executeUpdate();
} catch (SQLException e) {
safeClose(statement);
// Already exists
try {
PreparedStatement statement2 = prepare("UPDATE " + prefix + "internal SET value = ? WHERE name = ?");
statement2.setString(1, value) ;
statement2.setString(2, key);
statement2.executeUpdate();
} catch (SQLException ex) {
// Something bad went wrong
printException(ex);
}
} finally {
safeClose(statement);
}
}
/**
* Load the database internal version
*
* @return
*/
public int loadDatabaseVersion() {
PreparedStatement statement = null;
try {
statement = prepare("SELECT value FROM " + prefix + "internal WHERE name = ?");
statement.setString(1, "version");
// Execute it
ResultSet set = statement.executeQuery();
// load the version
if (set.next()) {
databaseVersion = Integer.parseInt(set.getString("value"));
} else {
throw new IllegalStateException("Internal is empty");
}
// close everything
set.close();
} catch (Exception e) {
safeClose(statement);
// Doesn't exist, create it
try {
PreparedStatement statement2 = prepare("INSERT INTO " + prefix + "internal (name, value) VALUES(?, ?)");
statement2.setString(1, "version");
statement2.setInt(2, databaseVersion);
// ok
statement2.executeUpdate();
statement2.close();
} catch (SQLException ex) { }
} finally {
safeClose(statement);
}
return databaseVersion;
}
/**
* Load a protection with the given id
*
* @param id
* @return the Chest object
*/
public Protection loadProtection(int id) {
// the protection cache
ProtectionCache cache = LWC.getInstance().getProtectionCache();
// check if the protection is already cached
Protection cached = cache.getProtectionById(id);
if (cached != null) {
return cached;
}
PreparedStatement statement = null;
try {
statement = prepare("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections WHERE id = ?");
statement.setInt(1, id);
Protection protection = resolveProtection(statement);
if (protection != null) {
cache.add(protection);
return protection;
}
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return null;
}
/**
* Load protections using a specific type
*
* @param type
* @return the Protection object
*/
public List<Protection> loadProtectionsUsingType(Protection.Type type) {
PreparedStatement statement = null;
try {
statement = prepare("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections WHERE type = ?");
statement.setInt(1, type.ordinal());
return resolveProtections(statement);
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return new ArrayList<Protection>();
}
/**
* Resolve one protection from a ResultSet. The ResultSet is not closed.
*
* @param set
* @return
*/
public Protection resolveProtection(ResultSet set) {
try {
Protection protection = new Protection();
int protectionId = set.getInt("id");
int x = set.getInt("x");
int y = set.getInt("y");
int z = set.getInt("z");
int blockId = set.getInt("blockId");
int type = set.getInt("type");
String world = set.getString("world");
String owner = set.getString("owner");
String password = set.getString("password");
String date = set.getString("date");
long lastAccessed = set.getLong("last_accessed");
protection.setId(protectionId);
protection.setX(x);
protection.setY(y);
protection.setZ(z);
protection.setBlockId(blockId);
protection.setType(Protection.Type.values()[type]);
protection.setWorld(world);
protection.setOwner(owner);
protection.setPassword(password);
protection.setCreation(date);
protection.setLastAccessed(lastAccessed);
// check for oh so beautiful data!
String data = set.getString("data");
if (data == null || data.trim().isEmpty()) {
return protection;
}
// rev up them JSON parsers!
Object object = null;
try {
object = jsonParser.parse(data);
} catch (Exception e) {
return protection;
} catch (Error e) {
return protection;
}
if (!(object instanceof JSONObject)) {
return protection;
}
// obtain the root
JSONObject root = (JSONObject) object;
protection.getData().putAll(root);
// Attempt to parse rights
Object rights = root.get("rights");
if (rights != null && (rights instanceof JSONArray)) {
JSONArray array = (JSONArray) rights;
for (Object node : array) {
// we only want to use the maps
if (!(node instanceof JSONObject)) {
continue;
}
JSONObject map = (JSONObject) node;
// decode the map
Permission permission = Permission.decodeJSON(map);
// bingo!
if (permission != null) {
protection.addPermission(permission);
}
}
}
// Attempt to parse flags
Object flags = root.get("flags");
if (flags != null && (rights instanceof JSONArray)) {
JSONArray array = (JSONArray) flags;
for (Object node : array) {
if (!(node instanceof JSONObject)) {
continue;
}
JSONObject map = (JSONObject) node;
Flag flag = Flag.decodeJSON(map);
if (flag != null) {
protection.addFlag(flag);
}
}
}
return protection;
} catch (SQLException e) {
printException(e);
return null;
}
}
/**
* Resolve every protection from a result set
*
* @param set
* @return
*/
private List<Protection> resolveProtections(ResultSet set) {
List<Protection> protections = new ArrayList<Protection>();
try {
while (set.next()) {
Protection protection = resolveProtection(set);
if (protection != null) {
protections.add(protection);
}
}
} catch (SQLException e) {
printException(e);
}
return protections;
}
/**
* Resolve a list of protections from a statement
*
* @param statement
* @return
*/
private List<Protection> resolveProtections(PreparedStatement statement) {
List<Protection> protections = new ArrayList<Protection>();
ResultSet set = null;
try {
set = statement.executeQuery();
protections = resolveProtections(set);
} catch (SQLException e) {
printException(e);
} finally {
if (set != null) {
try {
set.close();
} catch (SQLException e) {
}
}
}
return protections;
}
/**
* Resolve the first protection from a statement
*
* @param statement
* @return
*/
private Protection resolveProtection(PreparedStatement statement) {
List<Protection> protections = resolveProtections(statement);
if (protections.size() == 0) {
return null;
}
return protections.get(0);
}
/**
* Fill the protection cache as much as possible with protections
* Caches the most recent protections
*/
public void precache() {
LWC lwc = LWC.getInstance();
ProtectionCache cache = lwc.getProtectionCache();
// clear the cache incase we're working on a dirty cache
cache.clear();
int precacheSize = lwc.getConfiguration().getInt("core.precache", -1);
if (precacheSize == -1) {
precacheSize = lwc.getConfiguration().getInt("core.cacheSize", 10000);
}
PreparedStatement statement = null;
try {
statement = prepare("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections ORDER BY id DESC LIMIT ?");
statement.setInt(1, precacheSize);
statement.setFetchSize(10);
// scrape the protections from the result set now
List<Protection> protections = resolveProtections(statement);
// throw all of the protections in
for (Protection protection : protections) {
cache.add(protection);
}
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
// Cache them all
}
/**
* Load a protection at the given coordinates
*
* @param x
* @param y
* @param z
* @return the Protection object
*/
public Protection loadProtection(String worldName, int x, int y, int z) {
return loadProtection(worldName, x, y, z, false);
}
/**
* Load a protection at the given coordinates
*
* @param x
* @param y
* @param z
* @param ignoreProtectionCount
* @return the Protection object
*/
private Protection loadProtection(String worldName, int x, int y, int z, boolean ignoreProtectionCount) {
// the unique key to use in the cache
String cacheKey = worldName + ":" + x + ":" + y + ":" + z;
// the protection cache
ProtectionCache cache = LWC.getInstance().getProtectionCache();
// check if the protection is already cached
Protection cached = cache.getProtection(cacheKey);
if (cached != null) {
// System.out.println("loadProtection() => CACHE HIT");
return cached;
}
// Is it possible that there are protections in the cache?
if (!ignoreProtectionCount && hasAllProtectionsCached()) {
// System.out.println("loadProtection() => HAS_ALL_PROTECTIONS_CACHED");
return null; // nothing was in the cache, nothing assumed to be in the database
}
// System.out.println("loadProtection() => QUERYING");
// not in cache; abort
if (LWC.getInstance().isInSafeMode()) {
return null;
}
PreparedStatement statement = null;
try {
statement = prepare("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections WHERE x = ? AND y = ? AND z = ? AND world = ?");
statement.setInt(1, x);
statement.setInt(2, y);
statement.setInt(3, z);
statement.setString(4, worldName);
Protection protection = resolveProtection(statement);
if (protection != null) {
// cache the protection
cache.add(protection);
}
return protection;
} catch (SQLException e) {
printException(e);
} finally {
safeClose(statement);
}
return null;
}
/**
* Load all protections (use sparingly !!)
*
* @return
*/
public List<Protection> loadProtections() {
PreparedStatement statement = null;
try {
statement = prepare("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections");
return resolveProtections(statement);
} catch (Exception e) {
printException(e);
} finally {
safeClose(statement);
}
return new ArrayList<Protection>();
}
/**
* Load the first protection within a block's radius
*
* @param world
* @param x
* @param y
* @param z
* @param radius
* @return list of Protection objects found
*/
public List<Protection> loadProtections(String world, int x, int y, int z, int radius) {
PreparedStatement statement = null;
try {
statement = prepare("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections WHERE world = ? AND x >= ? AND x <= ? AND y >= ? AND y <= ? AND z >= ? AND z <= ?");
statement.setString(1, world);
statement.setInt(2, x - radius);
statement.setInt(3, x + radius);
statement.setInt(4, y - radius);
statement.setInt(5, y + radius);
statement.setInt(6, z - radius);
statement.setInt(7, z + radius);
return resolveProtections(statement);
} catch (Exception e) {
printException(e);
} finally {
safeClose(statement);
}
return new ArrayList<Protection>();
}
/**
* Remove all protections for a given player
*
* @param player
* @return the amount of protections removed
*/
public int removeProtectionsByPlayer(String player) {
int removed = 0;
for (Protection protection : loadProtectionsByPlayer(player)) {
protection.remove();
removed ++;
}
return removed;
}
/**
* Load all protections in the coordinate ranges
*
* @param world
* @param x1