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;
|
2021-03-10 17:12:24 +01:00
|
|
|
import de.steamwar.fightsystem.countdown.TimeOverCountdown;
|
2019-12-21 20:46:31 +01:00
|
|
|
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
|
|
|
|
|
|
|
import java.util.Collections;
|
2020-01-11 15:44:40 +01:00
|
|
|
import java.util.EnumSet;
|
2019-12-21 20:46:31 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2020-01-11 15:44:40 +01:00
|
|
|
public class WinconditionRelativePercent extends Wincondition{
|
2019-12-21 20:46:31 +01:00
|
|
|
|
|
|
|
private static final Set<Material> ignoredBlocks;
|
2020-01-27 17:36:00 +01:00
|
|
|
private static TeamPercent blue = null;
|
|
|
|
private static TeamPercent red = null;
|
2019-12-21 20:46:31 +01:00
|
|
|
|
|
|
|
static{
|
|
|
|
Set<Material> ignored = new HashSet<>();
|
|
|
|
for(String s : Config.IgnoredBlocks)
|
|
|
|
ignored.add(Material.valueOf(s));
|
|
|
|
ignoredBlocks = Collections.unmodifiableSet(ignored);
|
|
|
|
}
|
|
|
|
|
|
|
|
public WinconditionRelativePercent(){
|
2020-01-11 15:44:40 +01:00
|
|
|
super(Config.RelativePercent || Config.Points, EnumSet.of(FightState.PRE_RUNNING, FightState.RUNNING));
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static TeamPercent getBlue(){
|
2020-01-27 17:36:00 +01:00
|
|
|
if(blue == null)
|
|
|
|
blue = new TeamPercent(Fight.getBlueTeam());
|
2019-12-21 20:46:31 +01:00
|
|
|
return blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TeamPercent getRed(){
|
2020-01-27 17:36:00 +01:00
|
|
|
if(red == null)
|
|
|
|
red = new TeamPercent(Fight.getRedTeam());
|
2019-12-21 20:46:31 +01:00
|
|
|
return red;
|
|
|
|
}
|
|
|
|
|
2020-01-11 15:44:40 +01:00
|
|
|
@Override
|
|
|
|
public void enable() {
|
2020-01-27 17:36:00 +01:00
|
|
|
getBlue();
|
|
|
|
getRed();
|
2020-01-11 15:44:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disable() {
|
|
|
|
blue.task.cancel();
|
|
|
|
red.task.cancel();
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:46:31 +01:00
|
|
|
public static class TeamPercent {
|
|
|
|
private static final World world = Bukkit.getWorlds().get(0);
|
|
|
|
private final FightTeam team;
|
|
|
|
private final int blockCount;
|
2020-01-11 15:44:40 +01:00
|
|
|
private final BukkitTask task;
|
2020-07-26 12:13:36 +02:00
|
|
|
private boolean running = false;
|
2019-12-21 20:46:31 +01:00
|
|
|
|
|
|
|
private int currentBlocks;
|
|
|
|
|
2020-01-11 15:44:40 +01:00
|
|
|
private TeamPercent(FightTeam team){
|
2019-12-21 20:46:31 +01:00
|
|
|
this.team = team;
|
|
|
|
this.blockCount = currentBlocks();
|
2019-12-22 21:58:18 +01:00
|
|
|
this.currentBlocks = blockCount;
|
2019-12-21 20:46:31 +01:00
|
|
|
|
2020-01-11 15:44:40 +01:00
|
|
|
task = Bukkit.getScheduler().runTaskTimerAsynchronously(FightSystem.getPlugin(), () -> {
|
2020-07-26 12:13:36 +02:00
|
|
|
if(running)
|
|
|
|
return;
|
|
|
|
running = true;
|
2019-12-21 20:46:31 +01:00
|
|
|
currentBlocks = currentBlocks();
|
|
|
|
checkPercentDefeat();
|
2020-07-26 12:13:36 +02:00
|
|
|
running = false;
|
2019-12-21 20:46:31 +01:00
|
|
|
}, 400, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getPrintablePercent(){
|
|
|
|
return Math.round(10000.0 * getPercent()) / 100.0;
|
|
|
|
}
|
|
|
|
|
2019-12-22 15:20:50 +01:00
|
|
|
final int getBlockCount(){
|
2019-12-21 20:46:31 +01:00
|
|
|
return blockCount;
|
|
|
|
}
|
|
|
|
|
2019-12-22 15:20:50 +01:00
|
|
|
double getPercent(){
|
2019-12-21 20:46:31 +01:00
|
|
|
int blocksDestroyed = blockCount - currentBlocks;
|
|
|
|
return blocksDestroyed > 0 ? blocksDestroyed / (double) blockCount : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int currentBlocks(){
|
2020-06-03 10:19:57 +02:00
|
|
|
// Entern active
|
2021-03-10 17:12:24 +01:00
|
|
|
if(!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Config.TimeoutTime - TimeOverCountdown.getRemainingTime())
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkPercentDefeat(){
|
|
|
|
if(!Config.RelativePercent)
|
|
|
|
return;
|
2019-12-22 15:20:50 +01:00
|
|
|
if(getPercent() * 100 >= Config.PercentWin){
|
2019-12-21 20:46:31 +01:00
|
|
|
Bukkit.broadcastMessage(FightSystem.PREFIX + "§cDer Schiff von " + team.getColoredName() + " §cwurde zu stark beschädigt!");
|
2020-02-27 19:10:34 +01:00
|
|
|
Bukkit.getScheduler().runTask(FightSystem.getPlugin(), () -> FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"));
|
2019-12-21 20:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|