diff --git a/resources/plugin.yml b/resources/plugin.yml index 51ecf1e..75c9559 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -82,6 +82,10 @@ commands: description: Succession automatique de commandes usage: /muco [paramètres ...] permission: pandacraft.muco + modo: + description: Commande de modération + usage: /modo + permission: pandacraft.modo diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java index 1c21c75..9bbaec3 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/PandacraftUtils.java @@ -1,5 +1,7 @@ package net.mc_pandacraft.java.plugin.pandacraftutils; +import java.sql.SQLException; + import net.mc_pandacraft.java.plugin.pandacraftutils.commands.PandacraftUtilsCommandsManager; import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager; import net.mc_pandacraft.java.plugin.pandacraftutils.modules.AutoMessagesManager; @@ -24,6 +26,7 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.network_api.NetworkAPI; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayer; import net.mc_pandacraft.java.plugin.pandacraftutils.players.OnlinePlayerManager; import net.mc_pandacraft.java.plugin.pandacraftutils.players.PlayerDataCleaner; +import net.mc_pandacraft.java.util.mysql.DBConnection; import org.bukkit.plugin.java.JavaPlugin; @@ -36,12 +39,10 @@ public class PandacraftUtils extends JavaPlugin { } - /** - * 'u' = stopped - * 'l' = loading - * 'r' = running - * 's' = stopping - */ + + + + private static ServerState serverState = ServerState.STOPPED; public static ServerState getServerState() { return serverState; } @@ -57,8 +58,9 @@ public class PandacraftUtils extends JavaPlugin { - //public DBConnection databaseConnection; + public DBConnection databaseConnection; + public PacketOutServerInfoListener serverPingListener; public AfkManager afkManager; public WESelectionDisplayManager wESelectionDisplayManager; public CommandAliasManager commandAliasManager; @@ -79,7 +81,6 @@ public class PandacraftUtils extends JavaPlugin { - public PacketOutServerInfoListener serverPingListener; @Override @@ -89,17 +90,17 @@ public class PandacraftUtils extends JavaPlugin { instance = this; - /* + try { - databaseConnection = new DBConnection(ConfigManager.getInstance().Database_host, - ConfigManager.getInstance().Database_port, - ConfigManager.getInstance().Database_database, - ConfigManager.getInstance().Database_username, - ConfigManager.getInstance().Database_password); + databaseConnection = new DBConnection(ConfigManager.getInstance().defaultConfig.database_host, + ConfigManager.getInstance().defaultConfig.database_port, + ConfigManager.getInstance().defaultConfig.database_base, + ConfigManager.getInstance().defaultConfig.database_user, + ConfigManager.getInstance().defaultConfig.database_pass); } catch (ClassNotFoundException | SQLException e) { getLogger().severe("Impossible de se connecter à la base de donnée !"); e.printStackTrace(); - }*/ + } ConfigManager.loadNewInstance(); @@ -107,6 +108,7 @@ public class PandacraftUtils extends JavaPlugin { PandacraftUtilsCommandsManager.loadNewInstance(); + serverPingListener = new PacketOutServerInfoListener(); afkManager = new AfkManager(); wESelectionDisplayManager = new WESelectionDisplayManager(); commandAliasManager = new CommandAliasManager(); @@ -117,7 +119,6 @@ public class PandacraftUtils extends JavaPlugin { loginLogoutMessageManager = new LoginLogoutMessageManager(); calculatorManager = new CalculatorManager(); survivalCuboManager = new SurvivalCuboManager(); - serverPingListener = new PacketOutServerInfoListener(); staffQueueManager = new StaffQueueManager(); tpsAnalysisManager = new TPSAnalysisManager(); autoMessagesManager = new AutoMessagesManager(); diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandModo.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandModo.java new file mode 100644 index 0000000..22044c4 --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/CommandModo.java @@ -0,0 +1,23 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public class CommandModo extends AbstractCommandExecutor { + + + public CommandModo() { + super("modo"); + } + + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, + String[] args) { + + + + return false; + } + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/PandacraftUtilsCommandsManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/PandacraftUtilsCommandsManager.java index d4bbc31..fcb036c 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/PandacraftUtilsCommandsManager.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/commands/PandacraftUtilsCommandsManager.java @@ -55,6 +55,7 @@ public class PandacraftUtilsCommandsManager { add(new CommandAnimal()); add(new CommandCoeur()); add(new CommandMuco()); + add(new CommandModo()); } diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ModoHistoryElement.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ModoHistoryElement.java new file mode 100644 index 0000000..1484a78 --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ModoHistoryElement.java @@ -0,0 +1,124 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.data_model; + +public class ModoHistoryElement extends SQLElement { + + private String modoName; + private ActionType actionType; + private long time; + private String playerName; + private Long duration = null; + private String message; + + + public ModoHistoryElement(String modo, ActionType type, long time, String player, String message) { + super("pandacraft_modo_history"); + setModoName(modo); + setActionType(type); + setTime(time); + setPlayerName(player); + setMessage(message); + } + + @Override + protected String[] getValues() { + return new String[] { + modoName, + actionType.name(), + String.valueOf(time), + playerName, + (duration == null)?null:duration.toString(), + message + }; + } + + @Override + protected String[] getFieldsName() { + return new String[] { + "modoName", + "actionType", + "time", + "playerName", + "duration", + "message" + }; + } + + + + public String getModoName() { + return modoName; + } + + public void setModoName(String modoName) { + if (modoName == null) throw new IllegalArgumentException("le paramètre ne peut être null"); + this.modoName = modoName; + } + + + + public ActionType getActionType() { + return actionType; + } + + public void setActionType(ActionType actionType) { + if (actionType == null) throw new IllegalArgumentException("le paramètre ne peut être null"); + this.actionType = actionType; + } + + + + public long getDuration() { + return duration; + } + + public void setDuration(Long duration) { + if (duration != null && duration.longValue() < 0) duration = null; + this.duration = duration; + } + + + + public String getPlayerName() { + return playerName; + } + + public void setPlayerName(String playerName) { + if (playerName == null) throw new IllegalArgumentException("le paramètre ne peut être null"); + this.playerName = playerName; + } + + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + if (message == null) throw new IllegalArgumentException("le paramètre ne peut être null"); + this.message = message; + } + + + + + + + + public long getTime() { + return time; + } + + public void setTime(long time) { + this.time = time; + } + + + + + + + + public enum ActionType{ + TEMPBAN, MUTE, JAIL, REPORT, KICK, MONEY + } + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ModoHistoryTable.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ModoHistoryTable.java new file mode 100644 index 0000000..7c48d28 --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/ModoHistoryTable.java @@ -0,0 +1,39 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.data_model; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import net.mc_pandacraft.java.plugin.pandacraftutils.data_model.ModoHistoryElement.ActionType; + +public class ModoHistoryTable extends SQLTable { + + + + public ModoHistoryTable() throws SQLException { + super("pandacraft_modo_history"); + } + + @Override + protected String createTableParameters() { + return "id INT AUTO_INCREMENT PRIMARY KEY," + + "modoName VARCHAR(16) NOT NULL," + + "actionType ENUM('TEMPBAN', 'MUTE', 'JAIL', 'REPORT', 'KICK', 'MONEY') NOT NULL," + + "time BIGINT NOT NULL," + + "playerName VARCHAR(16) NOT NULL," + + "duration BIGINT NULL," + + "message VARCHAR(512) NOT NULL"; + } + + @Override + protected ModoHistoryElement getElementInstance(ResultSet sqlResult) throws SQLException { + ModoHistoryElement el = new ModoHistoryElement( + sqlResult.getString("modoName"), + ActionType.valueOf(sqlResult.getString("actionType")), + sqlResult.getLong("time"), + sqlResult.getString("playerName"), + sqlResult.getString("message")); + el.setDuration(sqlResult.getLong("duration")); + return el; + } + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/Table.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLElement.java similarity index 84% rename from src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/Table.java rename to src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLElement.java index a7735c0..260efc4 100644 --- a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/Table.java +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLElement.java @@ -9,21 +9,27 @@ import org.apache.commons.lang.StringUtils; import net.mc_pandacraft.java.util.mysql.DBConnection; -public abstract class Table { +public abstract class SQLElement { DBConnection db = null;//PandacraftUtils.getInstance().databaseConnection; private boolean saved = false; - + + protected final String tableName; // champ relatif aux données private int id = 0; - - public Table() { + + public SQLElement(String name) { + this(name, 0); + } + public SQLElement(String name, int id) { + tableName = name; + this.id = id; } @@ -96,6 +102,7 @@ public abstract class Table { { // supprimer la ligne de la base PreparedStatement st = db.getConnection().prepareStatement("DELETE FROM "+tableName+" WHERE id="+id); st.executeUpdate(); + saved = false; } } catch (SQLException e) { e.printStackTrace(); @@ -104,12 +111,21 @@ public abstract class Table { } + + + public int getId() { + if (!saved) + throw new UnsupportedOperationException("Ne peut pas fournir l'ID d'un élément non sauvegardé"); + return id; + } + + /** * Récupère la liste des valeurs des champs de la table correspondante, excepté * le champ id * @return les valeurs des champs sous la forme de chaine de caractères */ - public abstract String[] getValues(); + protected abstract String[] getValues(); @@ -118,5 +134,5 @@ public abstract class Table { * le champ id * @return les noms des champs sous la forme de chaine de caractères */ - public abstract String[] getFieldsName(); + protected abstract String[] getFieldsName(); } diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLManager.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLManager.java new file mode 100644 index 0000000..6e749ea --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLManager.java @@ -0,0 +1,51 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.data_model; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class SQLManager { + + private static SQLManager instance; + + /** + * Retourne l'unique instance de la classe. Si elle n'existe pas, on tente de créer + * @return L'unique instance de la classe + */ + + public synchronized static SQLManager getInstance() { + if (instance == null) + loadNewInstance(); + return instance; + } + + public synchronized static void loadNewInstance() { + instance = new SQLManager(); + } + + + + + @SuppressWarnings("rawtypes") + private Map tables = new HashMap(); + + + public SQLManager() { + try { + + /* + * Les tables SQL sont à instancier ici ! + */ + + tables.put("modo_history", new ModoHistoryTable()); + + + } catch (SQLException e) { + e.printStackTrace(); + } + + + } + + +} diff --git a/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLTable.java b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLTable.java new file mode 100644 index 0000000..ba92582 --- /dev/null +++ b/src/net/mc_pandacraft/java/plugin/pandacraftutils/data_model/SQLTable.java @@ -0,0 +1,86 @@ +package net.mc_pandacraft.java.plugin.pandacraftutils.data_model; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +import net.mc_pandacraft.java.util.mysql.DBConnection; + +public abstract class SQLTable { + + DBConnection db = null;//PandacraftUtils.getInstance().databaseConnection; + + protected final String tableName; + + + public SQLTable(String name) throws SQLException { + tableName = name; + + if (!tableExist()) + createTable(); + + + } + + + private void createTable() throws SQLException { + Statement stmt = db.getConnection().createStatement(); + String sql = "CREATE TABLE IF NOT EXISTS "+tableName+" " + + "("+createTableParameters()+")"; + + stmt.executeUpdate(sql); + } + + + private boolean tableExist() throws SQLException { + return db.getConnection().getMetaData().getTables(null, null, tableName, null).next(); + } + + + /** + * Retourne une chaine de caractère qui sera inclu dans la requête SQL de création de la table. + * La requête est de la forme : CRATE TABLE tableName (); + * La chaine retournée sera ajoutée entre les parenthèses. + */ + protected abstract String createTableParameters(); + + + + /** + * Crée une instance de l'élément courant se trouvant dans le resultSet passé en paramètre + * @param sqlResult le set de résultat, déjà positionné sur un élément. Ne surtout pas appeler la méthode next() ! + * @return + * @throws SQLException + */ + protected abstract T getElementInstance(ResultSet sqlResult) throws SQLException; + + + + public T get(int id) throws SQLException { + Statement stmt = db.getConnection().createStatement(); + String sql = "SELECT * FROM "+tableName+" WHERE id = "+id+";"; + + ResultSet set = stmt.executeQuery(sql); + if (set.next()) + return getElementInstance(set); + else + return null; + } + + + + public List getAll() throws SQLException { + Statement stmt = db.getConnection().createStatement(); + String sql = "SELECT * FROM "+tableName+";"; + + ResultSet set = stmt.executeQuery(sql); + List elmts = new ArrayList(); + while (set.next()) + elmts.add(getElementInstance(set)); + + return elmts; + } + +}