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 2285af0 commit 2f03bbb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.12.2

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

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void onPress() {
super.onPress();
SpeedRunOptions.setOption(SpeedRunOptions.TIMER_CATEGORY, this.category);
InGameTimer.getInstance().setCategory(this.category);
InGameTimer.getInstance().setUncompleted();
}

@Override
Expand Down
47 changes: 31 additions & 16 deletions src/main/java/com/redlimerl/speedrunigt/timer/InGameTimer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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;
import net.minecraft.SharedConstants;
import net.minecraft.util.math.MathHelper;
import org.apache.commons.compress.utils.Charsets;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;

Expand All @@ -26,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 @@ -47,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 @@ -84,10 +90,8 @@ public static void start() {
INSTANCE.setPause(true, TimerStatus.IDLE);
}

/**
* Start the Timer, Trigger when player to join(created) the world
*/
public static void reset() {
if (INSTANCE.isCompleted || INSTANCE.getStatus() == TimerStatus.COMPLETED_LEGACY) return;
INSTANCE = new InGameTimer(false);
INSTANCE.category = RunCategory.CUSTOM;
INSTANCE.setPause(true, TimerStatus.IDLE);
Expand Down Expand Up @@ -117,12 +121,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 @@ -141,24 +149,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, StandardCharsets.UTF_8);
FileUtils.writeStringToFile(new File(SpeedRunIGT.WORLDS_PATH.resolve(currentWorldName).toFile(), "igt_freeze.log"), logInfo + timer.freezeLog, StandardCharsets.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, StandardCharsets.UTF_8);
FileUtils.writeStringToFile(new File(SpeedRunIGT.WORLDS_PATH.resolve(currentWorldName).toFile(), "igt_freeze" + (timer.completeCount == 0 ? "" : "_"+timer.completeCount) + ".log"), logInfo + timer.freezeLog, StandardCharsets.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 @@ -213,10 +221,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 @@ -226,11 +239,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 @@ -242,12 +255,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 @@ -274,7 +289,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 @@ -314,7 +329,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
Original file line number Diff line number Diff line change
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 2f03bbb

Please sign in to comment.