Skip to content

Commit

Permalink
Fixed resetting active player's timeout at midnight.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkinnyDevi committed Jul 11, 2023
1 parent 56b7db4 commit 06e9949
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

mod_version=1.1.1-1.19.2
mod_version=1.1.2-1.19.2
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public static String getRemainingFormattedTime(ServerPlayer player) {
+ ChatFormatting.RED + getRemainingSeconds(player) + ChatFormatting.GREEN + " seconds";
}

public static void resetMidnight(ServerPlayer player) {
resetTimeout(player);
player.sendSystemMessage(Component.literal(
ChatFormatting.RED + "New day! " + ChatFormatting.GREEN + "Timeout has been refreshed."
));
}

public static void resetTimeout(ServerPlayer playerMP) {
stopTrackingTimeout(playerMP);
setRemainingTime(playerMP, getRemainingTime(playerMP));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import net.minecraftforge.eventbus.api.SubscribeEvent;

import java.text.DecimalFormat;
import java.time.Duration;
import java.time.LocalTime;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;


Expand All @@ -41,6 +43,7 @@ public void onMPPlayerLeave(PlayerEvent.PlayerLoggedOutEvent e) {

private int tick;
private LocalTime oldTrackerTime = LocalTime.now();
private long oldDayMillis = System.currentTimeMillis();

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent e) {
Expand All @@ -49,10 +52,8 @@ public void onServerTick(TickEvent.ServerTickEvent e) {
tick++;

if (tick % 20 == 0) {
LocalTime newTrackerTime = LocalTime.now();
float diff = Duration.between(oldTrackerTime, newTrackerTime).toMillis() / 1000f;
oldTrackerTime = newTrackerTime;
int workingSecond = Math.round(diff);
int workingSecond = this.deltaTime();
boolean dayChange = this.dayChange() && ConfigManager.PLAYTIME_RESET_MIDNIGHT.get();

PlaytimeDataManager.getTrackedPlayers().forEach(playerMP -> {
if (playerMP.hasDisconnected()) {
Expand All @@ -72,6 +73,14 @@ public void onServerTick(TickEvent.ServerTickEvent e) {
);
}

/*
Midnight playtime reset Logic
*/
if (dayChange) {
PlaytimeDataManager.resetMidnight(playerMP);
return;
}

/*
Playtime Logic
*/
Expand All @@ -93,7 +102,6 @@ public void onServerTick(TickEvent.ServerTickEvent e) {
/*
Timeout Logic
*/

long timeLeft = compound.getLong("timeout");
if (timeLeft >= System.currentTimeMillis()) {
// Add Delay to rightfully show the Kick Message each time
Expand All @@ -107,12 +115,35 @@ public void onServerTick(TickEvent.ServerTickEvent e) {
}

compound.remove("timeout");
compound.remove("playtime");
PlaytimeDataManager.resetTimeout(playerMP);
});
}
}

private int deltaTime() {
LocalTime newTrackerTime = LocalTime.now();
float diff = Duration.between(oldTrackerTime, newTrackerTime).toMillis() / 1000f;
oldTrackerTime = newTrackerTime;
return Math.round(diff);
}

private boolean dayChange() {
long newDayMillis = System.currentTimeMillis();
Date oldDate = new Date(oldDayMillis);
Date newDate = new Date(newDayMillis);

Calendar cal = Calendar.getInstance();
cal.setTime(oldDate);
int oldDay = cal.get(Calendar.DAY_OF_MONTH);

cal.setTime(newDate);
int newDay = cal.get(Calendar.DAY_OF_MONTH);

oldDayMillis = newDayMillis;

return oldDay != newDay;
}

private void kickPlayer(ServerPlayer playerMP, long duration){
if (playerMP == null)
return;
Expand Down

0 comments on commit 06e9949

Please sign in to comment.