Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
onPlayerJoin: Cancel current timer
Browse files Browse the repository at this point in the history
  • Loading branch information
GHYAKIMA committed Oct 3, 2023
1 parent 2aecc84 commit 127b553
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CommonClass {

public static void init() {
if (Services.PLATFORM.isModLoaded("emptyserverstopper")) {
timer = new Timer();
Constants.LOG.info("Ready!");
}
}

Expand All @@ -26,15 +26,28 @@ public static void onServerStarted(MinecraftServer server) {
}
}

public static void onPlayerJoin() {
if (server.getPlayerCount() <= 1 & timer != null) {
Constants.LOG.info("Server shutdown cancelled. Player has joined the server");
timer.cancel();
timer.purge();
timer = null;
}
}

public static void CheckPlayerNumber() {
if (server.getPlayerCount() <= 1) {
Constants.LOG.info(String.format("Server empty -> Shutdown in %d minute(s)", m_ShutdownTimeInMinutes));
new Timer().schedule(new TimerTask() {

TimerTask task = new TimerTask() {
@Override
public void run() {
CheckAndStop();
}
}, m_ShutdownTimeInMinutes * 60000L);
};

timer = new Timer();
timer.schedule(task, m_ShutdownTimeInMinutes * 60000L);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public void onInitializeServer() {

ServerLifecycleEvents.SERVER_STARTED.register(CommonClass::onServerStarted);
ServerLifecycleEvents.SERVER_STOPPING.register((server -> CommonClass.timer.cancel()));
ServerPlayConnectionEvents.JOIN.register(((handler, sender, server) -> CommonClass.onPlayerJoin()));
ServerPlayConnectionEvents.DISCONNECT.register(((handler, server) -> CommonClass.CheckPlayerNumber()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public void onServerStopping(ServerStoppingEvent event) {
CommonClass.timer.cancel();
}

@SubscribeEvent
public void onPlayerConnect(PlayerEvent.PlayerLoggedInEvent event) {
CommonClass.onPlayerJoin();
}

@SubscribeEvent
public void onPlayerDisconnect(PlayerEvent.PlayerLoggedOutEvent event) {
CommonClass.CheckPlayerNumber();
Expand Down

0 comments on commit 127b553

Please sign in to comment.