From c818414fd5d36298bf87853f1e9adbed0d6a3b84 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 10 Oct 2011 21:10:48 +0100 Subject: [PATCH] Updated to the new Configuration api --- .../craftbukkit/CraftOfflinePlayer.java | 26 +++++- .../org/bukkit/craftbukkit/CraftServer.java | 80 +++++++++---------- .../craftbukkit/entity/CraftPlayer.java | 13 +++ .../craftbukkit/inventory/CraftItemStack.java | 2 + src/main/resources/configurations/bukkit.yml | 14 ++++ 5 files changed, 93 insertions(+), 42 deletions(-) create mode 100644 src/main/resources/configurations/bukkit.yml diff --git a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java index 9c488ea277..0c005f85c6 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java @@ -1,9 +1,15 @@ package org.bukkit.craftbukkit; +import java.util.LinkedHashMap; +import java.util.Map; +import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Server; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.SerializableAs; -public class CraftOfflinePlayer implements OfflinePlayer { +@SerializableAs("Player") +public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializable { private final String name; private final CraftServer server; @@ -61,4 +67,22 @@ public class CraftOfflinePlayer implements OfflinePlayer { server.getHandle().removeWhitelist(name.toLowerCase()); } } + + public Map serialize() { + Map result = new LinkedHashMap(); + + result.put("name", name); + + return result; + } + + public static OfflinePlayer deserialize(Map args) { + System.out.println("Deserializing CraftOfflinePlayer with args " + args); + return Bukkit.getServer().getOfflinePlayer((String)args.get("name")); + } + + @Override + public String toString() { + return getClass().getSimpleName() + "[name=" + name + "]"; + } } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 400c5b4562..37c8d96909 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -52,6 +52,9 @@ import net.minecraft.server.WorldMap; import net.minecraft.server.WorldMapCollection; import net.minecraft.server.WorldSettings; import org.bukkit.*; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.permissions.Permissible; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; @@ -71,8 +74,6 @@ import org.bukkit.util.permissions.DefaultPermissions; import org.bukkit.event.world.WorldInitEvent; import org.bukkit.permissions.Permission; import org.bukkit.plugin.PluginLoadOrder; -import org.bukkit.util.config.Configuration; -import org.bukkit.util.config.ConfigurationNode; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.error.MarkedYAMLException; @@ -88,10 +89,14 @@ public final class CraftServer implements Server { protected final MinecraftServer console; protected final ServerConfigurationManager server; private final Map worlds = new LinkedHashMap(); - private final Configuration configuration; + private YamlConfiguration configuration; private final Yaml yaml = new Yaml(new SafeConstructor()); private final Map offlinePlayers = new MapMaker().softValues().makeMap(); + static { + ConfigurationSerialization.registerClass(CraftOfflinePlayer.class); + } + public CraftServer(MinecraftServer console, ServerConfigurationManager server) { this.console = console; this.server = server; @@ -99,35 +104,27 @@ public final class CraftServer implements Server { Bukkit.setServer(this); - configuration = new Configuration((File) console.options.valueOf("bukkit-settings")); - loadConfig(); + configuration = YamlConfiguration.loadConfiguration(getConfigFile()); + configuration.options().copyDefaults(true); + configuration.setDefaults(YamlConfiguration.loadConfiguration(getClass().getClassLoader().getResourceAsStream("configurations/bukkit.yml"))); + saveConfig(); + loadPlugins(); enablePlugins(PluginLoadOrder.STARTUP); ChunkCompressionThread.startThread(); } - - private void loadConfig() { - configuration.load(); - configuration.getString("database.url", "jdbc:sqlite:{DIR}{NAME}.db"); - configuration.getString("database.username", "bukkit"); - configuration.getString("database.password", "walrus"); - configuration.getString("database.driver", "org.sqlite.JDBC"); - configuration.getString("database.isolation", "SERIALIZABLE"); - - configuration.getString("settings.update-folder", "update"); - configuration.getInt("settings.spawn-radius", 16); - - configuration.getString("settings.permissions-file", "permissions.yml"); - - configuration.getInt("settings.ping-packet-limit", 100); - - if (configuration.getNode("aliases") == null) { - List icanhasbukkit = new ArrayList(); - icanhasbukkit.add("version"); - configuration.setProperty("aliases.icanhasbukkit", icanhasbukkit); + + private File getConfigFile() { + return (File)console.options.valueOf("bukkit-settings"); + } + + private void saveConfig() { + try { + configuration.save(getConfigFile()); + } catch (IOException ex) { + Logger.getLogger(CraftServer.class.getName()).log(Level.SEVERE, "Could not save " + getConfigFile(), ex); } - configuration.save(); } public void loadPlugins() { @@ -365,7 +362,7 @@ public final class CraftServer implements Server { } public void reload() { - loadConfig(); + configuration = YamlConfiguration.loadConfiguration(getConfigFile()); PropertyManager config = new PropertyManager(console.options); console.propertyManager = config; @@ -692,17 +689,18 @@ public final class CraftServer implements Server { } public Map getCommandAliases() { - ConfigurationNode node = configuration.getNode("aliases"); + ConfigurationSection section = configuration.getConfigurationSection("aliases"); Map result = new LinkedHashMap(); - if (node != null) { - for (String key : node.getKeys()) { - List commands = new ArrayList(); + if (section != null) { + for (String key : section.getKeys(false)) { + List commands = null; - if (node.getProperty(key) instanceof List) { - commands = node.getStringList(key, null); + if (section.isList(key)) { + commands = section.getList(key); } else { - commands.add(node.getString(key)); + commands = new ArrayList(); + commands.add(section.getString(key)); } result.put(key, commands.toArray(new String[0])); @@ -717,8 +715,8 @@ public final class CraftServer implements Server { } public void setSpawnRadius(int value) { - configuration.setProperty("settings.spawn-radius", value); - configuration.save(); + configuration.set("settings.spawn-radius", value); + saveConfig(); } public boolean getOnlineMode() { @@ -730,14 +728,14 @@ public final class CraftServer implements Server { } public ChunkGenerator getGenerator(String world) { - ConfigurationNode node = configuration.getNode("worlds"); + ConfigurationSection section = configuration.getConfigurationSection("worlds"); ChunkGenerator result = null; - if (node != null) { - node = node.getNode(world); + if (section != null) { + section = section.getConfigurationSection(world); - if (node != null) { - String name = node.getString("generator"); + if (section != null) { + String name = section.getString("generator"); if ((name != null) && (!name.equals(""))) { String[] split = name.split(":", 2); diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 3be52eac8a..e43f4f0eb5 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -2,6 +2,8 @@ package org.bukkit.craftbukkit.entity; import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.util.LinkedHashMap; +import java.util.Map; import net.minecraft.server.EntityHuman; import net.minecraft.server.EntityPlayer; import net.minecraft.server.Packet131; @@ -24,6 +26,8 @@ import org.bukkit.Material; import org.bukkit.Note; import org.bukkit.Statistic; import org.bukkit.World; +import org.bukkit.configuration.serialization.DelegateDeserialization; +import org.bukkit.craftbukkit.CraftOfflinePlayer; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.map.CraftMapView; @@ -33,6 +37,7 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.map.MapView; +@DelegateDeserialization(CraftOfflinePlayer.class) public class CraftPlayer extends CraftHumanEntity implements Player { public CraftPlayer(CraftServer server, EntityPlayer entity) { super(server, entity); @@ -535,4 +540,12 @@ public class CraftPlayer extends CraftHumanEntity implements Player { return null; } } + + public Map serialize() { + Map result = new LinkedHashMap(); + + result.put("name", getName()); + + return result; + } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index ebe28caced..0c3e853882 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -2,7 +2,9 @@ package org.bukkit.craftbukkit.inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.Material; +import org.bukkit.configuration.serialization.DelegateDeserialization; +@DelegateDeserialization(ItemStack.class) public class CraftItemStack extends ItemStack { protected net.minecraft.server.ItemStack item; diff --git a/src/main/resources/configurations/bukkit.yml b/src/main/resources/configurations/bukkit.yml new file mode 100644 index 0000000000..15f626f244 --- /dev/null +++ b/src/main/resources/configurations/bukkit.yml @@ -0,0 +1,14 @@ +settings: + spawn-radius: 16 + permissions-file: permissions.yml + update-folder: update + ping-packet-limit: 100 +aliases: + icanhasbukkit: + - version +database: + username: bukkit + isolation: SERIALIZABLE + driver: org.sqlite.JDBC + password: walrus + url: jdbc:sqlite:{DIR}{NAME}.db