diff --git a/gradle.properties b/gradle.properties index ac54901..3b05398 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx2G # Mod Properties -mod_version = 0.0.37 +mod_version = 0.0.38 maven_group = dev.ithundxr archives_base_name = RailwaysTweaks diff --git a/src/main/java/dev/ithundxr/railwaystweaks/RailwaysTweaks.java b/src/main/java/dev/ithundxr/railwaystweaks/RailwaysTweaks.java index e1ebd0b..1fd979f 100644 --- a/src/main/java/dev/ithundxr/railwaystweaks/RailwaysTweaks.java +++ b/src/main/java/dev/ithundxr/railwaystweaks/RailwaysTweaks.java @@ -1,6 +1,7 @@ package dev.ithundxr.railwaystweaks; import dev.ithundxr.railwaystweaks.compat.BanModCompat; +import dev.ithundxr.railwaystweaks.utils.MSPTTracker; import net.fabricmc.api.EnvType; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.FabricLoader; @@ -12,7 +13,9 @@ public class RailwaysTweaks implements ModInitializer { public static final String MODID = "railwaystweaks"; public static final String NAME = "RailwaysTweaks"; public static final Logger LOGGER = LoggerFactory.getLogger(NAME); - + + public static final MSPTTracker MSPT_TRACKER = new MSPTTracker(200); + @Override public void onInitialize() { LOGGER.info("Railways Tweaks is loading..."); diff --git a/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java b/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java index 07ecf0d..5fb2757 100644 --- a/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java +++ b/src/main/java/dev/ithundxr/railwaystweaks/commands/RailwaysTweaksCommands.java @@ -3,6 +3,7 @@ import com.mojang.brigadier.builder.ArgumentBuilder; import com.simibubi.create.Create; import com.simibubi.create.content.trains.entity.CarriageContraptionEntity; +import dev.ithundxr.railwaystweaks.RailwaysTweaks; import dev.ithundxr.railwaystweaks.mixin.compat.tconstruct.SimpleChannelAccessor; import me.pepperbell.simplenetworking.C2SPacket; import me.pepperbell.simplenetworking.S2CPacket; @@ -29,6 +30,12 @@ public static void init() { dispatcher.register(literal("railwaystweaks") .then($dump_trains())); }); + + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + dispatcher.register(literal("avgmspt") + .requires(cs -> cs.hasPermission(2)) + .executes(ctx -> avgMSPT(ctx.getSource()))); + }); } private static ArgumentBuilder $dump_hephaestus_packets() { @@ -97,4 +104,9 @@ private static int dumpCreateTrains(CommandSourceStack source) { return 0; } + + private static int avgMSPT(CommandSourceStack source) { + source.sendSuccess(() -> Component.literal("Average MSPT (10s): " + String.format("%.1f", RailwaysTweaks.MSPT_TRACKER.getAverageMSPT())), true); + return 0; + } } diff --git a/src/main/java/dev/ithundxr/railwaystweaks/mixin/compat/create/SchematicannonBlockEntityMixin.java b/src/main/java/dev/ithundxr/railwaystweaks/mixin/compat/create/SchematicannonBlockEntityMixin.java new file mode 100644 index 0000000..17dff0e --- /dev/null +++ b/src/main/java/dev/ithundxr/railwaystweaks/mixin/compat/create/SchematicannonBlockEntityMixin.java @@ -0,0 +1,28 @@ +package dev.ithundxr.railwaystweaks.mixin.compat.create; + +import com.simibubi.create.content.schematics.cannon.SchematicannonBlockEntity; +import dev.ithundxr.railwaystweaks.RailwaysTweaks; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(SchematicannonBlockEntity.class) +public class SchematicannonBlockEntityMixin { + + @Shadow private int printerCooldown; + + @Inject(method = "tickPrinter", at = @At("TAIL"), remap = false) + private void railwaytweaks$tickPrinter(CallbackInfo ci) { + double mspt = RailwaysTweaks.MSPT_TRACKER.getAverageMSPT(); + + if (mspt > 55) + this.printerCooldown = 60; + else if (mspt > 45) + this.printerCooldown = 20; + else + this.printerCooldown = 10; + } +} \ No newline at end of file diff --git a/src/main/java/dev/ithundxr/railwaystweaks/utils/MSPTTracker.java b/src/main/java/dev/ithundxr/railwaystweaks/utils/MSPTTracker.java new file mode 100644 index 0000000..5ced779 --- /dev/null +++ b/src/main/java/dev/ithundxr/railwaystweaks/utils/MSPTTracker.java @@ -0,0 +1,44 @@ +package dev.ithundxr.railwaystweaks.utils; + +import dev.ithundxr.railwaystweaks.RailwaysTweaks; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; + +public class MSPTTracker { + private final int averageIntervalTicks; + private long lastTickTime = System.nanoTime(); + private double totalMSPT = 0; + private double averageMSPT = 30; + private int tickCount = 0; + + public MSPTTracker(int averageIntervalTicks) { + this.averageIntervalTicks = averageIntervalTicks; + ServerTickEvents.END_SERVER_TICK.register(this::endServerTick); + } + + private void endServerTick(MinecraftServer server) { + long now = System.nanoTime(); + long tickDuration = now - lastTickTime; + double mspt = tickDuration / 1_000_000.0; + lastTickTime = now; + + // Accumulate the MSPT and increment the tick counter + totalMSPT += mspt; + tickCount++; + + // Calculate the average MSPT every AVERAGE_INTERVAL_TICKS + if (tickCount >= averageIntervalTicks) { + averageMSPT = totalMSPT / tickCount; + RailwaysTweaks.LOGGER.info("MSPT: " + averageMSPT); + + // Reset for the next interval + totalMSPT = 0; + tickCount = 0; + } + } + + public double getAverageMSPT() { + return averageMSPT; + } +} diff --git a/src/main/resources/railwaystweaks.mixins.json b/src/main/resources/railwaystweaks.mixins.json index b7cf8a5..7ed6dcc 100644 --- a/src/main/resources/railwaystweaks.mixins.json +++ b/src/main/resources/railwaystweaks.mixins.json @@ -13,6 +13,7 @@ "compat.appleskin.SyncHandlerMixin", "compat.create.Carriage$DimensionalCarriageEntityMixin", "compat.create.CarriageAccessor", + "compat.create.SchematicannonBlockEntityMixin", "compat.dcintegration.DiscordEventListenerMixin", "compat.enchancement.SlideComponentMixin", "compat.tconstruct.SimpleChannelAccessor"