diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java index 6bd86f96f..f51dec1f5 100644 --- a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java +++ b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java @@ -27,7 +27,7 @@ public abstract class ProxyServer public static void setInstance(ProxyServer instance) { Preconditions.checkNotNull(instance, "instance"); - Preconditions.checkArgument(instance == null, "Instance already set"); + Preconditions.checkArgument(ProxyServer.instance == null, "Instance already set"); ProxyServer.instance = instance; } diff --git a/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java b/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java index a53f9f5c5..4ed5ddfac 100644 --- a/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java +++ b/api/src/main/java/net/md_5/bungee/api/config/ServerInfo.java @@ -25,10 +25,6 @@ public class ServerInfo * Connectable address of this server. */ private final InetSocketAddress address; - /** - * Permission node required to access this server. - */ - private String permission; /** * Players connected to a server defined by these properties. */ diff --git a/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java b/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java index b9cfe5670..3982f229b 100644 --- a/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java +++ b/proxy/src/main/java/net/md_5/bungee/config/YamlConfig.java @@ -2,16 +2,20 @@ package net.md_5.bungee.config; import java.io.File; import java.io.FileInputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; +import java.util.logging.Level; 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.ListenerInfo; import net.md_5.bungee.api.config.ServerInfo; @@ -24,12 +28,12 @@ public class YamlConfig implements ConfigurationAdapter private boolean loaded; private Yaml yaml; private Map config; + File file = new File("config.yml"); public void load() { try { - File file = new File("config.yml"); file.createNewFile(); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); @@ -54,9 +58,14 @@ public class YamlConfig implements ConfigurationAdapter private T get(String path, T def) { + if (!loaded) + { + load(); + } return get(path, def, config); } + @SuppressWarnings("unchecked") private T get(String path, T def, Map submap) { if (!loaded) @@ -68,7 +77,13 @@ public class YamlConfig implements ConfigurationAdapter if (index == -1) { 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 { 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 public int getInt(String path, int def) { @@ -94,17 +123,16 @@ public class YamlConfig implements ConfigurationAdapter @SuppressWarnings("unchecked") public Map getServers() { - Map> base = get("servers", Collections.EMPTY_MAP); + Map> base = get("servers", (Map) Collections.singletonMap("lobby", new HashMap<>())); Map ret = new HashMap<>(); for (Map.Entry> entry : base.entrySet()) { Map val = entry.getValue(); - String name = get("name", null, val); - String permission = get("permission", null, val); - String addr = get("address", null, val); + String name = entry.getKey(); + String addr = get("address", "localhost:25565", val); InetSocketAddress address = Util.getAddr(addr); - ServerInfo info = new ServerInfo(name, address, permission); + ServerInfo info = new ServerInfo(name, address); ret.put(name, info); } @@ -115,17 +143,19 @@ public class YamlConfig implements ConfigurationAdapter @SuppressWarnings("unchecked") public Collection getListeners() { - Map> base = get("listeners", Collections.EMPTY_MAP); + Collection> base = get("listeners", (Collection) Arrays.asList(new Map[] + { + new HashMap() + })); Collection ret = new HashSet<>(); - for (Map.Entry> entry : base.entrySet()) + for (Map val : base) { - Map val = entry.getValue(); - String motd = get("motd", null, val); - int maxPlayers = get("motd", null, val); - String defaultServer = get("default", null, val); - boolean forceDefault = get("force_default", null, val); - String host = get("host", null, val); + String motd = get("motd", "Another Bungee server", val); + int maxPlayers = get("max_players", 1, val); + String defaultServer = get("default_server", "lobby", val); + boolean forceDefault = get("force_default_server", false, val); + String host = get("host", "0.0.0.0:25577", val); InetSocketAddress address = Util.getAddr(host); ListenerInfo info = new ListenerInfo(address, motd, maxPlayers, defaultServer, forceDefault); ret.add(info);