|
5 | 5 | */
|
6 | 6 | package org.geysermc.globallinkserver;
|
7 | 7 |
|
| 8 | +import java.time.Instant; |
8 | 9 | import java.util.ArrayList;
|
9 | 10 | import java.util.Collection;
|
10 | 11 | import java.util.List;
|
| 12 | +import java.util.UUID; |
| 13 | +import java.util.concurrent.TimeUnit; |
11 | 14 | import java.util.logging.Logger;
|
12 | 15 |
|
13 | 16 | import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
|
| 17 | +import com.google.common.cache.Cache; |
| 18 | +import com.google.common.cache.CacheBuilder; |
| 19 | +import com.google.common.cache.RemovalCause; |
14 | 20 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
15 | 21 | import io.papermc.paper.command.brigadier.Commands;
|
16 | 22 | import io.papermc.paper.event.player.AsyncChatEvent;
|
@@ -55,6 +61,22 @@ public class GlobalLinkServer extends JavaPlugin implements Listener {
|
55 | 61 | .append(Component.text("`/unlink`", NamedTextColor.RED))
|
56 | 62 | .append(Component.text("."));
|
57 | 63 |
|
| 64 | + private final Cache<UUID, Instant> playerIdleCache = CacheBuilder.newBuilder() |
| 65 | + .expireAfterWrite(5, TimeUnit.MINUTES) |
| 66 | + .removalListener(removalNotification -> { |
| 67 | + getLogger().info(removalNotification.getKey() + " was removed from the cache due to " + removalNotification.getCause()); |
| 68 | + if (removalNotification.wasEvicted() && removalNotification.getCause() == RemovalCause.EXPIRED) { |
| 69 | + Player player = Bukkit.getPlayer((UUID)removalNotification.getKey()); |
| 70 | + if (player != null) { |
| 71 | + Bukkit.getScheduler().callSyncMethod(plugin, () -> { |
| 72 | + player.kick(Component.text("You have been idle for too long!")); |
| 73 | + return null; |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + }) |
| 78 | + .build(); |
| 79 | + |
58 | 80 | @Override
|
59 | 81 | public void onEnable() {
|
60 | 82 | LOGGER = getLogger();
|
@@ -137,6 +159,9 @@ public void onEnable() {
|
137 | 159 | getServer().clearRecipes();
|
138 | 160 | getServer().setDefaultGameMode(GameMode.ADVENTURE);
|
139 | 161 |
|
| 162 | + // Clean up every 10 seconds (200 ticks) |
| 163 | + Bukkit.getScheduler().runTaskTimer(plugin, () -> playerIdleCache.cleanUp(), 0, 200); |
| 164 | + |
140 | 165 | LOGGER.info("Started Global Linking plugin!");
|
141 | 166 | }
|
142 | 167 |
|
@@ -218,12 +243,15 @@ public void onPlayerLoad(PlayerJoinEvent event) {
|
218 | 243 | player.hidePlayer(this, event.getPlayer());
|
219 | 244 | });
|
220 | 245 |
|
| 246 | + playerIdleCache.put(event.getPlayer().getUniqueId(), Instant.now()); |
| 247 | + |
221 | 248 | Utils.processJoin(event.getPlayer());
|
222 | 249 | }
|
223 | 250 |
|
224 | 251 | @EventHandler
|
225 | 252 | public void onPlayerLeave(PlayerQuitEvent event) {
|
226 | 253 | event.quitMessage(null);
|
| 254 | + playerIdleCache.invalidate(event.getPlayer().getUniqueId()); |
227 | 255 | Utils.processLeave(event.getPlayer());
|
228 | 256 | }
|
229 | 257 |
|
@@ -267,4 +295,13 @@ public void onServerListPing(PaperServerListPingEvent event) {
|
267 | 295 | event.setNumPlayers(0);
|
268 | 296 | event.setMaxPlayers(1);
|
269 | 297 | }
|
| 298 | + |
| 299 | + @EventHandler |
| 300 | + public void onPlayerMove(PlayerMoveEvent event) { |
| 301 | + int diffX = event.getFrom().getBlockX() - event.getTo().getBlockX(); |
| 302 | + int diffY = event.getFrom().getBlockZ() - event.getTo().getBlockZ(); |
| 303 | + if (Math.abs(diffX) > 0 || Math.abs(diffY) > 0) { |
| 304 | + playerIdleCache.put(event.getPlayer().getUniqueId(), Instant.now()); |
| 305 | + } |
| 306 | + } |
270 | 307 | }
|
0 commit comments