/*
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.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.utils.Region;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.HashMap;
import java.util.Map;
public class WinconditionPercentSystem extends Wincondition implements Listener, PrintableWincondition, PercentWincondition {
private static final int SCHEMATIC_SIZE = Math.abs(Config.SchemsizeX * Config.SchemsizeY * Config.SchemsizeZ);
private final Map teamMap = new HashMap<>();
public WinconditionPercentSystem() {
super("Percent", "§cTeam ", " §chat den Gegnern");
printableWinconditions.add(this);
percentWincondition = this;
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
new StateDependentListener(Winconditions.PERCENT_SYSTEM, FightState.Running, this);
}
@EventHandler
public void handleEntityExplode(EntityExplodeEvent event) {
teamMap.values().forEach(teamPercent -> teamPercent.check(event));
}
@Override
public String getDisplay(FightTeam team) {
return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%";
}
@Override
public double getPercent(FightTeam team) {
return teamMap.get(team).percent;
}
private class TeamPercent {
private final FightTeam team;
private double percent;
private int destroyedBlocks;
private TeamPercent(FightTeam team) {
this.team = team;
}
private void check(EntityExplodeEvent event) {
if(!Region.isInRange(event.getEntity().getLocation(), team.getCornerX(), team.getCornerY(), team.getCornerZ(), Config.SchemsizeX, Config.SchemsizeY, Config.SchemsizeZ, Config.BorderFromSchematic)){
return;
}
destroyedBlocks += event.blockList().size();
percent = (double)destroyedBlocks * 100 / SCHEMATIC_SIZE;
if(percent >= Config.PercentWin) {
win(Fight.getOpposite(team));
}
}
}
}