Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. package red.mirai.spawnsystems;
  2.  
  3. import org.bukkit.Chunk;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.World;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.block.BlockFace;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.scheduler.BukkitRunnable;
  11. import red.mirai.spawnsystems.Events.SpawnProtection;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Objects;
  15. import java.util.Random;
  16. import java.util.concurrent.ThreadLocalRandom;
  17.  
  18. import static org.bukkit.Bukkit.getServer;
  19.  
  20. public class RandomSpawnLocation {
  21.  
  22. public ArrayList<Player> respawnQueue = new ArrayList<>();
  23.  
  24. private static int randomInRange(int min, int max) {
  25. if ( min >= max ) {
  26. throw new IllegalArgumentException( "max must be greater than min" );
  27. }
  28.  
  29. Random r = ThreadLocalRandom.current();
  30. return r.nextInt( ( max - min ) + 1 ) + min;
  31. }
  32.  
  33. private static Block findSuitableBlock(World world, Block block, Chunk chunk) {
  34. boolean found = false;
  35. boolean checkingArea = false;
  36. boolean exit = false;
  37.  
  38. int x = chunk.getX();
  39. int z = chunk.getZ();
  40. ArrayList<Block> blocks = new ArrayList<>();
  41.  
  42. while ( !found && !exit ) {
  43. Material blockMaterial = block.getType();
  44.  
  45. if ( blockMaterial == Material.AIR ) {
  46. blockMaterial = block.getRelative( BlockFace.DOWN ).getType();
  47. }
  48.  
  49. switch ( blockMaterial ) {
  50. case LAVA:
  51. case CACTUS:
  52. case MAGMA_BLOCK:
  53. case SWEET_BERRY_BUSH:
  54. case ROSE_BUSH:
  55. case FIRE:
  56. case DEAD_BUSH:
  57. case CAMPFIRE:
  58. if ( !checkingArea ){
  59. checkingArea = true;
  60. for ( int ix = x - 7; ix <= x + 7; ix++ ) {
  61. for ( int iz = z - 7; iz <= z + 7; iz++ ) {
  62. blocks.add( chunk.getWorld().getHighestBlockAt( x, z ) );
  63. }
  64. }
  65. }
  66.  
  67. int size = blocks.size();
  68.  
  69. if ( size <= 0 ){
  70. exit = true;
  71. continue;
  72. }
  73.  
  74. int i = ThreadLocalRandom.current().nextInt( size );
  75. block = blocks.get( i );
  76.  
  77. blocks.remove( i );
  78. continue;
  79. default:
  80. found = true;
  81. }
  82. }
  83.  
  84. return found ? block : null;
  85. }
  86.  
  87. public void send(Player ply) {
  88. respawnQueue.add( ply );
  89.  
  90. World world = getServer().getWorld( "world" );
  91. Objects.requireNonNull(world);
  92.  
  93. int worldRadius = (int) world.getWorldBorder().getSize() / 2;
  94. int borderDistance = worldRadius / 1000;
  95. int max = worldRadius - borderDistance;
  96.  
  97. new BukkitRunnable() {
  98. Location location;
  99.  
  100. @Override
  101. public void run() {
  102. int x = randomInRange( -max, max );
  103. int z = randomInRange( -max, max );
  104.  
  105. for ( Player ply : getServer().getOnlinePlayers() ) {
  106. World plyWorld = ply.getWorld();
  107. if ( plyWorld != world ) {
  108. continue;
  109. }
  110.  
  111. Location plyLocation = ply.getLocation();
  112.  
  113. int px = plyLocation.getBlockX();
  114. int pz = plyLocation.getBlockZ();
  115.  
  116. int distance = (int) Math.sqrt( ( pz - z ) * ( pz - z ) + ( px - x ) * ( px - x ) );
  117. if ( distance < 512 ) {
  118. return;
  119. }
  120. }
  121.  
  122. Chunk chunk = world.getChunkAt( x, z );
  123. SpawnSystems.plugin.getLogger().info( "Loading Chunk @ X: " + x + " Z: " + z );
  124. world.loadChunk( chunk );
  125.  
  126. Block block = world.getHighestBlockAt( x, z );
  127.  
  128. switch ( block.getBiome() ) {
  129. case OCEAN:
  130. case COLD_OCEAN:
  131. case WARM_OCEAN:
  132. case FROZEN_OCEAN:
  133. case LUKEWARM_OCEAN:
  134. case DEEP_OCEAN:
  135. case DEEP_COLD_OCEAN:
  136. case DEEP_WARM_OCEAN:
  137. case DEEP_FROZEN_OCEAN:
  138. case DEEP_LUKEWARM_OCEAN:
  139. return;
  140. }
  141.  
  142. Block safeBlock = findSuitableBlock( world, block, chunk );
  143. if ( safeBlock == null ) {
  144. return;
  145. }
  146.  
  147. location = safeBlock.getLocation();
  148. world.strikeLightningEffect( location );
  149.  
  150. if ( !world.isDayTime() ) {
  151. SpawnProtection.apply( ply );
  152. }
  153.  
  154. ply.teleportAsync( location );
  155. respawnQueue.remove( ply );
  156. cancel();
  157. }
  158. }.runTaskTimer( SpawnSystems.plugin, 0L, 5L );
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement