From 70980bfddfaa1ff6c7ad62bb531a8d4cf8e285e8 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 9 Jun 2022 20:48:44 +0200 Subject: [PATCH] Small changes to increase code reliability in edge cases --- .../pistonqueue/shared/MainCommandShared.java | 12 ++++---- .../shared/QueueListenerShared.java | 28 ++++++++++++------- .../pistonqueue/shared/QueueType.java | 8 +++--- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/pistonmaster/pistonqueue/shared/MainCommandShared.java b/src/main/java/net/pistonmaster/pistonqueue/shared/MainCommandShared.java index fea94ab..f315496 100644 --- a/src/main/java/net/pistonmaster/pistonqueue/shared/MainCommandShared.java +++ b/src/main/java/net/pistonmaster/pistonqueue/shared/MainCommandShared.java @@ -73,8 +73,9 @@ default void onCommand(CommandSourceWrapper sender, String[] args, PistonQueuePl case "shadowban": if (sender.hasPermission(Config.ADMIN_PERMISSION)) { if (args.length > 1) { - if (plugin.getPlayer(args[1]).isPresent()) { - PlayerWrapper player = plugin.getPlayer(args[1]).get(); + Optional optionalPlayer = plugin.getPlayer(args[1]); + if (optionalPlayer.isPresent()) { + PlayerWrapper player = optionalPlayer.get(); if (args.length > 2) { Calendar calendar = Calendar.getInstance(); @@ -131,8 +132,9 @@ default void onCommand(CommandSourceWrapper sender, String[] args, PistonQueuePl case "unshadowban": if (sender.hasPermission(Config.ADMIN_PERMISSION)) { if (args.length > 1) { - if (plugin.getPlayer(args[1]).isPresent()) { - PlayerWrapper player = plugin.getPlayer(args[1]).get(); + Optional optionalPlayer = plugin.getPlayer(args[1]); + if (optionalPlayer.isPresent()) { + PlayerWrapper player = optionalPlayer.get(); if (StorageTool.unShadowBanPlayer(player.getUniqueId())) { sendLine(sender); @@ -238,7 +240,7 @@ default List onTab(String[] args, PermissibleWrapper wrapper, PistonQueu return completions; } else { - return null; + return Collections.emptyList(); } } diff --git a/src/main/java/net/pistonmaster/pistonqueue/shared/QueueListenerShared.java b/src/main/java/net/pistonmaster/pistonqueue/shared/QueueListenerShared.java index 771fe94..ed47795 100644 --- a/src/main/java/net/pistonmaster/pistonqueue/shared/QueueListenerShared.java +++ b/src/main/java/net/pistonmaster/pistonqueue/shared/QueueListenerShared.java @@ -33,6 +33,7 @@ import java.time.Duration; import java.time.Instant; import java.util.*; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicInteger; @RequiredArgsConstructor @@ -56,9 +57,11 @@ protected void onPostLogin(PlayerWrapper player) { protected void onKick(PQKickedFromServerEvent event) { if (Config.IF_MAIN_DOWN_SEND_TO_QUEUE && event.getKickedFrom().equals(Config.MAIN_SERVER)) { - if (event.getKickReason().isPresent()) { + Optional optionalKickReason = event.getKickReason(); + + if (optionalKickReason.isPresent()) { for (String str : Config.DOWN_WORD_LIST) { - if (!event.getKickReason().get().toLowerCase().contains(str)) + if (!optionalKickReason.get().toLowerCase().contains(str)) continue; event.setCancelServer(Config.QUEUE_SERVER); @@ -86,7 +89,8 @@ protected void onPreConnect(PQServerPreConnectEvent event) { if (isAnyoneQueuedOfType(player)) return; - if (!isPlayerMainFull(player) && event.getTarget().isPresent() && event.getTarget().get().equals(Config.QUEUE_SERVER)) + Optional optionalTarget = event.getTarget(); + if (!isPlayerMainFull(player) && optionalTarget.isPresent() && optionalTarget.get().equals(Config.QUEUE_SERVER)) event.setTarget(Config.MAIN_SERVER); } else { if (player.getCurrentServer().isPresent()) @@ -136,8 +140,9 @@ protected void onConnected(PQServerConnectedEvent event) { return; } - // Its null when joining! - if (!event.getPreviousServer().isPresent() && player.getCurrentServer().isPresent() && player.getCurrentServer().get().equals(Config.QUEUE_SERVER)) { + // It's not present when joining! + Optional currentServer = player.getCurrentServer(); + if (!event.getPreviousServer().isPresent() && currentServer.isPresent() && currentServer.get().equals(Config.QUEUE_SERVER)) { if (Config.ALLOW_AUTH_SKIP) putQueueAuthFirst(player); } else if (isAuthToQueue(event)) { @@ -187,7 +192,8 @@ protected boolean isAnyoneQueuedOfType(PlayerWrapper player) { } protected boolean isAuthToQueue(PQServerConnectedEvent event) { - return event.getPreviousServer().isPresent() && event.getPreviousServer().get().equals(Config.AUTH_SERVER) && event.getServer().equals(Config.QUEUE_SERVER); + Optional previousServer = event.getPreviousServer(); + return previousServer.isPresent() && previousServer.get().equals(Config.AUTH_SERVER) && event.getServer().equals(Config.QUEUE_SERVER); } public void moveQueue() { @@ -195,7 +201,8 @@ public void moveQueue() { for (Map.Entry entry : new LinkedHashMap<>(type.getQueueMap()).entrySet()) { Optional player = plugin.getPlayer(entry.getKey()); - if (!player.isPresent() || !player.get().getCurrentServer().isPresent() || !player.get().getCurrentServer().get().equals(Config.QUEUE_SERVER)) { + Optional optionalTarget = player.flatMap(PlayerWrapper::getCurrentServer); + if (!optionalTarget.isPresent() || !optionalTarget.get().equals(Config.QUEUE_SERVER)) { type.getQueueMap().remove(entry.getKey()); } } @@ -225,7 +232,8 @@ public void moveQueue() { protected void doRecovery(PlayerWrapper player) { QueueType type = QueueType.getQueueType(player::hasPermission); - if (!type.getQueueMap().containsKey(player.getUniqueId()) && player.getCurrentServer().isPresent() && player.getCurrentServer().get().equals(Config.QUEUE_SERVER)) { + Optional currentServer = player.getCurrentServer(); + if (!type.getQueueMap().containsKey(player.getUniqueId()) && currentServer.isPresent() && currentServer.get().equals(Config.QUEUE_SERVER)) { type.getQueueMap().putIfAbsent(player.getUniqueId(), Config.MAIN_SERVER); player.sendMessage(Config.RECOVERY_MESSAGE); @@ -259,8 +267,8 @@ protected void connectPlayer(QueueType type) { if (StorageTool.isShadowBanned(player.get().getUniqueId()) && (Config.SHADOW_BAN_TYPE == BanType.LOOP - || (Config.SHADOW_BAN_TYPE == BanType.TEN_PERCENT && new Random().nextInt(100) >= 10) - || (Config.SHADOW_BAN_TYPE == BanType.CUSTOM_PERCENT && new Random().nextInt(100) >= Config.CUSTOM_PERCENT_PERCENTAGE))) { + || (Config.SHADOW_BAN_TYPE == BanType.TEN_PERCENT && ThreadLocalRandom.current().nextInt(100) >= 10) + || (Config.SHADOW_BAN_TYPE == BanType.CUSTOM_PERCENT && ThreadLocalRandom.current().nextInt(100) >= Config.CUSTOM_PERCENT_PERCENTAGE))) { player.get().sendMessage(Config.SHADOW_BAN_MESSAGE); type.getQueueMap().put(entry.getKey(), entry.getValue()); diff --git a/src/main/java/net/pistonmaster/pistonqueue/shared/QueueType.java b/src/main/java/net/pistonmaster/pistonqueue/shared/QueueType.java index 1699ccd..20c4864 100644 --- a/src/main/java/net/pistonmaster/pistonqueue/shared/QueueType.java +++ b/src/main/java/net/pistonmaster/pistonqueue/shared/QueueType.java @@ -27,7 +27,7 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Function; +import java.util.function.Predicate; public enum QueueType { REGULAR, @@ -46,10 +46,10 @@ public enum QueueType { @Getter private final AtomicInteger playersWithTypeInMain = new AtomicInteger(); - public static QueueType getQueueType(Function player) { - if (player.apply(Config.QUEUE_VETERAN_PERMISSION)) { + public static QueueType getQueueType(Predicate player) { + if (player.test(Config.QUEUE_VETERAN_PERMISSION)) { return VETERAN; - } else if (player.apply(Config.QUEUE_PRIORITY_PERMISSION)) { + } else if (player.test(Config.QUEUE_PRIORITY_PERMISSION)) { return PRIORITY; } else { return REGULAR;