Skip to content

Commit

Permalink
Add tps-sync to Timer (#99)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
thelampgod authored and fr1kin committed Jul 25, 2019
1 parent 947e43f commit 457495e
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/main/java/com/matt/forgehax/mods/TimerMod.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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
Expand All @@ -28,6 +32,15 @@ public TimerMod() {
})
.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
Expand All @@ -41,16 +54,39 @@ public void onDisabled() {
}

private void updateTimer() {
setSpeed(DEFAULT_SPEED / factor.getAsFloat());
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);
FastReflection.Fields.Timer_tickLength.set(timer, value);
}

@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();
}
}

0 comments on commit 457495e

Please sign in to comment.