SteamWar/FightSystem
Archiviert
13
1

Update PercentWincondition and WinconditionRelativeWhitelistPercent

Dieser Commit ist enthalten in:
yoyosource 2021-05-15 21:35:48 +02:00
Ursprung a2bbc7818c
Commit be241a0c1e
9 geänderte Dateien mit 164 neuen und 261 gelöschten Zeilen

Datei anzeigen

@ -26,7 +26,7 @@ public enum Winconditions {
ALL_DEAD,
CAPTAIN_DEAD,
PERCENT_SYSTEM,
RELATIVE_REDSTONE_PERCENT,
RELATIVE_WHITELIST_PERCENT,
RELATIVE_PERCENT,
POINTS,
TIME_TECH_KO,

Datei anzeigen

@ -98,7 +98,7 @@ public class FightSystem extends JavaPlugin {
new WinconditionWaterTechKO();
new WinconditionPercentSystem();
new WinconditionRelativePercent();
new WinconditionRelativeRedstonePercent();
new WinconditionRelativeWhitelistPercent();
new WinconditionPoints();
new WinconditionTimeout();
new WinconditionHeartRatioTimeout();

Datei anzeigen

@ -20,6 +20,8 @@
package de.steamwar.fightsystem.states;
import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.winconditions.Winconditions;
import java.util.Set;
@ -27,6 +29,12 @@ public class OneShotStateDependent extends StateDependent{
private final Runnable runnable;
public OneShotStateDependent(Winconditions wincondition, Set<FightState> states, Runnable runnable) {
super(Config.ActiveWinconditions.contains(wincondition), states);
this.runnable = runnable;
register();
}
public OneShotStateDependent(Set<ArenaMode> mode, Set<FightState> states, Runnable runnable) {
super(mode, states);
this.runnable = runnable;

Datei anzeigen

@ -19,14 +19,17 @@
package de.steamwar.fightsystem.winconditions;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent;
import org.bukkit.Bukkit;
import de.steamwar.fightsystem.states.OneShotStateDependent;
import de.steamwar.fightsystem.states.StateDependentListener;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.EnumSet;
import java.util.function.Consumer;
import java.util.function.Predicate;
public abstract class PercentWincondition extends Wincondition implements PrintableWincondition {
@ -40,36 +43,49 @@ public abstract class PercentWincondition extends Wincondition implements Printa
abstract double getPercent(FightTeam team);
public class TeamPercent {
public final FightTeam team;
public int totalBlocks;
public int currentBlocks;
public static class TeamPercent implements Listener {
protected TeamPercent(FightTeam team) {
this.team = team;
public FightTeam fightTeam;
private Predicate<Material> testType;
public int totalBlocks = 0;
private int currentBlocks = 0;
private Consumer<TeamPercent> explodeConsumer;
public TeamPercent(FightTeam fightTeam, Winconditions wincondition, Predicate<Material> testType, Consumer<TeamPercent> enableConsumer, Consumer<TeamPercent> explodeConsumer) {
this.fightTeam = fightTeam;
this.testType = testType;
this.explodeConsumer = explodeConsumer;
new OneShotStateDependent(wincondition, FightState.Running, () -> {
enableConsumer.accept(TeamPercent.this);
currentBlocks = totalBlocks;
}).register();
new StateDependentListener(wincondition, FightState.Running, this).register();
}
public void lose() {
Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Schaden erlitten!");
win(Fight.getOpposite(team));
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
if (!fightTeam.getExtendRegion().inRegion(event.getEntity().getLocation())) {
return;
}
public abstract class StateDependentTeamPercent extends StateDependent {
public final FightTeam team;
public int totalBlocks;
public int currentBlocks;
protected StateDependentTeamPercent(FightTeam team, Winconditions winconditions) {
super(winconditions, FightState.Running);
this.team = team;
event.blockList().forEach(block -> {
if (testType.test(block.getType())) {
currentBlocks--;
}
});
explodeConsumer.accept(this);
}
public void lose() {
Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Schaden erlitten!");
win(Fight.getOpposite(team));
public double getPercent() {
if (currentBlocks >= totalBlocks) {
return 0;
}
return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks;
}
}
}

Datei anzeigen

@ -20,13 +20,11 @@
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 de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import org.bukkit.event.EventHandler;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.HashMap;
import java.util.Map;
@ -37,54 +35,28 @@ public class WinconditionPercentSystem extends PercentWincondition implements Li
public WinconditionPercentSystem() {
super("Percent");
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
teamMap.put(Fight.getBlueTeam(), create(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), create(Fight.getRedTeam()));
new StateDependentListener(Winconditions.PERCENT_SYSTEM, FightState.Running, this) {
@Override
public void enable() {
super.enable();
teamMap.forEach((team, percent) -> {
percent.currentBlocks = 0;
percent.percent = 0;
});
}
};
if (Config.ActiveWinconditions.contains(Winconditions.PERCENT_SYSTEM)) {
printableWinconditions.add(this);
percentWincondition = this;
}
}
@EventHandler
public void handleEntityExplode(EntityExplodeEvent event) {
teamMap.values().forEach(teamPercent -> teamPercent.check(event));
private TeamPercent create(FightTeam fightTeam) {
return new TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM, material -> true, teamPercent -> {
teamPercent.totalBlocks = teamPercent.fightTeam.getSchemRegion().volume();
}, teamPercent -> {
if (teamPercent.getPercent() >= Config.PercentWin) {
Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!");
win(Fight.getOpposite(teamPercent.fightTeam));
}
});
}
@Override
public double getPercent(FightTeam team) {
return teamMap.get(team).percent;
}
private class TeamPercent extends PercentWincondition.TeamPercent {
private double percent;
private TeamPercent(FightTeam team) {
super(team);
totalBlocks = team.getSchemRegion().volume();
}
private void check(EntityExplodeEvent event) {
if (!team.getExtendRegion().inRegion(event.getEntity().getLocation())) {
return;
}
currentBlocks += event.blockList().size();
percent = (double) currentBlocks * 100 / totalBlocks;
if (percent >= Config.PercentWin) {
lose();
}
}
return teamMap.get(team).getPercent();
}
}

Datei anzeigen

@ -25,10 +25,11 @@ import de.steamwar.fightsystem.countdown.TimeOverCountdown;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent;
import de.steamwar.fightsystem.states.OneShotStateDependent;
import de.steamwar.fightsystem.states.StateDependentCountdown;
import de.steamwar.fightsystem.states.StateDependentListener;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -37,9 +38,12 @@ import org.bukkit.event.player.PlayerQuitEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class WinconditionPoints extends Wincondition implements PrintableWincondition, Listener {
private static final World world = Bukkit.getWorlds().get(0);
private final Map<FightTeam, TeamPoints> teamMap = new HashMap<>();
public WinconditionPoints(){
@ -49,17 +53,9 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond
teamMap.put(Fight.getRedTeam(), new TeamPoints(Fight.getRedTeam()));
new StateDependentListener(Winconditions.POINTS, FightState.Ingame, this);
new StateDependent(Winconditions.POINTS, FightState.Ingame){
@Override
public void enable() {
new OneShotStateDependent(Winconditions.POINTS, FightState.Ingame, () -> {
teamMap.values().forEach(TeamPoints::enable);
}
@Override
public void disable() {
teamMap.values().forEach(TeamPoints::disable);
}
}.register();
}).register();
if(Config.ActiveWinconditions.contains(Winconditions.POINTS)){
timeOverCountdown = new StateDependentCountdown(Winconditions.POINTS, FightState.Running, new TimeOverCountdown(this::timeOver));
printableWinconditions.add(this);
@ -118,21 +114,30 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond
private static final int MAX_POINTS = 2000;
private final FightTeam team;
private final WinconditionRelativePercent.TeamPercent percent;
private final PercentWincondition.TeamPercent percent;
private double factor;
private int points;
TeamPoints(FightTeam team){
this.team = team;
this.percent = new WinconditionRelativePercent.TeamPercent(team);
this.percent = new PercentWincondition.TeamPercent(team, Winconditions.POINTS, material -> true, teamPercent -> {
this.points = 0;
AtomicInteger currentBlocks = new AtomicInteger();
teamPercent.fightTeam.getSchemRegion().forEach((x, y, z) -> {
if (!Config.Blocks.contains(world.getBlockAt(x, y, z).getType())) {
currentBlocks.getAndIncrement();
}
});
teamPercent.totalBlocks = currentBlocks.get();
}, teamPercent -> {
});
}
public void enable() {
this.points = 0;
percent.enable();
int ownBlocks = percent.getBlockCount();
int enemyBlocks = teamMap.get(Fight.getOpposite(team)).percent.getBlockCount();
int ownBlocks = percent.totalBlocks;
int enemyBlocks = teamMap.get(Fight.getOpposite(team)).percent.totalBlocks;
if(enemyBlocks < ownBlocks) {
this.factor = 100; //Original mit 20 (20% = 0.2 ergeben 2000 Punkte
@ -148,10 +153,6 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond
}
}
public void disable(){
percent.disable();
}
public int getPoints(){
int damagePoints = (int)(teamMap.get(Fight.getOpposite(team)).percent.getPercent() * factor);
if(damagePoints > MAX_POINTS)

Datei anzeigen

@ -25,10 +25,10 @@ import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightTeam;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class WinconditionRelativePercent extends PercentWincondition {
@ -38,8 +38,8 @@ public class WinconditionRelativePercent extends PercentWincondition {
public WinconditionRelativePercent() {
super("RelativePercent");
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
teamMap.put(Fight.getBlueTeam(), create(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), create(Fight.getRedTeam()));
if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)) {
printableWinconditions.add(this);
@ -52,61 +52,22 @@ public class WinconditionRelativePercent extends PercentWincondition {
return teamMap.get(team).getPercent();
}
public class TeamPercent extends StateDependentTeamPercent {
private BukkitTask task;
public TeamPercent(FightTeam team) {
super(team, Winconditions.RELATIVE_PERCENT);
currentBlocks = 1;
register();
}
@Override
public void enable() {
totalBlocks = currentBlocks();
task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400);
}
@Override
public void disable() {
task.cancel();
}
private void check() {
currentBlocks();
if (!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT))
return;
if (getPercent() >= Config.PercentWin) {
lose();
}
}
public double getPercent() {
if (currentBlocks > totalBlocks) {
return 0;
}
return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks;
}
public int getBlockCount() {
return totalBlocks;
}
private int currentBlocks() {
// Entern active
if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft())
return currentBlocks;
currentBlocks = 0;
team.getSchemRegion().forEach((x, y, z) -> {
private PercentWincondition.TeamPercent create(FightTeam fightTeam) {
return new PercentWincondition.TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM, material -> {
return !Config.Blocks.contains(material);
}, teamPercent -> {
AtomicInteger currentBlocks = new AtomicInteger();
teamPercent.fightTeam.getSchemRegion().forEach((x, y, z) -> {
if (!Config.Blocks.contains(world.getBlockAt(x, y, z).getType())) {
currentBlocks++;
currentBlocks.getAndIncrement();
}
});
return currentBlocks;
teamPercent.totalBlocks = currentBlocks.get();
}, teamPercent -> {
if (teamPercent.getPercent() >= Config.PercentWin) {
Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!");
win(Fight.getOpposite(teamPercent.fightTeam));
}
});
}
}

Datei anzeigen

@ -1,111 +0,0 @@
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 org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
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 WinconditionRelativeRedstonePercent extends PercentWincondition implements Listener {
private static final World world = Bukkit.getWorlds().get(0);
private final Map<FightTeam, TeamPercent> teamMap = new HashMap<>();
public WinconditionRelativeRedstonePercent() {
super("RelativeRedstonePercent");
teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam()));
new StateDependentListener(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running, this) {
@Override
public void enable() {
super.enable();
teamMap.forEach((team, percent) -> {
percent.totalBlocks = 0;
percent.currentBlocks = 1;
});
}
};
if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) {
printableWinconditions.add(this);
percentWincondition = this;
}
}
@Override
public double getPercent(FightTeam team) {
return teamMap.get(team).getPercent();
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
teamMap.values().forEach(teamPercent -> teamPercent.check(event));
}
private static boolean validBlock(Block block) {
return Config.Blocks.contains(block.getType());
}
public class TeamPercent extends StateDependentTeamPercent {
public TeamPercent(FightTeam team) {
super(team, Winconditions.RELATIVE_REDSTONE_PERCENT);
}
@Override
public void enable() {
totalBlocks = currentBlocks();
currentBlocks = totalBlocks;
}
@Override
public void disable() {
}
private void check(EntityExplodeEvent event) {
if (!team.getExtendRegion().inRegion(event.getEntity().getLocation())) {
return;
}
event.blockList().forEach(block -> {
if (validBlock(block)) {
currentBlocks--;
}
});
if (getPercent() >= Config.PercentWin) {
lose();
}
}
public double getPercent() {
if (currentBlocks > totalBlocks)
return 0;
return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks;
}
private int currentBlocks() {
totalBlocks = 0;
team.getSchemRegion().forEach((x, y, z) -> {
Block block = world.getBlockAt(x, y, z);
if (validBlock(block)) {
totalBlocks++;
}
});
return totalBlocks;
}
}
}

Datei anzeigen

@ -0,0 +1,56 @@
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.World;
import org.bukkit.event.Listener;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class WinconditionRelativeWhitelistPercent extends PercentWincondition implements Listener {
private static final World world = Bukkit.getWorlds().get(0);
private final Map<FightTeam, TeamPercent> teamMap = new HashMap<>();
public WinconditionRelativeWhitelistPercent() {
super("RelativeWhitelistPercent");
teamMap.put(Fight.getBlueTeam(), create(Fight.getBlueTeam()));
teamMap.put(Fight.getRedTeam(), create(Fight.getRedTeam()));
if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_WHITELIST_PERCENT)) {
printableWinconditions.add(this);
percentWincondition = this;
}
}
@Override
public double getPercent(FightTeam team) {
return teamMap.get(team).getPercent();
}
private PercentWincondition.TeamPercent create(FightTeam fightTeam) {
return new PercentWincondition.TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM, material -> {
return Config.Blocks.contains(material);
}, teamPercent -> {
AtomicInteger currentBlocks = new AtomicInteger();
teamPercent.fightTeam.getSchemRegion().forEach((x, y, z) -> {
if (Config.Blocks.contains(world.getBlockAt(x, y, z).getType())) {
currentBlocks.getAndIncrement();
}
});
teamPercent.totalBlocks = currentBlocks.get();
}, teamPercent -> {
if (teamPercent.getPercent() >= Config.PercentWin) {
Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!");
win(Fight.getOpposite(teamPercent.fightTeam));
}
});
}
}