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.FightTeam; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class WinconditionRelativePercent{ private static final Set ignoredBlocks; private static TeamPercent blue; private static TeamPercent red; static{ Set ignored = new HashSet<>(); for(String s : Config.IgnoredBlocks) ignored.add(Material.valueOf(s)); ignoredBlocks = Collections.unmodifiableSet(ignored); } public WinconditionRelativePercent(){ if(!Config.RelativePercent && !Config.Points) return; blue = new TeamPercent(Fight.getBlueTeam()); red = new TeamPercent(Fight.getRedTeam()); } public static TeamPercent getBlue(){ return blue; } public static TeamPercent getRed(){ return red; } public static class TeamPercent { private static final World world = Bukkit.getWorlds().get(0); private final FightTeam team; private final int blockCount; private int currentBlocks; TeamPercent(FightTeam team){ this.team = team; this.blockCount = currentBlocks(); Bukkit.getScheduler().runTaskTimerAsynchronously(FightSystem.getPlugin(), () -> { currentBlocks = currentBlocks(); checkPercentDefeat(); }, 400, 400); } public double getPrintablePercent(){ return Math.round(10000.0 * getPercent()) / 100.0; } public final int getBlockCount(){ return blockCount; } public int getCurrentBlocks(){ return currentBlocks; } private double getPercent(){ int blocksDestroyed = blockCount - currentBlocks; return blocksDestroyed > 0 ? blocksDestroyed / (double) blockCount : 0; } private int currentBlocks(){ int blocks = 0; for(int x = team.getCornerX(); x < team.getCornerX() + Config.SchemsizeX; x++){ for(int y = team.getCornerY(); y < team.getCornerY() + Config.SchemsizeY; y++){ for(int z = team.getCornerZ(); z < team.getCornerZ() + Config.SchemsizeZ; z++){ if(!ignoredBlocks.contains(world.getBlockAt(x,y,z).getType())) blocks++; } } } return blocks; } private void checkPercentDefeat(){ if(!Config.RelativePercent) return; if(getPercent() >= Config.PercentWin){ Bukkit.broadcastMessage(FightSystem.PREFIX + "§cDer Schiff von " + team.getColoredName() + " §cwurde zu stark beschädigt!"); FightSystem.setSpectateState(Fight.getOpposite(team)); } } } }