-
Notifications
You must be signed in to change notification settings - Fork 42
/
AutoManagerFeatures.java
3473 lines (2712 loc) · 108 KB
/
AutoManagerFeatures.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
package tech.mcprison.prison.spigot.autofeatures;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventException;
import org.bukkit.event.HandlerList;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredListener;
import com.cryptomorin.xseries.XMaterial;
import com.vk2gpz.tokenenchant.api.TokenEnchantAPI;
import tech.mcprison.prison.Prison;
import tech.mcprison.prison.autofeatures.AutoFeaturesFileConfig.AutoFeatures;
import tech.mcprison.prison.autofeatures.AutoFeaturesWrapper;
import tech.mcprison.prison.autofeatures.BlockConverterOptionEventTrigger;
import tech.mcprison.prison.cache.PlayerCache;
import tech.mcprison.prison.internal.block.PrisonBlock;
import tech.mcprison.prison.internal.block.PrisonBlock.PrisonBlockType;
import tech.mcprison.prison.mines.data.Mine;
import tech.mcprison.prison.output.ChatDisplay;
import tech.mcprison.prison.output.Output;
import tech.mcprison.prison.output.Output.DebugTarget;
import tech.mcprison.prison.spigot.SpigotPrison;
import tech.mcprison.prison.spigot.SpigotUtil;
import tech.mcprison.prison.spigot.api.PrisonMinesBlockBreakEvent;
import tech.mcprison.prison.spigot.block.BlockBreakPriority;
import tech.mcprison.prison.spigot.block.OnBlockBreakEventCore;
import tech.mcprison.prison.spigot.block.OnBlockBreakExternalEvents;
import tech.mcprison.prison.spigot.block.SpigotBlock;
import tech.mcprison.prison.spigot.block.SpigotItemStack;
import tech.mcprison.prison.spigot.compat.SpigotCompatibility;
import tech.mcprison.prison.spigot.game.SpigotHandlerList;
import tech.mcprison.prison.spigot.game.SpigotPlayer;
import tech.mcprison.prison.spigot.sellall.SellAllUtil;
import tech.mcprison.prison.spigot.spiget.BluesSpigetSemVerComparator;
import tech.mcprison.prison.spigot.utils.tasks.PlayerAutoRankupTask;
import tech.mcprison.prison.tasks.PrisonCommandTaskData;
import tech.mcprison.prison.tasks.PrisonCommandTaskData.TaskMode;
import tech.mcprison.prison.tasks.PrisonCommandTasks;
import tech.mcprison.prison.util.Text;
/**
* <p>This class controls the data and the basic functions for auto features.
* It loads the settings only once, and uses them on each event that is raised
* in order to reduce overhead in reloading all of them on each block break
* event. Then, if an external process should happen to save changes to
* these settings, then it will reload the settings so they are always
* current.
* </p>
*
*
*/
public abstract class AutoManagerFeatures
extends OnBlockBreakEventCore {
private Random random = new Random();
public AutoManagerFeatures() {
super();
setup();
}
public enum EventListenerCancelBy {
none,
event,
drops;
}
private void setup() {
// Register all external events such as mcMMO, EZBlocks, and Quests:
OnBlockBreakExternalEvents.getInstance().registerAllExternalEvents();
}
/**
* <p> NOTE: Check for the ACCESS priority and if someone does not have access, then return
* with a cancel on the event. Both ACCESSBLOCKEVENTS and ACCESSMONITOR will be
* converted to just ACCESS at this point, and the other part will run under either
* BLOCKEVENTS or MONITOR.
* </p>
*
* @param pmEvent
* @param start
* @return
*/
protected boolean checkIfNoAccess( PrisonMinesBlockBreakEvent pmEvent, double start ) {
boolean results = false;
// NOTE: Check for the ACCESS priority and if someone does not have access, then return
// with a cancel on the event.
if ( pmEvent.getBbPriority().isAccess() && pmEvent.getMine() != null &&
!pmEvent.getMine().hasMiningAccess( pmEvent.getSpigotPlayer() )) {
String message = String.format( "(&cACCESS fail: player %s does not have access to "
+ "mine %s&3. Event canceled) ",
pmEvent.getSpigotPlayer().getName(),
pmEvent.getMine().getTag() );
pmEvent.getDebugInfo().append( message );
printDebugInfo( pmEvent, start );
if ( pmEvent.getSpigotPlayer() != null &&
isBoolean( AutoFeatures.eventPriorityACCESSFailureTPToCurrentMine ) ) {
// run the `/mines tp` command for the player which will TP them to a
// mine they can access:
String debugInfo = String.format(
"ACCESS failed: teleport %s to valid mine.",
pmEvent.getSpigotPlayer().getName() );
PrisonCommandTaskData cmdTask = new PrisonCommandTaskData( debugInfo,
"mines tp", 0 );
cmdTask.setTaskMode( TaskMode.syncPlayer );
PrisonCommandTasks.submitTasks( pmEvent.getSpigotPlayer(), cmdTask );
}
results = true;
}
return results;
}
/**
* <p>Prints out the debugInfo if it has anything to print.
* </p>
*
* @param pmEvent
* @param start
*/
protected void printDebugInfo( PrisonMinesBlockBreakEvent pmEvent, double start ) {
if ( pmEvent != null && pmEvent.getDebugInfo().length() > 0 ) {
long stop = System.nanoTime();
pmEvent.getDebugInfo().append( "{br}|| ### ** End Event Debug Info ** ### [" )
.append( (stop - start) / 1000000d )
.append( " ms]" );
if ( !Output.get().isDebug() && pmEvent.isForceDebugLogging() ) {
pmEvent.getDebugInfo().insert(0, Output.get().getColorCodeDebug() );
Output.get().logInfo( pmEvent.getDebugInfo().toString(), pmEvent.getSpigotPlayer() );
}
else {
Output.get().logDebug( DebugTarget.blockBreak, pmEvent.getDebugInfo().toString(), pmEvent.getSpigotPlayer() );
}
}
}
/**
* <p>This provides for the basic dump of the event listeners.
* </p>
*
* @param handlers
* @param bbPriority
* @param sb
*/
protected void dumpEventListenersCore( String title, HandlerList handlers, BlockBreakPriority bbPriority,
StringBuilder sb ) {
String cdTitle = String.format(
"%s (&7%s&2)",
title,
( bbPriority == null ? "--none--" : bbPriority.name()) );
ChatDisplay eventDisplay = Prison.get().getPlatform().dumpEventListenersChatDisplay(
cdTitle,
new SpigotHandlerList( handlers ) );
if ( eventDisplay != null ) {
sb.append( eventDisplay.toStringBuilder() );
sb.append( "\n" );
}
if ( bbPriority.isComponentCompound() ) {
StringBuilder sbCP = new StringBuilder();
for ( BlockBreakPriority bbp : bbPriority.getComponentPriorities() ) {
if ( sbCP.length() > 0 ) {
sbCP.append( ", " );
}
sbCP.append( "'" ).append( bbp.name() ).append( "'" );
}
String msg = String.format( "&2Note '&7%s&2' is a compound of: [&7%s&2]",
bbPriority.name(),
sbCP );
sb.append( msg ).append( "\n" );
}
}
/**
* <p>For the event handlers that implement the BlockBreakEvent, this allows
* the other plugins to also process the event if forced.
* </p>
*
* @param pmEvent
* @param debugInfo
* @param e
*/
protected void processPMBBExternalEvents( PrisonMinesBlockBreakEvent pmEvent,
BlockBreakEvent e ) {
if ( pmEvent.getMine() != null || pmEvent.getMine() == null &&
!isBoolean( AutoFeatures.pickupLimitToMines ) ) {
// check all external events such as mcMMO and EZBlocks:
pmEvent.getDebugInfo().append(
OnBlockBreakExternalEvents.getInstance().checkAllExternalEvents( e ) );
}
}
protected EventListenerCancelBy processPMBBEvent(PrisonMinesBlockBreakEvent pmEvent ) {
EventListenerCancelBy cancelBy = EventListenerCancelBy.none;
// This is where the processing actually happens:
if ( pmEvent.getMine() != null || pmEvent.getMine() == null &&
!isBoolean( AutoFeatures.pickupLimitToMines ) ) {
pmEvent.getDebugInfo().append( "(Fire pmEvent) " );
// Set the mine's PrisonBlockTypes for the block. Used to identify custom blocks.
// Needed since processing of the block will lose track of which mine it came from.
if ( pmEvent.getMine() != null ) {
pmEvent.getSpigotBlock().setPrisonBlockTypes( pmEvent.getMine().getPrisonBlockTypes() );
}
// check all external events such as mcMMO and EZBlocks:
// debugInfo.append(
// OnBlockBreakExternalEvents.getInstance().checkAllExternalEvents( e ) );
List<SpigotBlock> explodedBlocks = new ArrayList<>();
pmEvent.setExplodedBlocks( explodedBlocks );
// String triggered = null;
// PrisonMinesBlockBreakEvent pmbbEvent = new PrisonMinesBlockBreakEvent( e.getBlock(), e.getPlayer(),
// pmEvent.getMine(), sBlock, explodedBlocks, BlockEventType.blockBreak, triggered );
Bukkit.getServer().getPluginManager().callEvent( pmEvent );
if ( pmEvent.isCancelled() ) {
pmEvent.setDebugColorCodeWarning();
pmEvent.getDebugInfo().append(
"(Fire pmEvent: PrisonMinesBlockBreakEvent was canceled by another plugin!) " );
pmEvent.setDebugColorCodeDebug();
}
else {
// Cancel drops if so configured:
if ( isBoolean( AutoFeatures.cancelAllBlockEventBlockDrops ) ) {
cancelBy = EventListenerCancelBy.drops;
}
// doAction returns a boolean that indicates if the event should be canceled or not:
if ( doAction( pmEvent ) ) {
// if ( doAction( sBlock, pmEvent.getMine(), pmEvent.getPlayer(), debugInfo ) ) {
if ( isBoolean( AutoFeatures.cancelAllBlockBreakEvents ) ) {
cancelBy = EventListenerCancelBy.event;
}
// else {
//
// pmEvent.getDebugInfo().append( "(event not canceled) " );
// }
finalizeBreakTheBlocks( pmEvent );
doBlockEvents( pmEvent );
}
else {
pmEvent.setDebugColorCodeWarning();
pmEvent.getDebugInfo().append( "(fire pmEvent:doAction failed without details) " );
pmEvent.setDebugColorCodeDebug();
}
}
pmEvent.getDebugInfo().append( "(Fire pmEvent completed) " );
}
else {
pmEvent.getDebugInfo().append( "(Fire pmEvent bypassed) " );
}
return cancelBy;
}
/**
* <p>If the fortune level is zero, then this function will always return a value of one.
* </p>
*
* <p>If it is non-zero, then this function will return a value of one, plus an
* equally distributed chance of returning an additional 0 to fortuneLevel
* bonuses of that material.
* </p>
*
* <p>This applies to "all" materials within the mine, including chests, shulkers,
* signs etc...
* </p>
*
* @param fortuneLevel
* @return
*/
public int getDropCount(int fortuneLevel) {
int j =
fortuneLevel == 0 ?
1 :
1 + getRandom().nextInt(fortuneLevel + 1);
return j;
}
protected boolean hasSilkTouch (SpigotItemStack itemInHand){
boolean results = false;
try {
if ( itemInHand != null && itemInHand.getBukkitStack() != null && itemInHand.getBukkitStack().getEnchantments() != null ) {
results = itemInHand.getBukkitStack().getEnchantments().containsKey(Enchantment.SILK_TOUCH);
}
}
catch ( NullPointerException e ) {
// Ignore. This happens when a TokeEnchanted tool is used when TE is not installed anymore.
// It throws this exception: Caused by: java.lang.NullPointerException: null key in entry: null=5
}
return results;
}
protected boolean hasFortune(SpigotItemStack itemInHand){
boolean results = false;
try {
if ( itemInHand != null && itemInHand.getBukkitStack() != null && itemInHand.getBukkitStack().getEnchantments() != null ) {
results = itemInHand.getBukkitStack().getEnchantments().containsKey(Enchantment.LOOT_BONUS_BLOCKS);
}
}
catch ( NullPointerException e ) {
// Ignore. This happens when a TokeEnchanted tool is used when TE is not installed anymore.
// It throws this exception: Caused by: java.lang.NullPointerException: null key in entry: null=5
}
return results;
}
protected int getFortune(SpigotItemStack itemInHand, StringBuilder debugInfo ){
int fortLevel = 0;
boolean usedTEFortune = false;
if ( isBoolean( AutoFeatures.isUseTokenEnchantsFortuneLevel ) &&
itemInHand != null &&
itemInHand.getBukkitStack() != null ) {
try {
if ( TokenEnchantAPI.getInstance() != null ) {
fortLevel = TokenEnchantAPI.getInstance().getEnchantments( itemInHand.getBukkitStack() )
.get( TokenEnchantAPI.getInstance().getEnchantment("Fortune"));
usedTEFortune = true;
}
}
catch ( Exception e ) {
// ignore: could not use TE.
}
}
try {
if ( !usedTEFortune &&
itemInHand != null &&
itemInHand.getBukkitStack() != null &&
itemInHand.getBukkitStack().containsEnchantment( Enchantment.LOOT_BONUS_BLOCKS ) &&
itemInHand.getBukkitStack().getEnchantments() != null ) {
fortLevel = itemInHand.getBukkitStack().getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
}
}
catch ( NullPointerException e ) {
// Ignore. This happens when a TokeEnchanted tool is used when TE is not installed anymore.
// It throws this exception: Caused by: java.lang.NullPointerException: null key in entry: null=5
}
int results = fortLevel;
// DecimalFormat dFmt = new DecimalFormat( "#,##0.0000" );
DecimalFormat iFmt = new DecimalFormat( "#,##0" );
int maxFortuneLevel = getInteger( AutoFeatures.fortuneMultiplierMax );
String maxFort = "";
if ( maxFortuneLevel > 0 && fortLevel > maxFortuneLevel ) {
results = maxFortuneLevel;
maxFort = String.format(" max=%s result=%s",
iFmt.format( maxFortuneLevel ),
iFmt.format( results ));
}
// double fortuneMultiplierGlobal = getDouble( AutoFeatures.fortuneMultiplierGlobal );
// results *= fortuneMultiplierGlobal;
String fortInfo = String.format( "(getToolFort: fort=%s%s) ",
iFmt.format( fortLevel ),
maxFort );
debugInfo.append( fortInfo );
return results;
}
// @Override
// public boolean doAction( PrisonMinesBlockBreakEvent pmEvent, StringBuilder debugInfo ) {
//
// return processAutoEvents( pmEvent, debugInfo );
// }
/**
* <p>This function overrides the doAction in OnBlockBreakEventListener and
* this is only enabled when auto manager is enabled.
* </p>
*
*/
@Override
public boolean doAction( PrisonMinesBlockBreakEvent pmEvent ) {
return applyAutoEvents( pmEvent );
}
// private boolean processAutoEvents( PrisonMinesBlockBreakEvent pmEvent, StringBuilder debugInfo ) {
// boolean cancel = false;
//
// if (isBoolean(AutoFeatures.isAutoManagerEnabled) && !pmEvent.getSpigotBlock().isEmpty() ) {
//
//
// debugInfo.append( "(doAction autoManager processAutoEvent single-block) ");
//
//
//// Output.get().logInfo( "#### AutoManager.applyAutoEvents: BlockBreakEvent: :: " + mine.getName() + " " +
//// " blocks remaining= " +
//// mine.getRemainingBlockCount() + " [" + block.toString() + "]"
//// );
//
//
// int count = applyAutoEventsDetails( pmEvent, debugInfo );
//
// if ( count > 0 ) {
// processBlockBreakage( pmEvent, count, true, debugInfo );
//
// cancel = true;
// }
//
// checkZeroBlockReset( pmEvent.getMine() );
//
// }
//
// return cancel;
// }
private int applyAutoEventsDetails( PrisonMinesBlockBreakEvent pmEvent ) {
int totalDrops = 0;
Player player = pmEvent.getPlayer();
Mine mine = pmEvent.getMine();
SpigotItemStack itemInHand = SpigotCompatibility.getInstance().getPrisonItemInMainHand( player );
boolean isLoreEnabled = isBoolean( AutoFeatures.isLoreEnabled );
boolean lorePickup = isLoreEnabled && checkLore( itemInHand, getMessage( AutoFeatures.lorePickupValue ) );
boolean loreSmelt = isLoreEnabled && checkLore( itemInHand, getMessage( AutoFeatures.loreSmeltValue) );
boolean loreBlock = isLoreEnabled && checkLore( itemInHand, getMessage( AutoFeatures.loreBlockValue ) );
boolean isAutoFeaturesEnabled = isBoolean( AutoFeatures.isAutoFeaturesEnabled );
String permAutoPickup = getMessage( AutoFeatures.permissionAutoPickup );
String permAutoSmelt = getMessage( AutoFeatures.permissionAutoSmelt );
String permAutoBlock = getMessage( AutoFeatures.permissionAutoBlock );
boolean permPickup = isAutoFeaturesEnabled &&
!"disable".equalsIgnoreCase( permAutoPickup ) &&
player.isPermissionSet( permAutoPickup );
boolean permSmelt = isAutoFeaturesEnabled &&
!"disable".equalsIgnoreCase( permAutoSmelt ) &&
player.isPermissionSet( permAutoSmelt );
boolean permBlock = isAutoFeaturesEnabled &&
!"disable".equalsIgnoreCase( permAutoBlock ) &&
player.isPermissionSet( permAutoBlock );
boolean configPickup = isAutoFeaturesEnabled && isBoolean( AutoFeatures.autoPickupEnabled );
boolean configSmelt = isAutoFeaturesEnabled && isBoolean( AutoFeatures.autoSmeltEnabled );
boolean configBlock = isAutoFeaturesEnabled && isBoolean( AutoFeatures.autoBlockEnabled );
boolean configNormalDrop = isBoolean( AutoFeatures.handleNormalDropsEvents );
boolean configNormalDropSmelt = isBoolean( AutoFeatures.normalDropSmelt );
boolean configNormalDropBlock = isBoolean( AutoFeatures.normalDropBlock );
boolean limit2minesPickup = isBoolean( AutoFeatures.pickupLimitToMines );
boolean limit2minesSmelt = isBoolean( AutoFeatures.smeltLimitToMines );
boolean limit2minesBlock = isBoolean( AutoFeatures.blockLimitToMines );
boolean isAutoPickup = lorePickup || configPickup || permPickup;
isAutoPickup = (mine != null || mine == null && !limit2minesPickup) && isAutoPickup;
boolean isAutoSmelt = loreSmelt || configSmelt || permSmelt;
isAutoSmelt = (mine != null || mine == null && !limit2minesSmelt) && isAutoSmelt;
boolean isAutoBlock = loreBlock || configBlock || permBlock;
isAutoBlock = (mine != null || mine == null && !limit2minesBlock) && isAutoBlock;
if ( Output.get().isDebug( DebugTarget.blockBreak ) ) {
pmEvent.getDebugInfo().append( "{br}|| (applyAutoEvents: " )
.append( pmEvent.getSpigotBlock().getBlockName() );
if ( !isAutoFeaturesEnabled ) {
pmEvent.getDebugInfo().append(" isAutoFeaturesEnabled=false (");
pmEvent.getDebugInfo().append( Output.get().getColorCodeError() );
pmEvent.getDebugInfo().append("disabled");
pmEvent.getDebugInfo().append( Output.get().getColorCodeDebug() );
pmEvent.getDebugInfo().append(")");
}
else {
pmEvent.getDebugInfo()
.append( " Pickup [")
.append( isAutoPickup ? "enabled: " : "disabled:" )
.append( lorePickup ? "lore " : "" )
.append( permPickup ? "perm " : "" )
.append( configPickup ? "config " : "" )
.append( limit2minesPickup ? "mines" : "noLimit" )
.append( "] ")
.append( " Smelt [")
.append( isAutoSmelt ? "enabled: " : "disabled:" )
.append( loreSmelt ? "lore " : "" )
.append( permSmelt ? "perm " : "" )
.append( configSmelt ? "config " : "" )
.append( limit2minesSmelt ? "mines" : "noLimit" )
.append( "] ")
.append( " Block [")
.append( isAutoBlock ? "enabled: " : "disabled:" )
.append( loreBlock ? "lore " : "" )
.append( permBlock ? "perm " : "" )
.append( configBlock ? "config " : "" )
.append( limit2minesBlock ? "mines" : "noLimit" )
.append( "] ");
}
pmEvent.getDebugInfo()
.append( ")" );
}
// NOTE: Using isPermissionSet so players that are op'd to not auto enable everything.
// Ops will have to have the perms set to actually use them.
// AutoPickup
if ( (mine != null || mine == null && !isBoolean( AutoFeatures.pickupLimitToMines )) ) {
if ( isAutoPickup ) {
// processing auto pickup
totalDrops = autoFeaturePickup( pmEvent, isAutoSmelt, isAutoBlock, pmEvent.getDebugInfo() );
// count = autoFeaturePickup( pmEvent.getSpigotBlock(), player, itemInHand, isAutoSmelt, isAutoBlock, debugInfo );
// Cannot set to air yet, or auto smelt and auto block will only get AIR:
// autoPickupCleanup( block, count );
}
else {
// Need to check to see if normal drops should be processed:
if ( configNormalDrop ) {
pmEvent.getDebugInfo()
.append( "{br}|| (NormalDrop handling enabled: " )
.append( "normalDropSmelt[" )
.append( configNormalDropSmelt ? "enabled" : "disabled" )
.append( "] " )
.append( "normalDropBlock[" )
.append( configNormalDropBlock ? "enabled" : "disabled" )
.append( "] " )
.append( ")" );
// process normal drops here:
totalDrops = calculateNormalDrop( pmEvent );
}
else {
pmEvent.getDebugInfo().append(" [Warning: normalDrop handling is disabled] " );
}
}
}
// else {
//
// // Warning!! The following should NOT happen if auto pickup is disabled. That's what
// //
//
// // Because auto pickup is not enabled, auto smelt and auto block cannot be applied through
// // the picked up item stacks. Therefore, if auto smelt or auto block is enabled with
// // auto pickup being disabled, they will act on the player's full inventory.
//
// // So breaking the block could cause a drop but the drop may not enter the players inventory
// // to be included in the smelt or block processing. Therefore odd behaviors could be seen.
// // But having those enabled without auto pickup is beyond the scope of support for prison...
// // you get what you got.
//
// XMaterial source = SpigotUtil.getXMaterial( block.getPrisonBlock() );
//
// // AutoSmelt
// if ( (mine != null || mine == null && !isBoolean( AutoFeatures.autoSmeltLimitToMines )) &&
// isAutoSmelt ){
//
// // Smelting needs to change the source to the smelted target so auto block will work:
// source = autoFeatureSmelt( player, source );
// }
//
// // AutoBlock
// if ( (mine != null || mine == null && !isBoolean( AutoFeatures.autoBlockLimitToMines )) &&
// isAutoBlock ) {
//
// autoFeatureBlock( player, source );
// }
//
// }
// // AutoPickup - Clean up (set block to air)
// if ( (mine != null || mine == null && !isBoolean( AutoFeatures.pickupLimitToMines )) &&
// isAutoPickup ) {
//
// autoPickupCleanup( pmEvent.getSpigotBlock(), count );
//
// }
return totalDrops;
}
/**
* <p>This function gets called for EACH block that is impacted by the
* explosion event. The event may have have a list of blocks, but not all
* blocks may be included in the mine. This function is called ONLY when
* a block is within a mine,
* </p>
*
* <p>The event TEBlockExplodeEvent has already taken place and it handles all
* actions such as auto pickup, auto smelt, and auto block. The only thing we
* need to do is to record the number of blocks that were removed during
* the explosion event. The blocks should have been replaced by air.
* </p>
*
* <p>Normally, prison based auto features, and or the perms need to be enabled
* for the auto features to be enabled, but since this is originating from another
* plugin and another external configuration, we cannot test for any real perms
* or configs. The fact that this event happens within one of the mines is
* good enough, and must be counted.
* </p>
*
* @param e
* @param mine
*/
private boolean applyAutoEvents( PrisonMinesBlockBreakEvent pmEvent ) {
// boolean success = false;
int totalDrops = applyAutoEventsDetails( pmEvent );
pmEvent.getDebugInfo().append( "(autoEvents totalDrops: " + totalDrops + ") ");
return applyDropsBlockBreakage( pmEvent, totalDrops );
// debugInfo.append( "(doAction autoManager applyAutoEvents multi-blocks: " + pmEvent.getExplodedBlocks().size() + ") ");
//
//
// // The explodedBlocks list have already been validated as being within the mine:
// boolean applyExhaustion = true;
//
// // First process the block that was hit by the player:
// if ( pmEvent.getTargetBlock() != null ) {
//
// processBlockBreakage( pmEvent, pmEvent.getTargetBlock(), 1, applyExhaustion, debugInfo );
// applyExhaustion = false;
//
// }
//
// // Then process all of the other blocks that were included in the explosion event, if there was one:
// for ( MineTargetPrisonBlock targetBlock : pmEvent.getTargetExplodedBlocks() )
// {
//
// processBlockBreakage( pmEvent, targetBlock, 1, applyExhaustion, debugInfo );
// }
//
// autosellPerBlockBreak( pmEvent.getPlayer() );
//
// if ( pmEvent.getMine() != null ) {
// checkZeroBlockReset( pmEvent.getMine() );
// }
//
// if ( totalDrops > 0 ) {
// success = true;
// }
//
// return success;
}
/*
* Drops are wrong with old 1.14.4 releases of spigot
* but got fixed in newer versions.
*
* For older versions, a good way to get the right drops would be to use
* BlockDropItemEvent.getItems(), but it's deprecated
* */
protected int autoPickup( PrisonMinesBlockBreakEvent pmEvent,
boolean isAutoSmelt, boolean isAutoBlock, StringBuilder debugInfo ) {
//, BlockBreakEvent e ) {
int count = 0;
Player player = pmEvent.getPlayer();
SpigotItemStack itemInHand = pmEvent.getItemInHand();
// SpigotBlock block = pmEvent.getSpigotBlock();
// Calculate silkTouch drops before processing drops:
calculateSilkTouch( pmEvent );
List<SpigotItemStack> drops = pmEvent.getBukkitDrops();
// The following may not be the correct drops for all versions of spigot,
// plus there are some extra items, such as flint, that will never be dropped.
// List<SpigotItemStack> drops = new ArrayList<>( SpigotUtil.getDrops(block, itemInHand) );
// This clears the drops for the given block, so if the event is not canceled, it will
// not result in duplicate drops.
// if ( isBoolean( AutoFeatures.cancelAllBlockEventBlockDrops ) ) {
// block.clearDrops();
// }
if (drops != null && drops.size() > 0 ) {
DecimalFormat drFmt = Prison.get().getDecimalFormat("#,##0.0000");
double bukkitDropsMultiplier =
isBoolean( AutoFeatures.isCalculateFortuneEnabled ) ?
getDouble( AutoFeatures.fortuneBukkitDropsMultiplier ) :
1.0d;
StringBuilder sb = new StringBuilder();
for (SpigotItemStack sItemStack : drops) {
if ( sb.length() > 0 ) {
sb.append( "," );
}
int amtBukkit = sItemStack.getAmount();
int amt = (int) (amtBukkit * bukkitDropsMultiplier);
if ( amt < 1 ) {
amt = 1;
}
sb.append( sItemStack.getName() ).append( ":" )
.append( amt );
if ( amt != amtBukkit ) {
sItemStack.setAmount( amt );
sb.append( "(bukkitAmt:").append( amtBukkit ).append( ")" );
}
}
if ( bukkitDropsMultiplier != 1.0d ) {
sb.insert( 0, ": " );
sb.insert( 0, drFmt.format( bukkitDropsMultiplier) );
sb.insert( 0, "bukkitDropMult=" );
}
debugInfo.append( " [autoPickupDrops:beforeFortune:: " ).append( sb ).append( "] ");
// Need better drop calculation that is not using the getDrops function.
// calculateSilkTouch( pmEvent, itemInHand, drops );
// Adds in additional drop items: Add Flint with gravel drops:
calculateDropAdditions( itemInHand, drops, pmEvent.getDebugInfo() );
// Add fortune to the items in the inventory
if ( isBoolean( AutoFeatures.isCalculateFortuneEnabled ) ) {
sb.setLength(0);
int fortuneLevel = getFortune(itemInHand, debugInfo );
// debugInfo.append( "(calculateFortune: fort " + fortuneLevel + ")" );
for ( SpigotItemStack itemStack : drops ) {
if ( sb.length() > 0 ) {
sb.append( "," );
}
// calculateFortune directly modifies the quantity on the blocks ItemStack:
calculateFortune( itemStack, fortuneLevel, pmEvent.getDebugInfo() );
sb.append( itemStack.getName() )
.append( ":" )
.append( itemStack.getAmount() );
}
debugInfo.append( " [totalDrops:afterFortune:: " ).append( sb ).append( "] ");
}
// NOTE: This should be done after applying fortune, otherwise it will get misreadings.
// Merge drops so each item is only represented once before adding to the player's inventory
drops = mergeDrops( drops );
// Smelt
if ( isAutoSmelt ) {
debugInfo.append( "(autoSmelting: drops)" );
normalDropSmelt( drops );
}
// Block
if ( isAutoBlock ) {
debugInfo.append( "(autoBlocking: drops)" );
normalDropBlock( drops );
}
String mineName = pmEvent.getMine() == null ? null : pmEvent.getMine().getName();
// PlayerCache log block breaks:
// TreeMap<String, Integer> targetBlockCounts = pmEvent.getTargetBlockCounts();
// for ( Entry<String, Integer> targetBlockCount : targetBlockCounts.entrySet() )
// {
//
// PlayerCache.getInstance().addPlayerBlocks( pmEvent.getSpigotPlayer(), mineName,
// targetBlockCount.getKey(), targetBlockCount.getValue().intValue() );
// }
DecimalFormat fFmt = Prison.get().getDecimalFormat("#,##0.0000");
DecimalFormat dFmt = Prison.get().getDecimalFormat("#,##0.00");
double autosellTotal = 0;
double autosellUnsellableCount = 0;
long nanoTime = 0L;
boolean isSellallEnabled = SpigotPrison.getInstance().isSellAllEnabled();
// boolean isPlayerAutoSellTurnedOff = SellAllUtil.get().isAutoSellPerUserToggleable &&
// !SellAllUtil.get().isSellallPlayerUserToggleEnabled(
// pmEvent.getSpigotPlayer().getWrapper() );
//
// if ( isPlayerAutoSellTurnedOff ) {
// debugInfo.append( Output.get().getColorCodeWarning() );
// debugInfo.append( "(Player toggled off autosell) " );
// debugInfo.append( Output.get().getColorCodeDebug() );
// }
//
// // This will return true (allow autosell) unless players can toggle autosell and they turned it off:
// // This is to be used with other auto sell setting, but never on it's own:
// boolean isPlayerAutosellEnabled =
// isSellallEnabled &&
// (!SellAllUtil.get().isAutoSellPerUserToggleable ||
// SellAllUtil.get().isSellallPlayerUserToggleEnabled(
// pmEvent.getSpigotPlayer().getWrapper() ));
// isAutoSellEnabled should include perms check too:
boolean isPlayerAutosellEnabled = pmEvent.getSpigotPlayer().isAutoSellEnabled( pmEvent.getDebugInfo() );
// In the event, forceAutoSell is enabled, which means the drops must be sold.
// The player's toggle cannot disable this.
boolean forceAutoSell = isSellallEnabled && pmEvent.isForceAutoSell();
// AutoFeature's autosell per block break - global setting
boolean autoSellBySettings =
isPlayerAutosellEnabled &&
isBoolean(AutoFeatures.isAutoSellPerBlockBreakEnabled);
// AutoFeature's autosell per block break - per player perms setting
// boolean autoSellByPerm =
// isPlayerAutosellEnabled &&
// !player.isOp() &&
// isBoolean(AutoFeatures.isAutoSellPerBlockBreakEnabled) &&
// !"disable".equalsIgnoreCase( getMessage( AutoFeatures.permissionAutoSellPerBlockBreakEnabled ) ) &&
// !"false".equalsIgnoreCase( getMessage( AutoFeatures.permissionAutoSellPerBlockBreakEnabled ) ) &&
// player.hasPermission( getMessage( AutoFeatures.permissionAutoSellPerBlockBreakEnabled ) );
// boolean autoSellByPerm = pmEvent.getSpigotPlayer().isAutoSellByPermEnabled( isPlayerAutosellEnabled, pmEvent.getDebugInfo() );
// Try to autosell if enabled in any of the following ways:
boolean autoSell = ( forceAutoSell || autoSellBySettings );
// boolean autoSell = ( forceAutoSell || autoSellBySettings || autoSellByPerm );
for ( SpigotItemStack itemStack : drops ) {
count += itemStack.getAmount();
// Try to autosell if enabled in any of the following ways:
if ( autoSell ) {
// // Try to autosell if enabled:
// if ( isSellallEnabled &&
//
// (isBoolean(AutoFeatures.isAutoSellPerBlockBreakEnabled) &&
// isPlayerAutosellEnabled ||
// pmEvent.isForceAutoSell() ||
// !player.isOp() && !"disable".equalsIgnoreCase( getMessage( AutoFeatures.permissionAutoSellPerBlockBreakEnabled ) ) &&
// player.hasPermission( getMessage( AutoFeatures.permissionAutoSellPerBlockBreakEnabled ) )) &&
// isPlayerAutosellEnabled ) {
final long nanoStart = System.nanoTime();
double amount = SellAllUtil.get().sellAllSell( player, itemStack, false, false, true );
final long nanoStop = System.nanoTime();
nanoTime += nanoStop - nanoStart;
autosellTotal += amount;
PlayerCache.getInstance().addPlayerEarnings( pmEvent.getSpigotPlayer(),
amount, mineName );
if ( amount != 0 ) {
debugInfo.append( "(sold: " + itemStack.getName() + " qty: " + itemStack.getAmount() +
" value: " + dFmt.format( amount ) + ") ");
// Set to zero quantity since they have all been sold.
itemStack.setAmount( 0 );
}
else {
// Unable to sell since amount was zero. Not configured to be sold.
pmEvent.setDebugColorCodeWarning();
debugInfo.append( "(unsellable: " + itemStack.getName() + " qty: " + itemStack.getAmount() + ") ");
pmEvent.setDebugColorCodeDebug();
autosellUnsellableCount += itemStack.getAmount();
}
}
// Add blocks to player's inventory IF autosell was unable to sell the item stack, hence