Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lycoon.lemnoslife.network;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.UUID;
- import com.lycoon.lemnoslife.Main;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.ChatComponentText;
- public class LemnosSQLUtils {
- ResultSet results;
- //SQL
- private static Connection connection;
- private static String host, database, username, password, table, url;
- private static int port;
- public static void initBDD()
- {
- try
- {
- url = "jdbc:mysql://adm.minecraft-mania.fr/a12014122813292385692366?useSSL=false";
- username = "identifiant";
- password = "mdp";
- table = "playerData";
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(url, username, password);
- if(connection != null)
- {
- System.out.println("######## MySQL connecté ########");
- System.out.println(connection);
- }
- else
- System.out.println("######## MySQL null ########");
- }
- catch(Exception f)
- {
- f.printStackTrace();
- }
- }
- public static void createPlayer(UUID uuid, EntityPlayer player)
- {
- try {
- if(connection == null)
- System.out.println("CONNEXION NULL");
- else
- System.out.println("CONNEXION = " +connection);
- PreparedStatement statement = connection.prepareStatement("SELECT * FROM " +table+ " WHERE UUID=?");
- statement.setString(1, uuid.toString());
- ResultSet results = statement.executeQuery();
- //Regarde si le joueur est dans la BDD
- if(!results.next())
- {
- PreparedStatement insert = connection.prepareStatement("INSERT INTO " +table+ " (UUID,NAME) VALUE (?,?)");
- insert.setString(1, uuid.toString());
- insert.setString(2, player.getDisplayName());
- insert.executeUpdate();
- System.out.println("MYSQL: Joueur inexistant et inséré à la base de données");
- }
- else
- System.out.println("MYSQL: Joueur déjà existant");
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- //Ajouter un véhicule à la fourrière
- public static void addVehicle(String uuid, String vehicle)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET VEHICULES = concat(VEHICULES, ',', ?) WHERE UUID=?");
- statement.setString(1, vehicle);
- statement.setString(2, uuid);
- statement.executeUpdate();
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- //Récupérer les véhicules à ajouter à l'extProp du joueur
- public static String getVehicles(String uuid)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("SELECT * FROM " +table+ " WHERE UUID=?");
- statement.setString(1, uuid);
- ResultSet results = statement.executeQuery();
- results.next();
- return results.getString("VEHICULES");
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- //Supprime tous les véhicules du joueur
- public static void deleteVehicles(UUID uuid)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET VEHICULES='' WHERE UUID=?");
- statement.setString(1, uuid.toString());
- statement.executeUpdate();
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- //Ajouter un gang au joueur
- public static boolean joinGang(UUID uuid, String nomGang)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET GANG=? WHERE UUID=?");
- statement.setString(1, nomGang);
- statement.setString(2, uuid.toString());
- statement.executeUpdate();
- return true;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return false;
- }
- //Enlève le joueur du gang
- public static void quitGang(UUID uuid)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET GANG='' WHERE UUID=?");
- statement.setString(1, uuid.toString());
- statement.executeUpdate();
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- //Définit le rôle du joueur dans le gang
- public static void setRole(String name, String role)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET ROLE=? WHERE NAME=?");
- statement.setString(1, role);
- statement.setString(2, name);
- statement.executeUpdate();
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- }
- //Retourne le rôle du joueur
- public static String getRole(String name)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("SELECT ROLE FROM " +table+ " WHERE NAME=?");
- statement.setString(1, name);
- ResultSet results = statement.executeQuery();
- results.next();
- return results.getString("ROLE");
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- //Récupérer la liste de tous les membres d'un gang
- public static ArrayList<String> getGangMembers(String nomGang)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("SELECT NAME FROM " +table+ " WHERE GANG=?");
- statement.setString(1, nomGang);
- ResultSet results = statement.executeQuery();
- ArrayList<String> gangMembers = new ArrayList<String>();
- while(results.next())
- {
- String member = results.getString("NAME");
- gangMembers.add(member);
- }
- return gangMembers;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- //Vérifie si le joueur appartient à un gang
- public static boolean isInGang(UUID uuid)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("SELECT GANG FROM " +table+ " WHERE UUID=?");
- statement.setString(1, uuid.toString());
- ResultSet results = statement.executeQuery();
- results.next();
- //Si le field GANG est vide
- if(results.getString("GANG").isEmpty())
- return false;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return true;
- }
- //Retourne si le gang à ce nom existe ou non
- public static boolean gangExists(String nomGang)
- {
- int size = 0;
- try {
- PreparedStatement statement = connection.prepareStatement("SELECT GANG FROM " +table);
- ResultSet results = statement.executeQuery();
- results.last();
- size = results.getRow();
- results.beforeFirst();
- results.next();
- for(int i = 0; i < size; i++)
- {
- if(results.getString("GANG").matches(nomGang))
- return true;
- results.next();
- }
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return false;
- }
- //Retourne le nom du gang auquel appartient le joueur
- public static String getGangName(UUID uuid)
- {
- try {
- PreparedStatement statement = connection.prepareStatement("SELECT GANG FROM " +table+ " WHERE UUID=?");
- statement.setString(1, uuid.toString());
- ResultSet results = statement.executeQuery();
- results.next();
- return results.getString("GANG");
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement