2020-08-26 18:24:44 +02:00
|
|
|
/*
|
|
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-12-21 20:46:31 +01:00
|
|
|
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;
|
2020-01-11 15:44:40 +01:00
|
|
|
import de.steamwar.fightsystem.states.FightState;
|
2021-03-12 15:43:32 +01:00
|
|
|
import de.steamwar.fightsystem.states.StateDependent;
|
2019-12-21 20:46:31 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
2020-01-11 15:44:40 +01:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2019-12-21 20:46:31 +01:00
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
import java.util.*;
|
2019-12-21 20:46:31 +01:00
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
public class WinconditionRelativePercent extends Wincondition implements PrintableWincondition, PercentWincondition {
|
2019-12-21 20:46:31 +01:00
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
private static final World world = Bukkit.getWorlds().get(0);
|
2019-12-21 20:46:31 +01:00
|
|
|
private static final Set<Material> ignoredBlocks;
|
|
|
|
|
|
|
|
static{
|
|
|
|
Set<Material> ignored = new HashSet<>();
|
|
|
|
for(String s : Config.IgnoredBlocks)
|
|
|
|
ignored.add(Material.valueOf(s));
|
|
|
|
ignoredBlocks = Collections.unmodifiableSet(ignored);
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
private final Map<FightTeam, TeamPercent> teamMap = new HashMap<>();
|
|
|
|
|
2019-12-21 20:46:31 +01:00
|
|
|
public WinconditionRelativePercent(){
|
2021-03-12 15:43:32 +01:00
|
|
|
super("RelativePercent", null);
|
2021-03-10 22:45:17 +01:00
|
|
|
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
|
|
|
|
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
|
2019-12-21 20:46:31 +01:00
|
|
|
|
2021-03-24 10:18:00 +01:00
|
|
|
if(Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)){
|
|
|
|
printableWinconditions.add(this);
|
|
|
|
percentWincondition = this;
|
|
|
|
}
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 15:44:40 +01:00
|
|
|
@Override
|
2021-03-10 22:45:17 +01:00
|
|
|
public double getPercent(FightTeam team) {
|
|
|
|
return teamMap.get(team).getPercent();
|
2020-01-11 15:44:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-10 22:45:17 +01:00
|
|
|
public String getDisplay(FightTeam team) {
|
|
|
|
return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%";
|
2020-01-11 15:44:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 21:58:21 +02:00
|
|
|
public static class TeamPercent extends StateDependent {
|
2019-12-21 20:46:31 +01:00
|
|
|
private final FightTeam team;
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
private int blockCount;
|
|
|
|
private BukkitTask task;
|
2019-12-21 20:46:31 +01:00
|
|
|
private int currentBlocks;
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
public TeamPercent(FightTeam team){
|
2021-03-31 21:58:21 +02:00
|
|
|
super(Winconditions.RELATIVE_PERCENT, FightState.Running);
|
2019-12-21 20:46:31 +01:00
|
|
|
this.team = team;
|
2021-03-10 22:45:17 +01:00
|
|
|
this.currentBlocks = 1;
|
2021-03-31 21:58:21 +02:00
|
|
|
register();
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 21:58:21 +02:00
|
|
|
@Override
|
2021-03-10 22:45:17 +01:00
|
|
|
public void enable(){
|
|
|
|
blockCount = currentBlocks();
|
|
|
|
task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400);
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 21:58:21 +02:00
|
|
|
@Override
|
2021-03-12 15:43:32 +01:00
|
|
|
public void disable(){
|
|
|
|
task.cancel();
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
private void check(){
|
2021-03-20 17:25:13 +01:00
|
|
|
currentBlocks();
|
2021-03-10 22:45:17 +01:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
public double getPercent(){
|
|
|
|
if(currentBlocks > blockCount)
|
|
|
|
return 0;
|
|
|
|
return (blockCount - currentBlocks) * 100 / (double) blockCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getBlockCount(){
|
|
|
|
return blockCount;
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private int currentBlocks(){
|
2020-06-03 10:19:57 +02:00
|
|
|
// Entern active
|
2021-03-31 21:58:21 +02:00
|
|
|
if(!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft())
|
2019-12-22 15:20:50 +01:00
|
|
|
return currentBlocks;
|
|
|
|
|
2021-03-20 17:25:13 +01:00
|
|
|
currentBlocks = 0;
|
|
|
|
team.getSchemRegion().forEach((x, y, z) -> {
|
|
|
|
if(!ignoredBlocks.contains(world.getBlockAt(x,y,z).getType()))
|
|
|
|
currentBlocks++;
|
|
|
|
});
|
|
|
|
return currentBlocks;
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|