Refactor more code for more Java 8 and Guava usage

This commit is contained in:
games647
2016-09-17 15:19:07 +02:00
parent b533197f05
commit ca42a7c19e
23 changed files with 138 additions and 194 deletions
@@ -54,21 +54,15 @@ public class BukkitCore extends FastLoginCore<Player> {
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
@@ -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;
@@ -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<String, BukkitLoginSession> 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<Player> 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());
}
}
@@ -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;
}
}
@@ -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());
}
}
}
@@ -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) {
@@ -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());
}
}
}
@@ -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<Player> 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<Player> 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<UUID> 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<String> lines = Files.readLines(whitelistFile, Charsets.UTF_8);
List<String> 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);
@@ -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<BukkitLoginSession> 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();
@@ -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
@@ -42,16 +42,15 @@ public class ProtocolSupportListener extends JoinManagement<Player, ProtocolLogi
@EventHandler(ignoreCancelled = true)
public void onPropertiesResolve(PlayerPropertiesResolveEvent propertiesResolveEvent) {
//skin was resolved -> 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);
}
}
@@ -66,6 +66,7 @@ public class DelayedAuthHook implements Runnable {
if (plugin.getCore().getAuthPluginHook() == null) {
plugin.getCore().setAuthPluginHook(authPluginHook);
plugin.setServerStarted();
}
return true;
@@ -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;