Skip to content

Commit

Permalink
Replace static getInstance() usage with dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
fr1kin committed Dec 19, 2020
1 parent 4364a20 commit 0afd1b6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/main/java/dev/fiki/forgehax/main/mods/misc/TimerMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class TimerMod extends ToggleMod {
@MapField(parentClass = Timer.class, value = "tickLength")
public final ReflectionField<Float> Timer_tickLength;

private final TickRateService tickRateService;

public final FloatSetting factor = newFloatSetting()
.name("speed")
.description("how fast to make the game run")
Expand Down Expand Up @@ -68,9 +70,8 @@ private void updateTimer() {
@SubscribeListener
public void onPacketPreceived(PacketInboundEvent event) {
if (event.getPacket() instanceof SUpdateTimePacket && tpsSync.getValue()) {
TickRateService monitor = TickRateService.getInstance();
if (!monitor.isEmpty()) {
setSpeed((float) (DEFAULT_SPEED / (monitor.getTickrate() / 20.f)));
if (!tickRateService.isEmpty()) {
setSpeed((float) (DEFAULT_SPEED / (tickRateService.getTickrate() / 20.f)));
}
} else {
updateTimer();
Expand All @@ -85,9 +86,8 @@ private void setSpeed(float value) {
@Override
public String getDisplayText() {
if (tpsSync.getValue()) {
TickRateService monitor = TickRateService.getInstance();
if (!monitor.isEmpty()) {
return String.format("%s[%.2f]", super.getDisplayText(), monitor.getTickrate() / 20);
if (!tickRateService.isEmpty()) {
return String.format("%s[%.2f]", super.getDisplayText(), tickRateService.getTickrate() / 20);
}
} else {
return String.format("%s[%.2f]", super.getDisplayText(), factor.getValue());
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/dev/fiki/forgehax/main/mods/ui/ActiveModList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.fiki.forgehax.api.mod.HudMod;
import dev.fiki.forgehax.api.modloader.RegisterMod;
import dev.fiki.forgehax.main.services.TickRateService;
import lombok.RequiredArgsConstructor;
import net.minecraft.client.gui.screen.ChatScreen;

import java.util.ArrayList;
Expand All @@ -29,6 +30,7 @@
flags = EnumFlag.HIDDEN,
enabled = true
)
@RequiredArgsConstructor
public class ActiveModList extends HudMod {
private final BooleanSetting tps_meter = newBooleanSetting()
.name("tps-meter")
Expand Down Expand Up @@ -61,6 +63,8 @@ public class ActiveModList extends HudMod {
.defaultTo(SortMode.ALPHABETICAL)
.build();

private final TickRateService tickRateService;

@Override
protected AlignHelper.Align getDefaultAlignment() {
return AlignHelper.Align.TOPLEFT;
Expand All @@ -83,13 +87,12 @@ protected double getDefaultScale() {

private String generateTickRateText() {
String text = "Tick-rate: ";
TickRateService monitor = TickRateService.getInstance();
if (!monitor.isEmpty()) {
text += String.format("%1.2f", monitor.getRealtimeTickrate());
if (!tickRateService.isEmpty()) {
text += String.format("%1.2f", tickRateService.getRealtimeTickrate());

if (showLag.getValue()) {
text += " : ";
TickRateService.TickrateTimer current = monitor.getCurrentTimer();
TickRateService.TickrateTimer current = tickRateService.getCurrentTimer();
if (current != null
&& current.getTimeElapsed() > timeoutDisplay.getValue()) {
text += String.format("%01.1fs", (float) (current.getTimeElapsed() - 1000L) / 1000.f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import dev.fiki.forgehax.api.mod.ServiceMod;
import dev.fiki.forgehax.api.modloader.RegisterMod;
import dev.fiki.forgehax.asm.events.packet.PacketInboundEvent;
import lombok.Getter;
import net.minecraft.network.play.server.SUpdateTimePacket;

import javax.annotation.Nullable;
Expand All @@ -32,9 +31,6 @@ public static double clampTickrate(double rate) {
return Math.min(MAX_TICKRATE, Math.max(MIN_TICKRATE, rate));
}

@Getter
private static TickRateService instance;

private final IntegerSetting sampleSize = newIntegerSetting()
.name("sample-size")
.description("Number of ticks to record")
Expand All @@ -50,10 +46,6 @@ public static double clampTickrate(double rate) {
private long currentTotal = 0;
private double currentTickrate;

{
instance = this;
}

private void resetData() {
Arrays.fill(data, null);
currentIndex = 0;
Expand Down

0 comments on commit 0afd1b6

Please sign in to comment.