Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package us.therighteo.warfront.gamemode.util;
  2.  
  3. import org.bukkit.Location;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.event.EventHandler;
  7. import org.bukkit.event.HandlerList;
  8. import org.bukkit.event.Listener;
  9. import org.bukkit.event.block.BlockBreakEvent;
  10. import org.bukkit.event.block.BlockPlaceEvent;
  11. import org.bukkit.event.entity.EntityExplodeEvent;
  12. import us.therighteo.warfront.RoundHelper;
  13. import us.therighteo.warfront.Team;
  14. import us.therighteo.warfront.WFP;
  15. import us.therighteo.warfront.WarFront;
  16. import us.therighteo.warfront.gamemode.GamemodeMain;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Random;
  21.  
  22.  
  23. public class Monument implements Listener {
  24. public int x1, y1, z1;
  25. public int x2, y2, z2;
  26. public Material composure;
  27. private WarFront instance = WarFront.getInstance();
  28.  
  29. List<Block> region = new ArrayList<Block>();
  30. int origSize = 0;
  31. int blocksBroken = 0;
  32. int prevBlocksBroken = 0;
  33.  
  34. public Monument(int x1, int y1, int z1, int x2, int y2, int z2, Material composure) {
  35. this.x1 = Math.min(x1, x2);
  36. this.y1 = Math.min(y1, y2);
  37. this.z1 = Math.min(z1, z2);
  38. this.x2 = Math.max(x1, x2);
  39. this.y2 = Math.max(y1, y2);
  40. this.z2 = Math.max(z1, z2);
  41. this.composure = composure;
  42. }
  43.  
  44. /**
  45. * Awaken this Cuboid for the round.
  46. */
  47. public void activate() {
  48. instance.getServer().getPluginManager().registerEvents(this, instance);
  49. region.addAll(getBlocks());
  50. origSize = region.size();
  51. }
  52.  
  53. /**
  54. * Put this Cuboid to sleep until it is needed again.
  55. */
  56. public void deactivate() {
  57. HandlerList.unregisterAll(this);
  58. region.clear();
  59. origSize = 0;
  60. blocksBroken = 0;
  61. prevBlocksBroken = 0;
  62. }
  63.  
  64. /**
  65. * Calculates current percentage of monument remaining.
  66. *
  67. * @return The remaining percent.
  68. */
  69. public int calculatePercentage() {
  70. int per = blocksBroken;
  71. int result = -1;
  72. if (per == 0) result = 100;
  73. if (result == -1) result = Math.abs(Math.round((per * 100) / origSize) - 100);
  74. if (per == 1) result = 100 - (result / origSize);
  75. return result;
  76. }
  77.  
  78. /**
  79. * Calculates previous percentage of monument remaining.
  80. *
  81. * @return The previous percent.
  82. */
  83. public int calculatePrevPercentage() {
  84. int per = prevBlocksBroken;
  85. int result = -1;
  86. if (per == 0) result = 100;
  87. if (result == -1) result = Math.abs(Math.round((per * 100) / origSize) - 100);
  88. if (per == 1) result = 100 - (result / origSize);
  89. return result;
  90. }
  91.  
  92. /**
  93. * Checks if a location is one of the remaining blocks.
  94. * @param loc The location.
  95. * @return Whether it is a monument block or not.
  96. */
  97. public boolean isInside(Location loc) {
  98. return region.contains(loc.getBlock());
  99. }
  100.  
  101. /**
  102. * Grabs a random block remaining in the cuboid.
  103. *
  104. * @return A remaining block (location).
  105. */
  106. public Location rLoc() {
  107. Random random = new Random();
  108. return region.get(random.nextInt(region.size())).getLocation().clone().add(0, 5, 0);
  109. }
  110.  
  111. public ArrayList<Block> getBlocks() {
  112. ArrayList<Block> blocks = new ArrayList<Block>();
  113. for (int x = this.x1; x <= this.x2; x++)
  114. for (int y = this.y1; y <= this.y2; y++)
  115. for (int z = this.z1; z <= this.z2; z++)
  116. if (RoundHelper.getCurrentWorld().getBlockAt(x, y, z).getType() == composure)
  117. blocks.add(RoundHelper.getCurrentWorld().getBlockAt(x, y, z));
  118. return blocks;
  119. }
  120.  
  121. private ArrayList<Block> ab() {
  122. ArrayList<Block> blocks = new ArrayList<Block>();
  123. for (int x = this.x1; x <= this.x2; x++)
  124. for (int y = this.y1; y <= this.y2; y++)
  125. for (int z = this.z1; z <= this.z2; z++)
  126. blocks.add(RoundHelper.getCurrentWorld().getBlockAt(x, y, z));
  127. return blocks;
  128. }
  129.  
  130. @EventHandler
  131. public void onBreak(BlockBreakEvent event) {
  132. if (event.getBlock().getType() == composure) {
  133. if (isInside(event.getBlock().getLocation())) {
  134. if (WFP.getWFP(event.getPlayer()).getCurrentTeam() == Team.TEAM_1) {
  135. event.setCancelled(true);
  136. return;
  137. }
  138. region.remove(event.getBlock());
  139. prevBlocksBroken = blocksBroken;
  140. blocksBroken++;
  141. GamemodeMain.getCore("GTM").updateScoreboard();
  142. }
  143. }
  144. }
  145.  
  146. @EventHandler
  147. public void onPlace(BlockPlaceEvent event) {
  148. if (ab().contains(event.getBlock())) event.setCancelled(true);
  149. }
  150.  
  151. @EventHandler
  152. public void onExplode(EntityExplodeEvent event) {
  153. for (Block block : event.blockList())
  154. if (block.getType() == composure)
  155. if (isInside(block.getLocation())) {
  156. region.remove(block);
  157. prevBlocksBroken = blocksBroken;
  158. blocksBroken++;
  159. GamemodeMain.getCore("GTM").updateScoreboard();
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement