Réactivation de la connexion à la base de donnée + Création de l'ORM pour l'historique de modération + Préparation de la commande /modo

This commit is contained in:
Marc Baloup 2015-03-08 01:43:48 -05:00
parent ab2860d326
commit 146efde718
9 changed files with 367 additions and 22 deletions

View File

@ -82,6 +82,10 @@ commands:
description: Succession automatique de commandes
usage: /muco <NomListe> [paramètres ...]
permission: pandacraft.muco
modo:
description: Commande de modération
usage: /modo
permission: pandacraft.modo

View File

@ -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();

View File

@ -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;
}
}

View File

@ -55,6 +55,7 @@ public class PandacraftUtilsCommandsManager {
add(new CommandAnimal());
add(new CommandCoeur());
add(new CommandMuco());
add(new CommandModo());
}

View File

@ -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
}
}

View File

@ -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<ModoHistoryElement> {
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;
}
}

View File

@ -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 <code>id</code>
* @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 <code>id</code>
* @return les noms des champs sous la forme de chaine de caractères
*/
public abstract String[] getFieldsName();
protected abstract String[] getFieldsName();
}

View File

@ -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<String, SQLTable> tables = new HashMap<String, SQLTable>();
public SQLManager() {
try {
/*
* Les tables SQL sont à instancier ici !
*/
tables.put("modo_history", new ModoHistoryTable());
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -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<T extends SQLElement> {
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 : <code>CRATE TABLE tableName ();</code>
* 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<T> getAll() throws SQLException {
Statement stmt = db.getConnection().createStatement();
String sql = "SELECT * FROM "+tableName+";";
ResultSet set = stmt.executeQuery(sql);
List<T> elmts = new ArrayList<T>();
while (set.next())
elmts.add(getElementInstance(set));
return elmts;
}
}