Gestion dans une classe à part des WorldBorder
This commit is contained in:
parent
f7f093bc52
commit
d1fec96e8e
18
Format des fichiers de configuration.md
Normal file
18
Format des fichiers de configuration.md
Normal file
@ -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.
|
@ -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 <code>true</code> si il s'agit d'un dossier, <code>false</code> 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 <code>true</code> si on doit ignorer les lignes vides
|
||||
* @param ignoreHashtagComment <code>true</code> si on doit ignorer les lignes commentés (commençant par un #)
|
||||
* @param trimOutput <code>true</code> si on doit appeller la méthode String.trim() sur chaque ligne retournée
|
||||
* @return la liste des lignes utiles
|
||||
* @throws IOException
|
||||
*/
|
||||
protected List<String> getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException {
|
||||
if (!configFile.isFile())
|
||||
return null;
|
||||
|
||||
BufferedReader reader = new BufferedReader(new FileReader(configFile));
|
||||
|
||||
List<String> lines = new ArrayList<String>();
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
@ -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<String, WorldBorderConfig> WorldBorder_config;
|
||||
|
||||
|
||||
private void initWorldBorder() {
|
||||
WorldBorder_config = new HashMap<String, WorldBorderConfig>();
|
||||
|
||||
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
|
||||
*/
|
||||
|
@ -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();
|
@ -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<String, WorldBorderConfigEntry> config;
|
||||
|
||||
public WorldBorderConfig() throws IOException {
|
||||
super("worldborder.txt", FileType.FILE);
|
||||
|
||||
|
||||
List<String> configLines = getFileLines(true, true, true);
|
||||
|
||||
config = new HashMap<String, WorldBorderConfigEntry>();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -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; }
|
||||
}
|
@ -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<OnlinePlayer> players = OnlinePlayerManager.getAll();
|
||||
|
||||
Map<String, WorldBorderConfig> 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<Player> pls = new ArrayList<Player>(1);
|
||||
pls.add(p);
|
||||
|
Loading…
Reference in New Issue
Block a user