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;
|
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-10 22:45:17 +01:00
|
|
|
super(Winconditions.RELATIVE_PERCENT, EnumSet.of(FightState.PRE_RUNNING, FightState.RUNNING), "RelativePercent");
|
|
|
|
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
|
|
|
|
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
|
|
|
|
printableWinconditions.add(this);
|
|
|
|
percentWincondition = this;
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
@Override
|
|
|
|
public void enable() {
|
|
|
|
teamMap.values().forEach(TeamPercent::enable);
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:45:17 +01:00
|
|
|
@Override
|
|
|
|
public void disable() {
|
|
|
|
teamMap.values().forEach(teamPercent -> teamPercent.task.cancel());
|
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
|
|
|
}
|
|
|
|
|
2019-12-21 20:46:31 +01:00
|
|
|
public static class TeamPercent {
|
|
|
|
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){
|
2019-12-21 20:46:31 +01:00
|
|
|
this.team = team;
|
2021-03-10 22:45:17 +01:00
|
|
|
this.currentBlocks = 1;
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
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-10 22:45:17 +01:00
|
|
|
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");
|
|
|
|
}
|
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-10 22:45:17 +01:00
|
|
|
if(!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Config.TimeoutTime - TimeoutWincondition.getTimeOverCountdown().getTimeLeft())
|
2019-12-22 15:20:50 +01:00
|
|
|
return currentBlocks;
|
|
|
|
|
2019-12-21 20:46:31 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|