From 504f23f1d93195553c0456ba556f4e67b75c908e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 24 May 2021 08:24:33 +0200 Subject: [PATCH] Fix the Bugfest! And the wrong behaviour. Signed-off-by: Lixfel --- .../winconditions/PercentWincondition.java | 84 ++++++++++--------- .../WinconditionPercentSystem.java | 18 ++-- .../winconditions/WinconditionPoints.java | 37 ++++---- .../WinconditionRelativePercent.java | 12 +-- .../WinconditionRelativeWhitelistPercent.java | 15 ++-- 5 files changed, 80 insertions(+), 86 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index 30a28ca..6a603d8 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -20,6 +20,8 @@ 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.OneShotStateDependent; @@ -33,11 +35,11 @@ import org.bukkit.event.entity.EntityExplodeEvent; import java.util.HashMap; import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Predicate; public abstract class PercentWincondition extends Wincondition implements PrintableWincondition { + private static final World world = Bukkit.getWorlds().get(0); + protected final Map teamMap = new HashMap<>(); protected PercentWincondition(String windescription, Winconditions winconditions) { @@ -57,63 +59,65 @@ public abstract class PercentWincondition extends Wincondition implements Printa return teamMap.get(team).getPercent(); } - public static class TeamPercent implements Listener { + protected class TeamPercent implements Listener { + protected final FightTeam fightTeam; - private static final World world = Bukkit.getWorlds().get(0); - - public final FightTeam fightTeam; - private final Predicate testType; - - public int totalBlocks = 0; + protected int totalBlocks = 0; private int currentBlocks = 0; - private final Consumer explodeConsumer; - - public TeamPercent(PercentWincondition percentWincondition, FightTeam fightTeam, Winconditions wincondition, Predicate testType, Consumer explodeConsumer) { - this(percentWincondition, fightTeam, wincondition, testType, explodeConsumer, () -> {}); - } - - public TeamPercent(PercentWincondition percentWincondition, FightTeam fightTeam, Winconditions wincondition, Predicate testType, Consumer explodeConsumer, Runnable afterEnable) { + protected TeamPercent(FightTeam fightTeam, Winconditions wincondition) { this.fightTeam = fightTeam; - this.testType = testType; - this.explodeConsumer = explodeConsumer; - - new OneShotStateDependent(wincondition, FightState.Running, () -> { - fightTeam.getSchemRegion().forEach((x, y, z) -> { - if (testType.test(world.getBlockAt(x, y, z).getType())) { - totalBlocks++; - } - }); - currentBlocks = totalBlocks; - afterEnable.run(); - }); + new OneShotStateDependent(wincondition, FightState.Running, this::enable); new StateDependentListener(wincondition, FightState.Running, this).register(); - if (percentWincondition != null) { - percentWincondition.teamMap.put(fightTeam, this); - } + teamMap.put(fightTeam, this); } @EventHandler public void onEntityExplode(EntityExplodeEvent event) { - // Entern active - if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) { - return; - } - - if (!fightTeam.getExtendRegion().inRegion(event.getEntity().getLocation())) { + if (checkEntern() || !fightTeam.getExtendRegion().inRegion(event.getEntity().getLocation())) { return; } event.blockList().forEach(block -> { - if (testType.test(block.getType())) { + if (test(block.getType())) { currentBlocks--; } }); - explodeConsumer.accept(this); + + checkWin(); } - public double getPercent() { + protected void enable(){ + countTotal(); + currentBlocks = totalBlocks; + } + + protected void countTotal(){ + totalBlocks = 0; + fightTeam.getSchemRegion().forEach((x, y, z) -> { + if (test(world.getBlockAt(x, y, z).getType())) { + totalBlocks++; + } + }); + } + + protected boolean checkEntern(){ + return !Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft(); + } + + protected boolean test(Material type){ + return !Config.Blocks.contains(type); + } + + protected void checkWin(){ + if (getPercent() >= Config.PercentWin) { + Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); + win(Fight.getOpposite(fightTeam)); + } + } + + protected double getPercent() { if (currentBlocks >= totalBlocks) { return 0; } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java index f58ba42..1285a61 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java @@ -19,11 +19,8 @@ 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; public class WinconditionPercentSystem extends PercentWincondition { @@ -34,11 +31,16 @@ public class WinconditionPercentSystem extends PercentWincondition { } private void create(FightTeam fightTeam) { - new TeamPercent(this, fightTeam, Winconditions.PERCENT_SYSTEM, material -> true, teamPercent -> { - if (teamPercent.getPercent() >= Config.PercentWin) { - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); - win(Fight.getOpposite(teamPercent.fightTeam)); + new TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM){ + @Override + protected boolean checkEntern() { + return false; } - }); + + @Override + protected void countTotal() { + totalBlocks = fightTeam.getSchemRegion().volume(); + } + }; } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java index 59390ab..b661148 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java @@ -37,20 +37,18 @@ import org.bukkit.event.player.PlayerQuitEvent; import java.util.HashMap; import java.util.Map; -public class WinconditionPoints extends Wincondition implements PrintableWincondition, Listener { +public class WinconditionPoints extends PercentWincondition implements Listener { private final Map teamMap = new HashMap<>(); public WinconditionPoints(){ - super("Points"); - + super("Points", Winconditions.POINTS); teamMap.put(Fight.getBlueTeam(), new TeamPoints(Fight.getBlueTeam())); teamMap.put(Fight.getRedTeam(), new TeamPoints(Fight.getRedTeam())); new StateDependentListener(Winconditions.POINTS, FightState.Ingame, this); if(Config.ActiveWinconditions.contains(Winconditions.POINTS)){ timeOverCountdown = new StateDependentCountdown(Winconditions.POINTS, FightState.Running, new TimeOverCountdown(this::timeOver)); - printableWinconditions.add(this); } } @@ -102,27 +100,30 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond return team.getPrefix() + "Punkte: " + teamMap.get(team).getPoints(); } - private static boolean run = false; - private class TeamPoints { + private class TeamPoints extends TeamPercent { private static final int MAX_POINTS = 2000; - private final FightTeam team; - private final PercentWincondition.TeamPercent percent; - private double factor; private int points; TeamPoints(FightTeam team){ - this.team = team; - this.percent = new PercentWincondition.TeamPercent(null, team, Winconditions.POINTS, material -> true, teamPercent -> {}, () -> { - if (run) teamMap.values().forEach(TeamPoints::enable); - run = !run; - }); + super(team, Winconditions.POINTS); } - public void enable() { - int ownBlocks = percent.totalBlocks; - int enemyBlocks = teamMap.get(Fight.getOpposite(team)).percent.totalBlocks; + @Override + protected void checkWin() { + //ignored + } + + @Override + protected void enable() { + super.enable(); + points = 0; + int ownBlocks = totalBlocks; + int enemyBlocks = teamMap.get(Fight.getOpposite(fightTeam)).totalBlocks; + + if(enemyBlocks == 0) + return; if(enemyBlocks < ownBlocks) { this.factor = 100; //Original mit 20 (20% = 0.2 ergeben 2000 Punkte @@ -139,7 +140,7 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond } public int getPoints(){ - int damagePoints = (int)(teamMap.get(Fight.getOpposite(team)).percent.getPercent() * factor); + int damagePoints = (int)(teamMap.get(Fight.getOpposite(fightTeam)).getPercent() * factor); if(damagePoints > MAX_POINTS) damagePoints = MAX_POINTS; return points + damagePoints; diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java index 69fd28a..c042646 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java @@ -19,11 +19,8 @@ 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; public class WinconditionRelativePercent extends PercentWincondition { @@ -34,13 +31,6 @@ public class WinconditionRelativePercent extends PercentWincondition { } private void create(FightTeam fightTeam) { - new TeamPercent(this, fightTeam, Winconditions.RELATIVE_WHITELIST_PERCENT, material -> { - return !Config.Blocks.contains(material); - }, teamPercent -> { - if (teamPercent.getPercent() >= Config.PercentWin) { - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); - win(Fight.getOpposite(teamPercent.fightTeam)); - } - }); + new TeamPercent(fightTeam, Winconditions.RELATIVE_PERCENT); } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java index ce1ef28..cc48b84 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java @@ -20,10 +20,9 @@ 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.event.Listener; public class WinconditionRelativeWhitelistPercent extends PercentWincondition implements Listener { @@ -35,14 +34,12 @@ public class WinconditionRelativeWhitelistPercent extends PercentWincondition im } private void create(FightTeam fightTeam) { - new PercentWincondition.TeamPercent(this, fightTeam, Winconditions.RELATIVE_WHITELIST_PERCENT, material -> { - return Config.Blocks.contains(material); - }, teamPercent -> { - if (teamPercent.getPercent() >= Config.PercentWin) { - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); - win(Fight.getOpposite(teamPercent.fightTeam)); + new PercentWincondition.TeamPercent(fightTeam, Winconditions.RELATIVE_WHITELIST_PERCENT){ + @Override + protected boolean test(Material type) { + return Config.Blocks.contains(type); } - }); + }; } }