Replace default/fallback servers with a server priority list.

This commit is contained in:
md_5 2016-03-01 09:13:11 +11:00
parent 7d2c2ab074
commit 219819b738
2 changed files with 86 additions and 10 deletions

View File

@ -1,8 +1,11 @@
package net.md_5.bungee.api.config; package net.md_5.bungee.api.config;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map; import java.util.Map;
import lombok.AccessLevel;
import lombok.Data; import lombok.Data;
import lombok.Getter;
/** /**
* Class representing the configuration of a server listener. Used for allowing * Class representing the configuration of a server listener. Used for allowing
@ -29,14 +32,10 @@ public class ListenerInfo
*/ */
private final int tabListSize; private final int tabListSize;
/** /**
* Name of the server which users will be taken to by default. * List of servers in order of join attempt. First attempt is the first
* element, second attempt is the next element, etc etc.
*/ */
private final String defaultServer; private final List<String> serverPriority;
/**
* Name of the server which users will be taken when current server goes
* down.
*/
private final String fallbackServer;
/** /**
* Whether reconnect locations will be used, or else the user is simply * Whether reconnect locations will be used, or else the user is simply
* transferred to the default server on connect. * transferred to the default server on connect.
@ -68,4 +67,29 @@ public class ListenerInfo
* Whether to enable udp query. * Whether to enable udp query.
*/ */
private final boolean queryEnabled; private final boolean queryEnabled;
/**
* Gets the highest priority server to join.
*
* @return default server
* @deprecated replaced by {@link #serverPriority}
*/
@Deprecated
public String getDefaultServer()
{
return serverPriority.get( 0 );
}
/**
* Gets the second highest priority server to join, or else the highest
* priority.
*
* @return fallback server
* @deprecated replaced by {@link #serverPriority}
*/
@Deprecated
public String getFallbackServer()
{
return ( serverPriority.size() > 1 ) ? serverPriority.get( 1 ) : getDefaultServer();
}
} }

View File

@ -7,12 +7,14 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -126,6 +128,34 @@ public class YamlConfig implements ConfigurationAdapter
} }
} }
@SuppressWarnings("unchecked")
private void set(String path, Object val, Map submap)
{
int index = path.indexOf( '.' );
if ( index == -1 )
{
if ( val == null )
{
submap.remove( path );
} else
{
submap.put( path, val );
}
save();
} else
{
String first = path.substring( 0, index );
String second = path.substring( index + 1, path.length() );
Map sub = (Map) submap.get( first );
if ( sub == null )
{
sub = new LinkedHashMap();
submap.put( first, sub );
}
set( second, val, sub );
}
}
private void save() private void save()
{ {
try try
@ -200,8 +230,6 @@ public class YamlConfig implements ConfigurationAdapter
motd = ChatColor.translateAlternateColorCodes( '&', motd ); motd = ChatColor.translateAlternateColorCodes( '&', motd );
int maxPlayers = get( "max_players", 1, val ); int maxPlayers = get( "max_players", 1, val );
String defaultServer = get( "default_server", "lobby", val );
String fallbackServer = get( "fallback_server", defaultServer, val );
boolean forceDefault = get( "force_default_server", false, val ); boolean forceDefault = get( "force_default_server", false, val );
String host = get( "host", "0.0.0.0:25577", val ); String host = get( "host", "0.0.0.0:25577", val );
int tabListSize = get( "tab_size", 60, val ); int tabListSize = get( "tab_size", 60, val );
@ -219,7 +247,31 @@ public class YamlConfig implements ConfigurationAdapter
boolean query = get( "query_enabled", false, val ); boolean query = get( "query_enabled", false, val );
int queryPort = get( "query_port", 25577, val ); int queryPort = get( "query_port", 25577, val );
ListenerInfo info = new ListenerInfo( address, motd, maxPlayers, tabListSize, defaultServer, fallbackServer, forceDefault, forced, value.toString(), setLocalAddress, pingPassthrough, queryPort, query ); List<String> serverPriority = new ArrayList<>( get( "priorities", Collections.EMPTY_LIST, val ) );
// Default server list migration
// TODO: Remove from submap
String defaultServer = get( "default_server", null, val );
String fallbackServer = get( "fallback_server", null, val );
if ( defaultServer != null )
{
serverPriority.add( defaultServer );
set( "default_server", null, val );
}
if ( fallbackServer != null )
{
serverPriority.add( fallbackServer );
set( "fallback_server", null, val );
}
// Add defaults if required
if ( serverPriority.isEmpty() )
{
serverPriority.add( "lobby" );
}
set( "priorities", serverPriority, val );
ListenerInfo info = new ListenerInfo( address, motd, maxPlayers, tabListSize, serverPriority, forceDefault, forced, value.toString(), setLocalAddress, pingPassthrough, queryPort, query );
ret.add( info ); ret.add( info );
} }