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(), () -> { try { WaterRemover.this.wateredCheck(); WaterRemover.this.removeWater(); } catch (Exception e) { e.printStackTrace(); } }, 0L, 20L); } private 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 = this.explodedBlocks.size() - 1; i >= 0; 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); blocksToRemove.addAll(this.getSourceBlocksOfWater(current)); 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; } private 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); } }