mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
Cache punishments for online players
This commit is contained in:
@@ -95,6 +95,7 @@ public final class SModerationPaper extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
if (isFeatureEnabled("punishments")) listen(new PunishmentListener(punishmentManager));
|
if (isFeatureEnabled("punishments")) listen(new PunishmentListener(punishmentManager));
|
||||||
|
if (isFeatureEnabled("punishments")) listen(new CacheListener(punishmentManager));
|
||||||
if (isFeatureEnabled("invsee")) listen(new InvSeeListener());
|
if (isFeatureEnabled("invsee")) listen(new InvSeeListener());
|
||||||
if (isFeatureEnabled("enderchestsee")) listen(new EnderchestSeeListener());
|
if (isFeatureEnabled("enderchestsee")) listen(new EnderchestSeeListener());
|
||||||
if (isFeatureEnabled("socialspy")) listen(new SocialSpyListener());
|
if (isFeatureEnabled("socialspy")) listen(new SocialSpyListener());
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package de.shiewk.smoderation.paper.listener;
|
||||||
|
|
||||||
|
import de.shiewk.smoderation.paper.punishments.PunishmentManager;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
|
|
||||||
|
import static de.shiewk.smoderation.paper.SModerationPaper.LOGGER;
|
||||||
|
import static net.kyori.adventure.text.Component.translatable;
|
||||||
|
|
||||||
|
public class CacheListener implements Listener {
|
||||||
|
|
||||||
|
private final PunishmentManager punishmentManager;
|
||||||
|
|
||||||
|
public CacheListener(PunishmentManager punishmentManager) {
|
||||||
|
this.punishmentManager = punishmentManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerJoin(PlayerJoinEvent event){
|
||||||
|
try {
|
||||||
|
punishmentManager.loadToCache(event.getPlayer().getUniqueId());
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("Failed to load punishments", e);
|
||||||
|
event.getPlayer().kick(translatable("mco.errorMessage.connectionFailure"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerQuit(PlayerQuitEvent event){
|
||||||
|
punishmentManager.removeFromCache(event.getPlayer().getUniqueId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ public final class PunishmentManager {
|
|||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(PunishmentManager.class);
|
private static final Logger log = LoggerFactory.getLogger(PunishmentManager.class);
|
||||||
private final Object2ObjectArrayMap<String, PunishmentFactory<?>> typeRegistry = new Object2ObjectArrayMap<>(1);
|
private final Object2ObjectArrayMap<String, PunishmentFactory<?>> typeRegistry = new Object2ObjectArrayMap<>(1);
|
||||||
|
private final ConcurrentHashMap<UUID, List<Punishment>> cache = new ConcurrentHashMap<>(1);
|
||||||
private final Object ioLock = new Object();
|
private final Object ioLock = new Object();
|
||||||
private final Path dataDir;
|
private final Path dataDir;
|
||||||
|
|
||||||
@@ -58,8 +60,13 @@ public final class PunishmentManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Punishment> byTargetUUID(UUID target) {
|
public List<Punishment> byTargetUUID(UUID target) {
|
||||||
|
List<Punishment> cached = cache.get(target);
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
synchronized (ioLock) {
|
synchronized (ioLock) {
|
||||||
Path file = getTargetFile(target);
|
Path file = getTargetFile(target);
|
||||||
|
|
||||||
if (!Files.exists(file)) {
|
if (!Files.exists(file)) {
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
@@ -129,6 +136,7 @@ public final class PunishmentManager {
|
|||||||
writer.append('\n');
|
writer.append('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
addToCachedList(punishment);
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull List<Punishment> getAll() throws IOException {
|
public @NotNull List<Punishment> getAll() throws IOException {
|
||||||
@@ -165,4 +173,21 @@ public final class PunishmentManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadToCache(UUID uuid) {
|
||||||
|
removeFromCache(uuid);
|
||||||
|
cache.put(uuid, byTargetUUID(uuid));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeFromCache(UUID uuid) {
|
||||||
|
cache.remove(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addToCachedList(Punishment punishment) {
|
||||||
|
cache.computeIfPresent(punishment.getTargetID(), (k, v) -> {
|
||||||
|
ObjectArrayList<Punishment> newList = new ObjectArrayList<>(v);
|
||||||
|
newList.add(punishment);
|
||||||
|
return List.copyOf(newList);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user