diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index b4c53e5..684f2a4 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -1,26 +1,16 @@ package de.steamwar.fightsystem.winconditions; import de.steamwar.fightsystem.Config; -import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.Fight; -import de.steamwar.fightsystem.fight.FightPlayer; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; -import de.steamwar.fightsystem.states.StateDependent; import de.steamwar.fightsystem.states.StateDependentListener; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.block.data.AnaloguePowerable; -import org.bukkit.block.data.BlockData; -import org.bukkit.block.data.Powerable; -import org.bukkit.block.data.Rail; -import org.bukkit.block.data.type.TechnicalPiston; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.scheduler.BukkitTask; +import org.bukkit.event.entity.EntityExplodeEvent; import java.util.HashMap; import java.util.Map; @@ -36,7 +26,7 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam())); teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam())); - new StateDependentListener(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running, this){ + new StateDependentListener(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running, this) { @Override public void enable() { super.enable(); @@ -58,99 +48,53 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp } @EventHandler - public void onBlockPlace(BlockPlaceEvent event) { - FightPlayer fightPlayer = Fight.getFightPlayer(event.getPlayer()); - if (fightPlayer == null) { - return; - } - if (validBlock(event.getBlockPlaced())) { - teamMap.get(fightPlayer.getTeam()).blockCount--; - } + public void onEntityExplode(EntityExplodeEvent event) { + teamMap.values().forEach(teamPercent -> teamPercent.check(event)); } - public static class TeamPercent extends StateDependent { - private final FightTeam team; + private static boolean validBlock(Block block) { + return Config.Blocks.contains(block.getType()); + } - private int blockCount; - private BukkitTask task; - private int currentBlocks; + public class TeamPercent extends PercentWincondition.TeamPercent { public TeamPercent(FightTeam team) { - super(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running); - this.team = team; - this.currentBlocks = 1; - register(); + super(team); + totalBlocks = currentBlocks(); + currentBlocks = totalBlocks; } - @Override - public void enable() { - blockCount = currentBlocks(); - task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 100, 100); - } - - @Override - public void disable() { - task.cancel(); - } - - private void check() { - currentBlocks(); - - if (!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) + private void check(EntityExplodeEvent event) { + if (!team.getExtendRegion().inRegion(event.getEntity().getLocation())) { return; + } + + event.blockList().forEach(block -> { + if (validBlock(block)) { + currentBlocks--; + } + }); if (getPercent() >= Config.PercentWin) { - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Redstone verloren!"); - FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"); + lose(); } } public double getPercent() { - if (currentBlocks > blockCount) + if (currentBlocks > totalBlocks) return 0; - return (blockCount - currentBlocks) * 100 / (double) blockCount; + return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks; } private int currentBlocks() { - // Entern active - if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) - return currentBlocks; - - currentBlocks = 0; + totalBlocks = 0; team.getSchemRegion().forEach((x, y, z) -> { Block block = world.getBlockAt(x, y, z); if (validBlock(block)) { - currentBlocks++; + totalBlocks++; } }); - return currentBlocks; - } - } - - private static boolean validBlock(Block block) { - Material material = block.getType(); - if (Config.Blocks.contains(material)) { - return false; - } - BlockData blockData = block.getBlockData(); - if (blockData instanceof Powerable - || blockData instanceof TechnicalPiston - || blockData instanceof AnaloguePowerable - || blockData instanceof Rail) { - return true; - } - switch (material) { - case NOTE_BLOCK: - case REDSTONE_BLOCK: - case REDSTONE: - case OBSERVER: - case HOPPER: - case REDSTONE_LAMP: - case REDSTONE_TORCH: - case REDSTONE_WALL_TORCH: - return true; - default: - return false; + return totalBlocks; } }