From 0f9a268bdf682e2a9a89aa5688c88c69d94e2839 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 9 Apr 2022 21:34:10 +0200 Subject: [PATCH] Improve TimeTechKO performance and behaviour Signed-off-by: Lixfel --- .../winconditions/WinconditionTimeTechKO.java | 124 ++++++++++-------- 1 file changed, 66 insertions(+), 58 deletions(-) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionTimeTechKO.java b/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionTimeTechKO.java index 6e3135c..1659edb 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionTimeTechKO.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/winconditions/WinconditionTimeTechKO.java @@ -19,87 +19,95 @@ package de.steamwar.fightsystem.winconditions; -import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.countdown.Countdown; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightTeam; -import de.steamwar.fightsystem.listener.Recording; import de.steamwar.fightsystem.states.FightState; +import de.steamwar.fightsystem.states.StateDependent; +import de.steamwar.fightsystem.states.StateDependentListener; import de.steamwar.fightsystem.states.StateDependentTask; import de.steamwar.fightsystem.utils.Message; import de.steamwar.fightsystem.utils.SWSound; +import org.bukkit.Location; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.EntitySpawnEvent; -public class WinconditionTimeTechKO extends Wincondition { +import java.util.HashMap; +import java.util.Map; + +public class WinconditionTimeTechKO extends Wincondition implements Listener { private static final int TECH_KO_TIME_IN_S = 90; - private static final int TECH_KO_HALF_TIME = TECH_KO_TIME_IN_S*10; + private static final int TECH_KO_HALF_TIME = TECH_KO_TIME_IN_S/2; - private final double toggle = Config.SpecSpawn.getZ(); - private final FightTeam smallerZteam; - private final FightTeam biggerZteam; + private final Map spawnLocations = new HashMap<>(); + private final Map countdowns = new HashMap<>(); + private final Map currentTime = new HashMap<>(); - private int smallerZtime = TECH_KO_HALF_TIME; - private int biggerZtime = TECH_KO_HALF_TIME; - private TechKOCountdown smallerZcountdown = null; - private TechKOCountdown biggerZcountdown = null; - - /** - * Works only for z-Axis fight direction for performance reasons - */ public WinconditionTimeTechKO(){ super("TechKO"); - if(Config.blueNegZ()) { - smallerZteam = Fight.getBlueTeam(); - biggerZteam = Fight.getRedTeam(); - }else{ - smallerZteam = Fight.getRedTeam(); - biggerZteam = Fight.getBlueTeam(); - } + new StateDependentListener(Winconditions.TIME_TECH_KO, FightState.Running, this); + new StateDependentTask(Winconditions.TIME_TECH_KO, FightState.Running, this::run, 20, 20); + new StateDependent(Winconditions.TIME_TECH_KO, FightState.Running) { + @Override + public void enable() { + Fight.teams().forEach(team -> currentTime.put(team, TECH_KO_HALF_TIME)); + } - new StateDependentTask(Winconditions.TIME_TECH_KO, FightState.Running, this::run, 1, 1){ @Override public void disable() { - super.disable(); - if(smallerZcountdown != null){ - smallerZcountdown.disable(); - smallerZcountdown = null; - } - if(biggerZcountdown != null){ - biggerZcountdown.disable(); - biggerZcountdown = null; - } + spawnLocations.clear(); + currentTime.clear(); + countdowns.values().forEach(Countdown::disable); + countdowns.clear(); } - }; + }.register(); + } + + @EventHandler + public void onSpawn(EntitySpawnEvent e) { + if(e.getEntityType() != EntityType.PRIMED_TNT) + return; + + Location location = e.getLocation(); + for(FightTeam team : Fight.teams()) { + if(team.getExtendRegion().inRegion(location)) { + spawnLocations.put(e.getEntity().getEntityId(), team); + break; + } + } + } + + @EventHandler + public void onExplode(EntityExplodeEvent e) { + if(e.getEntityType() != EntityType.PRIMED_TNT) + return; + + FightTeam spawn = spawnLocations.remove(e.getEntity().getEntityId()); + if(spawn == null) + return; + + Location location = e.getLocation(); + for(FightTeam team : Fight.teams()) { + if(team != spawn && team.getExtendRegion().inRegion(location)) { + currentTime.put(spawn, TECH_KO_HALF_TIME); + TechKOCountdown countdown = countdowns.remove(spawn); + if(countdown != null) + countdown.disable(); + } + } } private void run(){ - Recording.iterateOverEntities(Recording.primedTnt::isInstance, tnt -> { - double z = tnt.getLocation().getZ(); - boolean smallerZ = z < toggle; - boolean wasSmallerZ = z - tnt.getVelocity().getZ() < toggle; - if(wasSmallerZ && !smallerZ) { - smallerZtime = TECH_KO_HALF_TIME; - if(smallerZcountdown != null){ - smallerZcountdown.disable(); - smallerZcountdown = null; - } - }else if(!wasSmallerZ && smallerZ){ - biggerZtime = TECH_KO_HALF_TIME; - if(biggerZcountdown != null){ - biggerZcountdown.disable(); - biggerZcountdown = null; - } - } + currentTime.entrySet().forEach(entry -> { + entry.setValue(entry.getValue() - 1); + if(entry.getValue() == 0) + countdowns.put(entry.getKey(), new TechKOCountdown(entry.getKey(), TECH_KO_HALF_TIME)); }); - - if(smallerZtime == 0) - smallerZcountdown = new TechKOCountdown(smallerZteam, TECH_KO_HALF_TIME / 20); - if(biggerZtime == 0) - biggerZcountdown = new TechKOCountdown(biggerZteam, TECH_KO_HALF_TIME / 20); - - smallerZtime--; - biggerZtime--; } private class TechKOCountdown extends Countdown {