Skip to content

Commit

Permalink
Fixed issue (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
RedLime committed Dec 29, 2021
1 parent 42807aa commit 688500b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=20w14infinite+build.4
loader_version=0.12.2

# Mod Properties
mod_version = 2.14.2+20w14infinite
mod_version = 2.14.3+20w14infinite
maven_group = com.redlimerl.speedrunigt
archives_base_name = SpeedRunIGT

Expand Down
Expand Up @@ -46,7 +46,7 @@ private void onMove(MovementType movementType, Vec3d vec3d, CallbackInfo ci) {
timer.updateFirstInput();
}

if (timer.getStatus() == TimerStatus.NONE || timer.getStatus() == TimerStatus.COMPLETED) return;
if (timer.getStatus() == TimerStatus.NONE || timer.getStatus() == TimerStatus.COMPLETED_LEGACY) return;

//HIGH%
if (timer.getCategory() == RunCategory.HIGH && this.getY() >= 420) {
Expand Down
Expand Up @@ -20,7 +20,7 @@ public void onTick(CallbackInfo ci) {
public void disconnect(CallbackInfo ci) {
InGameTimer timer = InGameTimer.getInstance();
if (timer.getStatus() != TimerStatus.NONE) {
if (timer.getStatus() == TimerStatus.COMPLETED) {
if (timer.getStatus() == TimerStatus.COMPLETED_LEGACY) {
InGameTimer.end();
} else {
InGameTimer.leave();
Expand Down
Expand Up @@ -113,6 +113,7 @@ public void onPress() {
super.onPress();
SpeedRunOptions.setOption(SpeedRunOptions.TIMER_CATEGORY, this.category);
InGameTimer.getInstance().setCategory(this.category);
InGameTimer.getInstance().setUncompleted();
}

@Override
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/com/redlimerl/speedrunigt/timer/InGameTimer.java
@@ -1,5 +1,6 @@
package com.redlimerl.speedrunigt.timer;

import com.google.gson.Gson;
import com.redlimerl.speedrunigt.SpeedRunIGT;
import com.redlimerl.speedrunigt.crypt.Crypto;
import com.redlimerl.speedrunigt.option.SpeedRunOptions;
Expand Down Expand Up @@ -27,6 +28,8 @@ public class InGameTimer {

@NotNull
private static InGameTimer INSTANCE = new InGameTimer();
@NotNull
private static InGameTimer COMPLETED_INSTANCE = new InGameTimer();

@NotNull
public static InGameTimer getInstance() { return INSTANCE; }
Expand All @@ -48,6 +51,8 @@ public InGameTimer(boolean isResettable) {

private RunCategory category = RunCategory.ANY;
private final boolean isResettable;
private boolean isCompleted = false;
private int completeCount = 0;

//Timer time
private long startTime = 0;
Expand Down Expand Up @@ -118,12 +123,16 @@ public static String timeToStringFormat(long time) {
* End the Timer, Trigger when Complete Ender Dragon
*/
public static void complete() {
InGameTimer timer = INSTANCE;
if (timer.getStatus() == TimerStatus.COMPLETED) return;
COMPLETED_INSTANCE = new Gson().fromJson(new Gson().toJson(INSTANCE), InGameTimer.class);
COMPLETED_INSTANCE.isCompleted = false;
INSTANCE.isCompleted = true;
InGameTimer timer = COMPLETED_INSTANCE;

if (timer.getStatus() == TimerStatus.COMPLETED_LEGACY) return;
timer.endTime = System.currentTimeMillis();
timer.endIGTTime = timer.endTime - timer.leastTickTime;

timer.setStatus(TimerStatus.COMPLETED);
timer.setStatus(TimerStatus.COMPLETED_LEGACY);
timer.pauseLog.append("Result > IGT ").append(timeToStringFormat(timer.getInGameTime()))
.append(", R-RTA ").append(timeToStringFormat(timer.getRealTimeAttack()))
.append(", RTA ").append(timeToStringFormat(timer.getRealTimeAttack(false)))
Expand All @@ -142,24 +151,24 @@ public static void complete() {

new Thread(() -> {
try {
FileUtils.writeStringToFile(new File(SpeedRunIGT.WORLDS_PATH.resolve(currentWorldName).toFile(), "igt_timer.log"), logInfo + timer.firstInput + "\r\n" + timer.pauseLog, Charsets.UTF_8);
FileUtils.writeStringToFile(new File(SpeedRunIGT.WORLDS_PATH.resolve(currentWorldName).toFile(), "igt_freeze.log"), logInfo + timer.freezeLog, Charsets.UTF_8);
FileUtils.writeStringToFile(new File(SpeedRunIGT.WORLDS_PATH.resolve(currentWorldName).toFile(), "igt_timer" + (timer.completeCount == 0 ? "" : "_"+timer.completeCount) + ".log"), logInfo + timer.firstInput + "\r\n" + timer.pauseLog, Charsets.UTF_8);
FileUtils.writeStringToFile(new File(SpeedRunIGT.WORLDS_PATH.resolve(currentWorldName).toFile(), "igt_freeze" + (timer.completeCount == 0 ? "" : "_"+timer.completeCount) + ".log"), logInfo + timer.freezeLog, Charsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
}).start();

for (Consumer<InGameTimer> onCompleteConsumer : onCompleteConsumers) {
try {
onCompleteConsumer.accept(INSTANCE);
onCompleteConsumer.accept(timer);
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void leave() {
if (INSTANCE.getStatus() == TimerStatus.COMPLETED) return;
if (INSTANCE.isCompleted) return;

INSTANCE.leaveTime = System.currentTimeMillis();
INSTANCE.setPause(true, TimerStatus.IDLE);
Expand Down Expand Up @@ -214,10 +223,15 @@ public void updateMoreData(int key, int value) {
}

public void setStatus(@NotNull TimerStatus status) {
if (this.getStatus() == TimerStatus.COMPLETED && status != TimerStatus.NONE) return;
if (this.getStatus() == TimerStatus.COMPLETED_LEGACY && status != TimerStatus.NONE) return;
this.status = status;
}

public void setUncompleted() {
this.isCompleted = false;
this.completeCount++;
}

public boolean isStarted() {
return this.startTime != 0;
}
Expand All @@ -227,11 +241,11 @@ public boolean isPaused() {
}

public boolean isPlaying() {
return !this.isPaused() && this.getStatus() != TimerStatus.COMPLETED;
return !this.isPaused() && this.getStatus() != TimerStatus.COMPLETED_LEGACY;
}

private long getEndTime() {
return this.getStatus() == TimerStatus.COMPLETED ? this.endTime : System.currentTimeMillis();
return this.getStatus() == TimerStatus.COMPLETED_LEGACY ? this.endTime : System.currentTimeMillis();
}

public long getStartTime() {
Expand All @@ -243,12 +257,14 @@ public long getRealTimeAttack() {
}

public long getRealTimeAttack(boolean withRebased) {
return this.getStatus() == TimerStatus.NONE ? 0 : this.getEndTime() - this.getStartTime() + (withRebased ? rebaseRealTime : 0) - this.excludedTime;
return this.isCompleted && this != COMPLETED_INSTANCE ? COMPLETED_INSTANCE.getRealTimeAttack(withRebased) : this.getStatus() == TimerStatus.NONE ? 0 : this.getEndTime() - this.getStartTime() + (withRebased ? rebaseRealTime : 0) - this.excludedTime;
}

public long getInGameTime() { return getInGameTime(true); }

public long getInGameTime(boolean smooth) {
if (this.isCompleted && this != COMPLETED_INSTANCE) return COMPLETED_INSTANCE.getInGameTime(smooth);

long ms = System.currentTimeMillis();
return !isStarted() ? 0 :
(this.getTicks() * 50L) // Tick Based
Expand All @@ -275,7 +291,7 @@ public void updateFirstRendered() {


public void tick() {
if (this.getStatus() == TimerStatus.COMPLETED) return;
if (this.getStatus() == TimerStatus.COMPLETED_LEGACY) return;

if (isPlaying()) {
this.activateTicks++;
Expand Down Expand Up @@ -315,7 +331,7 @@ public void tick() {

public void setPause(boolean isPause) { this.setPause(isPause, TimerStatus.PAUSED); }
public void setPause(boolean toPause, TimerStatus toStatus) {
if (this.getStatus() == TimerStatus.COMPLETED) return;
if (this.getStatus() == TimerStatus.COMPLETED_LEGACY) return;

if (toPause) {
if (this.getStatus().getPause() <= toStatus.getPause()) {
Expand Down
Expand Up @@ -31,7 +31,7 @@ public enum TimerStatus {
* When the player kills the ender dragon and sees the credit screen.
* This status doesn't change until the player creates a new world.
*/
COMPLETED("Completed the category", 1),
COMPLETED_LEGACY("Completed the category", 1),

/**
* When the player leaves the world for some reason.
Expand Down

0 comments on commit 688500b

Please sign in to comment.