Fix config, up next, reconnect handler.

This commit is contained in:
md_5 2013-01-19 14:05:33 +11:00
parent bf1e7f09a9
commit 355afba6da
3 changed files with 46 additions and 20 deletions

View File

@ -27,7 +27,7 @@ public abstract class ProxyServer
public static void setInstance(ProxyServer instance) public static void setInstance(ProxyServer instance)
{ {
Preconditions.checkNotNull(instance, "instance"); Preconditions.checkNotNull(instance, "instance");
Preconditions.checkArgument(instance == null, "Instance already set"); Preconditions.checkArgument(ProxyServer.instance == null, "Instance already set");
ProxyServer.instance = instance; ProxyServer.instance = instance;
} }

View File

@ -25,10 +25,6 @@ public class ServerInfo
* Connectable address of this server. * Connectable address of this server.
*/ */
private final InetSocketAddress address; private final InetSocketAddress address;
/**
* Permission node required to access this server.
*/
private String permission;
/** /**
* Players connected to a server defined by these properties. * Players connected to a server defined by these properties.
*/ */

View File

@ -2,16 +2,20 @@ package net.md_5.bungee.config;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
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.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.Map; import java.util.Map;
import java.util.logging.Level;
import net.md_5.bungee.Util; import net.md_5.bungee.Util;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ConfigurationAdapter; import net.md_5.bungee.api.config.ConfigurationAdapter;
import net.md_5.bungee.api.config.ListenerInfo; import net.md_5.bungee.api.config.ListenerInfo;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
@ -24,12 +28,12 @@ public class YamlConfig implements ConfigurationAdapter
private boolean loaded; private boolean loaded;
private Yaml yaml; private Yaml yaml;
private Map config; private Map config;
File file = new File("config.yml");
public void load() public void load()
{ {
try try
{ {
File file = new File("config.yml");
file.createNewFile(); file.createNewFile();
DumperOptions options = new DumperOptions(); DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
@ -54,9 +58,14 @@ public class YamlConfig implements ConfigurationAdapter
private <T> T get(String path, T def) private <T> T get(String path, T def)
{ {
if (!loaded)
{
load();
}
return get(path, def, config); return get(path, def, config);
} }
@SuppressWarnings("unchecked")
private <T> T get(String path, T def, Map submap) private <T> T get(String path, T def, Map submap)
{ {
if (!loaded) if (!loaded)
@ -68,7 +77,13 @@ public class YamlConfig implements ConfigurationAdapter
if (index == -1) if (index == -1)
{ {
Object val = submap.get(path); Object val = submap.get(path);
return (val != null) ? (T) val : def; if (val == null)
{
val = def;
submap.put(path, def);
save();
}
return (T) val;
} else } else
{ {
String first = path.substring(0, index); String first = path.substring(0, index);
@ -78,6 +93,20 @@ public class YamlConfig implements ConfigurationAdapter
} }
} }
private void save()
{
try
{
try (FileWriter wr = new FileWriter(file))
{
yaml.dump(config, wr);
}
} catch (IOException ex)
{
ProxyServer.getInstance().getLogger().log(Level.WARNING, "Could not save config", ex);
}
}
@Override @Override
public int getInt(String path, int def) public int getInt(String path, int def)
{ {
@ -94,17 +123,16 @@ public class YamlConfig implements ConfigurationAdapter
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Map<String, ServerInfo> getServers() public Map<String, ServerInfo> getServers()
{ {
Map<String, Map<String, Object>> base = get("servers", Collections.EMPTY_MAP); Map<String, Map<String, Object>> base = get("servers", (Map) Collections.singletonMap("lobby", new HashMap<>()));
Map<String, ServerInfo> ret = new HashMap<>(); Map<String, ServerInfo> ret = new HashMap<>();
for (Map.Entry<String, Map<String, Object>> entry : base.entrySet()) for (Map.Entry<String, Map<String, Object>> entry : base.entrySet())
{ {
Map<String, Object> val = entry.getValue(); Map<String, Object> val = entry.getValue();
String name = get("name", null, val); String name = entry.getKey();
String permission = get("permission", null, val); String addr = get("address", "localhost:25565", val);
String addr = get("address", null, val);
InetSocketAddress address = Util.getAddr(addr); InetSocketAddress address = Util.getAddr(addr);
ServerInfo info = new ServerInfo(name, address, permission); ServerInfo info = new ServerInfo(name, address);
ret.put(name, info); ret.put(name, info);
} }
@ -115,17 +143,19 @@ public class YamlConfig implements ConfigurationAdapter
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Collection<ListenerInfo> getListeners() public Collection<ListenerInfo> getListeners()
{ {
Map<String, Map<String, Object>> base = get("listeners", Collections.EMPTY_MAP); Collection<Map<String, Object>> base = get("listeners", (Collection) Arrays.asList(new Map[]
{
new HashMap()
}));
Collection<ListenerInfo> ret = new HashSet<>(); Collection<ListenerInfo> ret = new HashSet<>();
for (Map.Entry<String, Map<String, Object>> entry : base.entrySet()) for (Map<String, Object> val : base)
{ {
Map<String, Object> val = entry.getValue(); String motd = get("motd", "Another Bungee server", val);
String motd = get("motd", null, val); int maxPlayers = get("max_players", 1, val);
int maxPlayers = get("motd", null, val); String defaultServer = get("default_server", "lobby", val);
String defaultServer = get("default", null, val); boolean forceDefault = get("force_default_server", false, val);
boolean forceDefault = get("force_default", null, val); String host = get("host", "0.0.0.0:25577", val);
String host = get("host", null, val);
InetSocketAddress address = Util.getAddr(host); InetSocketAddress address = Util.getAddr(host);
ListenerInfo info = new ListenerInfo(address, motd, maxPlayers, defaultServer, forceDefault); ListenerInfo info = new ListenerInfo(address, motd, maxPlayers, defaultServer, forceDefault);
ret.add(info); ret.add(info);