diff options
4 files changed, 61 insertions, 59 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java b/spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java new file mode 100644 index 0000000..9747784 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/tick/SimpleTickReporter.java @@ -0,0 +1,48 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.tick; + +public abstract class SimpleTickReporter extends AbstractTickReporter { + private boolean closed = false; + private long start = 0; + + protected void onStart() { + if (this.closed) { + return; + } + + this.start = System.nanoTime(); + } + + protected void onEnd() { + if (this.closed || this.start == 0) { + return; + } + + double duration = (System.nanoTime() - this.start) / 1000000d; + onTick(duration); + } + + @Override + public void close() { + this.closed = true; + } +} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickReporter.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickReporter.java index b581620..b71ac84 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickReporter.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickReporter.java @@ -20,7 +20,7 @@ package me.lucko.spark.fabric; -import me.lucko.spark.common.tick.AbstractTickReporter; +import me.lucko.spark.common.tick.SimpleTickReporter; import me.lucko.spark.common.tick.TickReporter; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; @@ -28,36 +28,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.server.MinecraftServer; -public abstract class FabricTickReporter extends AbstractTickReporter implements TickReporter { - private boolean closed = false; - - private long start = 0; - - protected void onStart() { - if (this.closed) { - return; - } - - this.start = System.nanoTime(); - } - - protected void onEnd() { - if (this.closed) { - return; - } - - if (this.start == 0) { - return; - } - - double duration = (System.nanoTime() - this.start) / 1000000d; - onTick(duration); - } - - @Override - public void close() { - this.closed = true; - } +public abstract class FabricTickReporter extends SimpleTickReporter implements TickReporter { public static final class Server extends FabricTickReporter implements ServerTickEvents.StartTick, ServerTickEvents.EndTick { @Override diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java index d03b2d6..c33443b 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java @@ -20,18 +20,16 @@ package me.lucko.spark.forge; -import me.lucko.spark.common.tick.AbstractTickReporter; +import me.lucko.spark.common.tick.SimpleTickReporter; import me.lucko.spark.common.tick.TickReporter; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; -public class ForgeTickReporter extends AbstractTickReporter implements TickReporter { +public class ForgeTickReporter extends SimpleTickReporter implements TickReporter { private final TickEvent.Type type; - private long start = 0; - public ForgeTickReporter(TickEvent.Type type) { this.type = type; } @@ -43,19 +41,9 @@ public class ForgeTickReporter extends AbstractTickReporter implements TickRepor } switch (e.phase) { - case START: - this.start = System.nanoTime(); - break; - case END: - if (this.start == 0) { - return; - } - - double duration = (System.nanoTime() - this.start) / 1000000d; - onTick(duration); - break; - default: - throw new AssertionError(e.phase); + case START -> onStart(); + case END -> onEnd(); + default -> throw new AssertionError(e.phase); } } @@ -67,6 +55,7 @@ public class ForgeTickReporter extends AbstractTickReporter implements TickRepor @Override public void close() { MinecraftForge.EVENT_BUS.unregister(this); + super.close(); } } diff --git a/spark-forge1122/src/main/java/me/lucko/spark/forge/Forge1122TickReporter.java b/spark-forge1122/src/main/java/me/lucko/spark/forge/Forge1122TickReporter.java index 97698d4..cd2cfd8 100644 --- a/spark-forge1122/src/main/java/me/lucko/spark/forge/Forge1122TickReporter.java +++ b/spark-forge1122/src/main/java/me/lucko/spark/forge/Forge1122TickReporter.java @@ -20,18 +20,16 @@ package me.lucko.spark.forge; -import me.lucko.spark.common.tick.AbstractTickReporter; +import me.lucko.spark.common.tick.SimpleTickReporter; import me.lucko.spark.common.tick.TickReporter; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent; -public class Forge1122TickReporter extends AbstractTickReporter implements TickReporter { +public class Forge1122TickReporter extends SimpleTickReporter implements TickReporter { private final TickEvent.Type type; - private long start = 0; - public Forge1122TickReporter(TickEvent.Type type) { this.type = type; } @@ -44,15 +42,10 @@ public class Forge1122TickReporter extends AbstractTickReporter implements TickR switch (e.phase) { case START: - this.start = System.nanoTime(); + onStart(); break; case END: - if (this.start == 0) { - return; - } - - double duration = (System.nanoTime() - this.start) / 1000000d; - onTick(duration); + onEnd(); break; default: throw new AssertionError(e.phase); @@ -67,6 +60,7 @@ public class Forge1122TickReporter extends AbstractTickReporter implements TickR @Override public void close() { MinecraftForge.EVENT_BUS.unregister(this); + super.close(); } } |