/HungerCraft/src/me/dr_madman/hungercraft/RandomTeleport.java

https://gitlab.com/vinnyjth/HungerCraft · Java · 96 lines · 69 code · 22 blank · 5 comment · 28 complexity · 168be3db94fd0cd61402483764f3e2cd MD5 · raw file

  1. package me.dr_madman.hungercraft;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.World;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.entity.Player;
  8. public class RandomTeleport {
  9. private HungerCraft plugin;
  10. // *------------------------------------------------------------------------------------------------------------*
  11. // | The random location methods contain code made by NuclearW |
  12. // | based on his SpawnArea plugin: |
  13. // | http://forums.bukkit.org/threads/tp-spawnarea-v0-1-spawns-targetPlayers-in-a-set-area-randomly-1060.20408/ |
  14. // *------------------------------------------------------------------------------------------------------------*
  15. public RandomTeleport(HungerCraft instance) {plugin = instance;}
  16. public Location randomLocation(World world){
  17. int xmin = -this.plugin.getConfig().getInt("spawnradius");
  18. int xmax = this.plugin.getConfig().getInt("spawnradius");
  19. int zmin = -this.plugin.getConfig().getInt("spawnradius");
  20. int zmax = this.plugin.getConfig().getInt("spawnradius");
  21. Bukkit.getServer().broadcastMessage("Getting values");
  22. int xrand = 0;
  23. int zrand = 0;
  24. int y = -1;
  25. do {
  26. Bukkit.getServer().broadcastMessage("Running do loop");
  27. xrand = xmin + (int) ( Math.random()*(xmax - xmin) + 0.5 );
  28. zrand = zmin + (int) ( Math.random()*(zmax - zmin) + 0.5 );
  29. y = getValidHighestBlock(world, xrand,zrand);
  30. }while (y == -1);
  31. return new Location(
  32. world,
  33. Double.parseDouble(Integer.toString(xrand)) + 0.5,
  34. Double.parseDouble(Integer.toString(y)),
  35. Double.parseDouble(Integer.toString(zrand)) + 0.5
  36. );
  37. }
  38. public int getValidHighestBlock(World world, int x, int z) {
  39. world.loadChunk(x, z);
  40. Bukkit.getServer().broadcastMessage("Getting chunk");
  41. world.getChunkAt(new Location(world, x, 0, z)).load();
  42. int y = world.getHighestBlockYAt(x, z);
  43. int blockid = world.getBlockTypeIdAt(x, y - 1, z);
  44. if (blockid == 8) return -1;
  45. if (blockid == 9) return -1;
  46. if (blockid == 10) return -1;
  47. if (blockid == 11) return -1;
  48. if (blockid == 51) return -1;
  49. if (blockid == 18) return -1;
  50. blockid = world.getBlockTypeIdAt(x, y + 1, z);
  51. if (blockid == 81) return -1;
  52. return y;
  53. }
  54. public void sendGround(Player player, Location location){
  55. Location groundLocation = location.subtract(0, 1, 0);
  56. groundLocation.getChunk().load();
  57. if(canCauseBlockUpdate(groundLocation.getBlock())){
  58. player.sendBlockChange(groundLocation, Material.DIRT, (byte) 0);
  59. }else{
  60. player.sendBlockChange(groundLocation, groundLocation.getBlock().getType(), groundLocation.getBlock().getData());
  61. }
  62. }
  63. public boolean canCauseBlockUpdate(Block block){
  64. block.getChunk().load();
  65. int blockid = block.getTypeId();
  66. if (blockid == 8) return true;
  67. if (blockid == 9) return true;
  68. if (blockid == 10) return true;
  69. if (blockid == 11) return true;
  70. if (blockid == 12) return true;
  71. if (blockid == 13) return true;
  72. return false;
  73. }
  74. }