Optimized yaml reconnect locations! Ding dong sqlite is dead..
This commit is contained in:
parent
526137be7b
commit
499337c98e
@ -67,12 +67,6 @@
|
||||
<version>3.18.0-GA</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.7.2</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -2,7 +2,7 @@ package net.md_5.bungee;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import net.md_5.bungee.log.BungeeLogger;
|
||||
import net.md_5.bungee.reconnect.SQLReconnectHandler;
|
||||
import net.md_5.bungee.reconnect.YamlReconnectHandler;
|
||||
import net.md_5.bungee.scheduler.BungeeScheduler;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import com.google.gson.Gson;
|
||||
@ -219,7 +219,7 @@ public class BungeeCord extends ProxyServer
|
||||
config.load();
|
||||
if ( reconnectHandler == null )
|
||||
{
|
||||
reconnectHandler = new SQLReconnectHandler();
|
||||
reconnectHandler = new YamlReconnectHandler();
|
||||
}
|
||||
isRunning = true;
|
||||
|
||||
|
@ -1,96 +0,0 @@
|
||||
package net.md_5.bungee.reconnect;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
|
||||
public class SQLReconnectHandler extends AbstractReconnectManager
|
||||
{
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
public SQLReconnectHandler() throws ClassNotFoundException, SQLException
|
||||
{
|
||||
Class.forName( "org.sqlite.JDBC" );
|
||||
connection = DriverManager.getConnection( "jdbc:sqlite:bungee.sqlite" );
|
||||
|
||||
try ( PreparedStatement ps = connection.prepareStatement(
|
||||
"CREATE TABLE IF NOT EXISTS players ("
|
||||
+ "playerId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
|
||||
+ "username TEXT NOT NULL UNIQUE COLLATE NOCASE,"
|
||||
+ "seen INTEGER,"
|
||||
+ "server TEXT"
|
||||
+ ");" ) )
|
||||
{
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ServerInfo getStoredServer(ProxiedPlayer player)
|
||||
{
|
||||
String server = null;
|
||||
try ( PreparedStatement ps = connection.prepareStatement( "SELECT server FROM players WHERE username = ?" ) )
|
||||
{
|
||||
ps.setString( 1, player.getName() );
|
||||
try ( ResultSet rs = ps.executeQuery() )
|
||||
{
|
||||
if ( rs.next() )
|
||||
{
|
||||
server = rs.getString( 1 );
|
||||
} else
|
||||
{
|
||||
try ( PreparedStatement playerUpdate = connection.prepareStatement( "INSERT INTO players( username ) VALUES( ? )" ) )
|
||||
{
|
||||
playerUpdate.setString( 1, player.getName() );
|
||||
playerUpdate.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch ( SQLException ex )
|
||||
{
|
||||
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not load location for player " + player.getName(), ex );
|
||||
}
|
||||
|
||||
return ProxyServer.getInstance().getServerInfo( server );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServer(ProxiedPlayer player)
|
||||
{
|
||||
|
||||
try ( PreparedStatement ps = connection.prepareStatement( "UPDATE players SET server = ?, seen = ? WHERE username = ?" ) )
|
||||
{
|
||||
ps.setString( 1, player.getServer().getInfo().getName() );
|
||||
ps.setInt( 2, (int) ( System.currentTimeMillis() / 1000L ) );
|
||||
ps.setString( 3, player.getName() );
|
||||
ps.executeUpdate();
|
||||
} catch ( SQLException ex )
|
||||
{
|
||||
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not save location for player " + player.getName() + " on server " + player.getServer().getInfo().getName(), ex );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save()
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
try
|
||||
{
|
||||
connection.close();
|
||||
} catch ( SQLException ex )
|
||||
{
|
||||
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Error closing SQLite connection", ex );
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package net.md_5.bungee.reconnect;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.logging.Level;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
public class YamlReconnectHandler extends AbstractReconnectManager
|
||||
{
|
||||
|
||||
private final Yaml yaml = new Yaml();
|
||||
private final File file = new File( "locations.yml" );
|
||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
/*========================================================================*/
|
||||
private Map<String, String> data;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public YamlReconnectHandler()
|
||||
{
|
||||
try
|
||||
{
|
||||
file.createNewFile();
|
||||
try ( FileReader rd = new FileReader( file ) )
|
||||
{
|
||||
data = yaml.loadAs( rd, Map.class );
|
||||
}
|
||||
} catch ( IOException ex )
|
||||
{
|
||||
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not load reconnect locations", ex );
|
||||
}
|
||||
|
||||
if ( data == null )
|
||||
{
|
||||
data = new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ServerInfo getStoredServer(ProxiedPlayer player)
|
||||
{
|
||||
ServerInfo server = null;
|
||||
lock.readLock().lock();
|
||||
try
|
||||
{
|
||||
server = ProxyServer.getInstance().getServerInfo( data.get( key( player ) ) );
|
||||
} finally
|
||||
{
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServer(ProxiedPlayer player)
|
||||
{
|
||||
lock.writeLock().lock();
|
||||
try
|
||||
{
|
||||
data.put( key( player ), player.getServer().getInfo().getName() );
|
||||
} finally
|
||||
{
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private String key(ProxiedPlayer player)
|
||||
{
|
||||
InetSocketAddress host = player.getPendingConnection().getVirtualHost();
|
||||
return player.getName() + ";" + host.getHostString() + ":" + host.getPort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save()
|
||||
{
|
||||
lock.readLock().lock();
|
||||
try ( FileWriter wr = new FileWriter( file ) )
|
||||
{
|
||||
yaml.dump( data, wr );
|
||||
} catch ( IOException ex )
|
||||
{
|
||||
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not save reconnect locations", ex );
|
||||
} finally
|
||||
{
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close()
|
||||
{
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user