package com.matt.forgehax.mods;

import com.matt.forgehax.asm.reflection.FastReflection;
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.util.Timer;

/** Created by Babbaj on 1/24/2018. */
@RegisterMod
public class TimerMod extends ToggleMod {

  public TimerMod() {
    super(Category.MISC, "Timer", false, "Speed up game time");
  }

  public final Setting<Float> factor =
      getCommandStub()
          .builders()
          .<Float>newSettingBuilder()
          .name("speed")
          .description("how fast to make the game run")
          .defaultTo(1f)
          .min(0f)
          .success(__ -> {
              if (this.isEnabled()) updateTimer();
          })
          .build();

  private final float DEFAULT_SPEED = 1000f / 20; // default speed - 50 ms

  @Override
  public void onEnabled() {
    updateTimer();
  }

  @Override
  public void onDisabled() {
    setSpeed(DEFAULT_SPEED);
  }

  private void updateTimer() {
    setSpeed(DEFAULT_SPEED / factor.getAsFloat());
  }

  private void setSpeed(float value) {
    Timer timer = FastReflection.Fields.Minecraft_timer.get(MC);
    FastReflection.Fields.Timer_tickLength.set(timer, value);
  }

  @Override
  public String getDebugDisplayText() {
    return String.format("%s[%.2f]", super.getDebugDisplayText(), factor.get());
  }
}

@@ -59,33 +59,138 @@
 asm.
-reflection.FastReflection
+events.PacketEvent;%0Aimport com.matt.forgehax.asm.reflection.FastReflection;%0Aimport com.matt.forgehax.mods.services.TickRateService
 ;%0Aim
@@ -402,18 +402,144 @@
 aft.
-util.Timer
+network.play.server.SPacketTimeUpdate;%0Aimport net.minecraft.util.Timer;%0Aimport net.minecraftforge.fml.common.eventhandler.SubscribeEvent
 ;%0A%0A/
@@ -1082,16 +1082,316 @@
 ild();%0A%0A
+  public final Setting%3CBoolean%3E tpsSync =%0A          getCommandStub()%0A                  .builders()%0A                  .%3CBoolean%3EnewSettingBuilder()%0A                  .name(%22tps-sync%22)%0A                  .description(%22sync timer to tps%22)%0A                  .defaultTo(false)%0A                  .build();%0A%0A
   privat
@@ -1624,24 +1624,67 @@
 teTimer() %7B%0A
+      if (!tpsSync.getAsBoolean()) %7B%0A      
     setSpeed
@@ -1728,234 +1728,1144 @@
 ;%0A  
-%7D%0A%0A  private void setSpeed(float value) %7B%0A    Timer timer = FastReflection.Fields.Minecraft_timer.get(MC);%0A    FastReflection.Fields.Timer_tickLength.set(timer, value);%0A  %7D%0A%0A  @Override%0A  public String getDebugDisplayText() %7B%0A
+    %7D%0A  %7D%0A%0A    @SubscribeEvent%0A    public void onPacketPreceived (PacketEvent.Incoming.Pre event)%7B%0A        if (event.getPacket() instanceof SPacketTimeUpdate && tpsSync.getAsBoolean()) %7B%0A            TickRateService.TickRateData data = TickRateService.getTickData();%0A            if (data.getSampleSize() %3E 0) %7B%0A                TickRateService.TickRateData.CalculationData point = data.getPoint();%0A                setSpeed((float) (DEFAULT_SPEED / (point.getAverage() / 20)));%0A            %7D%0A        %7Delse %7B%0A            updateTimer();%0A        %7D %0A    %7D%0A%0A  private void setSpeed(float value) %7B%0A    Timer timer = FastReflection.Fields.Minecraft_timer.get(MC);%0A    FastReflection.Fields.Timer_tickLength.set(timer, value);%0A  %7D%0A%0A  @Override%0A  public String getDisplayText() %7B%0A      if (tpsSync.getAsBoolean()) %7B%0A          TickRateService.TickRateData data = TickRateService.getTickData();%0A          if (data.getSampleSize() %3E 0) %7B%0A              TickRateService.TickRateData.CalculationData point = data.getPoint();%0A              return String.format(%22%25s%5B%25.2f%5D%22, super.getDisplayText(), point.getAverage() / 20);%0A          %7D%0A      %7Delse %7B%0A      
     
@@ -2911,13 +2911,8 @@
 getD
-ebugD
 ispl
@@ -2936,14 +2936,53 @@
 get());%0A
+      %7D return super.getDisplayText();%0A
   %7D%0A%7D%0A

Add tps-sync to Timer (#99)