From ed14b52948121581e98adc98a3db9723e80370b4 Mon Sep 17 00:00:00 2001 From: Yaruma3341 Date: Sat, 2 Mar 2019 13:31:07 +0100 Subject: [PATCH] Added Water Remover Signed-off-by: Yaruma3341 --- src/me/yaruma/fightsystem/FightSystem.java | 8 ++ .../fightsystem/fight/WaterRemover.java | 106 ++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/me/yaruma/fightsystem/fight/WaterRemover.java diff --git a/src/me/yaruma/fightsystem/FightSystem.java b/src/me/yaruma/fightsystem/FightSystem.java index 74742ad..dff47b9 100644 --- a/src/me/yaruma/fightsystem/FightSystem.java +++ b/src/me/yaruma/fightsystem/FightSystem.java @@ -34,6 +34,7 @@ public class FightSystem extends JavaPlugin { private FightManager fightManager; private Scoreboard scoreboard; private KitManager kitManager; + private WaterRemover waterRemover; private FightState fightState; @@ -86,6 +87,7 @@ public class FightSystem extends JavaPlugin { this.fightManager = new FightManager(); this.scoreboard = new Scoreboard(plugin); this.kitManager = new KitManager(); + this.waterRemover = new WaterRemover(); Fight.getRedTeam().setName(fileManager.getStringFromConfig("Output.TeamRedName")); Fight.getRedTeam().setPrefix(fileManager.getStringFromConfig("Output.TeamRedColor")); @@ -245,6 +247,10 @@ public class FightSystem extends JavaPlugin { return kitManager; } + public WaterRemover getWaterRemover() { + return waterRemover; + } + public void setSetupState() { if(this.fightState == null) { this.fightState = FightState.SETUP; @@ -286,6 +292,8 @@ public class FightSystem extends JavaPlugin { this.fightState = FightState.RUNNING; Countdown.cancelAllTimers(); + getWaterRemover().start(); + setAllPlayersGM(GameMode.SURVIVAL); if(fileManager.getBooleanFromConfig("WinConditions.Timeout")) { diff --git a/src/me/yaruma/fightsystem/fight/WaterRemover.java b/src/me/yaruma/fightsystem/fight/WaterRemover.java new file mode 100644 index 0000000..2c91df3 --- /dev/null +++ b/src/me/yaruma/fightsystem/fight/WaterRemover.java @@ -0,0 +1,106 @@ +package me.yaruma.fightsystem.fight; + +import me.yaruma.fightsystem.FightSystem; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.List; + +public class WaterRemover { + + private List> explodedBlocks = new ArrayList>(); + private List waterList = new ArrayList(); + private BukkitTask task; + + public void start() { + this.stop(); + this.explodedBlocks = new ArrayList>(); + this.waterList = new ArrayList(); + this.task = Bukkit.getScheduler().runTaskTimerAsynchronously((Plugin)FightSystem.getPlugin(), new Runnable(){ + + @Override + public void run() { + try { + WaterRemover.this.wateredCheck(); + WaterRemover.this.removeWater(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + }, 0L, 20L); + } + + public void stop() { + if (this.task != null) { + this.task.cancel(); + } + } + + public void add(Location loc) { + this.explodedBlocks.add(new AbstractMap.SimpleEntry(loc, 0)); + } + + private void wateredCheck() { + for (int i = 0; i < this.explodedBlocks.size(); ++i) { + if (this.explodedBlocks.get(i).getValue() >= 15) { + Block b = this.explodedBlocks.get(i).getKey().getBlock(); + if (b.getType() == Material.WATER || b.getType() == Material.STATIONARY_WATER) { + this.waterList.add(b); + } + this.explodedBlocks.remove(i); + continue; + } + this.explodedBlocks.get(i).setValue(this.explodedBlocks.get(i).getValue() + 1); + } + } + + private void removeWater() { + ArrayList blocksToRemove = new ArrayList(); + for (int i = this.waterList.size() - 1; i > -1; --i) { + Block current = this.waterList.get(i); + for (Block removeBlock : this.getSourceBlocksOfWater(current)) { + blocksToRemove.add(removeBlock); + } + if (current.getType() != Material.AIR) continue; + this.waterList.remove(i); + } + Bukkit.getScheduler().runTask((Plugin)FightSystem.getPlugin(), () -> { + for (Block block : blocksToRemove) { + block.setType(Material.AIR); + } + }); + } + + private List getSourceBlocksOfWater(Block startBlock) { + ArrayList water = new ArrayList(); + this.collectBlocks(startBlock, water, new ArrayList()); + return water; + } + + public void collectBlocks(Block anchor, List collected, List visitedBlocks) { + if (anchor.getType() != Material.WATER && anchor.getType() != Material.STATIONARY_WATER) { + return; + } + if (visitedBlocks.contains((Object)anchor)) { + return; + } + visitedBlocks.add(anchor); + if (anchor.getType() == Material.STATIONARY_WATER) { + collected.add(anchor); + } + this.collectBlocks(anchor.getRelative(BlockFace.UP), collected, visitedBlocks); + this.collectBlocks(anchor.getRelative(BlockFace.NORTH), collected, visitedBlocks); + this.collectBlocks(anchor.getRelative(BlockFace.EAST), collected, visitedBlocks); + this.collectBlocks(anchor.getRelative(BlockFace.SOUTH), collected, visitedBlocks); + this.collectBlocks(anchor.getRelative(BlockFace.WEST), collected, visitedBlocks); + } + +}