57
57
import world .bentobox .aoneblock .oneblocks .OneBlocksManager ;
58
58
import world .bentobox .aoneblock .oneblocks .Requirement ;
59
59
import world .bentobox .bank .Bank ;
60
+ import world .bentobox .bentobox .api .events .BentoBoxReadyEvent ;
60
61
import world .bentobox .bentobox .api .events .island .IslandCreatedEvent ;
61
62
import world .bentobox .bentobox .api .events .island .IslandDeleteEvent ;
62
63
import world .bentobox .bentobox .api .events .island .IslandResettedEvent ;
@@ -174,6 +175,23 @@ public BlockListener(@NonNull AOneBlock addon) {
174
175
oneBlocksManager = addon .getOneBlockManager ();
175
176
}
176
177
178
+ /**
179
+ * This cleans up the cache files in the database and removed old junk.
180
+ * This is to address a bug introduced 2 years ago that caused non AOneBlock islands
181
+ * to be stored in the AOneBlock database. This should be able to be
182
+ * removed in the future.
183
+ * @deprecated will be removed in the future
184
+ * @param e event
185
+ */
186
+ @ Deprecated (since = "1.12.3" , forRemoval = true )
187
+ @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
188
+ private void cleanCache (BentoBoxReadyEvent e ) {
189
+ handler .loadObjects ().forEach (i ->
190
+ addon .getIslandsManager ().getIslandById (i .getUniqueId ())
191
+ .filter (is -> !addon .inWorld (is .getWorld ()) || is .isUnowned ())
192
+ .ifPresent (is -> handler .deleteID (is .getUniqueId ())));
193
+ }
194
+
177
195
/**
178
196
* Save the island cache
179
197
*/
@@ -182,9 +200,9 @@ public void saveCache() {
182
200
}
183
201
184
202
185
- // ---------------------------------------------------------------------
186
- // Section: Listeners
187
- // ---------------------------------------------------------------------
203
+ // ---------------------------------------------------------------------
204
+ // Section: Listeners
205
+ // ---------------------------------------------------------------------
188
206
189
207
190
208
@ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
@@ -290,8 +308,8 @@ public void onItemStackSpawn(EntitySpawnEvent event)
290
308
Location location = event .getLocation ();
291
309
292
310
Optional <Island > optionalIsland = this .addon .getIslands ().
293
- getIslandAt (location ).
294
- filter (island -> location .getBlock ().getLocation ().equals (island .getCenter ()));
311
+ getIslandAt (location ).
312
+ filter (island -> location .getBlock ().getLocation ().equals (island .getCenter ()));
295
313
296
314
if (optionalIsland .isPresent ())
297
315
{
@@ -302,9 +320,9 @@ public void onItemStackSpawn(EntitySpawnEvent event)
302
320
}
303
321
304
322
305
- // ---------------------------------------------------------------------
306
- // Section: Processing methods
307
- // ---------------------------------------------------------------------
323
+ // ---------------------------------------------------------------------
324
+ // Section: Processing methods
325
+ // ---------------------------------------------------------------------
308
326
309
327
310
328
private void setUp (@ NonNull Island island ) {
@@ -488,24 +506,34 @@ private void playWarning(@NonNull OneBlockIslands is, @NonNull Block block) {
488
506
* @return true if this is a new phase, false if not
489
507
*/
490
508
private boolean checkPhase (@ Nullable Player player , @ NonNull Island i , @ NonNull OneBlockIslands is , @ NonNull OneBlockPhase phase ) {
509
+ // Handle NPCs
510
+ User user ;
511
+ if (player == null || player .hasMetadata ("NPC" )) {
512
+ // Default to the owner
513
+ user = addon .getPlayers ().getUser (i .getOwner ());
514
+ } else {
515
+ user = User .getInstance (player );
516
+ }
517
+
491
518
String phaseName = phase .getPhaseName () == null ? "" : phase .getPhaseName ();
492
519
if (!is .getPhaseName ().equalsIgnoreCase (phaseName )) {
493
520
// Run previous phase end commands
494
521
oneBlocksManager .getPhase (is .getPhaseName ()).ifPresent (oldPhase -> {
495
522
String oldPhaseName = oldPhase .getPhaseName () == null ? "" : oldPhase .getPhaseName ();
496
- Util .runCommands (User . getInstance ( player ) ,
523
+ Util .runCommands (user ,
497
524
replacePlaceholders (player , oldPhaseName , phase .getBlockNumber (), i , oldPhase .getEndCommands ()),
498
525
"Commands run for end of " + oldPhaseName );
499
526
});
500
527
// Set the phase name
501
528
is .setPhaseName (phaseName );
502
- if (player != null ) {
503
- player .sendTitle (phaseName , null , -1 , -1 , -1 );
504
- // Run phase start commands
505
- Util .runCommands (User .getInstance (player ),
506
- replacePlaceholders (player , phaseName , phase .getBlockNumber (), i , phase .getStartCommands ()),
507
- "Commands run for start of " + phaseName );
529
+ if (user .isPlayer () && user .isOnline () && addon .inWorld (user .getWorld ())) {
530
+ user .getPlayer ().sendTitle (phaseName , null , -1 , -1 , -1 );
508
531
}
532
+ // Run phase start commands
533
+ Util .runCommands (user ,
534
+ replacePlaceholders (player , phaseName , phase .getBlockNumber (), i , phase .getStartCommands ()),
535
+ "Commands run for start of " + phaseName );
536
+
509
537
saveIsland (i );
510
538
return true ;
511
539
}
@@ -644,7 +672,7 @@ private void makeSpace(@NonNull Entity entity, @NonNull Location spawnLocation)
644
672
// Make space for entity based on the entity's size
645
673
final BoundingBox boundingBox = entity .getBoundingBox ();
646
674
final boolean isWaterProtected = this .addon .getSettings ().isWaterMobProtection () &&
647
- WATER_ENTITIES .contains (entity .getType ());
675
+ WATER_ENTITIES .contains (entity .getType ());
648
676
649
677
for (double y = boundingBox .getMinY (); y <= Math .min (boundingBox .getMaxY (), world .getMaxHeight ()); y ++)
650
678
{
@@ -673,9 +701,9 @@ private void makeSpace(@NonNull Entity entity, @NonNull Location spawnLocation)
673
701
for (double z = boundingBox .getMinZ () - 0.5 ; z < boundingBox .getMaxZ () + 0.5 ; z ++)
674
702
{
675
703
block = world .getBlockAt (new Location (world ,
676
- x ,
677
- y ,
678
- z ));
704
+ x ,
705
+ y ,
706
+ z ));
679
707
680
708
// Check if block should be marked.
681
709
this .checkBlock (block , boundingBox , isWaterProtected , airBlocks , waterBlocks );
@@ -688,9 +716,9 @@ else if (boundingBox.getWidthX() > 1)
688
716
for (double x = boundingBox .getMinX () - 0.5 ; x < boundingBox .getMaxX () + 0.5 ; x ++)
689
717
{
690
718
block = world .getBlockAt (new Location (world ,
691
- x ,
692
- y ,
693
- spawnLocation .getZ ()));
719
+ x ,
720
+ y ,
721
+ spawnLocation .getZ ()));
694
722
695
723
// Check if block should be marked.
696
724
this .checkBlock (block , boundingBox , isWaterProtected , airBlocks , waterBlocks );
@@ -702,9 +730,9 @@ else if (boundingBox.getWidthZ() > 1)
702
730
for (double z = boundingBox .getMinZ () - 0.5 ; z < boundingBox .getMaxZ () + 0.5 ; z ++)
703
731
{
704
732
block = world .getBlockAt (new Location (world ,
705
- spawnLocation .getX (),
706
- y ,
707
- z ));
733
+ spawnLocation .getX (),
734
+ y ,
735
+ z ));
708
736
709
737
// Check if block should be marked.
710
738
this .checkBlock (block , boundingBox , isWaterProtected , airBlocks , waterBlocks );
@@ -749,10 +777,10 @@ else if (boundingBox.getWidthZ() > 1)
749
777
* @param waterBlocks List of water blocks.
750
778
*/
751
779
private void checkBlock (Block block ,
752
- BoundingBox boundingBox ,
753
- boolean isWaterEntity ,
754
- List <Block > airBlocks ,
755
- List <Block > waterBlocks )
780
+ BoundingBox boundingBox ,
781
+ boolean isWaterEntity ,
782
+ List <Block > airBlocks ,
783
+ List <Block > waterBlocks )
756
784
{
757
785
// Check if block should be marked for destroying.
758
786
if (block .getBoundingBox ().overlaps (boundingBox ))
@@ -815,6 +843,13 @@ public OneBlockIslands getIsland(@NonNull Island i) {
815
843
return cache .containsKey (i .getUniqueId ()) ? cache .get (i .getUniqueId ()) : loadIsland (i .getUniqueId ());
816
844
}
817
845
846
+ /**
847
+ * Get all the OneBlockIslands from the Database
848
+ * @return list of oneblock islands
849
+ */
850
+ public List <OneBlockIslands > getAllIslands () {
851
+ return handler .loadObjects ();
852
+ }
818
853
819
854
@ NonNull
820
855
private OneBlockIslands loadIsland (@ NonNull String uniqueId ) {
0 commit comments