Gestion dans une classe à part des alias de commandes
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package net.mc_pandacraft.java.plugin.pandacraftutils.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.config.elements.CommandAliasConfigEntry;
|
||||
|
||||
public class CommandAliasConfig extends AbstractConfig {
|
||||
|
||||
private List<CommandAliasConfigEntry> config = new ArrayList<CommandAliasConfigEntry>();
|
||||
|
||||
|
||||
public CommandAliasConfig() throws IOException {
|
||||
super("commandalias", FileType.DIR);
|
||||
|
||||
List<File> files = getFileList();
|
||||
|
||||
for (File f : files) {
|
||||
|
||||
String name = f.getName();
|
||||
|
||||
try {
|
||||
|
||||
List<String> lines = getFileLines(false, false, false, f);
|
||||
|
||||
if (lines.size() != 3) throw new Exception("Le fichier "+name+" n'est pas du bon format. Doit avoir 3 lignes");
|
||||
if (!lines.get(0).startsWith("/") || !lines.get(1).startsWith("/"))
|
||||
throw new Exception("Le fichier "+name+" n'est pas du bon format. La première et la deuxième ligne doit commencer par un slash.");
|
||||
|
||||
|
||||
|
||||
config.add(new CommandAliasConfigEntry(lines.get(0), lines.get(1), lines.get(2)));
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
warning("fichier '"+name+"' invalide");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<CommandAliasConfigEntry> getCommandAlias() {
|
||||
return Collections.unmodifiableList(config);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -2,6 +2,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -40,6 +41,34 @@ public class ConfigManager {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Découpe une chaine de caractère contenant une série de noeuds
|
||||
* de permissions séparés par des point-virgules et la retourne sous forme d'une liste.
|
||||
* @param perms la chaine de permissions à traiter
|
||||
* @return <code>null</code> si le paramètre est nulle ou si <code>perms.equals("*")</code>, ou alors la chaine splittée.
|
||||
*/
|
||||
public static List<String> splitPermissionsString(String perms) {
|
||||
if (perms == null || perms.equals("*"))
|
||||
return null;
|
||||
else
|
||||
return Arrays.asList(perms.split(";"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -53,6 +82,7 @@ public class ConfigManager {
|
||||
public final WorldBorderConfig worldBorderConfig;
|
||||
public final ChatBadWordsConfig chatBadWordsConfig;
|
||||
public final AutoMessagesConfig autoMessagesConfig;
|
||||
public final CommandAliasConfig commandAliasConfig;
|
||||
|
||||
|
||||
|
||||
@@ -69,20 +99,16 @@ public class ConfigManager {
|
||||
worldBorderConfig = new WorldBorderConfig();
|
||||
chatBadWordsConfig = new ChatBadWordsConfig();
|
||||
autoMessagesConfig = new AutoMessagesConfig();
|
||||
commandAliasConfig = new CommandAliasConfig();
|
||||
|
||||
|
||||
|
||||
// dossier qui doit contenir les alias de commandes
|
||||
File commandAliasDir = new File(configDir, "commandalias");
|
||||
commandAliasDir.mkdir();
|
||||
|
||||
// dossier qui doit contenir les séries de commandes
|
||||
File multiCommandsDir = new File(configDir, "multicommands");
|
||||
multiCommandsDir.mkdir();
|
||||
|
||||
|
||||
|
||||
initCommandAlias();
|
||||
initMultiCommand();
|
||||
}
|
||||
|
||||
@@ -94,82 +120,6 @@ public class ConfigManager {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Alias pour les commandes
|
||||
*/
|
||||
|
||||
public List<Map<String, String>> CommandAlias_alias;
|
||||
|
||||
private void initCommandAlias() {
|
||||
CommandAlias_alias = new ArrayList<Map<String, String>>();
|
||||
Map<String, String> alias;
|
||||
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/day");
|
||||
alias.put("replaceWith", "/ptime day");
|
||||
alias.put("permissions", "pandacraft.grade.users"); // multiple permission separated with semicolumn
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/night");
|
||||
alias.put("replaceWith", "/ptime night");
|
||||
alias.put("permissions", "pandacraft.grade.users");
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/time");
|
||||
alias.put("replaceWith", "/ptime");
|
||||
alias.put("permissions", "pandacraft.grade.users");
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/shop");
|
||||
alias.put("replaceWith", "/warp shop");
|
||||
alias.put("permissions", "*"); // tout le monde
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/lobby");
|
||||
alias.put("replaceWith", "/spawn");
|
||||
alias.put("permissions", "*"); // tout le monde
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/hub");
|
||||
alias.put("replaceWith", "/spawn");
|
||||
alias.put("permissions", "*"); // tout le monde
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/tete");
|
||||
alias.put("replaceWith", "/heads nick");
|
||||
alias.put("permissions", "*");
|
||||
CommandAlias_alias.add(alias);
|
||||
|
||||
alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "/inscription");
|
||||
alias.put("replaceWith", "/mail send marcbal Inscription");
|
||||
alias.put("permissions", "pandacraft.grade.users");
|
||||
CommandAlias_alias.add(alias);
|
||||
/*
|
||||
Map<String, String> alias = new HashMap<String, String>();
|
||||
alias.put("initialCommand", "");
|
||||
alias.put("replaceWith", "");
|
||||
alias.put("permissions", "");
|
||||
CommandAlias_alias.add(alias);
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Multicommand
|
||||
*/
|
||||
|
@@ -1,11 +1,11 @@
|
||||
package net.mc_pandacraft.java.plugin.pandacraftutils.config.elements;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
|
||||
|
||||
public class AutoMessagesConfigEntry implements Comparable<AutoMessagesConfigEntry> {
|
||||
@@ -13,13 +13,10 @@ public class AutoMessagesConfigEntry implements Comparable<AutoMessagesConfigEnt
|
||||
private final String message;
|
||||
private final List<String> permissions;
|
||||
|
||||
public AutoMessagesConfigEntry(int o, String perm, List<String> m) {
|
||||
public AutoMessagesConfigEntry(int o, String perms, List<String> m) {
|
||||
if (m == null) throw new IllegalArgumentException("message ne doit pas être null");
|
||||
message = ChatColor.translateAlternateColorCodes('&', StringUtils.join(m, '\n'));
|
||||
if (perm == null || perm.equals("*"))
|
||||
permissions = null;
|
||||
else
|
||||
permissions = Arrays.asList(perm.split(";"));
|
||||
permissions = ConfigManager.splitPermissionsString(perms);
|
||||
order = o;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,45 @@
|
||||
package net.mc_pandacraft.java.plugin.pandacraftutils.config.elements;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
|
||||
|
||||
public class CommandAliasConfigEntry {
|
||||
private final String initialCmd;
|
||||
private final String replacement;
|
||||
private final List<String> permissions;
|
||||
|
||||
public CommandAliasConfigEntry(String init, String repl, String perms) {
|
||||
|
||||
permissions = Collections.unmodifiableList(ConfigManager.splitPermissionsString(perms));
|
||||
|
||||
if (init == null || init.trim().equals(""))
|
||||
throw new IllegalArgumentException();
|
||||
if (repl == null || repl.trim().equals(""))
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
initialCmd = init;
|
||||
replacement = repl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getInitialCommand() { return initialCmd; }
|
||||
public String getReplacementCommand() { return replacement; }
|
||||
|
||||
|
||||
public boolean hasPlayerPermission(OnlinePlayer op) {
|
||||
if (op == null) throw new IllegalArgumentException("le joueur ne doit pas être null");
|
||||
if (permissions == null)
|
||||
return true;
|
||||
|
||||
for (String pe : permissions) {
|
||||
if (op.hasPermission(pe)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,12 +1,13 @@
|
||||
package net.mc_pandacraft.java.plugin.pandacraftutils.modules;
|
||||
|
||||
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.elements.CommandAliasConfigEntry;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer;
|
||||
import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -26,35 +27,21 @@ public class CommandAliasManager implements Listener {
|
||||
@EventHandler(priority=EventPriority.HIGHEST)
|
||||
public void onPlayerCommandPreprocess (PlayerCommandPreprocessEvent event)
|
||||
{
|
||||
Player p = event.getPlayer();
|
||||
OnlinePlayer op = OnlinePlayerManager.get(event.getPlayer());
|
||||
|
||||
List<Map<String, String>> aliases = ConfigManager.getInstance().CommandAlias_alias;
|
||||
for (Map<String, String> alias : aliases) {
|
||||
if (alias == null)
|
||||
continue;
|
||||
String perms = alias.get("permissions");
|
||||
String initialCmd = alias.get("initialCommand");
|
||||
String aliasCmd = alias.get("replaceWith");
|
||||
if (perms == null || initialCmd == null || aliasCmd == null)
|
||||
continue;
|
||||
List<CommandAliasConfigEntry> aliases = ConfigManager.getInstance().commandAliasConfig.getCommandAlias();
|
||||
for (CommandAliasConfigEntry alias : aliases) {
|
||||
|
||||
if (!perms.equals("*")) {
|
||||
String[] aPerms = perms.split(";");
|
||||
boolean found = false;
|
||||
for (String perm : aPerms) {
|
||||
if (perm == null || perm.equals("")) continue; // boucle qui parcours les permissions de l'alias
|
||||
if (p.hasPermission(perm)) found = true;
|
||||
}
|
||||
if (!found) continue; // boucle qui parcours les alias de commandes
|
||||
}
|
||||
if (!alias.hasPlayerPermission(op))
|
||||
continue;
|
||||
|
||||
String mess = event.getMessage();
|
||||
|
||||
if(mess.toLowerCase().startsWith(initialCmd))
|
||||
mess = mess.replaceFirst("(?i)"+initialCmd, aliasCmd);
|
||||
else
|
||||
if(!mess.toLowerCase().startsWith(alias.getInitialCommand()))
|
||||
continue;
|
||||
|
||||
mess = mess.replaceFirst("(?i)"+alias.getInitialCommand(), alias.getReplacementCommand());
|
||||
|
||||
event.setMessage(mess);
|
||||
return;
|
||||
}
|
||||
|
Reference in New Issue
Block a user