Fix saving bug

This commit is contained in:
games647
2016-04-27 20:35:23 +02:00
parent b697dc6655
commit 6edd40742d
6 changed files with 112 additions and 104 deletions
@@ -41,6 +41,10 @@ public class FastLoginBungee extends Plugin {
@Override
public void onEnable() {
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
File configFile = new File(getDataFolder(), "config.yml");
if (!configFile.exists()) {
try (InputStream in = getResourceAsStream("config.yml")) {
@@ -53,13 +57,13 @@ public class FastLoginBungee extends Plugin {
try {
configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
String driver = configuration.getString("storage.driver");
String host = configuration.getString("storage.host", "");
int port = configuration.getInt("storage.port", 3306);
String database = configuration.getString("storage.database");
String driver = configuration.getString("driver");
String host = configuration.getString("host", "");
int port = configuration.getInt("port", 3306);
String database = configuration.getString("database");
String username = configuration.getString("storage.username", "");
String password = configuration.getString("storage.password", "");
String username = configuration.getString("username", "");
String password = configuration.getString("password", "");
storage = new Storage(this, driver, host, port, database, username, password);
try {
storage.createTables();
@@ -1,7 +1,6 @@
package com.github.games647.fastlogin.bungee;
import com.github.games647.fastlogin.bungee.hooks.BungeeAuthPlugin;
import com.google.common.base.Charsets;
import com.google.common.cache.CacheBuilder;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
@@ -18,14 +17,10 @@ import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.event.PreLoginEvent;
import net.md_5.bungee.api.event.ServerConnectedEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.connection.LoginResult;
import net.md_5.bungee.connection.LoginResult.Property;
import net.md_5.bungee.event.EventHandler;
/**
@@ -57,10 +52,13 @@ public class PlayerConnectionListener implements Listener {
PlayerProfile playerProfile = plugin.getStorage().getProfile(username, true);
if (playerProfile != null) {
//user not exists in the db
if (!playerProfile.isPremium() && playerProfile.getUserId() == -1) {
if (playerProfile.isPremium()) {
connection.setOnlineMode(true);
} else if (playerProfile.getUserId() == -1) {
//user not exists in the db
BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin();
if (plugin.getConfiguration().getBoolean("autoRegister") && !authPlugin.isRegistered(username)) {
if (plugin.getConfiguration().getBoolean("autoRegister")
&& (authPlugin == null || !authPlugin.isRegistered(username))) {
UUID premiumUUID = plugin.getMojangApiConnector().getPremiumUUID(username);
if (premiumUUID != null) {
plugin.getLogger().log(Level.FINER, "Player {0} uses a premium username", username);
@@ -68,30 +66,6 @@ public class PlayerConnectionListener implements Listener {
pendingAutoRegister.put(connection, new Object());
}
}
} else if (playerProfile.isPremium()) {
connection.setOnlineMode(true);
}
}
}
@EventHandler
public void onLogin(LoginEvent loginEvent) {
PendingConnection connection = loginEvent.getConnection();
String username = connection.getName();
if (connection.isOnlineMode()) {
//bungeecord will do this automatically so override it on disabled option
if (!plugin.getConfiguration().getBoolean("premiumUuid")) {
UUID offlineUUID = UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
connection.setUniqueId(offlineUUID);
}
if (!plugin.getConfiguration().getBoolean("forwardSkin")) {
InitialHandler initialHandler = (InitialHandler) connection;
//this is null on offline mode
LoginResult loginProfile = initialHandler.getLoginProfile();
if (loginProfile != null) {
loginProfile.setProperties(new Property[]{});
}
}
}
}
@@ -100,12 +74,29 @@ public class PlayerConnectionListener implements Listener {
public void onServerConnected(ServerConnectedEvent serverConnectedEvent) {
ProxiedPlayer player = serverConnectedEvent.getPlayer();
//send message even when the online mode is activated by default
final PlayerProfile playerProfile = plugin.getStorage().getProfile(player.getName(), false);
if (playerProfile.getUserId() == -1) {
ProxyServer.getInstance().getScheduler().runAsync(plugin, new Runnable() {
@Override
public void run() {
plugin.getStorage().save(playerProfile);
}
});
}
if (player.getPendingConnection().isOnlineMode()) {
Server server = serverConnectedEvent.getServer();
boolean autoRegister = pendingAutoRegister.remove(player.getPendingConnection()) != null;
ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput();
//subchannel name
dataOutput.writeUTF("CHECKED");
if (autoRegister) {
dataOutput.writeUTF("AUTO_REGISTER");
} else {
dataOutput.writeUTF("AUTO_LOGIN");
}
//Data is sent through a random player. We have to tell the Bukkit version of this plugin the target
dataOutput.writeUTF(player.getName());
@@ -119,12 +110,11 @@ public class PlayerConnectionListener implements Listener {
BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin();
if (authPlugin != null) {
Object existed = pendingAutoRegister.remove(player.getPendingConnection());
if (existed == null) {
authPlugin.forceLogin(player);
} else {
if (autoRegister) {
String password = plugin.generateStringPassword();
authPlugin.forceRegister(player, password);
} else {
authPlugin.forceLogin(player);
}
}
}