-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPorkBarrel.java
970 lines (837 loc) · 33.1 KB
/
PorkBarrel.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.logging.*;
public class PorkBarrel extends Plugin
{
// Base Plugin Variables
private static String name = "PorkBarrel";
private static String version = "3.14";
private boolean debug = false;
static final Logger log = Logger.getLogger("Minecraft");
private Random randGen;
// Painting
private int[][] paintReactions = new int[16][16];
// Gifter config
private int giftId = 0;
private int giftQty = 1;
private String giftRecipientFile = "gift.recipients";
private ArrayList<String> giftRecipients = new ArrayList<String>();
// Auth config
private String preAuthGroupName = "guest";
private String postAuthGroupName = "member";
private String authPassword = "xyzzy";
private ArrayList<String> validPasswordList = new ArrayList<String>();
// Repair
private boolean likeRepairsLike = true;
private HashMap<Integer,Integer> repairingItems = new HashMap<Integer,Integer>();
private int lighterRepairPerCharge = 0;
private boolean allowArmorRepair = true;
ArrayList<Integer> repairables = new ArrayList<Integer>();
// SetGroup config
private ArrayList<String> sgGroupList = new ArrayList<String>();
public void enable() {
// TODO: Put the PB config location in the server.properties file
PropertiesFile properties = new PropertiesFile("porkbarrel.properties");
try {
debug = properties.getBoolean("debug", false);
} catch (Exception ex) {
log.log(Level.SEVERE, name + ": exception while reading from porkbarrel.properties", ex);
return;
}
// Gifter
giftId = properties.getInt("gift-id", giftId);
giftQty = properties.getInt("gift-quantity", giftQty);
giftRecipientFile = properties.getString("gift-recipient-file", giftRecipientFile);
// Auth
preAuthGroupName = properties.getString("pre-auth-group", preAuthGroupName);
postAuthGroupName = properties.getString("post-auth-group", postAuthGroupName);
authPassword = properties.getString("auth-password", authPassword);
// Repair
likeRepairsLike = properties.getBoolean("like-repairs-like", likeRepairsLike);
lighterRepairPerCharge = properties.getInt("lighter-repair-per-charge", lighterRepairPerCharge);
allowArmorRepair = properties.getBoolean("allow-armor-repair", allowArmorRepair);
String repairTemp = properties.getString("repairing-items", "322*1025");
// Parse repair list
String[] itemList = repairTemp.split(",");
for (int i = 0; i < itemList.length; i++) {
String[] itemDetails = itemList[i].split("\\*");
if (itemDetails.length == 2) {
try {
int itemId = Integer.parseInt(itemDetails[0]);
int repVal = Integer.parseInt(itemDetails[1]);
repairingItems.put(itemId, repVal);
} catch (Exception e) {
log.info("Error parsing repairing item: " + itemList[i]);
}
}
}
// Repairable Items:
// Wooden Tools (Durability: 33)
repairables.add(268); // Sword
repairables.add(269); // Shovel
repairables.add(270); // Pick
repairables.add(271); // Axe
repairables.add(290); // Hoe
// Leather Armor
repairables.add(298); // Helmet
repairables.add(299); // Chestplate
repairables.add(300); // Leggings
repairables.add(301); // Boots
// Chainmail Armor
repairables.add(302); // Helmet
repairables.add(303); // Chestplate
repairables.add(304); // Leggings
repairables.add(305); // Boots
// Golden Tools (Durability: 33)
repairables.add(283); // Sword
repairables.add(284); // Shovel
repairables.add(285); // Pick
repairables.add(286); // Axe
repairables.add(294); // Hoe
repairables.add(314); // Helmet
repairables.add(315); // Chestplate
repairables.add(316); // Leggings
repairables.add(317); // Boots
// Rock Tools (Durability: 65)
repairables.add(272); // Sword
repairables.add(273); // Shovel
repairables.add(274); // Pick
repairables.add(275); // Axe
repairables.add(291); // Hoe
// Iron Tools (Durability: 129)
repairables.add(267); // Sword
repairables.add(256); // Shovel
repairables.add(257); // Pick
repairables.add(258); // Axe
repairables.add(292); // Hoe
repairables.add(306); // Helmet
repairables.add(307); // Chestplate
repairables.add(308); // Leggings
repairables.add(309); // Boots
// Diamond Tools (Durability: 1025)
repairables.add(276); // Sword
repairables.add(277); // Shovel
repairables.add(278); // Pick
repairables.add(279); // Axe
repairables.add(293); // Hoe
repairables.add(310); // Helmet
repairables.add(311); // Chestplate
repairables.add(312); // Leggings
repairables.add(313); // Boots
// Painting
for (int dInc = 0; dInc < 16; dInc++) {
for (int wInc = 0; wInc < 16; wInc++) {
if (dInc == 0) {
// Black dye makes things black by default
paintReactions[dInc][wInc] = 15;
} else if (dInc == 15) {
// White dye makes things white by default
paintReactions[dInc][wInc] = 0;
} else if (wInc == 0) {
// Dye + white wool = that colored wool
paintReactions[dInc][wInc] = 15 - dInc;
} else {
paintReactions[dInc][wInc] = -1;
}
}
}
// paintMap[dyeColor][woolColor]
// White Dye + ____
paintReactions[15][7] = 8; // gray = light gray
paintReactions[15][11] = 3; // blue = light blue
paintReactions[15][13] = 5; // green = light green
paintReactions[15][14] = 6; // red = pink
paintReactions[15][15] = 7; // gray = light gray
paintReactions[11][14] = 1; // Yellow dye + red wool = orange
paintReactions[9][10] = 2; // Pink dye + purple wool = magenta
paintReactions[5][6] = 2; // Purple dye + pink wool = magenta
paintReactions[4][13] = 9; // Blue dye + green wool = cyan
paintReactions[4][14] = 10; // Blue dye + red wool = purple
paintReactions[2][11] = 9; // Green dye + blue wool = cyan
paintReactions[1][4] = 1; // Red dye + yellow wool = orange
paintReactions[1][11] = 10; // Red dye + blue wool = purple
// Black Dye + ____
paintReactions[0][2] = 10; // magenta = purple
paintReactions[0][3] = 11; // light blue = blue
paintReactions[0][5] = 13; // light green = green
paintReactions[0][6] = 14; // pink = red
paintReactions[0][8] = 7; // light gray = gray
// SetGroup
Collections.addAll(sgGroupList, properties.getString("setgroup-list", "default,vip").split(","));
// TODO: Add command help
etc.getInstance().addCommand("/authme", " - Get authorized to build.");
etc.getInstance().addCommand("/setgroup", " playername groupname - Put a player in a group.");
etc.getInstance().addCommand("/grouplist", " - List of groups available for /setgroup.");
etc.getInstance().addCommand("/danger", " - Display the status of monster spawning.");
etc.getInstance().addCommand("/repair", " - Repair a tool in slot 1 with item in slot 2.");
etc.getInstance().addCommand("/whatrepairs", " - Tells you what materials repair tools or armor.");
log.info(name + " v" + version + " enabled.");
}
public void disable() {
etc.getInstance().removeCommand("/authme");
etc.getInstance().removeCommand("/setgroup");
etc.getInstance().removeCommand("/grouplist");
etc.getInstance().removeCommand("/danger");
etc.getInstance().removeCommand("/repair");
etc.getInstance().removeCommand("/whatrepairs");
log.info(name + " v" + version + " disabled.");
}
public void initialize() {
PBListener listener = new PBListener();
randGen = new Random();
etc.getLoader().addListener(PluginLoader.Hook.IGNITE, listener, this, PluginListener.Priority.HIGH);
etc.getLoader().addListener(PluginLoader.Hook.ITEM_USE, listener, this, PluginListener.Priority.HIGH);
etc.getLoader().addListener(PluginLoader.Hook.BLOCK_PLACE, listener, this, PluginListener.Priority.HIGH);
etc.getLoader().addListener(PluginLoader.Hook.MOB_SPAWN, listener, this, PluginListener.Priority.HIGH);
etc.getLoader().addListener(PluginLoader.Hook.COMMAND, listener, this, PluginListener.Priority.HIGH);
etc.getLoader().addListener(PluginLoader.Hook.SERVERCOMMAND, listener, this, PluginListener.Priority.HIGH);
etc.getLoader().addListener(PluginLoader.Hook.LOGIN, listener, this, PluginListener.Priority.LOW);
etc.getLoader().addListener(PluginLoader.Hook.BLOCK_RIGHTCLICKED, listener, this, PluginListener.Priority.LOW);
etc.getLoader().addListener(PluginLoader.Hook.BLOCK_BROKEN, listener, this, PluginListener.Priority.MEDIUM);
}
public void debug(String msg) {
if (debug == true) {
log.info("[DEBUG] " + msg);
}
}
public String colorize(String msg) {
// Replaces !@0 - !@f with the appropriate color code
return(msg.replaceAll("\\!\\@([0-9a-f])", "§$1"));
}
public String join(String[] s, String glue) {
int k=s.length;
if (k==0) {
return null;
}
StringBuilder out=new StringBuilder();
out.append(s[0]);
for (int x=1;x<k;++x) {
out.append(glue).append(s[x]);
}
return out.toString();
}
public class PBListener extends PluginListener // start
{
/**
* Called when either a lava block or a lighter tries to light something on fire.
* block status depends on the light source:
* 1 = lava.
* 2 = lighter (flint + steel).
* 3 = spread (dynamic spreading of fire).
* @param block block that the fire wants to spawn in.
* @param player player
* @return true if you don't want the fire to ignite.
*/
public boolean onIgnite(Block block, Player player) {
// Feature: PorkRoast
if (block.getStatus() == 2 && !player.canUseCommand("/startfire")) {
debug(player.getName() + " was prevented from using fire.");
player.sendMessage(Colors.Rose + "You are not permitted to light fires.");
return true;
} else if (block.getStatus() == 1 || block.getStatus() == 3) {
// Blocking all fire spreading
return true;
}
return false;
}
/**
* Called when someone places a block. Return true to prevent the placement.
*
* @param player
* @param blockPlaced
* @param blockClicked
* @param itemInHand
* @return true if you want to undo the block placement
*/
public boolean onBlockPlace(Player player, Block blockPlaced, Block blockClicked, Item itemInHand) {
// Feature: PorkRoast
if (blockPlaced.getType() == 46 && !player.canUseCommand("/placetnt")) {
debug(player.getName() + " was prevented from placing TNT.");
player.sendMessage(Colors.Rose + "You are not permitted to place TNT.");
return true;
}
return false;
}
/**
* Called when a player uses an item (rightclick with item in hand)
* @param player the player
* @param blockPlaced where a block would end up when the item was a bucket
* @param blockClicked
* @param item the item being used (in hand)
* @return true to prevent using the item.
*/
public boolean onItemUse(Player player, Block blockPlaced, Block blockClicked, Item item) {
// Feature: PorkRoast
if ((item.itemType.getId() >= 325 && item.itemType.getId() <= 327) && !player.canUseCommand("/usebucket")) {
debug(player.getName() + " was prevented from using a bucket.");
player.sendMessage(Colors.Rose + "You are not permitted to use buckets.");
return true;
}
return false;
}
/**
* @param mob Mob attempting to spawn.
* @return true if you don't want mob to spawn.
*/
public boolean onMobSpawn(Mob mob) {
// Feature: MobGrounder
Location down = new Location(mob.getX(), mob.getY(), mob.getZ(), 0, 90);
HitBlox blox = new HitBlox(down);
Block block = blox.getTargetBlock();
if (block != null && block.getType() == 18) {
debug("Blocking " + mob.getName() + " from spawning on leaves.");
return true;
}
return false;
}
/**
* Called when someone presses right click aimed at a block.
* You can intercept this to add your own right click actions
* to different item types (see itemInHand)
*
* @param player
* @param blockClicked
* @param itemInHand
*/
public void onBlockRightClicked(Player player, Block blockClicked, Item item) {
if (item.getItemId() == 286 && player.canUseCommand("/removeanyblock") && blockClicked.getType() != 54) {
etc.getServer().setBlockAt(0, blockClicked.getX(), blockClicked.getY(), blockClicked.getZ());
}
if (player.canUseCommand("/paint") && item.getItemId() == 351 && blockClicked.getType() == 35) {
int dyeColor = item.getDamage();
int woolColor = etc.getServer().getBlockData(blockClicked.getX(), blockClicked.getY(), blockClicked.getZ());
int targetColor = paintReactions[dyeColor][woolColor];
if (targetColor >= 0 && targetColor != woolColor) {
if (removeOneDye(player, dyeColor)) {
etc.getServer().setBlockData(blockClicked.getX(), blockClicked.getY(), blockClicked.getZ(), targetColor);
}
}
}
}
private boolean removeOneDye(Player player, int dyeType) {
Item thisItem;
int targetSlot = -1;
boolean isFull = true;
for (int slotNum=35; slotNum >= 0; slotNum--) {
thisItem = player.getInventory().getItemFromSlot(slotNum);
if (thisItem == null) {
continue;
}
if (thisItem.getItemId() == 351 && thisItem.getDamage() == dyeType) {
if (thisItem.getAmount() < 64) {
targetSlot = slotNum;
isFull = false;
} else if (isFull) {
targetSlot = slotNum;
}
}
}
thisItem = player.getInventory().getItemFromSlot(targetSlot);
if (thisItem != null) {
int newQuantity = thisItem.getAmount() - 1;
if (newQuantity == 0) {
player.getInventory().removeItem(targetSlot);
} else {
player.getInventory().setSlot(351, newQuantity, dyeType, targetSlot);
}
return true;
}
return false;
}
/**
* Called when a person actually breaks the block.
*
* @param player
* @param block
* @return
*/
public boolean onBlockBreak(Player player, Block block) {
// Simulate fossil finds and lapis infusion
if (player.getItemInHand() == 278) {
if (block.getType() == 1) {
int r = randGen.nextInt(63);
if (r == 0) {
etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), 352);
}
} else if (block.getType() == 16) { // Coal
int r = randGen.nextInt(63);
if (r == 0) {
etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), 21);
}
} else if (block.getType() == 73 || block.getType() == 74) { // Redstone
int r = randGen.nextInt(31);
if (r == 0) {
etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), 21);
}
} else if (block.getType() == 56) { // Diamond
int r = randGen.nextInt(31);
if (r == 0) {
etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), 21);
}
} else if (block.getType() == 21) { // Lapis Lazuli
int r = randGen.nextInt(15);
if (r == 0) {
etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), 21);
}
}
}
// Glass should drop sand
if (block.getType() == 20) {
etc.getServer().dropItem(block.getX(), block.getY(), block.getZ(), 12);
}
return false;
}
/**
* Called before the command is parsed. Return true if you don't want the
* command to be parsed.
*
* @param player
* @param split
* @return false if you want the command to be parsed.
*/
public boolean onCommand(Player player, String[] split) {
// The plug-in will get any command issued in the game,
// not just the ones we specifically request, so we have to
// check to see if we should be acting.
if (!player.canUseCommand(split[0])) {
return false;
}
if (split[0].equalsIgnoreCase("/gift")){
// Feature: Server Gifts
if (giftId == 0 || giftQty == 0) {
player.sendMessage(Colors.Rose + "No gifts are available.");
debug(player.getName() + " attempted to receive a gift.");
return true;
}
boolean alreadyAuthed = giftRecipients.contains(player.getName());
if (alreadyAuthed) {
player.sendMessage(Colors.Rose + "You've already gotten a gift!");
return true;
} else {
PropertiesFile giftees = new PropertiesFile(giftRecipientFile);
Integer blockId = giftees.getInt(player.getName(), 0);
if (blockId == 0 || blockId != giftId) {
player.giveItem(giftId, giftQty);
giftees.setInt(player.getName(), giftId);
giftRecipients.add(player.getName());
player.sendMessage(Colors.Gold + "Here you go!");
return true;
} else {
giftRecipients.add(player.getName());
player.sendMessage(Colors.Rose + "You've already gotten a gift!");
return true;
}
}
} else if (split[0].equalsIgnoreCase("/authme")){
boolean alreadyAuthed = validPasswordList.contains(player.getName());
// The group is something other than 'default', so this person should be a member
// New users have one group in the array: an empty string.
String currentGroup = "";
if (player.getGroups().length > 0) {
currentGroup = player.getGroups()[0];
}
if (!currentGroup.equalsIgnoreCase(preAuthGroupName) && currentGroup != "") {
player.sendMessage(Colors.Rose + "You cannot auth.");
return true;
}
if (alreadyAuthed) {
if (split.length < 2 || !split[1].equals("agree")) {
player.sendMessage(Colors.Rose + "Please agree to the Terms of Service.");
player.sendMessage(Colors.Rose + "The TOS can be found in the MeFightClub.com Forums.");
player.sendMessage(Colors.Rose + "Auth syntax: /authme agree");
return true;
}
// Players who are members of multiple groups might have complicated permissions,
// but they should already be "members". In theory, this could be changed to add
// 'member' if it doesn't exist rather than just quitting here.
if (player.getGroups().length > 1) {
player.sendMessage(Colors.Rose + "You are a member of multiple groups, cannot auth.");
return true;
}
// Set the new group and save to users.txt or mysql
player.setGroups(new String[]{postAuthGroupName});
if (!etc.getDataSource().doesPlayerExist(player.getName())) {
etc.getDataSource().addPlayer(player);
} else {
etc.getDataSource().modifyPlayer(player);
}
// Send notifications to issuer and target
player.sendMessage(Colors.Green + "You are now a member.");
log.info(player.getName() + " has agreed to the TOS.");
validPasswordList.remove(player.getName());
return true;
} else {
if (split.length < 2) {
player.sendMessage(Colors.Rose + "Auth syntax: /authme <password>");
return true;
}
if (split[1].equals(authPassword)) {
player.sendMessage(Colors.Green + "Password accepted!");
log.info(player.getName() + " has entered the correct password.");
validPasswordList.add(player.getName());
player.sendMessage(Colors.Rose + "To be authorized you must agree to the Terms of Service.");
player.sendMessage(Colors.Rose + "The TOS can be found in the MeFightClub.com Forums.");
player.sendMessage(Colors.Rose + "If you agree, issue command: /authme agree");
return true;
}
player.sendMessage(Colors.Rose + "Invalid password.");
log.info(player.getName() + " has entered an invalid password. (" + split[1] + ")");
return true;
}
} else if (split[0].equalsIgnoreCase("/grouplist")) {
player.sendMessage(Colors.LightGray + "Available Groups:");
for (String g : sgGroupList) {
player.sendMessage(Colors.LightGray + " - " + g);
}
return true;
} else if (split[0].equalsIgnoreCase("/setgroup")) {
if (split.length != 3) {
player.sendMessage(Colors.Rose + "Syntax: /setgroup <player> <group>");
return true;
}
// Get a player if they're online
Player targetPlayer = etc.getServer().getPlayer(split[1]);
String targetGroup = split[2];
// We can only work with online players right now
if (targetPlayer == null) {
player.sendMessage(Colors.Rose + split[1] + " is not online.");
return true;
}
// Check that player is only in these groups
String[] currentGroups = targetPlayer.getGroups();
if (currentGroups.length > 0) {
for (String g : currentGroups) {
if (!sgGroupList.contains(g)) {
player.sendMessage(Colors.Rose + player.getName() + " is a member of " + g + " and cannot be changed.");
log.info(player.getName() + " failed to change " + targetPlayer.getName() + " to " + targetGroup);
return true;
}
}
}
// Check that the assigned group is in the allowed groups
if (!sgGroupList.contains(targetGroup)) {
player.sendMessage(Colors.Rose + targetGroup + " is not a valid group.");
log.info(player.getName() + " failed to change " + targetPlayer.getName() + " to " + targetGroup);
return true;
}
// Change the player groups
targetPlayer.setGroups(new String[]{targetGroup});
if (!etc.getDataSource().doesPlayerExist(targetPlayer.getName())) {
etc.getDataSource().addPlayer(targetPlayer);
} else {
etc.getDataSource().modifyPlayer(targetPlayer);
}
player.sendMessage(Colors.Green + targetPlayer.getName() + " is now a member of the '" + targetGroup + "' group.");
targetPlayer.sendMessage(Colors.Green + "You are now a member of the '" + targetGroup + "' group.");
log.info(player.getName() + " set " + targetPlayer.getName() + "'s group to '" + targetGroup + "'.");
return true;
} else if (split[0].equalsIgnoreCase("/rawmsg") || split[0].equalsIgnoreCase("/rm")) {
if (split.length < 3) {
player.sendMessage(Colors.Rose + "Incorrect message syntax.");
return true;
}
List<Player> targetList = new ArrayList<Player>();
if (split[1].equalsIgnoreCase("*")) {
targetList = etc.getServer().getPlayerList();
} else {
// Get a player if they're online
Player targetPlayer = etc.getServer().getPlayer(split[1]);
// We can only work with online players right now
if (targetPlayer == null) {
player.sendMessage(Colors.Rose + split[1] + " is not online.");
return true;
}
targetList.add(targetPlayer);
}
// Build the message
int k = split.length;
StringBuilder msg = new StringBuilder();
for (int i=2; i < k; i++) {
msg.append(split[i]).append(' ');
}
String message = msg.toString();
// Make colors work
int sentCount = 0;
message = colorize(message);
for (Player p: targetList) {
p.sendMessage(message);
sentCount++;
}
if (sentCount == 1) {
player.sendMessage(Colors.Green + "Message sent.");
} else {
player.sendMessage(Colors.Green + sentCount + " messages sent.");
}
return true;
} else if (split[0].equalsIgnoreCase("/danger")) {
// Check so this fails gracefully on updates:
try {
if (etc.getMCServer().e.k == 1) {
player.sendMessage(Colors.Rose + "The world feels dangerous.");
} else {
player.sendMessage(Colors.LightGreen + "The world feels safe.");
}
} catch (Throwable e) {
PropertiesFile serverProps = new PropertiesFile("server.properties");
if (serverProps.getBoolean("spawn-monsters", true)) {
player.sendMessage(Colors.Rose + "You're not sure, but the world seems dangerous.");
} else {
player.sendMessage(Colors.LightGreen + "You're not sure, but the world seems safe.");
}
serverProps = null;
}
return true;
} else if (split[0].equalsIgnoreCase("/repair")) {
// Empty slot
if (player.getInventory().getItemFromSlot(0) == null || player.getInventory().getItemFromSlot(1) == null) {
player.sendMessage(Colors.Rose + "Repairable tool goes in slot one, repairing item goes in slot two.");
return true;
}
// Get the info from the two slots
Item toRepair = player.getInventory().getItemFromSlot(0);
int toRepairId = toRepair.getItemId();
Item repairWith = player.getInventory().getItemFromSlot(1);
int repairWithId = repairWith.getItemId();
// Check against the items that can be repaired
if (!repairables.contains(toRepairId)) {
player.sendMessage(Colors.Rose + "Your " + etc.getDataSource().getItem(toRepairId) + " is not repairable.");
return true;
}
// Does the item actually need to be repaired?
if (toRepair.getDamage() == 0) {
player.sendMessage(Colors.Rose + "Your " + etc.getDataSource().getItem(toRepairId) + " does not need repairing.");
return true;
}
// "like repairs like" means one of what the item was built of is enough to fully repair a tool
// For example: one diamond fully repairs a diamond pick
if (likeRepairsLike) {
boolean doRepair = false;
switch(toRepairId) {
// Wooden Tools (Durability: 33)
case 268: // Sword
case 269: // Shovel
case 270: // Pick
case 271: // Axe
case 290: // Hoe
if (repairWithId == 5) { doRepair = true; }
break;
// Golden Tools (Durability: 33)
case 283: // Sword
case 284: // Shovel
case 285: // Pick
case 286: // Axe
case 294: // Hoe
case 314: // Helmet
case 315: // Chestplate
case 316: // Leggings
case 317: // Boots
if (repairWithId == 266) { doRepair = true; }
break;
// Rock Tools (Durability: 65)
case 272: // Sword
case 273: // Shovel
case 274: // Pick
case 275: // Axe
case 291: // Hoe
if (repairWithId == 4) { doRepair = true; }
break;
// Leather Armor
case 298: // Helmet
case 299: // Chestplate
case 300: // Leggings
case 301: // Boots
case 302: // Chain Helmet
case 303: // Chain Chestplate
case 304: // Chain Leggings
case 305: // Chain Boots
if (repairWithId == 334) { doRepair = true; }
break;
// Iron Tools (Durability: 129)
case 267: // Sword
case 256: // Shovel
case 257: // Pick
case 258: // Axe
case 292: // Hoe
case 306: // Helmet
case 307: // Chestplate
case 308: // Leggings
case 309: // Boots
if (repairWithId == 265) { doRepair = true; }
break;
// Diamond Tools (Durability: 1025)
case 276: // Sword
case 277: // Shovel
case 278: // Pick
case 279: // Axe
case 293: // Hoe
case 310: // Helmet
case 311: // Chestplate
case 312: // Leggings
case 313: // Boots
if (repairWithId == 264) { doRepair = true; }
break;
}
if (doRepair) {
// Place the tool in inventory
player.getInventory().setSlot(toRepairId, 1, 0, 0);
// Update the item being repaired with
if (repairWith.getAmount() > 1) {
repairWith.setAmount(repairWith.getAmount()-1);
player.getInventory().setSlot(repairWith, 1);
} else {
player.getInventory().removeItem(1);
}
player.sendMessage(Colors.LightGreen + "Your " + etc.getDataSource().getItem(toRepairId) + " is as good as new!");
return true;
}
}
boolean doLighterRepair = false;
if (lighterRepairPerCharge > 0 && repairWithId == 259) {
doLighterRepair = true;
}
// Check that the item in the repair-with slot is okay to repair with
if (!doLighterRepair && !repairingItems.containsKey(repairWithId)) {
player.sendMessage(Colors.Rose + "You cannot repair tools with " + etc.getDataSource().getItem(repairWithId) + ".");
return true;
}
int curDamage = toRepair.getDamage();
int repairPerItem = 0;
int repairItemQty = repairWith.getAmount();
int repairNumber = 1;
if (doLighterRepair) {
repairPerItem = lighterRepairPerCharge;
repairItemQty = 65 - repairWith.getDamage();
} else {
repairPerItem = repairingItems.get(repairWithId);
}
// "/repair max" will use as many items from slot1 as is required to fully-repair an item
if (split.length == 2 && split[1].equalsIgnoreCase("max")) {
int requestedQty = curDamage / repairPerItem;
// Integer division above discards remainders
if (curDamage % repairPerItem != 0) { requestedQty++; }
// Don't allow more than slot1 to be used.
repairNumber = Math.min(repairItemQty, requestedQty);
}
// Repair the tool
if (curDamage < (repairPerItem * repairNumber)) {
curDamage = 0;
player.sendMessage(Colors.LightGreen + "Your " + etc.getDataSource().getItem(toRepairId) + " is as good as new!");
} else {
curDamage = toRepair.getDamage() - (repairPerItem * repairNumber);
player.sendMessage(Colors.LightGreen + "Your " + etc.getDataSource().getItem(toRepairId) + " has been patched up.");
}
player.getInventory().setSlot(toRepairId, 1, curDamage, 0);
// Update slot1
if (doLighterRepair && (repairWith.getDamage() + repairNumber < 65)) {
player.getInventory().setSlot(259, 1, repairWith.getDamage() + repairNumber, 1);
} else if (repairWith.getAmount() > repairNumber) {
repairWith.setAmount(repairWith.getAmount() - repairNumber);
player.getInventory().setSlot(repairWith, 1);
} else {
player.getInventory().removeItem(1);
}
return true;
} else if (split[0].equalsIgnoreCase("/whatrepairs")) {
if (allowArmorRepair) {
player.sendMessage(Colors.LightBlue + "Tools and armor may both be repaired.");
} else {
player.sendMessage(Colors.LightBlue + "Only tools may be repaired.");
}
if (likeRepairsLike) {
player.sendMessage(Colors.LightGray + "One of an item's material will fully repair it.");
}
if (lighterRepairPerCharge > 0) {
player.sendMessage(Colors.LightGray + "One charge of a flint & steel repairs items for " + lighterRepairPerCharge + " durability.");
}
if (repairingItems.size() > 0) {
player.sendMessage(Colors.LightGray + "The following items will also perform repairs:");
for (int id : repairingItems.keySet()) {
player.sendMessage(Colors.LightGray + " - " + etc.getDataSource().getItem(id) + ": " + repairingItems.get(id));
}
}
return true;
} else if (split[0].equalsIgnoreCase("/givedye")) {
if (split.length > 1) {
int dyeColor = Integer.parseInt(split[1]);
int quantity = 1;
if (dyeColor > 15) { return false; }
if (split.length > 2) {
quantity = Integer.parseInt(split[2]);
}
if (quantity > 64) { return false; }
player.getInventory().setSlot(351, quantity, dyeColor, player.getInventory().getEmptySlot());
return true;
}
}
return false;
} //onCommand
/**
* Called before the console command is parsed. Return true if you don't
* want the server command to be parsed by the server.
*
* @param split
* @return false if you want the command to be parsed.
*/
public boolean onConsoleCommand(String[] split) {
// The plug-in will get any command issued in the game,
// not just the ones we specifically request, so we have to
// check to see if we should be acting.
if (split[0].equalsIgnoreCase("save-and-announce")){
etc.getServer().useConsoleCommand("say Writing world to disk...");
etc.getServer().useConsoleCommand("save-all");
etc.getServer().useConsoleCommand("say Write complete.");
return true;
} else if (split[0].equalsIgnoreCase("rawmsg") || split[0].equalsIgnoreCase("rm")) {
if (split.length < 3) {
log.info("[RM] Incorrect message syntax.");
return true;
}
List<Player> targetList = new ArrayList<Player>();
if (split[1].equalsIgnoreCase("*")) {
targetList = etc.getServer().getPlayerList();
} else {
// Get a player if they're online
Player targetPlayer = etc.getServer().getPlayer(split[1]);
// We can only work with online players right now
if (targetPlayer == null) {
log.info("[RM] " + split[1] + " is not online.");
return true;
}
targetList.add(targetPlayer);
}
// Build the message
int k = split.length;
StringBuilder msg = new StringBuilder();
for (int i=2; i < k; i++) {
msg.append(split[i]).append(' ');
}
String message = msg.toString();
// Make colors work
int sentCount = 0;
message = colorize(message);
for (Player p: targetList) {
p.sendMessage(message);
sentCount++;
}
if (sentCount == 1) {
log.info("[RM] " + "Message sent.");
} else {
log.info("[RM] " + sentCount + " messages sent.");
}
return true;
}
return false;
} //onConsoleCommand
/**
* Called during the later login process
*
* @param player
*/
public void onLogin(Player player) {
onCommand(player, new String[] {"/danger"});
}
} //PBListener
} //PorkBarrel