From 3321001b227672d284c9f5d0184480b25646d31c Mon Sep 17 00:00:00 2001 From: Stachelbeere1248 Date: Sat, 9 Dec 2023 14:51:27 +0100 Subject: [PATCH] v1.2.1 --- .../handlers/Round1Correction.java | 21 +++++++++++++++++++ .../zombiesutils/timer/Timer.java | 18 +++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/github/stachelbeere1248/zombiesutils/handlers/Round1Correction.java diff --git a/src/main/java/com/github/stachelbeere1248/zombiesutils/handlers/Round1Correction.java b/src/main/java/com/github/stachelbeere1248/zombiesutils/handlers/Round1Correction.java new file mode 100644 index 0000000..6a54894 --- /dev/null +++ b/src/main/java/com/github/stachelbeere1248/zombiesutils/handlers/Round1Correction.java @@ -0,0 +1,21 @@ +package com.github.stachelbeere1248.zombiesutils.handlers; + +import com.github.stachelbeere1248.zombiesutils.timer.Timer; +import net.minecraft.entity.Entity; +import net.minecraft.entity.monster.EntityZombie; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.EntityJoinWorldEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import org.jetbrains.annotations.NotNull; + +public class Round1Correction { + + @SubscribeEvent + public void onWaveSpawn(@NotNull EntityJoinWorldEvent event) { + final Entity entity = event.entity; + if (entity instanceof EntityZombie) { + Timer.getInstance().ifPresent(Timer::correctRn); + MinecraftForge.EVENT_BUS.unregister(this); + } + } +} diff --git a/src/main/java/com/github/stachelbeere1248/zombiesutils/timer/Timer.java b/src/main/java/com/github/stachelbeere1248/zombiesutils/timer/Timer.java index f211000..b82439a 100644 --- a/src/main/java/com/github/stachelbeere1248/zombiesutils/timer/Timer.java +++ b/src/main/java/com/github/stachelbeere1248/zombiesutils/timer/Timer.java @@ -5,10 +5,12 @@ import com.github.stachelbeere1248.zombiesutils.config.ZombiesUtilsConfig; import com.github.stachelbeere1248.zombiesutils.game.GameMode; import com.github.stachelbeere1248.zombiesutils.game.Map; import com.github.stachelbeere1248.zombiesutils.game.sla.SLA; +import com.github.stachelbeere1248.zombiesutils.handlers.Round1Correction; import com.github.stachelbeere1248.zombiesutils.timer.recorder.Category; import com.github.stachelbeere1248.zombiesutils.timer.recorder.files.GameFile; import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; +import net.minecraftforge.common.MinecraftForge; import org.jetbrains.annotations.NotNull; import java.util.Optional; @@ -17,22 +19,24 @@ public class Timer { private static Timer instance; private final GameMode gameMode; - private final long savedTotalWorldTime; + private long savedTotalWorldTime; private int passedRoundsTickSum = 0; private final String serverNumber; public Category category; private final GameFile gameFile; private boolean pbTracking = false; private int round; + private final Round1Correction correction; /** - * Constructs a timer and saves it to {@link #instance}. * @param serverNumber The game's server the timer should be bound to. * @param map The map the timer should be started for. * @param round If available, round to begin splitting. */ public Timer (@NotNull String serverNumber, @NotNull Map map, byte round) { - savedTotalWorldTime = getCurrentTotalWorldTime(); + dropInstances(); + + this.savedTotalWorldTime = getCurrentTotalWorldTime(); if (!serverNumber.trim().isEmpty()) this.serverNumber = serverNumber.trim(); else throw new RuntimeException("invalid servernumber"); @@ -43,6 +47,9 @@ public class Timer { this.round = round; if (ZombiesUtilsConfig.isSlaToggled()) SLA.instance = new SLA(map); + correction = new Round1Correction(); + MinecraftForge.EVENT_BUS.register(correction); + } @@ -65,6 +72,9 @@ public class Timer { passedRoundsTickSum = gameTime; round = passedRound; } + public void correctRn() { + savedTotalWorldTime = getCurrentTotalWorldTime()-200L; + } private void record(byte passedRound, short roundTime, int gameTime) { if (passedRound == (byte) 1) pbTracking = true; @@ -111,6 +121,8 @@ public class Timer { * Call to invalidate {@link #instance} to trigger the garbage collector */ public static void dropInstances() { + if (instance == null) return; + if (instance.correction != null) MinecraftForge.EVENT_BUS.unregister(instance.correction); instance = null; } public byte getRound() {