-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWC.java
1889 lines (1585 loc) · 58.3 KB
/
LWC.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.lwc;
import com.griefcraft.cache.ProtectionCache;
import com.griefcraft.integration.ICurrency;
import com.griefcraft.integration.IPermissions;
import com.griefcraft.integration.currency.BOSECurrency;
import com.griefcraft.integration.currency.EssentialsCurrency;
import com.griefcraft.integration.currency.NoCurrency;
import com.griefcraft.integration.currency.VaultCurrency;
import com.griefcraft.integration.currency.iConomy5Currency;
import com.griefcraft.integration.currency.iConomy6Currency;
import com.griefcraft.integration.permissions.BukkitPermissions;
import com.griefcraft.integration.permissions.PEXPermissions;
import com.griefcraft.integration.permissions.SuperPermsPermissions;
import com.griefcraft.integration.permissions.VaultPermissions;
import com.griefcraft.integration.permissions.bPermissions;
import com.griefcraft.io.BackupManager;
import com.griefcraft.listeners.LWCMCPCSupport;
import com.griefcraft.migration.ConfigPost300;
import com.griefcraft.migration.MySQLPost200;
import com.griefcraft.model.Flag;
import com.griefcraft.model.LWCPlayer;
import com.griefcraft.model.Permission;
import com.griefcraft.model.Protection;
import com.griefcraft.modules.admin.AdminBackup;
import com.griefcraft.modules.admin.AdminCache;
import com.griefcraft.modules.admin.AdminCleanup;
import com.griefcraft.modules.admin.AdminClear;
import com.griefcraft.modules.admin.AdminDump;
import com.griefcraft.modules.admin.AdminExpire;
import com.griefcraft.modules.admin.AdminFind;
import com.griefcraft.modules.admin.AdminFlush;
import com.griefcraft.modules.admin.AdminForceOwner;
import com.griefcraft.modules.admin.AdminLocale;
import com.griefcraft.modules.admin.AdminPurge;
import com.griefcraft.modules.admin.AdminPurgeBanned;
import com.griefcraft.modules.admin.AdminQuery;
import com.griefcraft.modules.admin.AdminRebuild;
import com.griefcraft.modules.admin.AdminReload;
import com.griefcraft.modules.admin.AdminRemove;
import com.griefcraft.modules.admin.AdminReport;
import com.griefcraft.modules.admin.AdminVersion;
import com.griefcraft.modules.admin.BaseAdminModule;
import com.griefcraft.modules.confirm.ConfirmModule;
import com.griefcraft.modules.create.CreateModule;
import com.griefcraft.modules.credits.CreditsModule;
import com.griefcraft.modules.debug.DebugModule;
import com.griefcraft.modules.destroy.DestroyModule;
import com.griefcraft.modules.doors.DoorsModule;
import com.griefcraft.modules.fix.FixModule;
import com.griefcraft.modules.flag.BaseFlagModule;
import com.griefcraft.modules.flag.MagnetModule;
import com.griefcraft.modules.free.FreeModule;
import com.griefcraft.modules.history.HistoryModule;
import com.griefcraft.modules.info.InfoModule;
import com.griefcraft.modules.limits.LimitsModule;
import com.griefcraft.modules.limits.LimitsV2;
import com.griefcraft.modules.modes.BaseModeModule;
import com.griefcraft.modules.modes.DropTransferModule;
import com.griefcraft.modules.modes.NoSpamModule;
import com.griefcraft.modules.modes.PersistModule;
import com.griefcraft.modules.modify.ModifyModule;
import com.griefcraft.modules.owners.OwnersModule;
import com.griefcraft.modules.pluginsupport.MobArena;
import com.griefcraft.modules.pluginsupport.Towny;
import com.griefcraft.modules.pluginsupport.WorldGuard;
import com.griefcraft.modules.redstone.RedstoneModule;
import com.griefcraft.modules.setup.BaseSetupModule;
import com.griefcraft.modules.setup.DatabaseSetupModule;
import com.griefcraft.modules.setup.LimitsSetup;
import com.griefcraft.modules.unlock.UnlockModule;
import com.griefcraft.scripting.Module;
import com.griefcraft.scripting.ModuleLoader;
import com.griefcraft.scripting.event.LWCAccessEvent;
import com.griefcraft.scripting.event.LWCReloadEvent;
import com.griefcraft.scripting.event.LWCSendLocaleEvent;
import com.griefcraft.sql.Database;
import com.griefcraft.sql.PhysDB;
import com.griefcraft.util.Colors;
import com.griefcraft.util.DatabaseThread;
import com.griefcraft.util.Metrics;
import com.griefcraft.util.ProtectionFinder;
import com.griefcraft.util.Statistics;
import com.griefcraft.util.StringUtil;
import com.griefcraft.util.config.Configuration;
import com.griefcraft.util.locale.LocaleUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class LWC {
/**
* If LWC is currently enabled
*/
public static boolean ENABLED = false;
/**
* The current instance of LWC
*/
private static LWC instance;
/**
* Core LWC configuration
*/
private Configuration configuration;
/**
* The module loader
*/
private final ModuleLoader moduleLoader;
/**
* The manager of backups
*/
private final BackupManager backupManager;
/**
* The protection cache
*/
private final ProtectionCache protectionCache;
/**
* Physical database instance
*/
private PhysDB physicalDatabase;
/**
* Plugin instance
*/
private LWCPlugin plugin;
/**
* Updates to the database that can be ran on a seperate thread
*/
private DatabaseThread databaseThread;
/**
* The permissions handler
*/
private IPermissions permissions;
/**
* The currency handler
*/
private ICurrency currency;
/**
* Protection configuration cache
*/
private final Map<String, String> protectionConfigurationCache = new HashMap<String, String>();
public LWC(LWCPlugin plugin) {
this.plugin = plugin;
LWC.instance = this;
configuration = Configuration.load("core.yml");
protectionCache = new ProtectionCache(this);
backupManager = new BackupManager();
moduleLoader = new ModuleLoader(this);
}
/**
* Get the currently loaded LWC instance
*
* @return
*/
public static LWC getInstance() {
return instance;
}
/**
* Get a string representation of a block type
*
* @param id
* @return
*/
public static String materialToString(int id) {
return materialToString(Material.getMaterial(id));
}
/**
* Get a string representation of a block material
*
* @param material
* @return
*/
public static String materialToString(Material material) {
if (material != null) {
String materialName = normalizeName(material);
// attempt to match the locale
String locale = LWC.getInstance().getPlugin().getMessageParser().parseMessage(materialName.toLowerCase());
// if it starts with UNKNOWN_LOCALE, use the default material name
if (locale == null) {
locale = materialName;
}
return StringUtil.capitalizeFirstLetter(locale);
}
return "";
}
/**
* Normalize a name to a more readable & usable form.
* <p/>
* E.g sign_post/wall_sign = Sign, furnace/burning_furnace = Furnace,
* iron_door_block = iron_door
*
* @param material
* @return
*/
private static String normalizeName(Material material) {
String name = StringUtils.replace(material.toString().toLowerCase(), "block", "");
// some name normalizations
if (name.contains("sign")) {
name = "Sign";
}
if (name.contains("furnace")) {
name = "furnace";
}
if (name.endsWith("_")) {
name = name.substring(0, name.length() - 1);
}
return name.toLowerCase();
}
/**
* Restore the direction the block is facing for when 1.8 broke it
*
* @param block
*/
public void adjustChestDirection(Block block, BlockFace face) {
if (block.getType() != Material.CHEST) {
return;
}
// Is there a double chest?
Block doubleChest = findAdjacentDoubleChest(block);
// Calculate the data byte to set
byte data = 0;
switch (face) {
case NORTH:
data = 4;
break;
case SOUTH:
data = 5;
break;
case EAST:
data = 2;
break;
case WEST:
data = 3;
break;
}
// set the data for both sides of the chest
block.setData(data);
if (doubleChest != null) {
doubleChest.setData(data);
}
}
/**
* Look for a double chest adjacent to a block
*
* @param block
* @return
*/
public Block findAdjacentDoubleChest(Block block) {
return findAdjacentBlock(block, Material.CHEST);
}
/**
* Check if a player has the ability to access a protection
*
* @param player
* @param block
* @return
*/
public boolean canAccessProtection(Player player, Block block) {
Protection protection = findProtection(block);
return protection != null && canAccessProtection(player, protection);
}
/**
* Check if a player has the ability to access a protection
*
* @param player
* @param x
* @param y
* @param z
* @return
*/
public boolean canAccessProtection(Player player, int x, int y, int z) {
return canAccessProtection(player, physicalDatabase.loadProtection(player.getWorld().getName(), x, y, z));
}
/**
* Check if a player has the ability to administrate a protection
*
* @param player
* @param block
* @return
*/
public boolean canAdminProtection(Player player, Block block) {
Protection protection = findProtection(block);
return protection != null && canAdminProtection(player, protection);
}
/**
* Check if a player has the ability to administrate a protection
*
* @param player
* @param protection
* @return
*/
public boolean canAdminProtection(Player player, Protection protection) {
if (protection == null || player == null) {
return true;
}
if (isAdmin(player)) {
return true;
}
// Their access level
Permission.Access access = Permission.Access.NONE;
String playerName = player.getName();
switch (protection.getType()) {
case PUBLIC:
if (player.getName().equalsIgnoreCase(protection.getOwner())) {
return true;
}
break;
case PASSWORD:
if (player.getName().equalsIgnoreCase(protection.getOwner()) && wrapPlayer(player).getAccessibleProtections().contains(protection)) {
return true;
}
break;
case PRIVATE:
case DONATION:
if (playerName.equalsIgnoreCase(protection.getOwner())) {
return true;
}
if (protection.getAccess(playerName, Permission.Type.PLAYER) == Permission.Access.ADMIN) {
return true;
}
for (String groupName : permissions.getGroups(player)) {
if (protection.getAccess(groupName, Permission.Type.GROUP) == Permission.Access.ADMIN) {
return true;
}
}
break;
}
// call the canAccessProtection hook
LWCAccessEvent event = new LWCAccessEvent(player, protection, access);
moduleLoader.dispatchEvent(event);
return event.getAccess() == Permission.Access.ADMIN;
}
/**
* Deposit items into an inventory chest
* Works with double chests.
*
* @param block
* @param itemStack
* @return remaining items (if any)
*/
public Map<Integer, ItemStack> depositItems(Block block, ItemStack itemStack) {
BlockState blockState;
if ((blockState = block.getState()) != null && (blockState instanceof InventoryHolder)) {
Block doubleChestBlock = null;
InventoryHolder holder = (InventoryHolder) blockState;
if (block.getType() == Material.CHEST) {
doubleChestBlock = findAdjacentBlock(block, Material.CHEST);
} else if (block.getType() == Material.FURNACE || block.getType() == Material.BURNING_FURNACE) {
Inventory inventory = holder.getInventory();
if (inventory.getItem(0) != null && inventory.getItem(1) != null) {
if (inventory.getItem(0).getType() == itemStack.getType()
&& inventory.getItem(0).getData().getData() == itemStack.getData().getData()
&& inventory.getItem(0).getMaxStackSize() >= (inventory.getItem(0).getAmount() + itemStack.getAmount())) {
// ItemStack fits on Slot 0
} else if (inventory.getItem(1).getType() == itemStack.getType()
&& inventory.getItem(1).getData().getData() == itemStack.getData().getData()
&& inventory.getItem(1).getMaxStackSize() >= (inventory.getItem(1).getAmount() + itemStack.getAmount())) {
// ItemStack fits on Slot 1
} else {
return null;
}
}
}
if (itemStack.getAmount() <= 0) {
return new HashMap<Integer, ItemStack>();
}
Map<Integer, ItemStack> remaining = holder.getInventory().addItem(itemStack);
// we have remainders, deal with it
if (remaining.size() > 0) {
int key = remaining.keySet().iterator().next();
ItemStack remainingItemStack = remaining.get(key);
// is it a double chest ?????
if (doubleChestBlock != null) {
InventoryHolder holder2 = (InventoryHolder) doubleChestBlock.getState();
remaining = holder2.getInventory().addItem(remainingItemStack);
}
// recheck remaining in the event of double chest being used
if (remaining.size() > 0) {
return remaining;
}
}
}
return new HashMap<Integer, ItemStack>();
}
/**
* Find a block that is adjacent to another block given a Material
*
* @param block
* @param material
* @param ignore
* @return
*/
public Block findAdjacentBlock(Block block, Material material, Block... ignore) {
BlockFace[] faces = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
List<Block> ignoreList = Arrays.asList(ignore);
for (BlockFace face : faces) {
Block adjacentBlock = block.getRelative(face);
if (adjacentBlock.getType() == material && !ignoreList.contains(adjacentBlock)) {
return adjacentBlock;
}
}
return null;
}
/**
* Find a block that is adjacent to another block on any of the block's 6 sides given a Material
*
* @param block
* @param material
* @param ignore
* @return
*/
public Block findAdjacentBlockOnAllSides(Block block, Material material, Block... ignore) {
BlockFace[] faces = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN};
List<Block> ignoreList = Arrays.asList(ignore);
for (BlockFace face : faces) {
Block adjacentBlock = block.getRelative(face);
if (adjacentBlock.getType() == material && !ignoreList.contains(adjacentBlock)) {
return adjacentBlock;
}
}
return null;
}
/**
* Find a protection that is adjacent to another block on any of the block's 6 sides
*
* @param block
* @param ignore
* @return
*/
public Protection findAdjacentProtectionOnAllSides(Block block, Block... ignore) {
BlockFace[] faces = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN};
List<Block> ignoreList = Arrays.asList(ignore);
for (BlockFace face : faces) {
Protection protection;
Block adjacentBlock = block.getRelative(face);
if (!ignoreList.contains(adjacentBlock) && (protection = findProtection(adjacentBlock)) != null) {
return protection;
}
}
return null;
}
/**
* Free some memory (LWC was disabled)
*/
public void destruct() {
// destroy the modules
moduleLoader.shutdown();
log("Flushing protection updates (" + databaseThread.size() + ")");
if (databaseThread != null) {
databaseThread.stop();
databaseThread = null;
}
if (physicalDatabase != null) {
physicalDatabase.dispose();
}
physicalDatabase = null;
}
/**
* Log a string
*
* @param str
*/
public void log(String str) {
plugin.getLogger().info(str);
}
/**
* Encrypt a string using SHA1
*
* @param text
* @return
*/
public String encrypt(String text) {
return StringUtil.encrypt(text);
}
/**
* Enforce access to a protected block
*
* @param player
* @param protection
* @param block
* @return true if the player was granted access
*/
public boolean enforceAccess(Player player, Protection protection, Block block, boolean hasAccess) {
MessageParser parser = plugin.getMessageParser();
if (block == null || protection == null) {
return true;
}
// support for old protection dbs that do not contain the block id
if (block != null && (protection.getBlockId() <= 0 && block.getTypeId() != protection.getBlockId())) {
protection.setBlockId(block.getTypeId());
protection.save();
}
// multi-world, update old protections
if (block != null && (protection.getWorld() == null || !block.getWorld().getName().equals(protection.getWorld()))) {
protection.setWorld(block.getWorld().getName());
protection.save();
}
// update timestamp
if (hasAccess) {
long timestamp = System.currentTimeMillis() / 1000L;
boolean isAdmin = isAdmin(player);
// check that they aren't an admin and if they are, they need to be the owner of the protection or have access through /cmodify
if (!isAdmin || protection.getOwner().equals(player.getName()) || protection.getAccess(player.getName(), Permission.Type.PLAYER) != Permission.Access.NONE) {
protection.setLastAccessed(timestamp);
protection.save();
}
}
boolean permShowNotices = hasPermission(player, "lwc.shownotices");
if ((permShowNotices && configuration.getBoolean("core.showNotices", true))
&& !Boolean.parseBoolean(resolveProtectionConfiguration(block.getType(), "quiet"))) {
boolean isOwner = protection.isOwner(player);
boolean showMyNotices = configuration.getBoolean("core.showMyNotices", true);
if (!isOwner || (isOwner && (showMyNotices || permShowNotices))) {
String owner = protection.getOwner();
// replace your username with "you" if you own the protection
if (owner.equalsIgnoreCase(player.getName())) {
owner = parser.parseMessage("you");
}
String blockName = materialToString(block);
String protectionTypeToString = parser.parseMessage(protection.typeToString().toLowerCase());
if (protectionTypeToString == null) {
protectionTypeToString = "Unknown";
}
if (parser.parseMessage("protection." + blockName.toLowerCase() + ".notice.protected") != null) {
sendLocale(player, "protection." + blockName.toLowerCase() + ".notice.protected", "type", protectionTypeToString, "block", blockName, "owner", owner);
} else {
sendLocale(player, "protection.general.notice.protected", "type", protectionTypeToString, "block", blockName, "owner", owner);
}
}
}
if (!hasAccess) {
Protection.Type type = protection.getType();
if (type == Protection.Type.PASSWORD) {
sendLocale(player, "protection.general.locked.password", "block", materialToString(block));
} else if (type == Protection.Type.PRIVATE || type == Protection.Type.DONATION) {
sendLocale(player, "protection.general.locked.private", "block", materialToString(block));
}
}
return hasAccess;
}
/**
* Check if a player has the ability to access a protection
*
* @param player
* @param protection
* @return
*/
public boolean canAccessProtection(Player player, Protection protection) {
if (protection == null || player == null) {
return true;
}
if (isAdmin(player)) {
return true;
}
if (isMod(player)) {
Player protectionOwner = plugin.getServer().getPlayer(protection.getOwner());
if (protectionOwner == null) {
return true;
}
if (!isAdmin(protectionOwner)) {
return true;
}
}
// Their access level
Permission.Access access = Permission.Access.NONE;
String playerName = player.getName();
switch (protection.getType()) {
case PUBLIC:
case DONATION:
return true;
case PASSWORD:
if (wrapPlayer(player).getAccessibleProtections().contains(protection)) {
return true;
}
break;
case PRIVATE:
if (playerName.equalsIgnoreCase(protection.getOwner())) {
return true;
}
if (protection.getAccess(playerName, Permission.Type.PLAYER).ordinal() >= Permission.Access.PLAYER.ordinal()) {
return true;
}
// Check for item keys
for (Permission permission : protection.getPermissions()) {
if (permission.getType() != Permission.Type.ITEM) {
continue;
}
// Get the item they need to have
int item = Integer.parseInt(permission.getName());
// Are they wielding it?
if (player.getItemInHand().getTypeId() == item) {
return true;
}
}
for (String groupName : permissions.getGroups(player)) {
if (protection.getAccess(groupName, Permission.Type.GROUP).ordinal() >= Permission.Access.PLAYER.ordinal()) {
return true;
}
}
break;
}
// call the canAccessProtection hook
LWCAccessEvent event = new LWCAccessEvent(player, protection, access);
moduleLoader.dispatchEvent(event);
return event.getAccess() == Permission.Access.PLAYER || event.getAccess() == Permission.Access.ADMIN;
}
/**
* Check if a player can do mod functions on LWC
*
* @param player the player to check
* @return true if the player is an LWC mod
*/
public boolean isMod(Player player) {
return hasPermission(player, "lwc.mod");
}
/**
* Check if a player can do admin functions on LWC
*
* @param player the player to check
* @return true if the player is an LWC admin
*/
public boolean isAdmin(Player player) {
if (player.isOp()) {
if (configuration.getBoolean("core.opIsLWCAdmin", true)) {
return true;
}
}
return hasPermission(player, "lwc.admin");
}
/**
* Check if a player has a permissions node
*
* @param player
* @param node
* @return
*/
public boolean hasPermission(Player player, String node) {
try {
return player.hasPermission(node);
} catch (NoSuchMethodError e) {
// their server does not support Superperms..
return !node.contains("admin") && !node.contains("mod");
}
}
/**
* Create an LWCPlayer object for a player
*
* @param sender
* @return
*/
public LWCPlayer wrapPlayer(CommandSender sender) {
if (sender instanceof LWCPlayer) {
return (LWCPlayer) sender;
}
if (!(sender instanceof Player)) {
return null;
}
return LWCPlayer.getPlayer((Player) sender);
}
/**
* Find a player in the given ranges
*
* @param minX
* @param maxX
* @param minY
* @param maxY
* @param minZ
* @param maxZ
* @return
*/
public Player findPlayer(int minX, int maxX, int minY, int maxY, int minZ, int maxZ) {
for (Player player : plugin.getServer().getOnlinePlayers()) {
Location location = player.getLocation();
int plrX = location.getBlockX();
int plrY = location.getBlockY();
int plrZ = location.getBlockZ();
// simple check of the ranges
if (plrX >= minX && plrX <= maxX && plrY >= plrY && plrY <= maxY && plrZ >= minZ && plrZ <= maxZ) {
return player;
}
}
return null;
}
/**
* Send a locale to a player or console
*
* @param sender
* @param key
* @param args
*/
public void sendLocale(CommandSender sender, String key, Object... args) {
String[] message; // The message to send to the player
MessageParser parser = plugin.getMessageParser();
String parsed = parser.parseMessage(key, args);
if (parsed == null) {
return; // Nothing to send
}
// message = parsed.split("\\n");
message = StringUtils.split(parsed, '\n');
// broadcast an event if they are a player
if (sender instanceof Player) {
LWCSendLocaleEvent evt = new LWCSendLocaleEvent((Player) sender, key);
moduleLoader.dispatchEvent(evt);
// did they cancel it?
if (evt.isCancelled()) {
return;
}
}
if (message == null) {
sender.sendMessage(Colors.Red + "LWC: " + Colors.White + "Undefined locale: \"" + Colors.Gray + key + Colors.White + "\"");
return;
}
if (message.length > 0 && message[0].equalsIgnoreCase("null")) {
return;
}
// Send the message!
// sender.sendMessage(message);
for (String line : message) {
sender.sendMessage(line);
}
}
/**
* Get a string representation of a block's material
*
* @param block
* @return
*/
public static String materialToString(Block block) {
return materialToString(block.getType());
}
/**
* Fast remove all protections for a player. ~100k protections / second.
* @param sender
* @param player
* @param shouldRemoveBlocks
* @return
*/
public int fastRemoveProtectionsByPlayer(CommandSender sender, String player, boolean shouldRemoveBlocks) {
// remove their protections first
int ret = fastRemoveProtections(sender, "Lower(owner) = Lower('" + player + "')", shouldRemoveBlocks);
// invalid any history objects associated with the player
physicalDatabase.invalidateHistory(player);
return ret;
}
/**
* Remove protections very quickly with raw SQL calls
*
* @param sender
* @param where
* @param shouldRemoveBlocks
* @return
*/
public int fastRemoveProtections(CommandSender sender, String where, boolean shouldRemoveBlocks) {
List<Integer> exemptedBlocks = configuration.getIntList("optional.exemptBlocks", new ArrayList<Integer>());
List<Integer> toRemove = new LinkedList<Integer>();
List<Block> removeBlocks = null;
int totalProtections = physicalDatabase.getProtectionCount();
int completed = 0;
int count = 0;
// flush all changes to the database before working on the live database
databaseThread.flush();
if (shouldRemoveBlocks) {
removeBlocks = new LinkedList<Block>();
}
if (where != null && !where.trim().isEmpty()) {
where = " WHERE " + where.trim();
}
sender.sendMessage("Loading protections via STREAM mode");
try {
Connection connection = physicalDatabase.createConnection();
Statement resultStatement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
if (physicalDatabase.getType() == Database.Type.MySQL) {
resultStatement.setFetchSize(Integer.MIN_VALUE);
}
String prefix = physicalDatabase.getPrefix();
ResultSet result = resultStatement.executeQuery("SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM " + prefix + "protections" + where);
while (result.next()) {
Protection protection = physicalDatabase.resolveProtection(result);
World world = protection.getBukkitWorld();
// check if the protection is exempt from being removed
if (protection.hasFlag(Flag.Type.EXEMPTION) || exemptedBlocks.contains(protection.getBlockId())) {
continue;
}
count++;
if (count % 100000 == 0 || count == totalProtections || count == 1) {
sender.sendMessage(Colors.Red + count + " / " + totalProtections);
}
if (world == null) {
continue;
}
// remove the protection
toRemove.add(protection.getId());
// remove the block ?
if (shouldRemoveBlocks) {
removeBlocks.add(protection.getBlock());
}