/* This file is a part of the SteamWar software. Copyright (C) 2020 SteamWar.de-Serverteam This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ 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 de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.scheduler.BukkitTask; import java.util.*; public class WinconditionRelativePercent extends Wincondition implements PrintableWincondition, PercentWincondition { private static final World world = Bukkit.getWorlds().get(0); private static final Set ignoredBlocks; static{ Set ignored = new HashSet<>(); for(String s : Config.IgnoredBlocks) ignored.add(Material.valueOf(s)); ignoredBlocks = Collections.unmodifiableSet(ignored); } private final Map teamMap = new HashMap<>(); public WinconditionRelativePercent(){ super("RelativePercent", null); teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam())); teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam())); if(Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)){ printableWinconditions.add(this); percentWincondition = this; } } @Override public double getPercent(FightTeam team) { return teamMap.get(team).getPercent(); } @Override public String getDisplay(FightTeam team) { return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; } public static class TeamPercent extends StateDependent { private final FightTeam team; private int blockCount; private BukkitTask task; private int currentBlocks; public TeamPercent(FightTeam team){ super(Winconditions.RELATIVE_PERCENT, FightState.Running); this.team = team; this.currentBlocks = 1; register(); } @Override public void enable(){ blockCount = currentBlocks(); task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400); } @Override public void disable(){ task.cancel(); } private void check(){ currentBlocks(); if(!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)) return; if(getPercent() >= Config.PercentWin){ Bukkit.broadcastMessage(FightSystem.PREFIX + "§cDer Schiff von " + team.getColoredName() + " §cwurde zu stark beschädigt!"); FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"); } } public double getPercent(){ if(currentBlocks > blockCount) return 0; return (blockCount - currentBlocks) * 100 / (double) blockCount; } public int getBlockCount(){ return blockCount; } private int currentBlocks(){ // Entern active if(!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) return currentBlocks; currentBlocks = 0; team.getSchemRegion().forEach((x, y, z) -> { if(!ignoredBlocks.contains(world.getBlockAt(x,y,z).getType())) currentBlocks++; }); return currentBlocks; } } }