Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package us.therighteo.warfront.gamemode.util;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.HandlerList;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.entity.EntityExplodeEvent;
- import us.therighteo.warfront.RoundHelper;
- import us.therighteo.warfront.Team;
- import us.therighteo.warfront.WFP;
- import us.therighteo.warfront.WarFront;
- import us.therighteo.warfront.gamemode.GamemodeMain;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class Monument implements Listener {
- public int x1, y1, z1;
- public int x2, y2, z2;
- public Material composure;
- private WarFront instance = WarFront.getInstance();
- List<Block> region = new ArrayList<Block>();
- int origSize = 0;
- int blocksBroken = 0;
- int prevBlocksBroken = 0;
- public Monument(int x1, int y1, int z1, int x2, int y2, int z2, Material composure) {
- this.x1 = Math.min(x1, x2);
- this.y1 = Math.min(y1, y2);
- this.z1 = Math.min(z1, z2);
- this.x2 = Math.max(x1, x2);
- this.y2 = Math.max(y1, y2);
- this.z2 = Math.max(z1, z2);
- this.composure = composure;
- }
- /**
- * Awaken this Cuboid for the round.
- */
- public void activate() {
- instance.getServer().getPluginManager().registerEvents(this, instance);
- region.addAll(getBlocks());
- origSize = region.size();
- }
- /**
- * Put this Cuboid to sleep until it is needed again.
- */
- public void deactivate() {
- HandlerList.unregisterAll(this);
- region.clear();
- origSize = 0;
- blocksBroken = 0;
- prevBlocksBroken = 0;
- }
- /**
- * Calculates current percentage of monument remaining.
- *
- * @return The remaining percent.
- */
- public int calculatePercentage() {
- int per = blocksBroken;
- int result = -1;
- if (per == 0) result = 100;
- if (result == -1) result = Math.abs(Math.round((per * 100) / origSize) - 100);
- if (per == 1) result = 100 - (result / origSize);
- return result;
- }
- /**
- * Calculates previous percentage of monument remaining.
- *
- * @return The previous percent.
- */
- public int calculatePrevPercentage() {
- int per = prevBlocksBroken;
- int result = -1;
- if (per == 0) result = 100;
- if (result == -1) result = Math.abs(Math.round((per * 100) / origSize) - 100);
- if (per == 1) result = 100 - (result / origSize);
- return result;
- }
- /**
- * Checks if a location is one of the remaining blocks.
- * @param loc The location.
- * @return Whether it is a monument block or not.
- */
- public boolean isInside(Location loc) {
- return region.contains(loc.getBlock());
- }
- /**
- * Grabs a random block remaining in the cuboid.
- *
- * @return A remaining block (location).
- */
- public Location rLoc() {
- Random random = new Random();
- return region.get(random.nextInt(region.size())).getLocation().clone().add(0, 5, 0);
- }
- public ArrayList<Block> getBlocks() {
- ArrayList<Block> blocks = new ArrayList<Block>();
- for (int x = this.x1; x <= this.x2; x++)
- for (int y = this.y1; y <= this.y2; y++)
- for (int z = this.z1; z <= this.z2; z++)
- if (RoundHelper.getCurrentWorld().getBlockAt(x, y, z).getType() == composure)
- blocks.add(RoundHelper.getCurrentWorld().getBlockAt(x, y, z));
- return blocks;
- }
- private ArrayList<Block> ab() {
- ArrayList<Block> blocks = new ArrayList<Block>();
- for (int x = this.x1; x <= this.x2; x++)
- for (int y = this.y1; y <= this.y2; y++)
- for (int z = this.z1; z <= this.z2; z++)
- blocks.add(RoundHelper.getCurrentWorld().getBlockAt(x, y, z));
- return blocks;
- }
- @EventHandler
- public void onBreak(BlockBreakEvent event) {
- if (event.getBlock().getType() == composure) {
- if (isInside(event.getBlock().getLocation())) {
- if (WFP.getWFP(event.getPlayer()).getCurrentTeam() == Team.TEAM_1) {
- event.setCancelled(true);
- return;
- }
- region.remove(event.getBlock());
- prevBlocksBroken = blocksBroken;
- blocksBroken++;
- GamemodeMain.getCore("GTM").updateScoreboard();
- }
- }
- }
- @EventHandler
- public void onPlace(BlockPlaceEvent event) {
- if (ab().contains(event.getBlock())) event.setCancelled(true);
- }
- @EventHandler
- public void onExplode(EntityExplodeEvent event) {
- for (Block block : event.blockList())
- if (block.getType() == composure)
- if (isInside(block.getLocation())) {
- region.remove(block);
- prevBlocksBroken = blocksBroken;
- blocksBroken++;
- GamemodeMain.getCore("GTM").updateScoreboard();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement