4949import world .bentobox .bentobox .BentoBox ;
5050import world .bentobox .bentobox .api .events .BentoBoxReadyEvent ;
5151import world .bentobox .bentobox .api .events .island .IslandCreatedEvent ;
52+ import world .bentobox .bentobox .api .events .island .IslandDeleteEvent ;
5253import world .bentobox .bentobox .api .events .island .IslandResettedEvent ;
5354import world .bentobox .bentobox .database .Database ;
5455import world .bentobox .bentobox .database .objects .Island ;
@@ -110,7 +111,10 @@ public class NewAreaListener implements Listener {
110111 private static String pluginPackageName ;
111112
112113 /**
113- * @param addon addon
114+ * Constructor for NewAreaListener.
115+ * Initializes structure files, databases, and starts the structure printer.
116+ *
117+ * @param addon The Boxed addon instance.
114118 */
115119 public NewAreaListener (Boxed addon ) {
116120 this .addon = addon ;
@@ -151,7 +155,8 @@ private void runStructurePrinter() {
151155 }
152156
153157 /**
154- * Build something in the queue. Structures are built one by one
158+ * Builds a structure from the queue if not already pasting.
159+ * Only one structure is built at a time.
155160 */
156161 private void buildStructure () {
157162 // Only kick off a build if there is something to build and something isn't
@@ -163,6 +168,11 @@ private void buildStructure() {
163168 }
164169 }
165170
171+ /**
172+ * Places a structure at the specified location and updates the island structure cache.
173+ *
174+ * @param item The structure record to place.
175+ */
166176 private void placeStructure (StructureRecord item ) {
167177 // Set the semaphore - only paste one at a time
168178 pasting = true ;
@@ -192,9 +202,7 @@ private void placeStructure(StructureRecord item) {
192202 }
193203
194204 /**
195- * Load known structures from the templates file. This makes them available for
196- * admins to use in the boxed place file. If this is not done, then no
197- * structures (templates) will be available.
205+ * Loads known structures from the templates file and registers them with the server.
198206 *
199207 * @param event BentoBoxReadyEvent
200208 */
@@ -217,7 +225,8 @@ public void onBentoBoxReady(BentoBoxReadyEvent event) {
217225 }
218226
219227 /**
220- * Add items to the queue when they are needed due to chunk loading
228+ * Adds items to the build queue when their chunk is loaded.
229+ *
221230 * @param e ChunkLoadEvent
222231 */
223232 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
@@ -281,10 +290,10 @@ public void onPlayerMove(PlayerMoveEvent e) {
281290 }
282291
283292 /**
284- * Gives a player all the advancements that have string as a named criteria
293+ * Gives a player all advancements that have the specified string as a named criteria.
285294 *
286- * @param player - player
287- * @param string - criteria
295+ * @param player The player to award.
296+ * @param string The advancement criteria string.
288297 */
289298 private void giveAdvFromCriteria (Player player , String string ) {
290299 // Give every advancement that requires a bastion
@@ -303,10 +312,10 @@ private void giveAdvFromCriteria(Player player, String string) {
303312 }
304313
305314 /**
306- * Get all the known island structures for this island
315+ * Gets all known island structures for the specified island.
307316 *
308- * @param islandId - island ID
309- * @return IslandStructures
317+ * @param islandId The island ID.
318+ * @return IslandStructures for the island.
310319 */
311320 private IslandStructures getIslandStructData (String islandId ) {
312321 // Return from cache if it exists
@@ -320,16 +329,66 @@ private IslandStructures getIslandStructData(String islandId) {
320329 return struct ;
321330 }
322331
332+ /**
333+ * Handles island creation event and sets up structures for the new island.
334+ *
335+ * @param event IslandCreatedEvent
336+ */
323337 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
324338 public void onIslandCreated (IslandCreatedEvent event ) {
325339 setUpIsland (event .getIsland ());
326340 }
327341
342+ /**
343+ * Handles island reset event and sets up structures for the reset island.
344+ *
345+ * @param event IslandResettedEvent
346+ */
328347 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
329348 public void onIslandReset (IslandResettedEvent event ) {
330349 setUpIsland (event .getIsland ());
331350 }
332351
352+ /**
353+ * Handles island deletion event and removes all pending structures for the deleted island.
354+ *
355+ * @param event IslandDeletedEvent
356+ */
357+ @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
358+ public void onIslandDeleted (IslandDeleteEvent event ) {
359+ String deletedIslandId = event .getIsland ().getUniqueId ();
360+
361+ // Remove from in-memory cache
362+ islandStructureCache .remove (deletedIslandId );
363+
364+ // Remove from in-memory pending structures
365+ for (List <StructureRecord > records : pending .values ()) {
366+ records .removeIf (record -> event .getIsland ().inIslandSpace (record .location ()));
367+ }
368+ pending .values ().removeIf (list -> list .isEmpty ());
369+
370+ // Remove from pending structures in database
371+ Map <Pair <Integer , Integer >, List <StructureRecord >> readyToBuild = loadToDos ().getReadyToBuild ();
372+ boolean dbChanged = false ;
373+ for (List <StructureRecord > records : readyToBuild .values ()) {
374+ if (records .removeIf (record -> event .getIsland ().inIslandSpace (record .location ()))) {
375+ dbChanged = true ;
376+ }
377+ }
378+
379+ // Save updated pending structures if any were removed
380+ if (dbChanged ) {
381+ ToBePlacedStructures tbd = new ToBePlacedStructures ();
382+ tbd .setReadyToBuild (readyToBuild );
383+ toPlace .saveObjectAsync (tbd );
384+ }
385+ }
386+
387+ /**
388+ * Sets up structures for the given island based on the configuration.
389+ *
390+ * @param island The island to set up.
391+ */
333392 private void setUpIsland (Island island ) {
334393 // Check if this island is in this game
335394 if (!(addon .inWorld (island .getWorld ()))) {
@@ -350,6 +409,13 @@ private void setUpIsland(Island island) {
350409
351410 }
352411
412+ /**
413+ * Places structures defined in the configuration section at the specified center location.
414+ *
415+ * @param section The configuration section.
416+ * @param center The center location.
417+ * @param env The world environment.
418+ */
353419 private void place (ConfigurationSection section , Location center , Environment env ) {
354420 if (section == null ) {
355421 return ;
@@ -412,11 +478,10 @@ private void place(ConfigurationSection section, Location center, Environment en
412478 }
413479
414480 /**
415- * Removes Jigsaw blocks from a placed structure. Fills underwater ruins with
416- * water.
481+ * Removes Jigsaw blocks from a placed structure and fills underwater ruins with water.
417482 *
418- * @param item - record of what's required
419- * @return the resulting bounding box of the structure
483+ * @param item The structure record.
484+ * @return The resulting bounding box of the structure.
420485 */
421486 public static BoundingBox removeJigsaw (StructureRecord item ) {
422487 Location loc = item .location ();
@@ -466,12 +531,9 @@ public static BoundingBox removeJigsaw(StructureRecord item) {
466531 }
467532
468533 /**
469- * Process a structure block. Sets it to a structure void at a minimum. If the
470- * structure block has metadata indicating it is a chest, then it will fill the
471- * chest with a buried treasure loot. If it is waterlogged, then it will change
472- * the void to water.
534+ * Processes a structure block, possibly spawning entities or filling chests with loot.
473535 *
474- * @param b structure block
536+ * @param b The structure block.
475537 */
476538 private static void processStructureBlock (Block b ) {
477539 // I would like to read the data from the block and do something with it!
@@ -499,6 +561,13 @@ private static void processStructureBlock(Block b) {
499561 }
500562 }
501563
564+ /**
565+ * Processes a jigsaw block, setting its final state and spawning mobs if needed.
566+ *
567+ * @param b The jigsaw block.
568+ * @param structureRotation The structure's rotation.
569+ * @param pasteMobs Whether to spawn mobs.
570+ */
502571 private static void processJigsaw (Block b , StructureRotation structureRotation , boolean pasteMobs ) {
503572 try {
504573 String data = nmsData (b );
@@ -517,6 +586,12 @@ private static void processJigsaw(Block b, StructureRotation structureRotation,
517586 }
518587 }
519588
589+ /**
590+ * Spawns a mob at the specified block based on the jigsaw block's pool.
591+ *
592+ * @param b The block.
593+ * @param bjb The BoxedJigsawBlock data.
594+ */
520595 private static void spawnMob (Block b , BoxedJigsawBlock bjb ) {
521596 // bjb.getPool contains a lot more than just mobs, so we have to filter it to
522597 // see if any mobs are in there. This list may need to grow in the future
@@ -555,12 +630,11 @@ private static void spawnMob(Block b, BoxedJigsawBlock bjb) {
555630 }
556631
557632 /**
558- * Corrects the direction of a block based on the structure's rotation
633+ * Corrects the direction of a block based on the structure's rotation.
559634 *
560- * @param finalState - the final block state of the block, which may include a
561- * facing: direction
562- * @param sr - the structure's rotation
563- * @return a rewritten blockstate with the updated direction, if required
635+ * @param finalState The final block state.
636+ * @param sr The structure's rotation.
637+ * @return The corrected block state string.
564638 */
565639 private static String correctDirection (String finalState , StructureRotation sr ) {
566640 if (sr .equals (StructureRotation .NONE )) {
@@ -579,11 +653,11 @@ private static String correctDirection(String finalState, StructureRotation sr)
579653 }
580654
581655 /**
582- * Adjusts the direction based on the StructureRotation
656+ * Adjusts the direction based on the StructureRotation.
583657 *
584- * @param oldDirection the old direction to adjust
585- * @param sr the structure rotation
586- * @return the new direction, or SELF if something weird happens
658+ * @param oldDirection The old direction.
659+ * @param sr The structure rotation.
660+ * @return The new direction, or SELF if not applicable.
587661 */
588662 private static BlockFace getNewDirection (BlockFace oldDirection , StructureRotation sr ) {
589663 if (sr .equals (StructureRotation .CLOCKWISE_180 )) {
@@ -609,16 +683,22 @@ private static BlockFace getNewDirection(BlockFace oldDirection, StructureRotati
609683 }
610684
611685 /**
612- * Looks for north, south, east, west in the blockstate.
686+ * Looks for north, south, east, or west in the blockstate string .
613687 *
614- * @param finalState - the final block state of the block
615- * @return direction, if found, otherwise SELF
688+ * @param finalState The final block state string.
689+ * @return The detected direction, or SELF if not found.
616690 */
617691 private static BlockFace getDirection (String finalState ) {
618692 return CARDINALS .stream ().filter (bf -> finalState .contains (bf .name ().toLowerCase (Locale .ENGLISH ))).findFirst ()
619693 .orElse (BlockFace .SELF );
620694 }
621695
696+ /**
697+ * Gets NMS data from a block using the appropriate handler.
698+ *
699+ * @param block The block.
700+ * @return The NMS data string.
701+ */
622702 private static String nmsData (Block block ) {
623703 AbstractMetaData handler ;
624704 try {
@@ -635,6 +715,11 @@ private static String nmsData(Block block) {
635715 return handler .nmsData (block );
636716 }
637717
718+ /**
719+ * Loads the list of structures to be placed from the database.
720+ *
721+ * @return ToBePlacedStructures instance.
722+ */
638723 private ToBePlacedStructures loadToDos () {
639724 if (!toPlace .objectExists (TODO )) {
640725 return new ToBePlacedStructures ();
0 commit comments