Add tps-sync to Timer
* Add tps-sync to Timer
* change name and fix bug
Changed name to tps-sync and fixed bug where disabling tps-sync wouldn't update timer to the set speed. Not sure if this is the best way to do it, but it works.
package com.matt.forgehax.mods;
+import com.matt.forgehax.asm.events.PacketEvent;
import com.matt.forgehax.asm.reflection.FastReflection;
+import com.matt.forgehax.mods.services.TickRateService;
import com.matt.forgehax.util.command.Setting;
import com.matt.forgehax.util.mod.Category;
import com.matt.forgehax.util.mod.ToggleMod;
import com.matt.forgehax.util.mod.loader.RegisterMod;
+import net.minecraft.network.play.server.SPacketTimeUpdate;
import net.minecraft.util.Timer;
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
/** Created by Babbaj on 1/24/2018. */
@RegisterMod
@@ -28,6 +32,15 @@ public class TimerMod extends ToggleMod {
})
.build();
+ public final Setting<Boolean> tpsSync =
+ getCommandStub()
+ .builders()
+ .<Boolean>newSettingBuilder()
+ .name("tps-sync")
+ .description("sync timer to tps")
+ .defaultTo(false)
+ .build();
+
private final float DEFAULT_SPEED = 1000f / 20; // default speed - 50 ms
@Override
@@ -41,8 +54,23 @@ public class TimerMod extends ToggleMod {
}
private void updateTimer() {
+ if (!tpsSync.getAsBoolean()) {
setSpeed(DEFAULT_SPEED / factor.getAsFloat());
}
+ }
+
+ @SubscribeEvent
+ public void onPacketPreceived (PacketEvent.Incoming.Pre event){
+ if (event.getPacket() instanceof SPacketTimeUpdate && tpsSync.getAsBoolean()) {
+ TickRateService.TickRateData data = TickRateService.getTickData();
+ if (data.getSampleSize() > 0) {
+ TickRateService.TickRateData.CalculationData point = data.getPoint();
+ setSpeed((float) (DEFAULT_SPEED / (point.getAverage() / 20)));
+ }
+ }else {
+ updateTimer();
+ }
+ }
private void setSpeed(float value) {
Timer timer = FastReflection.Fields.Minecraft_timer.get(MC);
@@ -50,7 +78,15 @@ public class TimerMod extends ToggleMod {
}
@Override
- public String getDebugDisplayText() {
- return String.format("%s[%.2f]", super.getDebugDisplayText(), factor.get());
+ public String getDisplayText() {
+ if (tpsSync.getAsBoolean()) {
+ TickRateService.TickRateData data = TickRateService.getTickData();
+ if (data.getSampleSize() > 0) {
+ TickRateService.TickRateData.CalculationData point = data.getPoint();
+ return String.format("%s[%.2f]", super.getDisplayText(), point.getAverage() / 20);
+ }
+ }else {
+ return String.format("%s[%.2f]", super.getDisplayText(), factor.get());
+ } return super.getDisplayText();
}
}
