/* 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())); printableWinconditions.add(this); percentWincondition = this; new StateDependent(Winconditions.RELATIVE_PERCENT, FightState.Running){ @Override public void enable() { teamMap.values().forEach(TeamPercent::enable); } @Override public void disable() { teamMap.values().forEach(TeamPercent::disable); } }; } @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 { private final FightTeam team; private int blockCount; private BukkitTask task; private int currentBlocks; public TeamPercent(FightTeam team){ this.team = team; this.currentBlocks = 1; } public void enable(){ blockCount = currentBlocks(); task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400); } public void disable(){ task.cancel(); } private void check(){ currentBlocks = 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) >= Config.TimeoutTime - Wincondition.getTimeOverCountdown().getTimeLeft()) return 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; } } }