From ca42a7c19ec687a4a4a5ea99d9300b4b0da6f3a0 Mon Sep 17 00:00:00 2001 From: games647 Date: Sat, 17 Sep 2016 15:18:26 +0200 Subject: [PATCH] Refactor more code for more Java 8 and Guava usage --- .../games647/fastlogin/bukkit/BukkitCore.java | 18 ++---- .../fastlogin/bukkit/BukkitLoginSession.java | 2 +- .../fastlogin/bukkit/FastLoginBukkit.java | 54 ++++++++++++------ .../fastlogin/bukkit/MojangApiBukkit.java | 5 +- .../bukkit/commands/CrackedCommand.java | 33 +---------- .../bukkit/commands/ImportCommand.java | 8 +-- .../bukkit/commands/PremiumCommand.java | 33 +---------- .../bukkit/listener/BungeeCordListener.java | 56 ++++++++++--------- .../protocollib/LoginSkinApplyListener.java | 4 +- .../protocollib/StartPacketListener.java | 5 +- .../ProtocolSupportListener.java | 17 +++--- .../bukkit/tasks/DelayedAuthHook.java | 1 + .../bukkit/tasks/ForceLoginTask.java | 1 - .../games647/fastlogin/bungee/BungeeCore.java | 12 ++-- .../fastlogin/bungee/BungeeLoginSession.java | 2 +- .../fastlogin/bungee/ImportCommand.java | 5 +- .../fastlogin/bungee/MojangApiBungee.java | 6 +- .../listener/PlayerConnectionListener.java | 2 +- .../listener/PluginMessageListener.java | 8 +-- .../bungee/tasks/AsyncToggleMessage.java | 25 ++++----- .../fastlogin/core/BalancedSSLFactory.java | 8 +-- .../core/{ => shared}/LoginSession.java | 6 +- .../core/shared/MojangApiConnector.java | 21 +++---- 23 files changed, 138 insertions(+), 194 deletions(-) rename core/src/main/java/com/github/games647/fastlogin/core/{ => shared}/LoginSession.java (81%) diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitCore.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitCore.java index 3d5ebb8b..78d79711 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitCore.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitCore.java @@ -54,21 +54,15 @@ public class BukkitCore extends FastLoginCore { InputStreamReader defaultReader = new InputStreamReader(plugin.getResource("messages.yml"), Charsets.UTF_8); YamlConfiguration defaults = YamlConfiguration.loadConfiguration(defaultReader); - for (String key : defaults.getKeys(false)) { - String message = ChatColor.translateAlternateColorCodes('&', defaults.getString(key)); + + messageConfig.setDefaults(defaults); + + messageConfig.getKeys(false).forEach((key) -> { + String message = ChatColor.translateAlternateColorCodes('&', messageConfig.getString(key)); if (!message.isEmpty()) { localeMessages.put(key, message); } - } - - for (String key : messageConfig.getKeys(false)) { - String message = ChatColor.translateAlternateColorCodes('&', messageConfig.getString(key)); - if (message.isEmpty()) { - localeMessages.remove(key); - } else { - localeMessages.put(key, message); - } - } + }); } @Override diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitLoginSession.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitLoginSession.java index 1f81be7e..9d4e2c86 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitLoginSession.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/BukkitLoginSession.java @@ -1,7 +1,7 @@ package com.github.games647.fastlogin.bukkit; -import com.github.games647.fastlogin.core.LoginSession; import com.github.games647.fastlogin.core.PlayerProfile; +import com.github.games647.fastlogin.core.shared.LoginSession; import java.util.UUID; diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java index 67e821bf..e9bf0e5a 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java @@ -14,13 +14,16 @@ import com.github.games647.fastlogin.bukkit.listener.protocollib.LoginSkinApplyL import com.github.games647.fastlogin.bukkit.listener.protocollib.StartPacketListener; import com.github.games647.fastlogin.bukkit.listener.protocolsupport.ProtocolSupportListener; import com.github.games647.fastlogin.bukkit.tasks.DelayedAuthHook; -import com.github.games647.fastlogin.core.hooks.AuthPlugin; import com.github.games647.fastlogin.core.shared.FastLoginCore; +import com.google.common.collect.Iterables; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; import java.security.KeyPair; import java.util.concurrent.ConcurrentMap; import java.util.logging.Level; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -40,7 +43,7 @@ public class FastLoginBukkit extends JavaPlugin { //1 minutes should be enough as a timeout for bad internet connection (Server, Client and Mojang) private final ConcurrentMap session = FastLoginCore.buildCache(1, -1); - + @Override public void onEnable() { core = new BukkitCore(this); @@ -51,7 +54,7 @@ public class FastLoginBukkit extends JavaPlugin { if (ClassUtil.isPresent("org.spigotmc.SpigotConfig")) { bungeeCord = Class.forName("org.spigotmc.SpigotConfig").getDeclaredField("bungee").getBoolean(null); } - } catch (Exception | NoSuchMethodError ex) { + } catch (Exception ex) { getLogger().log(Level.WARNING, "Cannot check bungeecord support. You use a non-spigot build", ex); } @@ -64,7 +67,7 @@ public class FastLoginBukkit extends JavaPlugin { if (bungeeCord) { setServerStarted(); - + //check for incoming messages from the bungeecord version of this plugin getServer().getMessenger().registerIncomingPluginChannel(this, getName(), new BungeeCordListener(this)); getServer().getMessenger().registerOutgoingPluginChannel(this, getName()); @@ -101,7 +104,7 @@ public class FastLoginBukkit extends JavaPlugin { //register commands using a unique name getCommand("premium").setExecutor(new PremiumCommand(this)); getCommand("cracked").setExecutor(new CrackedCommand(this)); - getCommand("import-auth").setExecutor(new ImportCommand(this)); + getCommand("import-auth").setExecutor(new ImportCommand(core)); } @Override @@ -113,15 +116,27 @@ public class FastLoginBukkit extends JavaPlugin { } //remove old blacklists - getServer().getOnlinePlayers().forEach(player -> { - player.removeMetadata(getName(), this); - }); + getServer().getOnlinePlayers().forEach(player -> player.removeMetadata(getName(), this)); } public BukkitCore getCore() { return core; } + public void sendBungeeActivateMessage(CommandSender sender, String target, boolean activate) { + if (sender instanceof Player) { + notifiyBungeeCord((Player) sender, target, activate); + } else { + Player firstPlayer = Iterables.getFirst(getServer().getOnlinePlayers(), null); + if (firstPlayer == null) { + getLogger().info("No player online to send a plugin message to the proxy"); + return; + } + + notifiyBungeeCord(firstPlayer, target, activate); + } + } + @Deprecated public void setPasswordGenerator(PasswordGenerator passwordGenerator) { core.setPasswordGenerator(passwordGenerator); @@ -154,16 +169,7 @@ public class FastLoginBukkit extends JavaPlugin { */ @Deprecated public BukkitAuthPlugin getAuthPlugin() { - AuthPlugin authPlugin = core.getAuthPluginHook(); - if (authPlugin == null) { - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - getLogger().log(Level.SEVERE, null, ex); - } - } - - return (BukkitAuthPlugin) authPlugin; + return (BukkitAuthPlugin) core.getAuthPluginHook(); } @Deprecated @@ -190,4 +196,16 @@ public class FastLoginBukkit extends JavaPlugin { this.serverStarted = true; } } + + private void notifiyBungeeCord(Player sender, String target, boolean activate) { + ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput(); + if (activate) { + dataOutput.writeUTF("ON"); + } else { + dataOutput.writeUTF("OFF"); + } + + dataOutput.writeUTF(target); + sender.sendPluginMessage(this, getName(), dataOutput.toByteArray()); + } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/MojangApiBukkit.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/MojangApiBukkit.java index c8639aa5..a0e35eb0 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/MojangApiBukkit.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/MojangApiBukkit.java @@ -7,7 +7,6 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.util.List; -import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -66,7 +65,7 @@ public class MojangApiBukkit extends MojangApiConnector { } @Override - protected UUID getUUIDFromJson(String json) { + protected String getUUIDFromJson(String json) { boolean isArray = json.startsWith("["); JSONObject mojangPlayer; @@ -82,6 +81,6 @@ public class MojangApiBukkit extends MojangApiConnector { return null; } - return FastLoginCore.parseId(uuid); + return uuid; } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java index a6476107..977c727f 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/CrackedCommand.java @@ -2,9 +2,6 @@ package com.github.games647.fastlogin.bukkit.commands; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.PlayerProfile; -import com.google.common.collect.Iterables; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -14,7 +11,7 @@ import org.bukkit.entity.Player; public class CrackedCommand implements CommandExecutor { - protected final FastLoginBukkit plugin; + private final FastLoginBukkit plugin; public CrackedCommand(FastLoginBukkit plugin) { this.plugin = plugin; @@ -30,7 +27,7 @@ public class CrackedCommand implements CommandExecutor { } if (plugin.isBungeeCord()) { - notifiyBungeeCord(sender, sender.getName()); + plugin.sendBungeeActivateMessage(sender, sender.getName(), false); String message = plugin.getCore().getMessage("wait-on-proxy"); if (message != null) { sender.sendMessage(message); @@ -66,7 +63,7 @@ public class CrackedCommand implements CommandExecutor { } if (plugin.isBungeeCord()) { - notifiyBungeeCord(sender, args[0]); + plugin.sendBungeeActivateMessage(sender, args[0], false); String message = plugin.getCore().getMessage("wait-on-proxy"); if (message != null) { sender.sendMessage(message); @@ -91,28 +88,4 @@ public class CrackedCommand implements CommandExecutor { } } } - - private void notifiyBungeeCord(CommandSender sender, String target) { - if (sender instanceof Player) { - notifiyBungeeCord((Player) sender, target); - } else { - Player firstPlayer = Iterables.getFirst(Bukkit.getOnlinePlayers(), null); - if (firstPlayer == null) { - plugin.getLogger().info("No player online to send a plugin message to the proxy"); - return; - } - - notifiyBungeeCord(firstPlayer, target); - } - } - - private void notifiyBungeeCord(Player sender, String target) { - if (plugin.isBungeeCord()) { - ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput(); - dataOutput.writeUTF("OFF"); - dataOutput.writeUTF(target); - - sender.sendPluginMessage(plugin, plugin.getName(), dataOutput.toByteArray()); - } - } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java index bdc1f917..91130a3f 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/ImportCommand.java @@ -1,7 +1,6 @@ package com.github.games647.fastlogin.bukkit.commands; import com.github.games647.fastlogin.bukkit.BukkitCore; -import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.AuthStorage; import com.github.games647.fastlogin.core.importer.ImportPlugin; import org.bukkit.ChatColor; @@ -12,10 +11,10 @@ import org.bukkit.command.CommandSender; public class ImportCommand implements CommandExecutor { - protected final FastLoginBukkit plugin; + private final BukkitCore core; - public ImportCommand(FastLoginBukkit plugin) { - this.plugin = plugin; + public ImportCommand(BukkitCore core) { + this.core = core; } @Override @@ -71,7 +70,6 @@ public class ImportCommand implements CommandExecutor { password = args[5]; } - BukkitCore core = plugin.getCore(); AuthStorage storage = core.getStorage(); boolean success = core.importDatabase(importPlugin, true, storage, host, database, username, password); if (success) { diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/PremiumCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/PremiumCommand.java index 03e715fe..69407958 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/PremiumCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/commands/PremiumCommand.java @@ -2,9 +2,6 @@ package com.github.games647.fastlogin.bukkit.commands; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.PlayerProfile; -import com.google.common.collect.Iterables; -import com.google.common.io.ByteArrayDataOutput; -import com.google.common.io.ByteStreams; import java.util.UUID; @@ -21,7 +18,7 @@ import org.bukkit.entity.Player; */ public class PremiumCommand implements CommandExecutor { - protected final FastLoginBukkit plugin; + private final FastLoginBukkit plugin; public PremiumCommand(FastLoginBukkit plugin) { this.plugin = plugin; @@ -37,7 +34,7 @@ public class PremiumCommand implements CommandExecutor { } if (plugin.isBungeeCord()) { - notifiyBungeeCord(sender, sender.getName()); + plugin.sendBungeeActivateMessage(sender, sender.getName(), true); String message = plugin.getCore().getMessage("wait-on-proxy"); if (message != null) { sender.sendMessage(message); @@ -82,7 +79,7 @@ public class PremiumCommand implements CommandExecutor { } if (plugin.isBungeeCord()) { - notifiyBungeeCord(sender, args[0]); + plugin.sendBungeeActivateMessage(sender, args[0], true); String message = plugin.getCore().getMessage("wait-on-proxy"); if (message != null) { sender.sendMessage(message); @@ -108,28 +105,4 @@ public class PremiumCommand implements CommandExecutor { } } } - - private void notifiyBungeeCord(CommandSender sender, String target) { - if (sender instanceof Player) { - notifiyBungeeCord((Player) sender, target); - } else { - Player firstPlayer = Iterables.getFirst(Bukkit.getOnlinePlayers(), null); - if (firstPlayer == null) { - plugin.getLogger().info("No player online to send a plugin message to the proxy"); - return; - } - - notifiyBungeeCord(firstPlayer, target); - } - } - - private void notifiyBungeeCord(Player sender, String target) { - if (plugin.isBungeeCord()) { - ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput(); - dataOutput.writeUTF("ON"); - dataOutput.writeUTF(target); - - sender.sendPluginMessage(plugin, plugin.getName(), dataOutput.toByteArray()); - } - } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/BungeeCordListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/BungeeCordListener.java index e515f5d9..f652732c 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/BungeeCordListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/BungeeCordListener.java @@ -4,13 +4,13 @@ import com.github.games647.fastlogin.bukkit.BukkitLoginSession; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.bukkit.tasks.ForceLoginTask; import com.github.games647.fastlogin.core.hooks.AuthPlugin; -import com.google.common.base.Charsets; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; -import com.google.common.io.Files; import java.io.File; import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.file.Files; import java.util.List; import java.util.Set; import java.util.UUID; @@ -69,33 +69,37 @@ public class BungeeCordListener implements PluginMessageListener { //fail if BungeeCord support is disabled (id = null) if (proxyIds.contains(sourceId)) { - String id = '/' + checkedPlayer.getAddress().getAddress().getHostAddress() + ':' - + checkedPlayer.getAddress().getPort(); - if ("AUTO_LOGIN".equalsIgnoreCase(subchannel)) { - BukkitLoginSession playerSession = new BukkitLoginSession(playerName, true); - playerSession.setVerified(true); - plugin.getSessions().put(id, playerSession); - Bukkit.getScheduler().runTaskAsynchronously(plugin, new ForceLoginTask(plugin, player)); - } else if ("AUTO_REGISTER".equalsIgnoreCase(subchannel)) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - AuthPlugin authPlugin = plugin.getCore().getAuthPluginHook(); - try { - //we need to check if the player is registered on Bukkit too - if (authPlugin == null || !authPlugin.isRegistered(playerName)) { - BukkitLoginSession playerSession = new BukkitLoginSession(playerName, false); - playerSession.setVerified(true); - plugin.getSessions().put(id, playerSession); - new ForceLoginTask(plugin, player).run(); - } - } catch (Exception ex) { - plugin.getLogger().log(Level.SEVERE, "Failed to query isRegistered", ex); - } - }); - } + readMessage(checkedPlayer, subchannel, playerName, player); } } } + private void readMessage(Player checkedPlayer, String subchannel, String playerName, Player player) { + InetSocketAddress address = checkedPlayer.getAddress(); + String id = '/' + address.getAddress().getHostAddress() + ':' + address.getPort(); + if ("AUTO_LOGIN".equalsIgnoreCase(subchannel)) { + BukkitLoginSession playerSession = new BukkitLoginSession(playerName, true); + playerSession.setVerified(true); + plugin.getSessions().put(id, playerSession); + Bukkit.getScheduler().runTaskAsynchronously(plugin, new ForceLoginTask(plugin, player)); + } else if ("AUTO_REGISTER".equalsIgnoreCase(subchannel)) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + AuthPlugin authPlugin = plugin.getCore().getAuthPluginHook(); + try { + //we need to check if the player is registered on Bukkit too + if (authPlugin == null || !authPlugin.isRegistered(playerName)) { + BukkitLoginSession playerSession = new BukkitLoginSession(playerName, false); + playerSession.setVerified(true); + plugin.getSessions().put(id, playerSession); + new ForceLoginTask(plugin, player).run(); + } + } catch (Exception ex) { + plugin.getLogger().log(Level.SEVERE, "Failed to query isRegistered", ex); + } + }); + } + } + public Set loadBungeeCordIds() { File whitelistFile = new File(plugin.getDataFolder(), FILE_NAME); //create a new folder if it doesn't exist. Fail silently otherwise @@ -105,7 +109,7 @@ public class BungeeCordListener implements PluginMessageListener { whitelistFile.createNewFile(); } - List lines = Files.readLines(whitelistFile, Charsets.UTF_8); + List lines = Files.readAllLines(whitelistFile.toPath()); return lines.stream().map(String::trim).map(UUID::fromString).collect(Collectors.toSet()); } catch (IOException ex) { plugin.getLogger().log(Level.SEVERE, "Failed to create file for Proxy whitelist", ex); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java index 528705e7..311fcdd5 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/LoginSkinApplyListener.java @@ -10,7 +10,6 @@ import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Collection; import java.util.logging.Level; import org.bukkit.entity.Player; @@ -39,8 +38,7 @@ public class LoginSkinApplyListener implements Listener { if (plugin.getConfig().getBoolean("forwardSkin")) { //go through every session, because player.getAddress is null //loginEvent.getAddress is just a InetAddress not InetSocketAddres, so not unique enough - Collection sessions = plugin.getSessions().values(); - for (BukkitLoginSession session : sessions) { + for (BukkitLoginSession session : plugin.getSessions().values()) { if (session.getUsername().equals(player.getName())) { String signature = session.getSkinSignature(); String skinData = session.getEncodedSkinData(); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/StartPacketListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/StartPacketListener.java index a3c99581..5ff7ce1c 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/StartPacketListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/StartPacketListener.java @@ -51,12 +51,11 @@ public class StartPacketListener extends PacketAdapter { */ @Override public void onPacketReceiving(PacketEvent packetEvent) { - if (packetEvent.isCancelled() || plugin.getAuthPlugin() == null) { + if (packetEvent.isCancelled() + || plugin.getCore().getAuthPluginHook()== null || !plugin.isServerFullyStarted()) { return; } - plugin.setServerStarted(); - Player player = packetEvent.getPlayer(); //this includes ip:port. Should be unique for an incoming login request with a timeout of 2 minutes diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocolsupport/ProtocolSupportListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocolsupport/ProtocolSupportListener.java index 986dfb93..d14d58fb 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocolsupport/ProtocolSupportListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocolsupport/ProtocolSupportListener.java @@ -42,16 +42,15 @@ public class ProtocolSupportListener extends JoinManagement premium player - if (propertiesResolveEvent.hasProperty("textures")) { - InetSocketAddress address = propertiesResolveEvent.getAddress(); - BukkitLoginSession session = plugin.getSessions().get(address.toString()); - if (session != null) { - String ip = address.getAddress().getHostAddress(); - plugin.getCore().getPendingLogins().remove(ip + session.getUsername()); + InetSocketAddress address = propertiesResolveEvent.getAddress(); + BukkitLoginSession session = plugin.getSessions().get(address.toString()); - session.setVerified(true); - } + //skin was resolved -> premium player + if (propertiesResolveEvent.hasProperty("textures") && session != null) { + String ip = address.getAddress().getHostAddress(); + plugin.getCore().getPendingLogins().remove(ip + session.getUsername()); + + session.setVerified(true); } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java index 7a253942..ed89e60d 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/DelayedAuthHook.java @@ -66,6 +66,7 @@ public class DelayedAuthHook implements Runnable { if (plugin.getCore().getAuthPluginHook() == null) { plugin.getCore().setAuthPluginHook(authPluginHook); + plugin.setServerStarted(); } return true; diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/ForceLoginTask.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/ForceLoginTask.java index 584365cc..c07b40aa 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/ForceLoginTask.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/tasks/ForceLoginTask.java @@ -8,7 +8,6 @@ import com.github.games647.fastlogin.core.hooks.AuthPlugin; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.logging.Level; diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeCore.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeCore.java index fc939b4d..f22e86a9 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeCore.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeCore.java @@ -23,7 +23,7 @@ import net.md_5.bungee.config.YamlConfiguration; public class BungeeCore extends FastLoginCore { private static Map generateConfigMap(Configuration config) { - return config.getKeys().stream().collect(Collectors.toMap(key -> key, key -> config.get(key))); + return config.getKeys().stream().collect(Collectors.toMap(key -> key, config::get)); } private final FastLoginBungee plugin; @@ -58,14 +58,14 @@ public class BungeeCore extends FastLoginCore { public void loadMessages() { try { plugin.saveDefaultFile("messages.yml"); + ConfigurationProvider configProvider = ConfigurationProvider.getProvider(YamlConfiguration.class); - Configuration defaults = ConfigurationProvider.getProvider(YamlConfiguration.class) - .load(getClass().getResourceAsStream("/messages.yml")); + Configuration defaults = configProvider.load(getClass().getResourceAsStream("/messages.yml")); File messageFile = new File(getDataFolder(), "messages.yml"); - Configuration messageConfig = ConfigurationProvider.getProvider(YamlConfiguration.class) - .load(messageFile, defaults); - messageConfig.getKeys().forEach((key) -> { + Configuration messageConfig = configProvider.load(messageFile, defaults); + + messageConfig.getKeys().forEach(key -> { String message = ChatColor.translateAlternateColorCodes('&', messageConfig.getString(key)); if (!message.isEmpty()) { localeMessages.put(key, message); diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeLoginSession.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeLoginSession.java index e3224b11..4b0b29cb 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeLoginSession.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/BungeeLoginSession.java @@ -1,7 +1,7 @@ package com.github.games647.fastlogin.bungee; -import com.github.games647.fastlogin.core.LoginSession; import com.github.games647.fastlogin.core.PlayerProfile; +import com.github.games647.fastlogin.core.shared.LoginSession; public class BungeeLoginSession extends LoginSession { diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java index 199cd4a2..f4f3087f 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/ImportCommand.java @@ -11,12 +11,12 @@ import net.md_5.bungee.api.plugin.Command; public class ImportCommand extends Command { - private final FastLoginBungee plugin; + private final BungeeCore core; public ImportCommand(FastLoginBungee plugin) { super("import-db", plugin.getDescription().getName().toLowerCase() + ".import"); - this.plugin = plugin; + this.core = plugin.getCore(); } @Override @@ -76,7 +76,6 @@ public class ImportCommand extends Command { password = args[5]; } - BungeeCore core = plugin.getCore(); AuthStorage storage = core.getStorage(); boolean success = core.importDatabase(importPlugin, true, storage, host, database, username, password); if (success) { diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/MojangApiBungee.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/MojangApiBungee.java index 47258922..40fd78a0 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/MojangApiBungee.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/MojangApiBungee.java @@ -1,10 +1,8 @@ package com.github.games647.fastlogin.bungee; -import com.github.games647.fastlogin.core.shared.FastLoginCore; import com.github.games647.fastlogin.core.shared.MojangApiConnector; import java.util.List; -import java.util.UUID; import java.util.logging.Logger; import net.md_5.bungee.BungeeCord; @@ -16,7 +14,7 @@ public class MojangApiBungee extends MojangApiConnector { } @Override - protected UUID getUUIDFromJson(String json) { + protected String getUUIDFromJson(String json) { boolean isArray = json.startsWith("["); MojangPlayer mojangPlayer; @@ -30,7 +28,7 @@ public class MojangApiBungee extends MojangApiConnector { return null; } - return FastLoginCore.parseId(mojangPlayer.getId()); + return mojangPlayer.getId(); } @Override diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java index 14786197..7168a176 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PlayerConnectionListener.java @@ -3,7 +3,7 @@ package com.github.games647.fastlogin.bungee.listener; import com.github.games647.fastlogin.bungee.FastLoginBungee; import com.github.games647.fastlogin.bungee.tasks.AsyncPremiumCheck; import com.github.games647.fastlogin.bungee.tasks.ForceLoginTask; -import com.github.games647.fastlogin.core.LoginSession; +import com.github.games647.fastlogin.core.shared.LoginSession; import com.github.games647.fastlogin.core.PlayerProfile; import com.google.common.base.Charsets; diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java index ca79a477..e15d1ae0 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/PluginMessageListener.java @@ -42,9 +42,7 @@ public class PluginMessageListener implements Listener { byte[] data = Arrays.copyOf(pluginMessageEvent.getData(), pluginMessageEvent.getData().length); ProxiedPlayer forPlayer = (ProxiedPlayer) pluginMessageEvent.getReceiver(); - ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> { - readMessage(forPlayer, data); - }); + ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> readMessage(forPlayer, data)); } } @@ -63,12 +61,12 @@ public class PluginMessageListener implements Listener { } plugin.getCore().getPendingConfirms().remove(forPlayer.getUniqueId()); - AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, true); + AsyncToggleMessage task = new AsyncToggleMessage(plugin.getCore(), forPlayer, playerName, true); ProxyServer.getInstance().getScheduler().runAsync(plugin, task); } else if ("OFF".equals(subchannel)) { String playerName = dataInput.readUTF(); - AsyncToggleMessage task = new AsyncToggleMessage(plugin, forPlayer, playerName, false); + AsyncToggleMessage task = new AsyncToggleMessage(plugin.getCore(), forPlayer, playerName, false); ProxyServer.getInstance().getScheduler().runAsync(plugin, task); } else if ("SUCCESS".equals(subchannel)) { onSuccessMessage(forPlayer); diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java index 012a57b3..07a44efe 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/tasks/AsyncToggleMessage.java @@ -1,6 +1,6 @@ package com.github.games647.fastlogin.bungee.tasks; -import com.github.games647.fastlogin.bungee.FastLoginBungee; +import com.github.games647.fastlogin.bungee.BungeeCore; import com.github.games647.fastlogin.core.PlayerProfile; import net.md_5.bungee.api.chat.TextComponent; @@ -8,14 +8,13 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; public class AsyncToggleMessage implements Runnable { - private final FastLoginBungee plugin; + private final BungeeCore core; private final ProxiedPlayer fromPlayer; private final String targetPlayer; private final boolean toPremium; - public AsyncToggleMessage(FastLoginBungee plugin, ProxiedPlayer fromPlayer, String targetPlayer - , boolean toPremium) { - this.plugin = plugin; + public AsyncToggleMessage(BungeeCore core, ProxiedPlayer fromPlayer, String targetPlayer, boolean toPremium) { + this.core = core; this.fromPlayer = fromPlayer; this.targetPlayer = targetPlayer; this.toPremium = toPremium; @@ -31,28 +30,28 @@ public class AsyncToggleMessage implements Runnable { } private void turnOffPremium() { - PlayerProfile playerProfile = plugin.getCore().getStorage().loadProfile(targetPlayer); + PlayerProfile playerProfile = core.getStorage().loadProfile(targetPlayer); //existing player is already cracked if (playerProfile.getUserId() != -1 && !playerProfile.isPremium()) { - fromPlayer.sendMessage(TextComponent.fromLegacyText(plugin.getCore().getMessage("not-premium"))); + fromPlayer.sendMessage(TextComponent.fromLegacyText(core.getMessage("not-premium"))); return; } playerProfile.setPremium(false); playerProfile.setUuid(null); - plugin.getCore().getStorage().save(playerProfile); - fromPlayer.sendMessage(TextComponent.fromLegacyText(plugin.getCore().getMessage("remove-premium"))); + core.getStorage().save(playerProfile); + fromPlayer.sendMessage(TextComponent.fromLegacyText(core.getMessage("remove-premium"))); } private void activatePremium() { - PlayerProfile playerProfile = plugin.getCore().getStorage().loadProfile(targetPlayer); + PlayerProfile playerProfile = core.getStorage().loadProfile(targetPlayer); if (playerProfile.isPremium()) { - fromPlayer.sendMessage(TextComponent.fromLegacyText(plugin.getCore().getMessage("already-exists"))); + fromPlayer.sendMessage(TextComponent.fromLegacyText(core.getMessage("already-exists"))); return; } playerProfile.setPremium(true); - plugin.getCore().getStorage().save(playerProfile); - fromPlayer.sendMessage(TextComponent.fromLegacyText(plugin.getCore().getMessage("add-premium"))); + core.getStorage().save(playerProfile); + fromPlayer.sendMessage(TextComponent.fromLegacyText(core.getMessage("add-premium"))); } } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java b/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java index cc56083d..b0908ade 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/BalancedSSLFactory.java @@ -1,12 +1,12 @@ package com.github.games647.fastlogin.core; +import com.google.common.collect.ImmutableList; + import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; -import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import javax.net.ssl.SSLSocketFactory; @@ -20,9 +20,9 @@ public class BalancedSSLFactory extends SSLSocketFactory { private AtomicInteger id; - public BalancedSSLFactory(SSLSocketFactory oldFactory, Set localAddresses) { + public BalancedSSLFactory(SSLSocketFactory oldFactory, Iterable localAddresses) { this.oldFactory = oldFactory; - this.localAddresses = new ArrayList<>(localAddresses); + this.localAddresses = ImmutableList.copyOf(localAddresses); } @Override diff --git a/core/src/main/java/com/github/games647/fastlogin/core/LoginSession.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSession.java similarity index 81% rename from core/src/main/java/com/github/games647/fastlogin/core/LoginSession.java rename to core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSession.java index 47b07518..a22345e1 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/LoginSession.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/LoginSession.java @@ -1,6 +1,8 @@ -package com.github.games647.fastlogin.core; +package com.github.games647.fastlogin.core.shared; -public class LoginSession { +import com.github.games647.fastlogin.core.PlayerProfile; + +public abstract class LoginSession { private final String username; private final PlayerProfile profile; diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/MojangApiConnector.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/MojangApiConnector.java index 9d5cd929..d31d7c24 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/MojangApiConnector.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/MojangApiConnector.java @@ -2,6 +2,7 @@ package com.github.games647.fastlogin.core.shared; import com.github.games647.fastlogin.core.BalancedSSLFactory; import com.google.common.collect.Sets; +import com.google.common.io.CharStreams; import java.io.BufferedReader; import java.io.IOException; @@ -98,8 +99,8 @@ public abstract class MojangApiConnector { if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = reader.readLine(); - if (line != null && !line.equals("null")) { - return getUUIDFromJson(line); + if (!line.equals("null")) { + return FastLoginCore.parseId(getUUIDFromJson(line)); } } else if (connection.getResponseCode() == RATE_LIMIT_CODE) { logger.info("RATE_LIMIT REACHED - TRYING THIRD-PARTY API"); @@ -118,9 +119,7 @@ public abstract class MojangApiConnector { public UUID getUUIDFromAPI(String playerName) { try { - HttpURLConnection httpConnection = (HttpURLConnection) new URL(MCAPI_UUID_URL + playerName).openConnection(); - httpConnection.addRequestProperty("Content-Type", "application/json"); - httpConnection.setRequestProperty("User-Agent", USER_AGENT); + HttpURLConnection httpConnection = getConnection(MCAPI_UUID_URL + playerName); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { //cracked @@ -128,14 +127,8 @@ public abstract class MojangApiConnector { } BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream())); - StringBuilder inputBuilder = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - inputBuilder.append(line); - } - - String input = inputBuilder.toString(); - return getUUIDFromJson(input); + String input = CharStreams.toString(reader); + return FastLoginCore.parseId(getUUIDFromJson(input)); } catch (IOException iOException) { logger.log(Level.SEVERE, "Tried converting name->uuid from third-party api", iOException); } @@ -145,7 +138,7 @@ public abstract class MojangApiConnector { public abstract boolean hasJoinedServer(Object session, String serverId); - protected abstract UUID getUUIDFromJson(String json); + protected abstract String getUUIDFromJson(String json); protected HttpsURLConnection getConnection(String url) throws IOException { HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();