From 793c6fe6b61f85f20d9c0b36bf78732a63207d9a Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 14 Mar 2022 09:49:27 +0100 Subject: [PATCH] Improve WaterTechKO performance Signed-off-by: Lixfel --- .../WinconditionWaterTechKO.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java b/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java index 8552e24..86bc839 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionWaterTechKO.java @@ -28,8 +28,11 @@ import de.steamwar.fightsystem.utils.FlatteningWrapper; import de.steamwar.fightsystem.utils.Message; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.block.Block; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; public class WinconditionWaterTechKO extends Wincondition implements PrintableWincondition { @@ -43,7 +46,13 @@ public class WinconditionWaterTechKO extends Wincondition implements PrintableWi teamMap.put(Fight.getBlueTeam(), new TeamWater(Fight.getBlueTeam())); teamMap.put(Fight.getRedTeam(), new TeamWater(Fight.getRedTeam())); - new StateDependentTask(Winconditions.WATER_TECH_KO, FightState.Ingame, this::check, 200, 200); + new StateDependentTask(Winconditions.WATER_TECH_KO, FightState.Ingame, this::check, 200, 200){ + @Override + public void enable() { + teamMap.values().forEach(TeamWater::find); + super.enable(); + } + }; if(Config.ActiveWinconditions.contains(Winconditions.WATER_TECH_KO)){ printableWinconditions.add(this); } @@ -55,25 +64,29 @@ public class WinconditionWaterTechKO extends Wincondition implements PrintableWi @Override public Message getDisplay(FightTeam team) { - return new Message("BAR_WATER", team.getPrefix() + teamMap.get(team).water); + return new Message("BAR_WATER", team.getPrefix() + teamMap.get(team).water.size()); } private class TeamWater { private final FightTeam team; - private int water; + private final List water = new ArrayList<>(); private TeamWater(FightTeam team) { this.team = team; } - private void check() { - water = 0; + private void find() { + water.clear(); team.getExtendRegion().forEach((x, y, z) -> { - if (FlatteningWrapper.impl.isWater(WORLD.getBlockAt(x, y, z))) - water++; + Block block = WORLD.getBlockAt(x, y, z); + if (FlatteningWrapper.impl.isWater(block)) + water.add(block); }); + } - if(water == 0){ + private void check() { + water.removeIf(block -> !FlatteningWrapper.impl.isWater(block)); + if(water.isEmpty()){ win(Fight.getOpposite(team), "WIN_TECHKO", team.getColoredName()); } }