Skip to content

Commit

Permalink
Make Schematicannon fire rate depend on the avg MSPT
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-evelyn committed Nov 13, 2024
1 parent f113218 commit 57abcfe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CommandSourceStack, ?> $dump_hephaestus_packets() {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
44 changes: 44 additions & 0 deletions src/main/java/dev/ithundxr/railwaystweaks/utils/MSPTTracker.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/railwaystweaks.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 57abcfe

Please sign in to comment.