Ajout du système d'historique de commande pour la console (avec le NetworkAPI)
This commit is contained in:
		| @@ -1,5 +1,9 @@ | |||||||
| Format des fichiers de configuration | Format des fichiers de configuration | ||||||
| ========== | ========== | ||||||
|  | - *.txt : fichier modifiable à la main que le serveur ne fait que lire | ||||||
|  | - \*.yml : pareil que les fichiers *.txt, mais le contenu est au format YAML | ||||||
|  | - *.save : fichier à ne pas modifier, dont seul le serveur gère le contenu | ||||||
|  |  | ||||||
| ___________________ | ___________________ | ||||||
|  |  | ||||||
| ## config.yml | ## config.yml | ||||||
| @@ -68,3 +72,10 @@ Une ligne aura cette forme : | |||||||
| (T, C et A ne peuvent pas contenir d'espace) | (T, C et A ne peuvent pas contenir d'espace) | ||||||
|  |  | ||||||
| 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 ligne sont conservés | 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 ligne sont conservés | ||||||
|  |  | ||||||
|  | ___________________ | ||||||
|  |  | ||||||
|  | ## command_history.save | ||||||
|  | Contient l'historique des commandes envoyés en utilisant le Network API. Une limite peut être fixé concernant le nombre commandes enregistrés. | ||||||
|  |  | ||||||
|  | Ce fichier n'a pas à être éditée à la main, car c'est le serveur qui gère son contenu. | ||||||
|   | |||||||
| @@ -77,6 +77,14 @@ La partie **donnée** de la requête contient d'abord le pseudo du joueur source | |||||||
|  |  | ||||||
| Cet exemple enverra de la part de `marcbal` le message `salut :)` à `toto`. | Cet exemple enverra de la part de `marcbal` le message `salut :)` à `toto`. | ||||||
|  |  | ||||||
|  | #### `console_command_history` | ||||||
|  | Retourne la liste des commandes se trouvant dans l'historique. Cette liste servira à l'interface Web pour aider à l'exécution des commandes. | ||||||
|  |  | ||||||
|  | La partie **donnée** de la requête ne contient rien. | ||||||
|  |  | ||||||
|  | Chaque ligne de la réponse sera une commande de l'historique, sachant que la dernière retournée sera la plus récente. Les doublons sont ignorés. C'est à dire que si une commande déjà existante l'historique est réexécutée, elle sera retirée de son ancien emplacement pour aller à la dernière place. | ||||||
|  |  | ||||||
|  | Les commandes ajoutés à cette liste seront les commandes envoyés via les requêtes `command` et `command_async`. | ||||||
|  |  | ||||||
| ## Structure d'une réponse | ## Structure d'une réponse | ||||||
| La réponse est construite en mode texte, sur plusieurs lignes. Chaque ligne se fini par un '\n' : | La réponse est construite en mode texte, sur plusieurs lignes. Chaque ligne se fini par un '\n' : | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8" standalone="no"?> | <?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||||||
| <jardesc> | <jardesc> | ||||||
|     <jar path="PandacraftUtils/jar_export/PandacraftUtils-3.12.jar"/> |     <jar path="PandacraftUtils/jar_export/PandacraftUtils-3.13.jar"/> | ||||||
|     <options buildIfNeeded="true" compress="true" descriptionLocation="/PandacraftUtils/make_jar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/> |     <options buildIfNeeded="true" compress="true" descriptionLocation="/PandacraftUtils/make_jar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/> | ||||||
|     <storedRefactorings deprecationInfo="true" structuralOnly="false"/> |     <storedRefactorings deprecationInfo="true" structuralOnly="false"/> | ||||||
|     <selectedProjects/> |     <selectedProjects/> | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| name: PandacraftUtils | name: PandacraftUtils | ||||||
| main: net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils | main: net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils | ||||||
| version: 3.12 | version: 3.13 | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -117,6 +117,8 @@ public class PandacraftUtils extends JavaPlugin { | |||||||
| 	@Override | 	@Override | ||||||
| 	public void onDisable(){ | 	public void onDisable(){ | ||||||
| 		 | 		 | ||||||
|  | 		ConfigManager.getInstance().saveAll(); | ||||||
|  |          | ||||||
|         afkManager = null; |         afkManager = null; | ||||||
|         wESelectionDisplayManager = null; |         wESelectionDisplayManager = null; | ||||||
|         commandAliasManager = null; |         commandAliasManager = null; | ||||||
|   | |||||||
| @@ -112,7 +112,7 @@ public abstract class AbstractConfig { | |||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
| 	protected void warning(String message) { | 	protected void warning(String message) { | ||||||
| 		plugin.getLogger().warning("Error while loading configuration in '"+configFile.getName()+"' : "+message); | 		plugin.getLogger().warning("Erreur dans la configuration de '"+configFile.getName()+"' : "+message); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
|   | |||||||
| @@ -0,0 +1,61 @@ | |||||||
|  | package net.mc_pandacraft.java.plugin.pandacraftutils.config; | ||||||
|  |  | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.PrintWriter; | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
|  | public class CommandHistoryConfig extends AbstractConfig implements Saveable { | ||||||
|  | 	 | ||||||
|  | 	private List<String> history; | ||||||
|  | 	 | ||||||
|  | 	private final int nbMaxHistory = 100; | ||||||
|  |  | ||||||
|  | 	public CommandHistoryConfig() | ||||||
|  | 			throws IOException { | ||||||
|  | 		super("command_history.save", FileType.FILE); | ||||||
|  | 		 | ||||||
|  | 		history = getFileLines(true, false, false); | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
|  | 	public synchronized String getStringList() { | ||||||
|  | 		 | ||||||
|  | 		StringBuilder sb = new StringBuilder(); | ||||||
|  | 		for (String s : history) { | ||||||
|  | 			sb.append(s+"\n"); | ||||||
|  | 		} | ||||||
|  | 		sb.deleteCharAt(sb.length()-1); // pour supprimer le dernir retour à la ligne | ||||||
|  | 		return sb.toString(); | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
|  | 	public synchronized void add(String s) { | ||||||
|  | 		if (s == null || s.trim().equals("")) return; | ||||||
|  | 		history.remove(s); // assure que l'élément ne se trouve pas déjà dans la liste | ||||||
|  | 		history.add(s); | ||||||
|  | 		if (history.size() > nbMaxHistory) | ||||||
|  | 			history.remove(0); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public synchronized void save() { | ||||||
|  | 		try { | ||||||
|  | 			 | ||||||
|  | 			PrintWriter br = new PrintWriter(configFile); | ||||||
|  | 			br.print(getStringList()); | ||||||
|  | 			br.flush(); | ||||||
|  | 			br.close(); | ||||||
|  | 			 | ||||||
|  | 		} catch (Exception e) { | ||||||
|  | 			warning("impossible de sauvegarder"); | ||||||
|  | 			e.printStackTrace(); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -21,6 +21,9 @@ public class ConfigManager { | |||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	public synchronized static void loadNewInstance() { | 	public synchronized static void loadNewInstance() { | ||||||
|  | 		if (instance != null) { | ||||||
|  | 			instance.saveAll(); | ||||||
|  | 		} | ||||||
| 		try { | 		try { | ||||||
| 			instance = new ConfigManager(); | 			instance = new ConfigManager(); | ||||||
| 		} catch (Exception e) { | 		} catch (Exception e) { | ||||||
| @@ -81,6 +84,7 @@ public class ConfigManager { | |||||||
| 	public final AutoMessagesConfig autoMessagesConfig; | 	public final AutoMessagesConfig autoMessagesConfig; | ||||||
| 	public final CommandAliasConfig commandAliasConfig; | 	public final CommandAliasConfig commandAliasConfig; | ||||||
| 	public final MultiCommandConfig multiCommandConfig; | 	public final MultiCommandConfig multiCommandConfig; | ||||||
|  | 	public final CommandHistoryConfig commandHistoryConfig; | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
| @@ -99,11 +103,21 @@ public class ConfigManager { | |||||||
| 		autoMessagesConfig = new AutoMessagesConfig(); | 		autoMessagesConfig = new AutoMessagesConfig(); | ||||||
| 		commandAliasConfig = new CommandAliasConfig(); | 		commandAliasConfig = new CommandAliasConfig(); | ||||||
| 		multiCommandConfig = new MultiCommandConfig(); | 		multiCommandConfig = new MultiCommandConfig(); | ||||||
|  | 		commandHistoryConfig = new CommandHistoryConfig(); | ||||||
| 		 | 		 | ||||||
| 		 | 		 | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Effectue les sauvegardes nécessaires pour les éléments de configuration sauvegardable | ||||||
|  | 	 */ | ||||||
|  | 	 | ||||||
|  | 	public void saveAll() { | ||||||
|  | 		 | ||||||
|  | 		commandHistoryConfig.save(); | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
|   | |||||||
| @@ -0,0 +1,7 @@ | |||||||
|  | package net.mc_pandacraft.java.plugin.pandacraftutils.config; | ||||||
|  |  | ||||||
|  | public interface Saveable { | ||||||
|  | 	 | ||||||
|  | 	public void save(); | ||||||
|  | 	 | ||||||
|  | } | ||||||
| @@ -4,6 +4,7 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executo | |||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorChatSend; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorChatSend; | ||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorCommand; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorCommand; | ||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorCommandAsync; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorCommandAsync; | ||||||
|  | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorConsoleCommandHistory; | ||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorPlayerList; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorPlayerList; | ||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorPrivateMessage; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors.RequestExecutorPrivateMessage; | ||||||
|  |  | ||||||
| @@ -24,6 +25,7 @@ public class NetworkAPI { | |||||||
| 		new RequestExecutorPlayerList(); | 		new RequestExecutorPlayerList(); | ||||||
| 		new RequestExecutorChatSend(); | 		new RequestExecutorChatSend(); | ||||||
| 		new RequestExecutorPrivateMessage(); | 		new RequestExecutorPrivateMessage(); | ||||||
|  | 		new RequestExecutorConsoleCommandHistory(); | ||||||
| 		 | 		 | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|   | |||||||
| @@ -12,6 +12,10 @@ public class Response { | |||||||
| 		this.data = data; | 		this.data = data; | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|  | 	/** | ||||||
|  | 	 * Construit une réponse positive avec aucune donnée. Équivaut à | ||||||
|  | 	 * <code>new Response(true, "")</code> | ||||||
|  | 	 */ | ||||||
| 	public Response() { | 	public Response() { | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| package net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors; | package net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors; | ||||||
|  |  | ||||||
|  | import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager; | ||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.Response; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.Response; | ||||||
|  |  | ||||||
| public class RequestExecutorCommand extends AbstractRequestExecutor { | public class RequestExecutorCommand extends AbstractRequestExecutor { | ||||||
| @@ -19,6 +20,8 @@ public class RequestExecutorCommand extends AbstractRequestExecutor { | |||||||
| 				{ | 				{ | ||||||
| 					boolean succes = plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), command); | 					boolean succes = plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), command); | ||||||
| 					 | 					 | ||||||
|  | 					ConfigManager.getInstance().commandHistoryConfig.add(command); | ||||||
|  | 					 | ||||||
| 					if (!succes) | 					if (!succes) | ||||||
| 						plugin.getLogger().warning("La commande ne peut pas s'exécuter : /"+command); | 						plugin.getLogger().warning("La commande ne peut pas s'exécuter : /"+command); | ||||||
| 				} | 				} | ||||||
| @@ -38,12 +41,8 @@ public class RequestExecutorCommand extends AbstractRequestExecutor { | |||||||
| 			 | 			 | ||||||
| 		}.init(data)); | 		}.init(data)); | ||||||
| 		 | 		 | ||||||
| 		Response rep = new Response(); |  | ||||||
| 		rep.good = true; |  | ||||||
| 		rep.data = ""; |  | ||||||
| 		 | 		 | ||||||
| 		 | 		return new Response(); | ||||||
| 		return rep; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| package net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors; | package net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors; | ||||||
|  |  | ||||||
|  | import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager; | ||||||
| import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.Response; | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.Response; | ||||||
|  |  | ||||||
| public class RequestExecutorCommandAsync extends AbstractRequestExecutor { | public class RequestExecutorCommandAsync extends AbstractRequestExecutor { | ||||||
| @@ -16,6 +17,8 @@ public class RequestExecutorCommandAsync extends AbstractRequestExecutor { | |||||||
| 		{ | 		{ | ||||||
| 			rep.good = plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), data); | 			rep.good = plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), data); | ||||||
| 			 | 			 | ||||||
|  | 			ConfigManager.getInstance().commandHistoryConfig.add(data); | ||||||
|  | 			 | ||||||
| 			if (!rep.good) | 			if (!rep.good) | ||||||
| 			{ | 			{ | ||||||
| 				plugin.getLogger().warning("La commande ne peut pas s'exécuter : /"+data); | 				plugin.getLogger().warning("La commande ne peut pas s'exécuter : /"+data); | ||||||
|   | |||||||
| @@ -0,0 +1,20 @@ | |||||||
|  | package net.mc_pandacraft.java.plugin.pandacraftutils.network_api.request_executors; | ||||||
|  |  | ||||||
|  | import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager; | ||||||
|  | import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.Response; | ||||||
|  |  | ||||||
|  | public class RequestExecutorConsoleCommandHistory extends | ||||||
|  | 		AbstractRequestExecutor { | ||||||
|  | 	 | ||||||
|  | 	public RequestExecutorConsoleCommandHistory() { | ||||||
|  | 		super("console_command_history"); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	protected Response run(String data) { | ||||||
|  | 		 | ||||||
|  | 		return new Response(true, ConfigManager.getInstance().commandHistoryConfig.getStringList()); | ||||||
|  | 		 | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user