Skip to content

Commit

Permalink
Small changes to increase code reliability in edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jun 9, 2022
1 parent f214e4d commit 70980bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerWrapper> optionalPlayer = plugin.getPlayer(args[1]);
if (optionalPlayer.isPresent()) {
PlayerWrapper player = optionalPlayer.get();

if (args.length > 2) {
Calendar calendar = Calendar.getInstance();
Expand Down Expand Up @@ -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<PlayerWrapper> optionalPlayer = plugin.getPlayer(args[1]);
if (optionalPlayer.isPresent()) {
PlayerWrapper player = optionalPlayer.get();

if (StorageTool.unShadowBanPlayer(player.getUniqueId())) {
sendLine(sender);
Expand Down Expand Up @@ -238,7 +240,7 @@ default List<String> onTab(String[] args, PermissibleWrapper wrapper, PistonQueu

return completions;
} else {
return null;
return Collections.emptyList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> 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);
Expand Down Expand Up @@ -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<String> optionalTarget = event.getTarget();
if (!isPlayerMainFull(player) && optionalTarget.isPresent() && optionalTarget.get().equals(Config.QUEUE_SERVER))
event.setTarget(Config.MAIN_SERVER);
} else {
if (player.getCurrentServer().isPresent())
Expand Down Expand Up @@ -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<String> 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)) {
Expand Down Expand Up @@ -187,15 +192,17 @@ 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<String> previousServer = event.getPreviousServer();
return previousServer.isPresent() && previousServer.get().equals(Config.AUTH_SERVER) && event.getServer().equals(Config.QUEUE_SERVER);
}

public void moveQueue() {
for (QueueType type : QueueType.values()) {
for (Map.Entry<UUID, String> entry : new LinkedHashMap<>(type.getQueueMap()).entrySet()) {
Optional<PlayerWrapper> player = plugin.getPlayer(entry.getKey());

if (!player.isPresent() || !player.get().getCurrentServer().isPresent() || !player.get().getCurrentServer().get().equals(Config.QUEUE_SERVER)) {
Optional<String> optionalTarget = player.flatMap(PlayerWrapper::getCurrentServer);
if (!optionalTarget.isPresent() || !optionalTarget.get().equals(Config.QUEUE_SERVER)) {
type.getQueueMap().remove(entry.getKey());
}
}
Expand Down Expand Up @@ -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<String> 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);
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,10 +46,10 @@ public enum QueueType {
@Getter
private final AtomicInteger playersWithTypeInMain = new AtomicInteger();

public static QueueType getQueueType(Function<String, Boolean> player) {
if (player.apply(Config.QUEUE_VETERAN_PERMISSION)) {
public static QueueType getQueueType(Predicate<String> 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;
Expand Down

0 comments on commit 70980bf

Please sign in to comment.