From 43f4d3bb664deab5e7624a910240fbbcfe7d7cca Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 9 May 2021 12:41:05 +0200 Subject: [PATCH 01/15] Add WinconditionRelativeRedstonePercent --- .../winconditions/Winconditions.java | 1 + .../de/steamwar/fightsystem/FightSystem.java | 3 + .../WinconditionRelativeRedstonePercent.java | 145 ++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java diff --git a/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java b/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java index 0394ee7..4399515 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/winconditions/Winconditions.java @@ -26,6 +26,7 @@ public enum Winconditions { ALL_DEAD, CAPTAIN_DEAD, PERCENT_SYSTEM, + RELATIVE_REDSTONE_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 16a8072..dffc3f6 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -98,6 +98,9 @@ public class FightSystem extends JavaPlugin { new WinconditionWaterTechKO(); new WinconditionPercentSystem(); new WinconditionRelativePercent(); + if (Core.getVersion() >= 15) { + new WinconditionRelativeRedstonePercent(); + } new WinconditionPoints(); new WinconditionTimeout(); new WinconditionHeartRatioTimeout(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java new file mode 100644 index 0000000..e763385 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -0,0 +1,145 @@ +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.StateDependent; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.data.AnaloguePowerable; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Powerable; +import org.bukkit.block.data.Rail; +import org.bukkit.block.data.type.TechnicalPiston; +import org.bukkit.scheduler.BukkitTask; + +import java.util.*; + +public class WinconditionRelativeRedstonePercent extends Wincondition implements PrintableWincondition, PercentWincondition { + + private static final World world = Bukkit.getWorlds().get(0); + private static final Set ignoredBlocks; + + static { + Set ignored = new HashSet<>(); + for (String s : Config.IgnoredBlocks) + ignored.add(Material.valueOf(s)); + ignoredBlocks = Collections.unmodifiableSet(ignored); + } + + 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())); + + if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) { + printableWinconditions.add(this); + percentWincondition = this; + } + } + + @Override + public double getPercent(FightTeam team) { + return teamMap.get(team).getPercent(); + } + + @Override + public String getDisplay(FightTeam team) { + return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; + } + + public static class TeamPercent extends StateDependent { + private final FightTeam team; + + private int blockCount; + private BukkitTask task; + private int currentBlocks; + + public TeamPercent(FightTeam team) { + super(Winconditions.RELATIVE_PERCENT, FightState.Running); + this.team = team; + this.currentBlocks = 1; + register(); + } + + @Override + public void enable() { + blockCount = 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_REDSTONE_PERCENT)) + return; + + if (getPercent() >= Config.PercentWin) { + Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Redstone verloren!"); + FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"); + } + } + + public double getPercent() { + if (currentBlocks > blockCount) + return 0; + return (blockCount - currentBlocks) * 100 / (double) blockCount; + } + + public int getBlockCount() { + return blockCount; + } + + 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) -> { + Block block = world.getBlockAt(x, y, z); + Material material = block.getType(); + if (ignoredBlocks.contains(material)) { + return; + } + BlockData blockData = block.getBlockData(); + if (blockData instanceof Powerable + || blockData instanceof TechnicalPiston + || blockData instanceof AnaloguePowerable + || blockData instanceof Rail) { + currentBlocks++; + return; + } + switch (material) { + case NOTE_BLOCK: + case REDSTONE_BLOCK: + case REDSTONE: + case REPEATER: + case COMPARATOR: + case OBSERVER: + case HOPPER: + case REDSTONE_LAMP: + case REDSTONE_TORCH: + case REDSTONE_WALL_TORCH: + case REDSTONE_WIRE: + currentBlocks++; + break; + default: + break; + } + }); + return currentBlocks; + } + } +} -- 2.39.5 From 08ef10a3d8c607d127c4ed4d3398f17e95dc52be Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 9 May 2021 14:39:55 +0200 Subject: [PATCH 02/15] Add WinconditionRelativeRedstonePercent --- .../WinconditionRelativeRedstonePercent.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index e763385..7b1026e 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -62,7 +62,7 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements private int currentBlocks; public TeamPercent(FightTeam team) { - super(Winconditions.RELATIVE_PERCENT, FightState.Running); + super(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running); this.team = team; this.currentBlocks = 1; register(); @@ -71,7 +71,7 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements @Override public void enable() { blockCount = currentBlocks(); - task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400); + task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 100, 100); } @Override @@ -97,10 +97,6 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements return (blockCount - currentBlocks) * 100 / (double) blockCount; } - public int getBlockCount() { - return blockCount; - } - private int currentBlocks() { // Entern active if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) @@ -125,14 +121,10 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements case NOTE_BLOCK: case REDSTONE_BLOCK: case REDSTONE: - case REPEATER: - case COMPARATOR: - case OBSERVER: case HOPPER: case REDSTONE_LAMP: case REDSTONE_TORCH: case REDSTONE_WALL_TORCH: - case REDSTONE_WIRE: currentBlocks++; break; default: -- 2.39.5 From 784bd88e43753dd3a826f0b2b32bc866d0f07f77 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 9 May 2021 15:36:43 +0200 Subject: [PATCH 03/15] Add WinconditionRelativeRedstonePercent --- .../WinconditionRelativeRedstonePercent.java | 70 ++++++++++++------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index 7b1026e..a2da0b8 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -3,6 +3,7 @@ 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.FightPlayer; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; @@ -15,11 +16,14 @@ import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Powerable; import org.bukkit.block.data.Rail; import org.bukkit.block.data.type.TechnicalPiston; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.scheduler.BukkitTask; import java.util.*; -public class WinconditionRelativeRedstonePercent extends Wincondition implements PrintableWincondition, PercentWincondition { +public class WinconditionRelativeRedstonePercent extends Wincondition implements PrintableWincondition, PercentWincondition, Listener { private static final World world = Bukkit.getWorlds().get(0); private static final Set ignoredBlocks; @@ -41,6 +45,7 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) { printableWinconditions.add(this); percentWincondition = this; + Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin()); } } @@ -54,6 +59,17 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; } + @EventHandler + public void onBlockPlace(BlockPlaceEvent event) { + FightPlayer fightPlayer = Fight.getFightPlayer(event.getPlayer()); + if (fightPlayer == null) { + return; + } + if (validBlock(event.getBlockPlaced())) { + teamMap.get(fightPlayer.getTeam()).blockCount--; + } + } + public static class TeamPercent extends StateDependent { private final FightTeam team; @@ -105,33 +121,39 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements currentBlocks = 0; team.getSchemRegion().forEach((x, y, z) -> { Block block = world.getBlockAt(x, y, z); - Material material = block.getType(); - if (ignoredBlocks.contains(material)) { - return; - } - BlockData blockData = block.getBlockData(); - if (blockData instanceof Powerable - || blockData instanceof TechnicalPiston - || blockData instanceof AnaloguePowerable - || blockData instanceof Rail) { + if (validBlock(block)) { currentBlocks++; - return; - } - switch (material) { - case NOTE_BLOCK: - case REDSTONE_BLOCK: - case REDSTONE: - case HOPPER: - case REDSTONE_LAMP: - case REDSTONE_TORCH: - case REDSTONE_WALL_TORCH: - currentBlocks++; - break; - default: - break; } }); return currentBlocks; } } + + private static boolean validBlock(Block block) { + Material material = block.getType(); + if (ignoredBlocks.contains(material)) { + return false; + } + BlockData blockData = block.getBlockData(); + if (blockData instanceof Powerable + || blockData instanceof TechnicalPiston + || blockData instanceof AnaloguePowerable + || blockData instanceof Rail) { + return true; + } + switch (material) { + case NOTE_BLOCK: + case REDSTONE_BLOCK: + case REDSTONE: + case OBSERVER: + case HOPPER: + case REDSTONE_LAMP: + case REDSTONE_TORCH: + case REDSTONE_WALL_TORCH: + return true; + default: + return false; + } + } + } -- 2.39.5 From c37abd7d54027c6336119a3bcc30a5d17287ce92 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 13 May 2021 12:52:26 +0200 Subject: [PATCH 04/15] Update Config Update PercentWincondition --- .../src/de/steamwar/fightsystem/Config.java | 38 ++++++++++++++++++- .../winconditions/PercentWincondition.java | 13 ++++++- .../WinconditionPercentSystem.java | 2 +- .../WinconditionRelativePercent.java | 16 ++------ .../WinconditionRelativeRedstonePercent.java | 27 +++++++------ 5 files changed, 67 insertions(+), 29 deletions(-) diff --git a/FightSystem_API/src/de/steamwar/fightsystem/Config.java b/FightSystem_API/src/de/steamwar/fightsystem/Config.java index 1fcf4e8..5f3f2ba 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/Config.java @@ -26,6 +26,7 @@ import de.steamwar.sql.EventFight; import de.steamwar.sql.Team; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.World; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -33,7 +34,12 @@ import org.bukkit.util.Vector; import java.io.File; import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; import java.util.logging.Level; +import java.util.stream.Collector; import java.util.stream.Collectors; public class Config { @@ -97,7 +103,7 @@ public class Config { //win condition parameters public static final int TimeoutTime; public static final double PercentWin; - public static final List IgnoredBlocks; + public static final Set Blocks; //default kits public static final String MemberDefault; @@ -186,7 +192,35 @@ public class Config { TimeoutTime = config.getInt("WinConditionParams.TimeoutTime"); PercentWin = config.getDouble("WinConditionParams.PercentWin"); - IgnoredBlocks = Collections.unmodifiableList(config.getStringList("WinConditionParams.IgnoredBlocks")); + Blocks = config.getStringList("WinConditionParams.IgnoredBlocks").stream().map(Material::valueOf).collect(new Collector, Set>() { + @Override + public Supplier> supplier() { + return HashSet::new; + } + + @Override + public BiConsumer, Material> accumulator() { + return HashSet::add; + } + + @Override + public BinaryOperator> combiner() { + return (materials, materials2) -> { + materials.addAll(materials2); + return materials; + }; + } + + @Override + public Function, Set> finisher() { + return Collections::unmodifiableSet; + } + + @Override + public Set characteristics() { + return Collections.emptySet(); + } + }); EnterStages = Collections.unmodifiableList(config.getIntegerList("EnterStages")); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index eacac0b..685166d 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -21,6 +21,15 @@ package de.steamwar.fightsystem.winconditions; import de.steamwar.fightsystem.fight.FightTeam; -public interface PercentWincondition { - double getPercent(FightTeam team); +public abstract class PercentWincondition extends Wincondition implements PrintableWincondition { + + protected PercentWincondition(String windescription) { + super(windescription); + } + + public String getDisplay(FightTeam team) { + return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; + } + + abstract double getPercent(FightTeam team); } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java index 4b4dd1d..f726c9a 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java @@ -33,7 +33,7 @@ import org.bukkit.event.entity.EntityExplodeEvent; import java.util.HashMap; import java.util.Map; -public class WinconditionPercentSystem extends Wincondition implements Listener, PrintableWincondition, PercentWincondition { +public class WinconditionPercentSystem extends PercentWincondition implements Listener { private final Map teamMap = new HashMap<>(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java index db34dec..420379f 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java @@ -26,23 +26,15 @@ import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.scheduler.BukkitTask; -import java.util.*; +import java.util.HashMap; +import java.util.Map; -public class WinconditionRelativePercent extends Wincondition implements PrintableWincondition, PercentWincondition { +public class WinconditionRelativePercent extends PercentWincondition { private static final World world = Bukkit.getWorlds().get(0); - private static final Set ignoredBlocks; - - static{ - Set ignored = new HashSet<>(); - for(String s : Config.IgnoredBlocks) - ignored.add(Material.valueOf(s)); - ignoredBlocks = Collections.unmodifiableSet(ignored); - } private final Map teamMap = new HashMap<>(); @@ -121,7 +113,7 @@ public class WinconditionRelativePercent extends Wincondition implements Printab currentBlocks = 0; team.getSchemRegion().forEach((x, y, z) -> { - if(!ignoredBlocks.contains(world.getBlockAt(x,y,z).getType())) + if(!Config.Blocks.contains(world.getBlockAt(x,y,z).getType())) currentBlocks++; }); return currentBlocks; diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index a2da0b8..8f02e11 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -7,6 +7,7 @@ import de.steamwar.fightsystem.fight.FightPlayer; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; +import de.steamwar.fightsystem.states.StateDependentListener; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; @@ -21,19 +22,12 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.scheduler.BukkitTask; -import java.util.*; +import java.util.HashMap; +import java.util.Map; -public class WinconditionRelativeRedstonePercent extends Wincondition implements PrintableWincondition, PercentWincondition, Listener { +public class WinconditionRelativeRedstonePercent extends PercentWincondition implements Listener { private static final World world = Bukkit.getWorlds().get(0); - private static final Set ignoredBlocks; - - static { - Set ignored = new HashSet<>(); - for (String s : Config.IgnoredBlocks) - ignored.add(Material.valueOf(s)); - ignoredBlocks = Collections.unmodifiableSet(ignored); - } private final Map teamMap = new HashMap<>(); @@ -42,10 +36,19 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements 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.currentBlocks = 0; + }); + } + }; + if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) { printableWinconditions.add(this); percentWincondition = this; - Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin()); } } @@ -131,7 +134,7 @@ public class WinconditionRelativeRedstonePercent extends Wincondition implements private static boolean validBlock(Block block) { Material material = block.getType(); - if (ignoredBlocks.contains(material)) { + if (Config.Blocks.contains(material)) { return false; } BlockData blockData = block.getBlockData(); -- 2.39.5 From cd3ec80f4359a34a00bafb74c6b91f0f51ff6636 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 13 May 2021 13:14:42 +0200 Subject: [PATCH 05/15] Update Config Update PercentWincondition --- .../winconditions/PercentWincondition.java | 40 +++++++++++++ .../WinconditionPercentSystem.java | 33 ++++------- .../WinconditionRelativePercent.java | 56 ++++++++----------- .../WinconditionRelativeRedstonePercent.java | 5 -- 4 files changed, 74 insertions(+), 60 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index 685166d..5abe011 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -19,7 +19,14 @@ 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 java.util.EnumSet; public abstract class PercentWincondition extends Wincondition implements PrintableWincondition { @@ -32,4 +39,37 @@ 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; + + protected TeamPercent(FightTeam team) { + this.team = team; + } + + public void lose() { + Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Schaden erlitten!"); + win(Fight.getOpposite(team)); + } + } + + 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; + } + + public void lose() { + Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Schaden erlitten!"); + win(Fight.getOpposite(team)); + } + } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java index f726c9a..a7fa389 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java @@ -20,12 +20,10 @@ 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.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityExplodeEvent; @@ -42,17 +40,17 @@ public class WinconditionPercentSystem extends PercentWincondition implements Li teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam())); teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam())); - new StateDependentListener(Winconditions.PERCENT_SYSTEM, FightState.Running, this){ + new StateDependentListener(Winconditions.PERCENT_SYSTEM, FightState.Running, this) { @Override public void enable() { super.enable(); teamMap.forEach((team, percent) -> { - percent.destroyedBlocks = 0; + percent.currentBlocks = 0; percent.percent = 0; }); } }; - if(Config.ActiveWinconditions.contains(Winconditions.PERCENT_SYSTEM)){ + if (Config.ActiveWinconditions.contains(Winconditions.PERCENT_SYSTEM)) { printableWinconditions.add(this); percentWincondition = this; } @@ -63,38 +61,29 @@ public class WinconditionPercentSystem extends PercentWincondition implements Li teamMap.values().forEach(teamPercent -> teamPercent.check(event)); } - @Override - public String getDisplay(FightTeam team) { - return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; - } - @Override public double getPercent(FightTeam team) { return teamMap.get(team).percent; } - private class TeamPercent { + private class TeamPercent extends PercentWincondition.TeamPercent { - private final FightTeam team; - private final int volume; private double percent; - private int destroyedBlocks; private TeamPercent(FightTeam team) { - this.team = team; - this.volume = team.getSchemRegion().volume(); + super(team); + totalBlocks = team.getSchemRegion().volume(); } private void check(EntityExplodeEvent event) { - if(!team.getExtendRegion().inRegion(event.getEntity().getLocation())){ + if (!team.getExtendRegion().inRegion(event.getEntity().getLocation())) { return; } - destroyedBlocks += event.blockList().size(); - percent = (double)destroyedBlocks * 100 / volume; - if(percent >= Config.PercentWin) { - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Schaden erlitten!"); - win(Fight.getOpposite(team)); + currentBlocks += event.blockList().size(); + percent = (double) currentBlocks * 100 / totalBlocks; + if (percent >= Config.PercentWin) { + lose(); } } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java index 420379f..13011ee 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java @@ -23,8 +23,6 @@ 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.StateDependent; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.scheduler.BukkitTask; @@ -38,12 +36,12 @@ public class WinconditionRelativePercent extends PercentWincondition { private final Map teamMap = new HashMap<>(); - public WinconditionRelativePercent(){ + public WinconditionRelativePercent() { super("RelativePercent"); teamMap.put(Fight.getBlueTeam(), new TeamPercent(Fight.getBlueTeam())); teamMap.put(Fight.getRedTeam(), new TeamPercent(Fight.getRedTeam())); - if(Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)){ + if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)) { printableWinconditions.add(this); percentWincondition = this; } @@ -54,67 +52,59 @@ public class WinconditionRelativePercent extends PercentWincondition { return teamMap.get(team).getPercent(); } - @Override - public String getDisplay(FightTeam team) { - return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; - } + public class TeamPercent extends StateDependentTeamPercent { - public static class TeamPercent extends StateDependent { - private final FightTeam team; - - private int blockCount; private BukkitTask task; - private int currentBlocks; - public TeamPercent(FightTeam team){ - super(Winconditions.RELATIVE_PERCENT, FightState.Running); - this.team = team; - this.currentBlocks = 1; + public TeamPercent(FightTeam team) { + super(team, Winconditions.RELATIVE_PERCENT); + currentBlocks = 1; register(); } @Override - public void enable(){ - blockCount = currentBlocks(); + public void enable() { + totalBlocks = currentBlocks(); task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 400, 400); } @Override - public void disable(){ + public void disable() { task.cancel(); } - private void check(){ + private void check() { currentBlocks(); - if(!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)) + if (!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)) return; - if(getPercent() >= Config.PercentWin){ - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Schaden erlitten!"); - FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"); + if (getPercent() >= Config.PercentWin) { + lose(); } } - public double getPercent(){ - if(currentBlocks > blockCount) + public double getPercent() { + if (currentBlocks > totalBlocks) { return 0; - return (blockCount - currentBlocks) * 100 / (double) blockCount; + } + return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks; } - public int getBlockCount(){ - return blockCount; + public int getBlockCount() { + return totalBlocks; } - private int currentBlocks(){ + private int currentBlocks() { // Entern active - if(!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) + if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) return currentBlocks; currentBlocks = 0; team.getSchemRegion().forEach((x, y, z) -> { - if(!Config.Blocks.contains(world.getBlockAt(x,y,z).getType())) + if (!Config.Blocks.contains(world.getBlockAt(x, y, z).getType())) { currentBlocks++; + } }); return currentBlocks; } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index 8f02e11..b4c53e5 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -57,11 +57,6 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp return teamMap.get(team).getPercent(); } - @Override - public String getDisplay(FightTeam team) { - return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; - } - @EventHandler public void onBlockPlace(BlockPlaceEvent event) { FightPlayer fightPlayer = Fight.getFightPlayer(event.getPlayer()); -- 2.39.5 From a3c073e04d59975d3cf537627c3ec5ebbad330df Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 13 May 2021 13:20:07 +0200 Subject: [PATCH 06/15] Update WinconditionRelativeRedstonePercent --- .../WinconditionRelativeRedstonePercent.java | 108 +++++------------- 1 file changed, 26 insertions(+), 82 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index b4c53e5..684f2a4 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -1,26 +1,16 @@ 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.FightPlayer; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; -import de.steamwar.fightsystem.states.StateDependent; import de.steamwar.fightsystem.states.StateDependentListener; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; -import org.bukkit.block.data.AnaloguePowerable; -import org.bukkit.block.data.BlockData; -import org.bukkit.block.data.Powerable; -import org.bukkit.block.data.Rail; -import org.bukkit.block.data.type.TechnicalPiston; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPlaceEvent; -import org.bukkit.scheduler.BukkitTask; +import org.bukkit.event.entity.EntityExplodeEvent; import java.util.HashMap; import java.util.Map; @@ -36,7 +26,7 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp 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){ + new StateDependentListener(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running, this) { @Override public void enable() { super.enable(); @@ -58,99 +48,53 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp } @EventHandler - public void onBlockPlace(BlockPlaceEvent event) { - FightPlayer fightPlayer = Fight.getFightPlayer(event.getPlayer()); - if (fightPlayer == null) { - return; - } - if (validBlock(event.getBlockPlaced())) { - teamMap.get(fightPlayer.getTeam()).blockCount--; - } + public void onEntityExplode(EntityExplodeEvent event) { + teamMap.values().forEach(teamPercent -> teamPercent.check(event)); } - public static class TeamPercent extends StateDependent { - private final FightTeam team; + private static boolean validBlock(Block block) { + return Config.Blocks.contains(block.getType()); + } - private int blockCount; - private BukkitTask task; - private int currentBlocks; + public class TeamPercent extends PercentWincondition.TeamPercent { public TeamPercent(FightTeam team) { - super(Winconditions.RELATIVE_REDSTONE_PERCENT, FightState.Running); - this.team = team; - this.currentBlocks = 1; - register(); + super(team); + totalBlocks = currentBlocks(); + currentBlocks = totalBlocks; } - @Override - public void enable() { - blockCount = currentBlocks(); - task = Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), this::check, 100, 100); - } - - @Override - public void disable() { - task.cancel(); - } - - private void check() { - currentBlocks(); - - if (!Config.ActiveWinconditions.contains(Winconditions.RELATIVE_REDSTONE_PERCENT)) + 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) { - Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + team.getColoredName() + " §chat zu viel Redstone verloren!"); - FightSystem.setSpectateState(Fight.getOpposite(team), "RelativePercent"); + lose(); } } public double getPercent() { - if (currentBlocks > blockCount) + if (currentBlocks > totalBlocks) return 0; - return (blockCount - currentBlocks) * 100 / (double) blockCount; + return (totalBlocks - currentBlocks) * 100 / (double) totalBlocks; } private int currentBlocks() { - // Entern active - if (!Config.EnterStages.isEmpty() && Config.EnterStages.get(0) >= Wincondition.getTimeOverCountdown().getTimeLeft()) - return currentBlocks; - - currentBlocks = 0; + totalBlocks = 0; team.getSchemRegion().forEach((x, y, z) -> { Block block = world.getBlockAt(x, y, z); if (validBlock(block)) { - currentBlocks++; + totalBlocks++; } }); - return currentBlocks; - } - } - - private static boolean validBlock(Block block) { - Material material = block.getType(); - if (Config.Blocks.contains(material)) { - return false; - } - BlockData blockData = block.getBlockData(); - if (blockData instanceof Powerable - || blockData instanceof TechnicalPiston - || blockData instanceof AnaloguePowerable - || blockData instanceof Rail) { - return true; - } - switch (material) { - case NOTE_BLOCK: - case REDSTONE_BLOCK: - case REDSTONE: - case OBSERVER: - case HOPPER: - case REDSTONE_LAMP: - case REDSTONE_TORCH: - case REDSTONE_WALL_TORCH: - return true; - default: - return false; + return totalBlocks; } } -- 2.39.5 From 2e7228835bfa5ab25cf79e528a086843a63f0f00 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 13 May 2021 13:22:11 +0200 Subject: [PATCH 07/15] Update WinconditionRelativeRedstonePercent --- .../WinconditionRelativeRedstonePercent.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java index 684f2a4..2d67e3a 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeRedstonePercent.java @@ -31,7 +31,8 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp public void enable() { super.enable(); teamMap.forEach((team, percent) -> { - percent.currentBlocks = 0; + percent.totalBlocks = 0; + percent.currentBlocks = 1; }); } }; @@ -56,14 +57,23 @@ public class WinconditionRelativeRedstonePercent extends PercentWincondition imp return Config.Blocks.contains(block.getType()); } - public class TeamPercent extends PercentWincondition.TeamPercent { + public class TeamPercent extends StateDependentTeamPercent { public TeamPercent(FightTeam team) { - super(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; -- 2.39.5 From a2bbc7818ca2f97278dc91ec49559860dd767a4e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 13 May 2021 13:22:57 +0200 Subject: [PATCH 08/15] Update FightSystem --- FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index dffc3f6..68ea75b 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -98,9 +98,7 @@ public class FightSystem extends JavaPlugin { new WinconditionWaterTechKO(); new WinconditionPercentSystem(); new WinconditionRelativePercent(); - if (Core.getVersion() >= 15) { - new WinconditionRelativeRedstonePercent(); - } + new WinconditionRelativeRedstonePercent(); new WinconditionPoints(); new WinconditionTimeout(); new WinconditionHeartRatioTimeout(); -- 2.39.5 From be241a0c1e9c9e55cc6e3feac3331ebccbed508a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 15 May 2021 21:35:48 +0200 Subject: [PATCH 09/15] 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)); + } + }); + } + +} -- 2.39.5 From fcf75a12ba94a96011b5f7af09792b0360011319 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 22 May 2021 15:33:17 +0200 Subject: [PATCH 10/15] Update Config --- .../src/de/steamwar/fightsystem/Config.java | 30 +------------------ 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/FightSystem_API/src/de/steamwar/fightsystem/Config.java b/FightSystem_API/src/de/steamwar/fightsystem/Config.java index 5f3f2ba..55cccf3 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/Config.java @@ -192,35 +192,7 @@ public class Config { TimeoutTime = config.getInt("WinConditionParams.TimeoutTime"); PercentWin = config.getDouble("WinConditionParams.PercentWin"); - Blocks = config.getStringList("WinConditionParams.IgnoredBlocks").stream().map(Material::valueOf).collect(new Collector, Set>() { - @Override - public Supplier> supplier() { - return HashSet::new; - } - - @Override - public BiConsumer, Material> accumulator() { - return HashSet::add; - } - - @Override - public BinaryOperator> combiner() { - return (materials, materials2) -> { - materials.addAll(materials2); - return materials; - }; - } - - @Override - public Function, Set> finisher() { - return Collections::unmodifiableSet; - } - - @Override - public Set characteristics() { - return Collections.emptySet(); - } - }); + Blocks = config.getStringList("WinConditionParams.IgnoredBlocks").stream().map(Material::valueOf).collect(Collectors.toSet()); EnterStages = Collections.unmodifiableList(config.getIntegerList("EnterStages")); -- 2.39.5 From 0b78786bc2be7bb0bc7b964b52f51a4331c5cde9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 22 May 2021 15:39:51 +0200 Subject: [PATCH 11/15] Update PercentWincondition and other --- .../src/de/steamwar/fightsystem/Config.java | 2 +- .../winconditions/PercentWincondition.java | 17 ++++++---- .../WinconditionPercentSystem.java | 13 +------ .../winconditions/WinconditionPoints.java | 4 +-- .../WinconditionRelativePercent.java | 15 +------- .../WinconditionRelativeWhitelistPercent.java | 34 +++++++++++-------- 6 files changed, 35 insertions(+), 50 deletions(-) diff --git a/FightSystem_API/src/de/steamwar/fightsystem/Config.java b/FightSystem_API/src/de/steamwar/fightsystem/Config.java index 55cccf3..9156fba 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/Config.java @@ -192,7 +192,7 @@ public class Config { TimeoutTime = config.getInt("WinConditionParams.TimeoutTime"); PercentWin = config.getDouble("WinConditionParams.PercentWin"); - Blocks = config.getStringList("WinConditionParams.IgnoredBlocks").stream().map(Material::valueOf).collect(Collectors.toSet()); + Blocks = Collections.unmodifiableSet(config.getStringList("WinConditionParams.IgnoredBlocks").stream().map(Material::valueOf).collect(Collectors.toSet())); EnterStages = Collections.unmodifiableList(config.getIntegerList("EnterStages")); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index 43aeef7..8cce497 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -28,11 +28,15 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; 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 { + protected final Map teamMap = new HashMap<>(); + protected PercentWincondition(String windescription) { super(windescription); } @@ -41,18 +45,19 @@ public abstract class PercentWincondition extends Wincondition implements Printa return team.getPrefix() + "Schaden: " + (Math.round(100.0 * getPercent(team)) / 100.0) + "%"; } - abstract double getPercent(FightTeam team); - + public double getPercent(FightTeam team) { + return teamMap.get(team).getPercent(); + } public static class TeamPercent implements Listener { - public FightTeam fightTeam; - private Predicate testType; + public final FightTeam fightTeam; + private final Predicate testType; public int totalBlocks = 0; private int currentBlocks = 0; - private Consumer explodeConsumer; + private final Consumer explodeConsumer; public TeamPercent(FightTeam fightTeam, Winconditions wincondition, Predicate testType, Consumer enableConsumer, Consumer explodeConsumer) { this.fightTeam = fightTeam; @@ -62,7 +67,7 @@ public abstract class PercentWincondition extends Wincondition implements Printa new OneShotStateDependent(wincondition, FightState.Running, () -> { enableConsumer.accept(TeamPercent.this); currentBlocks = totalBlocks; - }).register(); + }); new StateDependentListener(wincondition, FightState.Running, this).register(); } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java index 307bb7d..aee2740 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java @@ -24,14 +24,8 @@ import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightTeam; import org.bukkit.Bukkit; -import org.bukkit.event.Listener; -import java.util.HashMap; -import java.util.Map; - -public class WinconditionPercentSystem extends PercentWincondition implements Listener { - - private final Map teamMap = new HashMap<>(); +public class WinconditionPercentSystem extends PercentWincondition { public WinconditionPercentSystem() { super("Percent"); @@ -54,9 +48,4 @@ public class WinconditionPercentSystem extends PercentWincondition implements Li } }); } - - @Override - public double getPercent(FightTeam team) { - 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 548d0b1..11083b3 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java @@ -130,9 +130,7 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond } }); teamPercent.totalBlocks = currentBlocks.get(); - }, teamPercent -> { - - }); + }, teamPercent -> {}); } public void enable() { diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java index ddf2e9c..0f833a2 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java @@ -26,16 +26,10 @@ import de.steamwar.fightsystem.fight.FightTeam; import org.bukkit.Bukkit; import org.bukkit.World; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - public class WinconditionRelativePercent extends PercentWincondition { private static final World world = Bukkit.getWorlds().get(0); - private final Map teamMap = new HashMap<>(); - public WinconditionRelativePercent() { super("RelativePercent"); teamMap.put(Fight.getBlueTeam(), create(Fight.getBlueTeam())); @@ -47,22 +41,15 @@ public class WinconditionRelativePercent extends PercentWincondition { } } - @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++; } }); - teamPercent.totalBlocks = currentBlocks.get(); }, teamPercent -> { if (teamPercent.getPercent() >= Config.PercentWin) { Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java index 71f9ea0..fcd9acf 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java @@ -1,3 +1,22 @@ +/* + 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 . +*/ + package de.steamwar.fightsystem.winconditions; import de.steamwar.fightsystem.Config; @@ -8,16 +27,10 @@ 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())); @@ -29,22 +42,15 @@ public class WinconditionRelativeWhitelistPercent extends PercentWincondition im } } - @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++; } }); - teamPercent.totalBlocks = currentBlocks.get(); }, teamPercent -> { if (teamPercent.getPercent() >= Config.PercentWin) { Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); -- 2.39.5 From 21ff7fddaf906c45022165f13d5c993e5b843326 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 23 May 2021 16:59:56 +0200 Subject: [PATCH 12/15] Update PercentWincondition and other --- .../winconditions/PercentWincondition.java | 28 +++++++++++++++++-- .../WinconditionPercentSystem.java | 17 ++++------- .../winconditions/WinconditionPoints.java | 12 +------- .../WinconditionRelativePercent.java | 24 ++++------------ .../WinconditionRelativeWhitelistPercent.java | 24 ++++------------ 5 files changed, 41 insertions(+), 64 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index 8cce497..5508dae 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -19,11 +19,14 @@ package de.steamwar.fightsystem.winconditions; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.OneShotStateDependent; import de.steamwar.fightsystem.states.StateDependentListener; +import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityExplodeEvent; @@ -37,8 +40,13 @@ public abstract class PercentWincondition extends Wincondition implements Printa protected final Map teamMap = new HashMap<>(); - protected PercentWincondition(String windescription) { + protected PercentWincondition(String windescription, Winconditions winconditions) { super(windescription); + + if (Config.ActiveWinconditions.contains(winconditions)) { + printableWinconditions.add(this); + percentWincondition = this; + } } public String getDisplay(FightTeam team) { @@ -51,6 +59,8 @@ public abstract class PercentWincondition extends Wincondition implements Printa public static class TeamPercent implements Listener { + private static final World world = Bukkit.getWorlds().get(0); + public final FightTeam fightTeam; private final Predicate testType; @@ -59,21 +69,33 @@ public abstract class PercentWincondition extends Wincondition implements Printa private final Consumer explodeConsumer; - public TeamPercent(FightTeam fightTeam, Winconditions wincondition, Predicate testType, Consumer enableConsumer, Consumer explodeConsumer) { + public TeamPercent(PercentWincondition percentWincondition, FightTeam fightTeam, Winconditions wincondition, Predicate testType, Consumer explodeConsumer) { this.fightTeam = fightTeam; this.testType = testType; this.explodeConsumer = explodeConsumer; new OneShotStateDependent(wincondition, FightState.Running, () -> { - enableConsumer.accept(TeamPercent.this); + fightTeam.getSchemRegion().forEach((x, y, z) -> { + if (testType.test(world.getBlockAt(x, y, z).getType())) { + totalBlocks++; + } + }); currentBlocks = totalBlocks; }); new StateDependentListener(wincondition, FightState.Running, this).register(); + if (percentWincondition != null) { + percentWincondition.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())) { return; } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java index aee2740..f58ba42 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPercentSystem.java @@ -28,20 +28,13 @@ import org.bukkit.Bukkit; public class WinconditionPercentSystem extends PercentWincondition { public WinconditionPercentSystem() { - super("Percent"); - teamMap.put(Fight.getBlueTeam(), create(Fight.getBlueTeam())); - teamMap.put(Fight.getRedTeam(), create(Fight.getRedTeam())); - - if (Config.ActiveWinconditions.contains(Winconditions.PERCENT_SYSTEM)) { - printableWinconditions.add(this); - percentWincondition = this; - } + super("Percent", Winconditions.PERCENT_SYSTEM); + create(Fight.getBlueTeam()); + create(Fight.getRedTeam()); } - private TeamPercent create(FightTeam fightTeam) { - return new TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM, material -> true, teamPercent -> { - teamPercent.totalBlocks = teamPercent.fightTeam.getSchemRegion().volume(); - }, teamPercent -> { + 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)); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java index 11083b3..c4a3af2 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java @@ -38,7 +38,6 @@ 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 { @@ -121,16 +120,7 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond TeamPoints(FightTeam team){ this.team = 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 -> {}); + this.percent = new PercentWincondition.TeamPercent(null, team, Winconditions.POINTS, material -> true, teamPercent -> {}); } public void enable() { diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java index 0f833a2..69fd28a 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativePercent.java @@ -24,32 +24,18 @@ 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; public class WinconditionRelativePercent extends PercentWincondition { - private static final World world = Bukkit.getWorlds().get(0); - public WinconditionRelativePercent() { - super("RelativePercent"); - teamMap.put(Fight.getBlueTeam(), create(Fight.getBlueTeam())); - teamMap.put(Fight.getRedTeam(), create(Fight.getRedTeam())); - - if (Config.ActiveWinconditions.contains(Winconditions.RELATIVE_PERCENT)) { - printableWinconditions.add(this); - percentWincondition = this; - } + super("RelativePercent", Winconditions.RELATIVE_PERCENT); + create(Fight.getBlueTeam()); + create(Fight.getRedTeam()); } - private PercentWincondition.TeamPercent create(FightTeam fightTeam) { - return new PercentWincondition.TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM, material -> { + private void create(FightTeam fightTeam) { + new TeamPercent(this, fightTeam, Winconditions.RELATIVE_WHITELIST_PERCENT, material -> { return !Config.Blocks.contains(material); - }, teamPercent -> { - teamPercent.fightTeam.getSchemRegion().forEach((x, y, z) -> { - if (!Config.Blocks.contains(world.getBlockAt(x, y, z).getType())) { - teamPercent.totalBlocks++; - } - }); }, teamPercent -> { if (teamPercent.getPercent() >= Config.PercentWin) { Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java index fcd9acf..ce1ef28 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java @@ -24,33 +24,19 @@ 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; public class WinconditionRelativeWhitelistPercent extends PercentWincondition implements Listener { - private static final World world = Bukkit.getWorlds().get(0); - 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; - } + super("RelativeWhitelistPercent", Winconditions.RELATIVE_WHITELIST_PERCENT); + create(Fight.getBlueTeam()); + create(Fight.getRedTeam()); } - private PercentWincondition.TeamPercent create(FightTeam fightTeam) { - return new PercentWincondition.TeamPercent(fightTeam, Winconditions.PERCENT_SYSTEM, material -> { + private void create(FightTeam fightTeam) { + new PercentWincondition.TeamPercent(this, fightTeam, Winconditions.RELATIVE_WHITELIST_PERCENT, material -> { return Config.Blocks.contains(material); - }, teamPercent -> { - teamPercent.fightTeam.getSchemRegion().forEach((x, y, z) -> { - if (Config.Blocks.contains(world.getBlockAt(x, y, z).getType())) { - teamPercent.totalBlocks++; - } - }); }, teamPercent -> { if (teamPercent.getPercent() >= Config.PercentWin) { Bukkit.broadcastMessage(FightSystem.PREFIX + "§cTeam " + teamPercent.fightTeam.getColoredName() + " §chat zu viel Schaden erlitten!"); -- 2.39.5 From 21c6baf40fe33b4ec4027bc39e95a1c08fda1ac0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 23 May 2021 19:23:55 +0200 Subject: [PATCH 13/15] Update PercentWincondition Update WinconditionPoints --- .../winconditions/PercentWincondition.java | 5 +++++ .../winconditions/WinconditionPoints.java | 13 +++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java index 5508dae..30a28ca 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/PercentWincondition.java @@ -70,6 +70,10 @@ public abstract class PercentWincondition extends Wincondition implements Printa 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) { this.fightTeam = fightTeam; this.testType = testType; this.explodeConsumer = explodeConsumer; @@ -81,6 +85,7 @@ public abstract class PercentWincondition extends Wincondition implements Printa } }); currentBlocks = totalBlocks; + afterEnable.run(); }); new StateDependentListener(wincondition, FightState.Running, this).register(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java index c4a3af2..59390ab 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java @@ -25,11 +25,9 @@ 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.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; @@ -41,8 +39,6 @@ import java.util.Map; 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(){ @@ -52,9 +48,6 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond teamMap.put(Fight.getRedTeam(), new TeamPoints(Fight.getRedTeam())); new StateDependentListener(Winconditions.POINTS, FightState.Ingame, this); - 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); @@ -109,6 +102,7 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond return team.getPrefix() + "Punkte: " + teamMap.get(team).getPoints(); } + private static boolean run = false; private class TeamPoints { private static final int MAX_POINTS = 2000; @@ -120,7 +114,10 @@ public class WinconditionPoints extends Wincondition implements PrintableWincond TeamPoints(FightTeam team){ this.team = team; - this.percent = new PercentWincondition.TeamPercent(null, team, Winconditions.POINTS, material -> true, teamPercent -> {}); + this.percent = new PercentWincondition.TeamPercent(null, team, Winconditions.POINTS, material -> true, teamPercent -> {}, () -> { + if (run) teamMap.values().forEach(TeamPoints::enable); + run = !run; + }); } public void enable() { -- 2.39.5 From 504f23f1d93195553c0456ba556f4e67b75c908e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 24 May 2021 08:24:33 +0200 Subject: [PATCH 14/15] 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); } - }); + }; } } -- 2.39.5 From bffa12619c4d2c7026f3962ca8b3820a8534d74e Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 24 May 2021 08:30:02 +0200 Subject: [PATCH 15/15] Fix points Signed-off-by: Lixfel --- .../winconditions/WinconditionPoints.java | 17 ++++++++++------- .../WinconditionRelativeWhitelistPercent.java | 1 - 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java index b661148..84959d4 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionPoints.java @@ -118,17 +118,20 @@ public class WinconditionPoints extends PercentWincondition implements Listener @Override protected void enable() { super.enable(); - points = 0; - int ownBlocks = totalBlocks; - int enemyBlocks = teamMap.get(Fight.getOpposite(fightTeam)).totalBlocks; - - if(enemyBlocks == 0) + TeamPoints opponent = teamMap.get(Fight.getOpposite(fightTeam)); + if(opponent.totalBlocks == 0) return; - if(enemyBlocks < ownBlocks) { + setup(opponent.totalBlocks); + opponent.setup(totalBlocks); + } + + private void setup(int enemyBlocks){ + points = 0; + if(enemyBlocks < totalBlocks) { this.factor = 100; //Original mit 20 (20% = 0.2 ergeben 2000 Punkte } else { - double f = 100.0 * ownBlocks / enemyBlocks; + double f = 100.0 * totalBlocks / enemyBlocks; if(f > 100) f = 100; diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java index cc48b84..b64df9c 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/winconditions/WinconditionRelativeWhitelistPercent.java @@ -41,5 +41,4 @@ public class WinconditionRelativeWhitelistPercent extends PercentWincondition im } }; } - } -- 2.39.5