diff --git a/src/main/java/wtf/beatrice/limbomanager/listeners/PlayerTeleporter.java b/src/main/java/wtf/beatrice/limbomanager/listeners/PlayerTeleporter.java index 3d9bd16..bf07568 100644 --- a/src/main/java/wtf/beatrice/limbomanager/listeners/PlayerTeleporter.java +++ b/src/main/java/wtf/beatrice/limbomanager/listeners/PlayerTeleporter.java @@ -32,6 +32,8 @@ import wtf.beatrice.limbomanager.utils.LocationUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.UUID; public class PlayerTeleporter implements Listener { @@ -76,7 +78,6 @@ public class PlayerTeleporter implements Listener // configure here .build(); Operations.complete(operation); - editSession.flushSession(); } catch (WorldEditException e) { throw new RuntimeException(e); } @@ -116,22 +117,40 @@ public class PlayerTeleporter implements Listener islandSpawnLocation.getWorld().getMaxHeight(), islandSpawnLocation.getZ() + maxRange); + // announce area cleanup LimboManager.getInstance().getLogger().info("Player " + playerName + " quit, clearing region..."); + // WorldEdit API integration for faster area cleanup World weWorld = BukkitAdapter.adapt(islandSpawnLocation.getWorld()); try(EditSession editSession = WorldEdit.getInstance().newEditSession(weWorld)) { + // we only need to set everything to air BlockState air = BukkitAdapter.adapt(Material.AIR.createBlockData()); + // convert our area borders to WE blockvectors BlockVector3 min = BlockVector3.at(startFrom.getBlockX(), startFrom.getBlockY(), startFrom.getBlockZ()); BlockVector3 max = BlockVector3.at(endAt.getBlockX(), endAt.getBlockY(), endAt.getBlockZ()); + + // create a cuboid region CuboidRegion cuboidRegion = new CuboidRegion(weWorld, min, max); + + // do the cleaning editSession.setBlocks(cuboidRegion, air); + // set that island spot to "cool-down mode" + String randUUID = UUID.randomUUID().toString(); + Coordinates islandCoords = new Coordinates(Cache.playerIslands.get(playerName)); + Cache.playerIslands.put(randUUID, islandCoords); + Cache.playerIslands.remove(playerName); + + // start a timer to set the spot to free after X time. Bukkit.getScheduler().runTaskLater(LimboManager.getInstance(), () -> { - LimboManager.getInstance().getLogger().info("Region for " + playerName + " is now available again."); - Cache.playerIslands.remove(playerName); + LimboManager.getInstance().getLogger().info("Region [" + randUUID + ", " + + islandCoords.getX() + ", " + + islandCoords.getZ() + + "] of " + playerName + " is now available again."); + Cache.playerIslands.remove(randUUID); }, 60 * 20L); @@ -150,31 +169,26 @@ public class PlayerTeleporter implements Listener */ Coordinates islandCoords = new Coordinates(Cache.baseCoords); - for(Coordinates currentCoords : Cache.playerIslands.values()) { + ArrayList playerIslands = new ArrayList<>(Cache.playerIslands.values()); - // if the two are not the same, it means we found a free spot. - // we can thus quit the loop and keep the current coords as valid. - if(!islandCoords.equals(currentCoords)) break; + for(int i = 0; i < playerIslands.size(); i++) + { + Coordinates checkCoords = playerIslands.get(i); - // else, if they are the same, - // we have to either increase X or move to a new row and reset X, in case it's over 10000. - if(islandCoords.getX() >= 10000) // if we need to create a new row + if(islandCoords.equals(checkCoords)) { - islandCoords.setX(1000); - islandCoords.setZ(islandCoords.getZ() + Cache.islandsDistance); - } else { // if we just need to increase the column - islandCoords.setX(islandCoords.getX() + Cache.islandsDistance); + // we have to either increase X or move to a new row and reset X, in case it's over 10000. + if(islandCoords.getX() >= 10000) // if we need to create a new row + { + islandCoords.setX(1000); + islandCoords.setZ(islandCoords.getZ() + Cache.islandsDistance); + } else { // if we just need to increase the column + islandCoords.setX(islandCoords.getX() + Cache.islandsDistance); + } + i = 0; // restart the loop and check again (ugh) } - - } + } // at the end of the loop, we should have unique coordinates. return islandCoords; } } - - -/* - player joins -> we look for the first free space -> we add it to the list and generate the island - - - */ \ No newline at end of file