From d1fec96e8e084077264c9c8403eaaae35b7ac604 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 17 Feb 2015 02:40:58 -0500 Subject: [PATCH] =?UTF-8?q?Gestion=20dans=20une=20classe=20=C3=A0=20part?= =?UTF-8?q?=20des=20WorldBorder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Format des fichiers de configuration.md | 18 ++++ .../config/AbstractConfig.java | 100 ++++++++++++++++++ .../pandacraftutils/config/ConfigManager.java | 58 +--------- ...tConfigManager.java => DefaultConfig.java} | 4 +- .../config/WorldBorderConfig.java | 69 ++++++++++++ .../elements/WorldBorderConfigEntry.java | 16 +++ .../protection/WorldBorderManager.java | 16 ++- 7 files changed, 215 insertions(+), 66 deletions(-) create mode 100644 Format des fichiers de configuration.md create mode 100644 src/net/mc_pandacraft/java/plugin/pandacraftutils/config/AbstractConfig.java rename src/net/mc_pandacraft/java/plugin/pandacraftutils/config/{DefaultConfigManager.java => DefaultConfig.java} (98%) create mode 100644 src/net/mc_pandacraft/java/plugin/pandacraftutils/config/WorldBorderConfig.java create mode 100644 src/net/mc_pandacraft/java/plugin/pandacraftutils/config/elements/WorldBorderConfigEntry.java diff --git a/Format des fichiers de configuration.md b/Format des fichiers de configuration.md new file mode 100644 index 0000000..7f40508 --- /dev/null +++ b/Format des fichiers de configuration.md @@ -0,0 +1,18 @@ +Format des fichiers de configuration +========== +_____ + +## config.yml +Ce fichier est au format YAML, et sa lecture est déjà géré par l'API Bukkit. +_____ + +## worldborder.txt +Ce fichier est au format texte. Chaque ligne correspond au paramètre de bordure pour un monde donné. Voici un format exemple que doit prendre une ligne : + + WorldName 14 134.5 2000 + +- `WorldName` correspond au nom du monde que vous voulez configurer +- `14` et `134.5` correspondent aux coordonnées x et z du centre du carré de bordure +- `2000` correspond à la distance de chaque bordure par rapport aux coordonnées indiquées + +Les lignes vides et les lignes dont le premier caractère est un `#` seront ignorés. Les caractères non imprimables en début et fin de lignes seront supprimés avant analyse. \ No newline at end of file diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/AbstractConfig.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/AbstractConfig.java new file mode 100644 index 0000000..05e3aaf --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/AbstractConfig.java @@ -0,0 +1,100 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.config; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; +/** + * Classe chargeant en mémoire un fichier de configuration ou un dossier donné + * @author Marc Baloup + * + */ +public abstract class AbstractConfig { + + protected PandacraftUtils plugin = PandacraftUtils.getInstance(); + /** + * Correspond au dossier de configuration de pandacraftUtils + */ + protected File pluginDir = plugin.getDataFolder(); + + /** + * Correspond au dossier ou au fichier de configuration traité par la sous-classe + * courante de {@link AbstractConfig} + */ + protected File configFile; + + /** + * @param fileOrDirName le nom du fichier ou du dossier correspondant à la sous-classe de {@link AbstractConfig} + * @param isDir true si il s'agit d'un dossier, false sinon + * @throws IOException si le fichier est impossible à créer + */ + public AbstractConfig(String fileOrDirName, FileType type) throws IOException { + configFile = new File(pluginDir, fileOrDirName); + if (type == FileType.DIR) + configFile.mkdir(); + else + configFile.createNewFile(); + } + + /** + * Retourne toutes les lignes du fichier de configuration + * @param ignoreEmpty true si on doit ignorer les lignes vides + * @param ignoreHashtagComment true si on doit ignorer les lignes commentés (commençant par un #) + * @param trimOutput true si on doit appeller la méthode String.trim() sur chaque ligne retournée + * @return la liste des lignes utiles + * @throws IOException + */ + protected List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException { + if (!configFile.isFile()) + return null; + + BufferedReader reader = new BufferedReader(new FileReader(configFile)); + + List lines = new ArrayList(); + + String line; + while ((line = reader.readLine()) != null) { + String trimmedLine = line.trim(); + + if (ignoreEmpty && trimmedLine.equals("")) + continue; + + if (ignoreHashtagComment && trimmedLine.startsWith("#")) + continue; + + if (trimOutput) + lines.add(trimmedLine); + else + lines.add(line); + } + + + reader.close(); + + return lines; + } + + + + protected void warning(String message) { + plugin.getLogger().warning("Error while loading configuration in '"+configFile.getName()+"' : "+message); + } + + + + + + + + + + + protected enum FileType { + FILE, DIR + } + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/ConfigManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/ConfigManager.java index 6912586..a1f5b1b 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/ConfigManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/ConfigManager.java @@ -51,7 +51,8 @@ public class ConfigManager { private PandacraftUtils plugin = PandacraftUtils.getInstance(); - public final DefaultConfigManager defaultConfig; + public final DefaultConfig defaultConfig; + public final WorldBorderConfig worldBorderConfig; @@ -64,8 +65,8 @@ public class ConfigManager { configDir.mkdir(); - defaultConfig = new DefaultConfigManager(); - + defaultConfig = new DefaultConfig(); + worldBorderConfig = new WorldBorderConfig(); // dossier qui doit contenir les messages automatiques @@ -89,7 +90,6 @@ public class ConfigManager { initChatAnalysisBadWord(); initCommandAlias(); initAutomessages(); - initWorldBorder(); initMultiCommand(); } @@ -276,56 +276,6 @@ public class ConfigManager { - - - /* - * World Border - */ - - public HashMap WorldBorder_config; - - - private void initWorldBorder() { - WorldBorder_config = new HashMap(); - - WorldBorder_config.put("Faction", new WorldBorderConfig(40, 296, 2000)); - WorldBorder_config.put("Survival_nether", new WorldBorderConfig(-11.4F, 18.5F, 1500)); - WorldBorder_config.put("Survival", new WorldBorderConfig(800, 1536, 5000)); - WorldBorder_config.put("NewFaction", new WorldBorderConfig(136, -136, 3000)); - WorldBorder_config.put("Faction_nether", new WorldBorderConfig(-81.6F, -76.9F, 500)); - WorldBorder_config.put("PlatCreative", new WorldBorderConfig(456.6f, -1367.5f, 735)); - WorldBorder_config.put("world", new WorldBorderConfig(0, 0, 2000)); - WorldBorder_config.put("spawn", new WorldBorderConfig(-224, 366, 1000)); - WorldBorder_config.put("creative", new WorldBorderConfig(0, 0, 1700)); - - - } - - - - - public static class WorldBorderConfig { - public final float X, Z, radius; - - public WorldBorderConfig(float x, float z, float r) { - X = x; - Z = z; - radius = Math.abs(r); - } - - public double getMinX() { return X - radius; } - public double getMaxX() { return X + radius; } - public double getMinZ() { return Z - radius; } - public double getMaxZ() { return Z + radius; } - - - - } - - - - - /* * Multicommand */ diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/DefaultConfigManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/DefaultConfig.java similarity index 98% rename from src/net/mc_pandacraft/java/plugin/pandacraftutils/config/DefaultConfigManager.java rename to src/net/mc_pandacraft/java/plugin/pandacraftutils/config/DefaultConfig.java index 1814aad..236e5cd 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/DefaultConfigManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/DefaultConfig.java @@ -9,7 +9,7 @@ import org.bukkit.configuration.file.FileConfiguration; import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; -public class DefaultConfigManager { +public class DefaultConfig { private PandacraftUtils plugin = PandacraftUtils.getInstance(); @@ -103,7 +103,7 @@ public class DefaultConfigManager { - DefaultConfigManager() { + DefaultConfig() { plugin.reloadConfig(); plugin.saveDefaultConfig(); diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/WorldBorderConfig.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/WorldBorderConfig.java new file mode 100644 index 0000000..c5a33c2 --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/WorldBorderConfig.java @@ -0,0 +1,69 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.config; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +import org.bukkit.World; + +import net.mc_pandacraft.java.plugin.pandacraftutils.config.elements.WorldBorderConfigEntry; + +public class WorldBorderConfig extends AbstractConfig { + + private HashMap config; + + public WorldBorderConfig() throws IOException { + super("worldborder.txt", FileType.FILE); + + + List configLines = getFileLines(true, true, true); + + config = new HashMap(); + + for (String line : configLines) { + String[] lineSplit = line.split(" "); + if (lineSplit.length != 4) { + warning("mauvait format de ligne : "+line); + continue; + } + if (lineSplit[0].equals("")) { + warning("mauvait format de ligne : "+line); + continue; + } + + String worldName = lineSplit[0]; + float x, z, r; + + try { + x = Float.parseFloat(lineSplit[1]); + z = Float.parseFloat(lineSplit[2]); + r = Float.parseFloat(lineSplit[3]); + } catch (NumberFormatException e) { + warning("mauvait format de ligne : "+line); + continue; + } + + config.put(worldName, new WorldBorderConfigEntry(x, z, r)); + + } + + + + + + } + + + public WorldBorderConfigEntry getEntry(String world) { + return config.get(world); + } + + public WorldBorderConfigEntry getEntry(World world) { + return getEntry(world.getName()); + } + + + + + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/elements/WorldBorderConfigEntry.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/elements/WorldBorderConfigEntry.java new file mode 100644 index 0000000..0be093f --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/config/elements/WorldBorderConfigEntry.java @@ -0,0 +1,16 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.config.elements; + +public class WorldBorderConfigEntry { + public final float X, Z, radius; + + public WorldBorderConfigEntry(float x, float z, float r) { + X = x; + Z = z; + radius = Math.abs(r); + } + + public double getMinX() { return X - radius; } + public double getMaxX() { return X + radius; } + public double getMinZ() { return Z - radius; } + public double getMaxZ() { return Z + radius; } +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/protection/WorldBorderManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/protection/WorldBorderManager.java index 799a70b..fc3a406 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/protection/WorldBorderManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/modules/protection/WorldBorderManager.java @@ -3,11 +3,10 @@ package net.mc_pandacraft.java.plugin.pandacraftutils.modules.protection; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Map; import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils; import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager; -import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager.WorldBorderConfig; +import net.mc_pandacraft.java.plugin.pandacraftutils.config.elements.WorldBorderConfigEntry; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager; import net.mc_pandacraft.java.util.bukkit.protocol.ParticleEffect; @@ -39,11 +38,8 @@ public class WorldBorderManager extends BukkitRunnable implements Listener { Collection players = OnlinePlayerManager.getAll(); - Map configs = ConfigManager.getInstance().WorldBorder_config; - - for (OnlinePlayer op : players) { - WorldBorderConfig config = configs.get(op.getPlayer().getWorld().getName()); + WorldBorderConfigEntry config = ConfigManager.getInstance().worldBorderConfig.getEntry(op.getPlayer().getWorld()); if (config == null) continue; @@ -68,7 +64,7 @@ public class WorldBorderManager extends BukkitRunnable implements Listener { Location l = event.getTo(); - WorldBorderConfig config = ConfigManager.getInstance().WorldBorder_config.get(l.getWorld().getName()); + WorldBorderConfigEntry config = ConfigManager.getInstance().worldBorderConfig.getEntry(l.getWorld()); if (config == null) return; @@ -93,7 +89,7 @@ public class WorldBorderManager extends BukkitRunnable implements Listener { * @param config la configuration sur lequel confronter la position * @return une nouvelle position si le joueur doit bouger, ou null si ce n'est pas nécessaire */ - public Location checkPosition(Location l, WorldBorderConfig config) { + public Location checkPosition(Location l, WorldBorderConfigEntry config) { // si le joueur ne dépasse pas la bordure if (isInSquare(l, config)) return null; @@ -121,7 +117,7 @@ public class WorldBorderManager extends BukkitRunnable implements Listener { - public boolean isInSquare(Location pLoc, WorldBorderConfig config) { + public boolean isInSquare(Location pLoc, WorldBorderConfigEntry config) { if (Math.abs(config.X - pLoc.getX()) > config.radius) return false; if (Math.abs(config.Z - pLoc.getZ()) > config.radius) @@ -135,7 +131,7 @@ public class WorldBorderManager extends BukkitRunnable implements Listener { - public void drawLimit(Player p, WorldBorderConfig config) { + public void drawLimit(Player p, WorldBorderConfigEntry config) { List pls = new ArrayList(1); pls.add(p);