Skip to content

Commit

Permalink
Make game time code easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
PancakeTAS committed Jul 24, 2023
1 parent feb16eb commit cafc86d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/minecrafttas/lotas/mods/TickAdvance.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ protected void onInitialize() {
@Override
@Environment(EnvType.CLIENT)
protected void onClientsidePayload(FriendlyByteBuf buf) {
if (buf.readInt() == 0)
if (buf.readInt() == 0) {
this.tickadvance = buf.readBoolean();
else

TickrateChanger trc = TickrateChanger.instance;
trc.updateGameTime(trc.getGamespeed());
} else {
this.shouldTickClient = true; // Tick the client
}
}

/**
Expand All @@ -82,6 +86,7 @@ protected void onServerPayload(FriendlyByteBuf buf) {
@Override
protected void onClientsideTick() {
this.shouldTickClient = false;
TickrateChanger.instance.advanceGameTime(50L);
}

/**
Expand Down
69 changes: 31 additions & 38 deletions src/main/java/com/minecrafttas/lotas/mods/TickrateChanger.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ public TickrateChanger() {
@Environment(EnvType.CLIENT)
private double prevGamespeed = 1.0;

/**
* Previous tick advance and or tick pass, clientside
*/
@Environment(EnvType.CLIENT)
private boolean prevTickadvance, prevShouldTick;

/**
* Array of the most common tickrates available via decrease and increase tickrate keybinds
*/
Expand All @@ -82,27 +76,43 @@ public TickrateChanger() {
* System time in milliseconds since last tickrate change
*/
@Environment(EnvType.CLIENT)
private long timeSinceTC = System.currentTimeMillis();
private long systemTimeSinceUpdate = System.currentTimeMillis();

/**
* Game time in milliseconds since last tickrate change
*/
@Environment(EnvType.CLIENT)
private long fakeTimeSinceTC = 0L;
private long gameTime = 0L;

/**
* Updates the Client tickrate when receiving a packet
* @param buf Packet Data
*/
@Override
protected void onClientsidePayload(FriendlyByteBuf buf) {
// Update the local time
long time = System.currentTimeMillis() - this.timeSinceTC;
this.fakeTimeSinceTC += (time * this.prevGamespeed);
this.timeSinceTC = System.currentTimeMillis();
// Update the tickrate
this.prevGamespeed = this.gamespeed;
this.updateGameTime(this.prevGamespeed);
this.internallyUpdateTickrate(buf.readDouble());
this.prevGamespeed = this.gamespeed;
}

/**
* Update game time using gamespeed
* @param gamespeed Speed of game
*/
@Environment(EnvType.CLIENT)
public void updateGameTime(double gamespeed) {
this.gameTime += ((System.currentTimeMillis() - this.systemTimeSinceUpdate) * gamespeed);
this.systemTimeSinceUpdate = System.currentTimeMillis();
}

/**
* Advance game time by millis
* @param millis Milliseconds
*/
@Environment(EnvType.CLIENT)
public void advanceGameTime(long millis) {
this.gameTime += millis;
this.systemTimeSinceUpdate = System.currentTimeMillis();
}

@Override
Expand Down Expand Up @@ -164,32 +174,15 @@ private void internallyUpdateTickrate(double tickrate) {
*/
@Environment(EnvType.CLIENT)
public long getMilliseconds() {
long time = System.currentTimeMillis() - this.timeSinceTC;
time *= this.gamespeed;

boolean tickadvance = TickAdvance.instance.isTickadvanceEnabled();

if (tickadvance && prevShouldTick && !TickAdvance.instance.shouldTickClient) {
this.fakeTimeSinceTC += 50;
}

this.prevShouldTick = TickAdvance.instance.shouldTickClient;

if (!this.prevTickadvance && tickadvance) { // tick advance was enabled
this.timeSinceTC = System.currentTimeMillis();
this.fakeTimeSinceTC += (time * this.gamespeed);

this.prevTickadvance = tickadvance;
return this.fakeTimeSinceTC;
} if (tickadvance && !TickAdvance.instance.shouldTickClient) { // tick advance is on
this.timeSinceTC = System.currentTimeMillis();

this.prevTickadvance = tickadvance;
return this.fakeTimeSinceTC;
TickAdvance adv = TickAdvance.instance;
if (adv.isTickadvanceEnabled() && !adv.shouldTickClient) {
this.systemTimeSinceUpdate = System.currentTimeMillis();
return this.gameTime;
}
this.prevTickadvance = tickadvance;

return this.fakeTimeSinceTC + time;
long gameTimePassed = System.currentTimeMillis() - this.systemTimeSinceUpdate;
gameTimePassed *= this.gamespeed;
return this.gameTime + gameTimePassed;
}

/**
Expand Down

0 comments on commit cafc86d

Please sign in to comment.