From be241a0c1e9c9e55cc6e3feac3331ebccbed508a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 15 May 2021 21:35:48 +0200 Subject: [PATCH] Update PercentWincondition and WinconditionRelativeWhitelistPercent --- .../winconditions/Winconditions.java | 2 +- .../de/steamwar/fightsystem/FightSystem.java | 2 +- .../states/OneShotStateDependent.java | 8 ++ .../winconditions/PercentWincondition.java | 72 +++++++----- .../WinconditionPercentSystem.java | 56 +++------ .../winconditions/WinconditionPoints.java | 45 +++---- .../WinconditionRelativePercent.java | 73 +++--------- .../WinconditionRelativeRedstonePercent.java | 111 ------------------ .../WinconditionRelativeWhitelistPercent.java | 56 +++++++++ 9 files changed, 164 insertions(+), 261 deletions(-) delete mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java diff --git a/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java b/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java index 4399515..a4263cc 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java @@ -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, diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index 68ea75b..f38b48a 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -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(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/states/OneShotStateDependent.java b/FightSystem_Main/src/de/steamwar/fightsystem/states/OneShotStateDependent.java index 564415b..988e287 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/states/OneShotStateDependent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/states/OneShotStateDependent.java @@ -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 states, Runnable runnable) { + super(Config.ActiveWinconditions.contains(wincondition), states); + this.runnable = runnable; + register(); + } + public OneShotStateDependent(Set mode, Set states, Runnable runnable) { super(mode, states); this.runnable = runnable; diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index 5abe011..43aeef7 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -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 testType; + + public int totalBlocks = 0; + private int currentBlocks = 0; + + private Consumer explodeConsumer; + + public TeamPercent(FightTeam fightTeam, Winconditions wincondition, Predicate testType, Consumer enableConsumer, Consumer 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; } } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java index a7fa389..307bb7d 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java @@ -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(); } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java index 421188e..548d0b1 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java @@ -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 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() { - teamMap.values().forEach(TeamPoints::enable); - } - - @Override - public void disable() { - teamMap.values().forEach(TeamPoints::disable); - } - }.register(); + new OneShotStateDependent(Winconditions.POINTS, FightState.Ingame, () -> { + teamMap.values().forEach(TeamPoints::enable); + }).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) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java index 13011ee..ddf2e9c 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java @@ -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)); + } + }); } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java deleted file mode 100644 index 2d67e3a..0000000 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ /dev/null @@ -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 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; - } - } - -} diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java new file mode 100644 index 0000000..71f9ea0 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java @@ -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 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)); + } + }); + } + +}