Ajout du système d'historique de commande pour la console (avec le NetworkAPI)
This commit is contained in:
@@ -116,6 +116,8 @@ public class PandacraftUtils extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onDisable(){
|
||||
|
||||
ConfigManager.getInstance().saveAll();
|
||||
|
||||
afkManager = null;
|
||||
wESelectionDisplayManager = null;
|
||||
|
@@ -112,7 +112,7 @@ public abstract class AbstractConfig {
|
||||
|
||||
|
||||
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() {
|
||||
if (instance != null) {
|
||||
instance.saveAll();
|
||||
}
|
||||
try {
|
||||
instance = new ConfigManager();
|
||||
} catch (Exception e) {
|
||||
@@ -81,6 +84,7 @@ public class ConfigManager {
|
||||
public final AutoMessagesConfig autoMessagesConfig;
|
||||
public final CommandAliasConfig commandAliasConfig;
|
||||
public final MultiCommandConfig multiCommandConfig;
|
||||
public final CommandHistoryConfig commandHistoryConfig;
|
||||
|
||||
|
||||
|
||||
@@ -99,11 +103,21 @@ public class ConfigManager {
|
||||
autoMessagesConfig = new AutoMessagesConfig();
|
||||
commandAliasConfig = new CommandAliasConfig();
|
||||
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.RequestExecutorCommand;
|
||||
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.RequestExecutorPrivateMessage;
|
||||
|
||||
@@ -24,6 +25,7 @@ public class NetworkAPI {
|
||||
new RequestExecutorPlayerList();
|
||||
new RequestExecutorChatSend();
|
||||
new RequestExecutorPrivateMessage();
|
||||
new RequestExecutorConsoleCommandHistory();
|
||||
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,11 @@ public class Response {
|
||||
this.good = good;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construit une réponse positive avec aucune donnée. Équivaut à
|
||||
* <code>new Response(true, "")</code>
|
||||
*/
|
||||
public Response() {
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
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 RequestExecutorCommand extends AbstractRequestExecutor {
|
||||
@@ -18,7 +19,9 @@ public class RequestExecutorCommand extends AbstractRequestExecutor {
|
||||
try
|
||||
{
|
||||
boolean succes = plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), command);
|
||||
|
||||
|
||||
ConfigManager.getInstance().commandHistoryConfig.add(command);
|
||||
|
||||
if (!succes)
|
||||
plugin.getLogger().warning("La commande ne peut pas s'exécuter : /"+command);
|
||||
}
|
||||
@@ -38,12 +41,8 @@ public class RequestExecutorCommand extends AbstractRequestExecutor {
|
||||
|
||||
}.init(data));
|
||||
|
||||
Response rep = new Response();
|
||||
rep.good = true;
|
||||
rep.data = "";
|
||||
|
||||
|
||||
return rep;
|
||||
return new Response();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
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 RequestExecutorCommandAsync extends AbstractRequestExecutor {
|
||||
@@ -15,7 +16,9 @@ public class RequestExecutorCommandAsync extends AbstractRequestExecutor {
|
||||
try
|
||||
{
|
||||
rep.good = plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), data);
|
||||
|
||||
|
||||
ConfigManager.getInstance().commandHistoryConfig.add(data);
|
||||
|
||||
if (!rep.good)
|
||||
{
|
||||
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