Advertisement
JackOUT

Untitled

Feb 7th, 2021
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package games.coob.skywars.model;
  2.  
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.block.BlockFace;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.player.PlayerMoveEvent;
  11. import org.bukkit.potion.PotionEffect;
  12. import org.bukkit.potion.PotionEffectType;
  13. import org.mineacademy.fo.Common;
  14. import org.mineacademy.fo.Messenger;
  15. import org.mineacademy.fo.RandomUtil;
  16. import org.mineacademy.fo.region.Region;
  17. import org.mineacademy.fo.remain.CompMaterial;
  18. import org.mineacademy.fo.remain.CompSound;
  19.  
  20. public class PortalListener implements Listener {
  21.  
  22. private final static int LOCATION_SEARCH_TRIES = 4;
  23.  
  24. @EventHandler
  25. public void onPlayerMoveEvent(final PlayerMoveEvent event) {
  26.  
  27. final Player player = event.getPlayer();
  28. final ArenaPlayer cache = ArenaPlayer.getCache(player);
  29. final Arena arena = cache.getArena();
  30. final Region range = arena.getSettings().getRegion();
  31. final ArenaJoinMode mode = cache.getMode();
  32. final Block portal = event.getPlayer().getLocation().getBlock().getRelative(BlockFace.DOWN);
  33. final Location location = findLocation(new Location(player.getWorld(), 0, 0, 0), range);
  34.  
  35. if (portal.getType() == Material.RESPAWN_ANCHOR) {
  36.  
  37. if (!location.getChunk().isLoaded())
  38. location.getChunk().load(true);
  39.  
  40. Common.runLater(20 * 5, () -> {
  41. player.teleport(location);
  42.  
  43. Common.tell(player, "&5Teleport success!");
  44. });
  45.  
  46. // Play sound and add a potion effect to the player standing on a respawn anchor
  47. CompSound.PORTAL_TRAVEL.play(player);
  48. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 100, 1, false, false, false));
  49.  
  50. /**
  51. * Cancel the event if the player is in spectate mode, moves away or is standing on an uncharged respawn anchor
  52. **/
  53. if (mode == ArenaJoinMode.SPECTATING) {
  54. Messenger.error(player, "You cannot enter portals while spectating!");
  55.  
  56. event.setCancelled(true);
  57. }
  58. }
  59. }
  60.  
  61. private Location findLocation(final Location center, final int range) {
  62.  
  63. for (int i /* try attempt number */ = 0; i < LOCATION_SEARCH_TRIES; ++i) {
  64.  
  65. final Location location = RandomUtil.nextLocation(center, 10, true);
  66.  
  67. final Block block = location.getBlock();
  68. final Block blockBelow = block.getRelative(BlockFace.DOWN);
  69.  
  70. if (blockBelow.getType() == Material.RESPAWN_ANCHOR && CompMaterial.isAir(block))
  71. return location;
  72. }
  73.  
  74. return null;
  75. }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement