From 6005f0db44a1fce7adab54df9f16a105d0769ad7 Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 21 May 2024 15:09:28 +0200 Subject: [PATCH] Minor clean up --- .github/workflows/codeql-analysis.yml | 2 +- .../bukkit/command/ToggleCommand.java | 2 +- .../listener/protocollib/EncryptionUtil.java | 4 ++-- .../protocollib/ProtocolLibListener.java | 4 ++-- .../protocollib/EncryptionUtilTest.java | 2 +- .../listener/protocollib/ResourceLoader.java | 4 ++++ core/pom.xml | 2 +- .../core/hooks/DefaultPasswordGenerator.java | 4 ++-- .../core/hooks/bedrock/FloodgateService.java | 15 ++++++--------- .../fastlogin/core/shared/FastLoginCore.java | 6 +++--- .../fastlogin/core/shared/FloodgateState.java | 19 +++++++------------ .../fastlogin/core/shared/JoinManagement.java | 2 +- .../fastlogin/core/shared/LoginSource.java | 2 +- .../fastlogin/core/shared/PlatformPlugin.java | 5 ++++- .../fastlogin/core/storage/StoredProfile.java | 3 +-- .../hooks/DefaultPasswordGeneratorTest.java | 4 ++-- .../velocity/VelocityLoginSource.java | 7 ++++--- .../VelocityFastLoginAutoLoginEvent.java | 3 ++- .../velocity/listener/ConnectListener.java | 7 +++---- .../listener/PluginMessageListener.java | 3 ++- .../velocity/task/FloodgateAuthTask.java | 4 ++-- 21 files changed, 52 insertions(+), 52 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 93e5b9aa..f9106620 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -25,7 +25,7 @@ jobs: if: ${{ github.event.workflow_run.conclusion == 'success' }} permissions: - # Only allow write for security, then all others default to read only + # Only allow 'write' permission for security, then all others default to read only security-events: write strategy: diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/ToggleCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/ToggleCommand.java index 47aa5cfc..c0f9b032 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/ToggleCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/ToggleCommand.java @@ -80,7 +80,7 @@ public abstract class ToggleCommand implements CommandExecutor { plugin.getBungeeManager().sendPluginMessage((PluginMessageRecipient) invoker, message); } else { Optional optPlayer = Bukkit.getServer().getOnlinePlayers().stream().findFirst(); - if (!optPlayer.isPresent()) { + if (optPlayer.isEmpty()) { plugin.getLog().info("No player online to send a plugin message to the proxy"); return; } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java index 0f86a3e4..c3c5dad3 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java @@ -58,8 +58,8 @@ import java.time.Instant; import java.util.Arrays; import java.util.Base64; import java.util.Base64.Encoder; -import java.util.Random; import java.util.UUID; +import java.util.random.RandomGenerator; /** * Encryption and decryption minecraft util for connection between servers @@ -116,7 +116,7 @@ final class EncryptionUtil { * @param random random generator * @return a token with 4 bytes long */ - public static byte[] generateVerifyToken(Random random) { + public static byte[] generateVerifyToken(RandomGenerator random) { byte[] token = new byte[VERIFY_TOKEN_LENGTH]; random.nextBytes(token); return token; diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java index 2f861048..c781b80e 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java @@ -204,7 +204,7 @@ public class ProtocolLibListener extends PacketAdapter { Either either = packet.getSpecificModifier(Either.class).read(0); if (clientPublicKey == null) { Optional left = either.left(); - if (!left.isPresent()) { + if (left.isEmpty()) { plugin.getLog().error("No verify token sent if requested without player signed key {}", sender); return false; } @@ -212,7 +212,7 @@ public class ProtocolLibListener extends PacketAdapter { return EncryptionUtil.verifyNonce(expectedToken, keyPair.getPrivate(), left.get()); } else { Optional optSignatureData = either.right(); - if (!optSignatureData.isPresent()) { + if (optSignatureData.isEmpty()) { plugin.getLog().error("No signature given to sent player signing key {}", sender); return false; } diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java index 4b7a8024..148e88c5 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java @@ -60,7 +60,7 @@ class EncryptionUtilTest { @Test void testVerifyToken() { - val random = ThreadLocalRandom.current(); + @SuppressWarnings("SharedThreadLocalRandom") val random = ThreadLocalRandom.current(); byte[] token = EncryptionUtil.generateVerifyToken(random); assertAll( diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java index 792d216c..32e1dd0d 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java @@ -50,6 +50,10 @@ import java.util.Base64; public class ResourceLoader { + private ResourceLoader() { + // Utility + } + public static RSAPrivateKey parsePrivateKey(String keySpec) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { try ( diff --git a/core/pom.xml b/core/pom.xml index cfea327a..f32781fb 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -125,7 +125,7 @@ com.zaxxer HikariCP - 4.0.3 + 5.1.0 diff --git a/core/src/main/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGenerator.java b/core/src/main/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGenerator.java index 7d27bdf4..549039ea 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGenerator.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGenerator.java @@ -26,7 +26,7 @@ package com.github.games647.fastlogin.core.hooks; import java.security.SecureRandom; -import java.util.Random; +import java.util.random.RandomGenerator; import java.util.stream.IntStream; public class DefaultPasswordGenerator

implements PasswordGenerator

{ @@ -35,7 +35,7 @@ public class DefaultPasswordGenerator

implements PasswordGenerator

{ private static final char[] PASSWORD_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" .toCharArray(); - private final Random random = new SecureRandom(); + private final RandomGenerator random = new SecureRandom(); @Override public String getRandomPassword(P player) { diff --git a/core/src/main/java/com/github/games647/fastlogin/core/hooks/bedrock/FloodgateService.java b/core/src/main/java/com/github/games647/fastlogin/core/hooks/bedrock/FloodgateService.java index 7c14f43b..d48c62dc 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/hooks/bedrock/FloodgateService.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/hooks/bedrock/FloodgateService.java @@ -59,16 +59,13 @@ public class FloodgateService extends BedrockService { */ public boolean isValidFloodgateConfigString(String key) { String value = core.getConfig().get(key).toString().toLowerCase(Locale.ENGLISH); - switch (value) { - case "true": - case "linked": - case "false": - case "no-conflict": - return true; - default: + return switch (value) { + case "true", "linked", "false", "no-conflict" -> true; + default -> { core.getPlugin().getLog().error("Invalid value detected for {} in FastLogin/config.yml.", key); - return false; - } + yield false; + } + }; } @Override diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java index 56423b87..7503d0ec 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java @@ -163,12 +163,12 @@ public class FastLoginCore

> { Action action = Action.Ignore; switch (botSection.getString("action", "ignore")) { - case "ignore": - action = Action.Ignore; - break; case "block": action = Action.Block; break; + case "ignore": + action = Action.Ignore; + break; default: plugin.getLog().warn("Invalid anti bot action - defaulting to ignore"); } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/FloodgateState.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/FloodgateState.java index 14e916e7..f2f03eb7 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/FloodgateState.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/FloodgateState.java @@ -70,17 +70,12 @@ public enum FloodgateState { */ public static FloodgateState fromInt(int num) { // using Enum.values()[i] is expensive as per https://stackoverflow.com/a/8762387/9767089 - switch (num) { - case 0: - return FloodgateState.FALSE; - case 1: - return FloodgateState.TRUE; - case 2: - return FloodgateState.LINKED; - case 3: - return FloodgateState.NOT_MIGRATED; - default: - return null; - } + return switch (num) { + case 0 -> FloodgateState.FALSE; + case 1 -> FloodgateState.TRUE; + case 2 -> FloodgateState.LINKED; + case 3 -> FloodgateState.NOT_MIGRATED; + default -> null; + }; } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java index 73430c81..48085861 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/JoinManagement.java @@ -109,7 +109,7 @@ public abstract class JoinManagement

{ premiumUUID = core.getResolver().findProfile(username); } - if (!premiumUUID.isPresent() + if (premiumUUID.isEmpty() || (!isNameChanged(source, username, premiumUUID.get()) && !isUsernameAvailable(source, username, profile))) { //nothing detected the player as premium -> start a cracked session diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSource.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSource.java index 4ce5cc5e..eff001e7 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSource.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSource.java @@ -29,7 +29,7 @@ import java.net.InetSocketAddress; public interface LoginSource { - void enableOnlinemode() throws Exception; + void enableOnlinemode(); void kick(String message); diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java index 46c431d3..8a55b9df 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/PlatformPlugin.java @@ -32,9 +32,12 @@ import org.slf4j.Logger; import java.nio.file.Path; import java.util.concurrent.ThreadFactory; +import java.util.regex.Pattern; public interface PlatformPlugin { + Pattern PATTERN = Pattern.compile("%nl%"); + String getName(); Path getPluginFolder(); @@ -48,7 +51,7 @@ public interface PlatformPlugin { boolean isPluginInstalled(String name); default void sendMultiLineMessage(C receiver, String message) { - for (String line : message.split("%nl%")) { + for (String line : PATTERN.split(message)) { sendMessage(receiver, line); } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/storage/StoredProfile.java b/core/src/main/java/com/github/games647/fastlogin/core/storage/StoredProfile.java index e80d9d12..313f6939 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/storage/StoredProfile.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/storage/StoredProfile.java @@ -168,11 +168,10 @@ public class StoredProfile extends Profile { return true; } - if (!(o instanceof StoredProfile)) { + if (!(o instanceof StoredProfile that)) { return false; } - StoredProfile that = (StoredProfile) o; if (!super.equals(o)) { return false; } diff --git a/core/src/test/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGeneratorTest.java b/core/src/test/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGeneratorTest.java index ba7cec33..a7169318 100644 --- a/core/src/test/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGeneratorTest.java +++ b/core/src/test/java/com/github/games647/fastlogin/core/hooks/DefaultPasswordGeneratorTest.java @@ -27,13 +27,13 @@ package com.github.games647.fastlogin.core.hooks; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; class DefaultPasswordGeneratorTest { @Test void smokeTestPassword() { PasswordGenerator passwordGenerator = new DefaultPasswordGenerator<>(); - assertTrue(passwordGenerator.getRandomPassword(null).length() == 8); + assertEquals(8, passwordGenerator.getRandomPassword(null).length()); } } diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/VelocityLoginSource.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/VelocityLoginSource.java index dfd98acb..3468b025 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/VelocityLoginSource.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/VelocityLoginSource.java @@ -27,6 +27,7 @@ package com.github.games647.fastlogin.velocity; import com.github.games647.fastlogin.core.shared.LoginSource; import com.velocitypowered.api.event.connection.PreLoginEvent; +import com.velocitypowered.api.event.connection.PreLoginEvent.PreLoginComponentResult; import com.velocitypowered.api.proxy.InboundConnection; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -46,17 +47,17 @@ public class VelocityLoginSource implements LoginSource { @Override public void enableOnlinemode() { - preLoginEvent.setResult(PreLoginEvent.PreLoginComponentResult.forceOnlineMode()); + preLoginEvent.setResult(PreLoginComponentResult.forceOnlineMode()); } @Override public void kick(String message) { if (message == null) { - preLoginEvent.setResult(PreLoginEvent.PreLoginComponentResult.denied( + preLoginEvent.setResult(PreLoginComponentResult.denied( Component.text("Kicked").color(NamedTextColor.WHITE)) ); } else { - preLoginEvent.setResult(PreLoginEvent.PreLoginComponentResult.denied( + preLoginEvent.setResult(PreLoginComponentResult.denied( LegacyComponentSerializer.legacyAmpersand().deserialize(message)) ); } diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/event/VelocityFastLoginAutoLoginEvent.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/event/VelocityFastLoginAutoLoginEvent.java index 38346097..d9f1a00c 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/event/VelocityFastLoginAutoLoginEvent.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/event/VelocityFastLoginAutoLoginEvent.java @@ -29,11 +29,12 @@ import com.github.games647.fastlogin.core.shared.LoginSession; import com.github.games647.fastlogin.core.shared.event.FastLoginAutoLoginEvent; import com.github.games647.fastlogin.core.storage.StoredProfile; import com.velocitypowered.api.event.ResultedEvent; +import com.velocitypowered.api.event.ResultedEvent.GenericResult; import java.util.Objects; public class VelocityFastLoginAutoLoginEvent - implements FastLoginAutoLoginEvent, ResultedEvent { + implements FastLoginAutoLoginEvent, ResultedEvent { private final LoginSession session; private final StoredProfile profile; diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java index e9c57369..0246ce25 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java @@ -46,7 +46,6 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.server.RegisteredServer; -import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.util.GameProfile.Property; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -130,9 +129,9 @@ public class ConnectListener { } } - private List removeSkin(Collection oldProperties) { - List newProperties = new ArrayList<>(oldProperties.size()); - for (GameProfile.Property property : oldProperties) { + private List removeSkin(Collection oldProperties) { + List newProperties = new ArrayList<>(oldProperties.size()); + for (Property property : oldProperties) { if (!"textures".equals(property.getName())) { newProperties.add(property); } diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/PluginMessageListener.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/PluginMessageListener.java index 12b19f60..8dcb014c 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/PluginMessageListener.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/PluginMessageListener.java @@ -38,6 +38,7 @@ import com.google.common.io.ByteStreams; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.event.connection.PluginMessageEvent.ForwardResult; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; @@ -69,7 +70,7 @@ public class PluginMessageListener { //the client shouldn't be able to read the messages in order to know something about server internal states //moreover the client shouldn't be able to fake a running premium check by sending the result message - pluginMessageEvent.setResult(PluginMessageEvent.ForwardResult.handled()); + pluginMessageEvent.setResult(ForwardResult.handled()); if (!(pluginMessageEvent.getSource() instanceof ServerConnection)) { //check if the message is sent from the server diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/FloodgateAuthTask.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/FloodgateAuthTask.java index b64bfae6..33ed1fea 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/FloodgateAuthTask.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/FloodgateAuthTask.java @@ -55,8 +55,8 @@ public class FloodgateAuthTask core.getPlugin().getSession().put(player.getRemoteAddress(), session); // enable auto login based on the value of 'autoLoginFloodgate' in config.yml - boolean forcedOnlineMode = autoLoginFloodgate.equals("true") - || (autoLoginFloodgate.equals("linked") && isLinked); + boolean forcedOnlineMode = "true".equals(autoLoginFloodgate) + || ("linked".equals(autoLoginFloodgate) && isLinked); // delay sending force command, because Paper will process the login event asynchronously // In this case it means that the force command (plugin message) is already received and processed while