Début d'implémentation d'un ORM pour les données des joueurs

This commit is contained in:
Marc Baloup 2014-12-24 19:33:08 +01:00
parent 452da3bce8
commit 37f8ea1b00
5 changed files with 211 additions and 25 deletions

View File

@ -1,6 +1,5 @@
package net.mc_pandacraft.java.plugin.pandacraftutils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -10,22 +9,45 @@ import java.util.Map;
* Configuration du plugin
*/
public class ConfigManager {
private static ConfigManager instance;
private static ConfigManager instance = null;
public static ConfigManager getInstance() { return instance; }
public static ConfigManager getInstance() {
if (instance == null)
instance = new ConfigManager();
return instance;
}
@SuppressWarnings("unused")
private PandacraftUtils plugin;
private PandacraftUtils plugin = PandacraftUtils.getInstance();
public ConfigManager(File f, PandacraftUtils pl) {
plugin = pl;
instance = this;
private ConfigManager() {
initChatAnalysisBadWord();
initCommandAlias();
}
/*
* Connexion à la base de donnée
*/
public String Database_host = "localhost";
public int Database_port = 3306;
public String Database_username = "pandacraft";
public String Database_database = "pandacraft";
public String Database_password = "HYtKq92pfx9ucwzq";
/*
* Configuration AFK
*/
@ -57,13 +79,12 @@ public class ConfigManager {
*/
ChatAnalysis_badWords.add("pu+tes?");
ChatAnalysis_badWords.add("conn?a(rd?|ss?e?)");
ChatAnalysis_badWords.add("sal(o|au)pe?s?");
ChatAnalysis_badWords.add("sal(o+|au)pe?s?");
ChatAnalysis_badWords.add("[ea]ncul(é|e|er|ai(s|t|))");
ChatAnalysis_badWords.add("merdes?");
ChatAnalysis_badWords.add("ni(qu|k)e? ta m(è|e|é)re?");
ChatAnalysis_badWords.add("fil?s de putes?");
ChatAnalysis_badWords.add("ta m(è|e|é)re? l(a|e) putes?");
ChatAnalysis_badWords.add("ta m(è|e|é)re?");
ChatAnalysis_badWords.add("tafiole?s?");
ChatAnalysis_badWords.add("vas? te pendre");
ChatAnalysis_badWords.add("fuck");

View File

@ -1,6 +1,8 @@
package net.mc_pandacraft.java.plugin.pandacraftutils;
import java.sql.SQLException;
import net.mc_pandacraft.java.plugin.pandacraftutils.afk.CommandAfk;
import net.mc_pandacraft.java.plugin.pandacraftutils.chat_analyzer.ChatAnalysisManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.cheat_protect.creative.CreativCheatManager;
@ -16,11 +18,17 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.simple_commands.staff_playe
import net.mc_pandacraft.java.plugin.pandacraftutils.spawntime.SpawnTimeManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.survival_cuboid.CommandWandSelection;
import net.mc_pandacraft.java.plugin.pandacraftutils.system_analyzer.CommandSystem;
import net.mc_pandacraft.java.util.mysql.DBConnection;
import org.bukkit.plugin.java.JavaPlugin;
public class PandacraftUtils extends JavaPlugin {
private static PandacraftUtils instance;
public static PandacraftUtils getInstance() { return instance; }
public DBConnection databaseConnection;
public CommandAfk commandAfk;
public CommandList commandPlayers;
public CommandSetblock commandSetblock;
@ -44,8 +52,18 @@ public class PandacraftUtils extends JavaPlugin {
@Override
public void onEnable(){
// initialisation de la configuration
new ConfigManager(null, this);
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);
} catch (ClassNotFoundException | SQLException e) {
getLogger().severe("Impossible de se connecter à la base de donnée !");
e.printStackTrace();
}
commandPlayers = new CommandList(this);
commandSetblock = new CommandSetblock(this);
@ -83,6 +101,9 @@ public class PandacraftUtils extends JavaPlugin {
noPvpProtectManager = null;
serverPingListener = null;
try { databaseConnection.getConnection().close(); } catch (SQLException e) { }
}
}

View File

@ -0,0 +1,34 @@
package net.mc_pandacraft.java.plugin.pandacraftutils.data_model;
/**
* Représente les données persistante correspondant à un joueur donné sur le serveur
*
* <i>Le nom de cette classe correspond à celle de la table dans la base de donnée,
* d' la présence d'un <code>_</code>(underscore), qui n'est pas admis en général
* dans le nom d'une classe</i>
* @author Marc Baloup
*
*/
public class PandacraftUtils_Player extends Table {
// TODO Créer la table dans la base de donnée
// TODO Ajouter les champs
@Override
public String[] getValues() {
// TODO Auto-generated method stub
return null;
}
@Override
public String[] getFieldsName() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -0,0 +1,123 @@
package net.mc_pandacraft.java.plugin.pandacraftutils.data_model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.lang.StringUtils;
import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
import net.mc_pandacraft.java.util.mysql.DBConnection;
public abstract class Table {
DBConnection db = PandacraftUtils.getInstance().databaseConnection;
private boolean saved = false;
// champ relatif aux données
private int id = 0;
public Table() {
}
public void save() {
String tableName = this.getClass().getSimpleName();
try {
Connection conn;
conn = db.getConnection();
String[] fields = getFieldsName(), values = getValues();
if (saved)
{ // mettre à jour les valeurs dans la base
String sql = "";
for (int i=0; i<fields.length && i<values.length; i++)
{
sql += fields[i]+" = ? ";
}
PreparedStatement st = conn.prepareStatement("UPDATE "+tableName+" SET "+sql+" WHERE id="+id);
for (int i=0; i<fields.length && i<values.length; i++)
{
st.setString(i+1, values[i]);
}
st.executeUpdate();
}
else
{ // ajouter dans la base
String concat_vals = "";
String concat_fields = StringUtils.join(fields, ',');
for (int i=0; i<fields.length && i<values.length; i++)
{
if (i!=0) concat_vals += ",";
concat_vals += " ? ";
}
PreparedStatement st = conn.prepareStatement("INSERT INTO "+tableName+" ("+concat_fields+") VALUES ("+concat_vals+")", Statement.RETURN_GENERATED_KEYS);
for (int i=0; i<fields.length && i<values.length; i++)
{
st.setString(i+1, values[i]);
}
id = st.executeUpdate();
saved = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete() {
String tableName = this.getClass().getSimpleName();
try {
if (saved)
{ // supprimer la ligne de la base
PreparedStatement st = db.getConnection().prepareStatement("DELETE FROM "+tableName+" WHERE id="+id);
st.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 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();
/**
* Récupère la liste des noms des champs de la table correspondante, excepté
* le champ <code>id</code>
* @return les noms des champs sous la forme de chaine de caractères
*/
public abstract String[] getFieldsName();
}

View File

@ -2,9 +2,7 @@ package net.mc_pandacraft.java.util.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.bukkit.plugin.java.JavaPlugin;
@ -26,7 +24,7 @@ public class DBConnection {
}
public void reconnect() throws SQLException
public void reconnectIfNecessary() throws SQLException
{
try
{
@ -38,17 +36,6 @@ public class DBConnection {
}
}
public Statement createStatement() throws SQLException
{
return conn.createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException
{
return conn.prepareStatement(sql);
}
public Connection getConnection()
{
return conn;