Skip to content

Commit

Permalink
Kick players after 5 mins
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jan 17, 2025
1 parent 6571d42 commit 8053995
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/org/geysermc/globallinkserver/GlobalLinkServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
*/
package org.geysermc.globallinkserver;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalCause;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.event.player.AsyncChatEvent;
Expand Down Expand Up @@ -55,6 +61,22 @@ public class GlobalLinkServer extends JavaPlugin implements Listener {
.append(Component.text("`/unlink`", NamedTextColor.RED))
.append(Component.text("."));

private final Cache<UUID, Instant> playerIdleCache = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.removalListener(removalNotification -> {
getLogger().info(removalNotification.getKey() + " was removed from the cache due to " + removalNotification.getCause());
if (removalNotification.wasEvicted() && removalNotification.getCause() == RemovalCause.EXPIRED) {
Player player = Bukkit.getPlayer((UUID)removalNotification.getKey());
if (player != null) {
Bukkit.getScheduler().callSyncMethod(plugin, () -> {
player.kick(Component.text("You have been idle for too long!"));
return null;
});
}
}
})
.build();

@Override
public void onEnable() {
LOGGER = getLogger();
Expand Down Expand Up @@ -137,6 +159,9 @@ public void onEnable() {
getServer().clearRecipes();
getServer().setDefaultGameMode(GameMode.ADVENTURE);

// Clean up every 10 seconds (200 ticks)
Bukkit.getScheduler().runTaskTimer(plugin, () -> playerIdleCache.cleanUp(), 0, 200);

LOGGER.info("Started Global Linking plugin!");
}

Expand Down Expand Up @@ -218,12 +243,15 @@ public void onPlayerLoad(PlayerJoinEvent event) {
player.hidePlayer(this, event.getPlayer());
});

playerIdleCache.put(event.getPlayer().getUniqueId(), Instant.now());

Utils.processJoin(event.getPlayer());
}

@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
event.quitMessage(null);
playerIdleCache.invalidate(event.getPlayer().getUniqueId());
Utils.processLeave(event.getPlayer());
}

Expand Down Expand Up @@ -267,4 +295,13 @@ public void onServerListPing(PaperServerListPingEvent event) {
event.setNumPlayers(0);
event.setMaxPlayers(1);
}

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
int diffX = event.getFrom().getBlockX() - event.getTo().getBlockX();
int diffY = event.getFrom().getBlockZ() - event.getTo().getBlockZ();
if (Math.abs(diffX) > 0 || Math.abs(diffY) > 0) {
playerIdleCache.put(event.getPlayer().getUniqueId(), Instant.now());
}
}
}

0 comments on commit 8053995

Please sign in to comment.