Guest User

Untitled

a guest
May 19th, 2019
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class WallBorderListener implements Listener {
  2.  
  3. private static final int WALL_HEIGHT_DOWN_DIST = 3;
  4. private static final int WALL_HEIGHT_UP_DIST = 4;
  5. private static final int WALL_HORIZONTAL_DIST = 7;
  6.  
  7. private final HCF plugin;
  8.  
  9. public WallBorderListener(final HCF plugin) {
  10. this.plugin = plugin;
  11. }
  12.  
  13. @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
  14. public void onPlayerJoin(final PlayerSpawnLocationEvent event) {
  15. final Player player = event.getPlayer();
  16. Location location = event.getSpawnLocation();
  17. updateVisualWalls(player, location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
  18. }
  19.  
  20. @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
  21. public void onPlayerMove(final PlayerMoveEvent event) {
  22. final Location to = event.getTo();
  23. final int toX = to.getBlockX();
  24. final int toY = to.getBlockY();
  25. final int toZ = to.getBlockZ();
  26. final Location from = event.getFrom();
  27. if (from.getBlockX() != toX || from.getBlockY() != toY || from.getBlockZ() != toZ) {
  28. this.updateVisualWalls(event.getPlayer(), to.getWorld(), toX, toY, toZ);
  29. }
  30. }
  31.  
  32. @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
  33. public void onPlayerTeleport(final PlayerTeleportEvent event) {
  34. this.onPlayerMove(event);
  35. }
  36.  
  37. private void updateVisualWalls(Player player, final World toWorld, final int toX, final int toY, final int toZ) {
  38. BlockGenerator blockGenerator;
  39. if (plugin.getTimerManager().getCombatTimer().getRemaining(player) > 0L) {
  40. blockGenerator = Constants.SPAWN_TAG_WALL;
  41. } else {
  42. if (this.plugin.getTimerManager().getPvPProtectionTimer().getRemaining(player) > 0L) {
  43. blockGenerator = Constants.CLAIM_BORDER_WALL;
  44. } else {
  45. return;
  46. }
  47. }
  48.  
  49. this.plugin.getBlockSender().clearBlocks(player, fakeBlock -> {
  50. Location location = fakeBlock.getLocation();
  51. return location.getWorld().equals(toWorld) && fakeBlock.getGenerator().equals(blockGenerator)
  52. && (Math.abs(toX - location.getBlockX()) > WALL_HORIZONTAL_DIST
  53. || Math.abs(toZ - location.getBlockZ()) > WALL_HORIZONTAL_DIST
  54. || Math.abs(toY - location.getBlockY()) > WALL_HEIGHT_UP_DIST); //TODO WALL_HEIGHT_DOWN_DIST
  55. });
  56.  
  57. final int minX = toX - WALL_HORIZONTAL_DIST;
  58. final int maxX = toX + WALL_HORIZONTAL_DIST;
  59.  
  60. final int minY = toY - WALL_HEIGHT_UP_DIST; //TODO WALL_HEIGHT_DOWN_DIST
  61. final int maxY = toY + WALL_HEIGHT_UP_DIST;
  62.  
  63. final int minZ = toZ - WALL_HORIZONTAL_DIST;
  64. final int maxZ = toZ + WALL_HORIZONTAL_DIST;
  65.  
  66. Collection<Location> locations = null;
  67. for (int x = minX; x <= maxX; ++x) {
  68. for (int z = minZ; z <= maxZ; ++z) {
  69. final Faction faction = this.plugin.getFactionsManager().getFactionAt(toWorld, x, z);
  70. if (blockGenerator == Constants.SPAWN_TAG_WALL) {
  71. if (!(faction instanceof SpawnFaction)) {
  72. continue;
  73. }
  74. } else if (blockGenerator == Constants.CLAIM_BORDER_WALL) {
  75. if (faction instanceof RoadFaction) {
  76. continue;
  77. }
  78. if (faction instanceof SpawnFaction) {
  79. continue;
  80. }
  81. //TODO ignore for own faction.
  82. }
  83. final Collection<Claim> claims = faction.getClaim();
  84. for (Claim claim : claims) {
  85. if (toWorld.equals(claim.getWorld())) {
  86. final Location[] edges = claim.edges(minY, maxY);
  87. for (final Location edge : edges) {
  88. if (Math.abs(edge.getBlockX() - toX) > WALL_HORIZONTAL_DIST || Math.abs(edge.getBlockZ() - toZ) > WALL_HORIZONTAL_DIST) {
  89. continue;
  90. }
  91.  
  92. if (locations == null) locations = new ArrayList<>();
  93. locations.add(edge);
  94. }
  95. }
  96. }
  97. }
  98. }
  99.  
  100. if (locations != null) {
  101. plugin.getBlockSender().sendBlocks(player, blockGenerator, locations);
  102. locations.clear();
  103. }
  104. }
  105. }
RAW Paste Data