diff options
5 files changed, 128 insertions, 13 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java index 87fb214..6a0b980 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricSparkMod.java @@ -29,6 +29,7 @@ import net.fabricmc.loader.api.ModContainer; import net.minecraft.client.MinecraftClient; import java.nio.file.Path; +import java.util.Objects; public class FabricSparkMod implements ModInitializer { private static FabricSparkMod mod; @@ -44,12 +45,14 @@ public class FabricSparkMod implements ModInitializer { this.container = loader.getModContainer("spark") .orElseThrow(() -> new IllegalStateException("Unable to get container for spark")); this.configDirectory = loader.getConfigDir().resolve("spark"); + // load hooks ServerLifecycleEvents.SERVER_STARTING.register(server -> FabricServerSparkPlugin.register(this, server)); } // called be entrypoint defined in fabric.mod.json public static void initializeClient() { + Objects.requireNonNull(FabricSparkMod.mod, "mod"); FabricClientSparkPlugin.register(FabricSparkMod.mod, MinecraftClient.getInstance()); } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickHook.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickHook.java index 9003091..c48f095 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickHook.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickHook.java @@ -28,39 +28,41 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.server.MinecraftServer; public abstract class FabricTickHook extends AbstractTickHook implements TickHook { - protected boolean closed = false; @Override + protected void onTick() { + if (!this.closed) { + super.onTick(); + } + } + + @Override public void close() { this.closed = true; } - public static final class Server extends FabricTickHook implements ServerTickEvents.EndTick { + public static final class Server extends FabricTickHook implements ServerTickEvents.StartTick { @Override - public void onEndTick(MinecraftServer minecraftServer) { - if (!this.closed) { - onTick(); - } + public void onStartTick(MinecraftServer minecraftServer) { + onTick(); } @Override public void start() { - ServerTickEvents.END_SERVER_TICK.register(this); + ServerTickEvents.START_SERVER_TICK.register(this); } } - public static final class Client extends FabricTickHook implements ClientTickEvents.EndTick { + public static final class Client extends FabricTickHook implements ClientTickEvents.StartTick { @Override - public void onEndTick(MinecraftClient minecraftClient) { - if (!this.closed) { - onTick(); - } + public void onStartTick(MinecraftClient minecraftClient) { + onTick(); } @Override public void start() { - ClientTickEvents.END_CLIENT_TICK.register(this); + ClientTickEvents.START_CLIENT_TICK.register(this); } } } 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 new file mode 100644 index 0000000..21a5f52 --- /dev/null +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricTickReporter.java @@ -0,0 +1,96 @@ +/* + * 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.fabric; + +import me.lucko.spark.common.sampler.tick.AbstractTickReporter; +import me.lucko.spark.common.sampler.tick.TickReporter; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +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 static final class Server extends FabricTickReporter implements ServerTickEvents.StartTick, ServerTickEvents.EndTick { + @Override + public void onStartTick(MinecraftServer minecraftServer) { + onStart(); + } + + @Override + public void onEndTick(MinecraftServer minecraftServer) { + onEnd(); + } + + @Override + public void start() { + ServerTickEvents.START_SERVER_TICK.register(this); + ServerTickEvents.END_SERVER_TICK.register(this); + } + } + + public static final class Client extends FabricTickReporter implements ClientTickEvents.StartTick, ClientTickEvents.EndTick { + @Override + public void onStartTick(MinecraftClient minecraftClient) { + onStart(); + } + + @Override + public void onEndTick(MinecraftClient minecraftClient) { + onEnd(); + } + + @Override + public void start() { + ClientTickEvents.START_CLIENT_TICK.register(this); + ClientTickEvents.END_CLIENT_TICK.register(this); + } + } +} diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java index 9c2fbe4..ef39fd2 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricClientSparkPlugin.java @@ -29,11 +29,13 @@ import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.sampler.tick.TickHook; +import me.lucko.spark.common.sampler.tick.TickReporter; import me.lucko.spark.fabric.FabricCommandSender; import me.lucko.spark.fabric.FabricPlatformInfo; import me.lucko.spark.fabric.FabricSparkGameHooks; import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickHook; +import me.lucko.spark.fabric.FabricTickReporter; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.server.command.CommandOutput; @@ -129,6 +131,11 @@ public class FabricClientSparkPlugin extends FabricSparkPlugin implements Sugges } @Override + public TickReporter createTickReporter() { + return new FabricTickReporter.Client(); + } + + @Override public PlatformInfo getPlatformInfo() { return new FabricPlatformInfo(PlatformInfo.Type.CLIENT); } diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java index 7d7c474..9375668 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java @@ -28,10 +28,12 @@ import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.sampler.tick.TickHook; +import me.lucko.spark.common.sampler.tick.TickReporter; import me.lucko.spark.fabric.FabricCommandSender; import me.lucko.spark.fabric.FabricPlatformInfo; import me.lucko.spark.fabric.FabricSparkMod; import me.lucko.spark.fabric.FabricTickHook; +import me.lucko.spark.fabric.FabricTickReporter; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; @@ -118,6 +120,11 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman } @Override + public TickReporter createTickReporter() { + return new FabricTickReporter.Server(); + } + + @Override public PlatformInfo getPlatformInfo() { return new FabricPlatformInfo(PlatformInfo.Type.SERVER); } |