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;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class WinconditionRelativePercent{
|
|
|
|
|
|
|
|
private static final Set<Material> ignoredBlocks;
|
|
|
|
private static TeamPercent blue;
|
|
|
|
private static TeamPercent red;
|
|
|
|
|
|
|
|
static{
|
|
|
|
Set<Material> ignored = new HashSet<>();
|
|
|
|
for(String s : Config.IgnoredBlocks)
|
|
|
|
ignored.add(Material.valueOf(s));
|
|
|
|
ignoredBlocks = Collections.unmodifiableSet(ignored);
|
|
|
|
}
|
|
|
|
|
|
|
|
public WinconditionRelativePercent(){
|
|
|
|
if(!Config.RelativePercent && !Config.Points)
|
|
|
|
return;
|
|
|
|
|
|
|
|
blue = new TeamPercent(Fight.getBlueTeam());
|
|
|
|
red = new TeamPercent(Fight.getRedTeam());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TeamPercent getBlue(){
|
|
|
|
return blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TeamPercent getRed(){
|
|
|
|
return red;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class TeamPercent {
|
|
|
|
private static final World world = Bukkit.getWorlds().get(0);
|
|
|
|
private final FightTeam team;
|
|
|
|
private final int blockCount;
|
|
|
|
|
|
|
|
private int currentBlocks;
|
|
|
|
|
|
|
|
TeamPercent(FightTeam team){
|
|
|
|
this.team = team;
|
|
|
|
this.blockCount = currentBlocks();
|
|
|
|
|
|
|
|
Bukkit.getScheduler().runTaskTimerAsynchronously(FightSystem.getPlugin(), () -> {
|
|
|
|
currentBlocks = currentBlocks();
|
|
|
|
checkPercentDefeat();
|
|
|
|
}, 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(){
|
2019-12-22 15:20:50 +01:00
|
|
|
if(FightSystem.isEntern())
|
|
|
|
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!");
|
|
|
|
FightSystem.setSpectateState(Fight.getOpposite(team));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|