Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. package com.lycoon.lemnoslife.network;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.UUID;
  10.  
  11. import com.lycoon.lemnoslife.Main;
  12.  
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.util.ChatComponentText;
  15.  
  16. public class LemnosSQLUtils {
  17.  
  18.     ResultSet results;
  19.    
  20.     //SQL
  21.     private static Connection connection;
  22.     private static String host, database, username, password, table, url;
  23.     private static int port;
  24.    
  25.     public static void initBDD()
  26.     {
  27.         try
  28.         {
  29.             url = "jdbc:mysql://adm.minecraft-mania.fr/a12014122813292385692366?useSSL=false";
  30.             username = "identifiant";
  31.             password = "mdp";
  32.             table = "playerData";
  33.            
  34.             Class.forName("com.mysql.jdbc.Driver");
  35.             connection = DriverManager.getConnection(url, username, password);
  36.            
  37.             if(connection != null)
  38.             {
  39.                 System.out.println("######## MySQL connecté ########");
  40.                 System.out.println(connection);
  41.             }
  42.             else
  43.                 System.out.println("######## MySQL null ########");
  44.            
  45.         }
  46.         catch(Exception f)
  47.         {
  48.             f.printStackTrace();
  49.         }
  50.     }
  51.    
  52.     public static void createPlayer(UUID uuid, EntityPlayer player)
  53.     {
  54.         try {
  55.             if(connection == null)
  56.                 System.out.println("CONNEXION NULL");
  57.             else
  58.                 System.out.println("CONNEXION = " +connection);
  59.            
  60.             PreparedStatement statement = connection.prepareStatement("SELECT * FROM " +table+ " WHERE UUID=?");
  61.             statement.setString(1, uuid.toString());
  62.            
  63.             ResultSet results = statement.executeQuery();
  64.            
  65.             //Regarde si le joueur est dans la BDD
  66.             if(!results.next())
  67.             {
  68.                 PreparedStatement insert = connection.prepareStatement("INSERT INTO " +table+ " (UUID,NAME) VALUE (?,?)");
  69.                 insert.setString(1, uuid.toString());
  70.                 insert.setString(2, player.getDisplayName());
  71.                 insert.executeUpdate();
  72.                
  73.                 System.out.println("MYSQL: Joueur inexistant et inséré à la base de données");
  74.             }
  75.             else
  76.                 System.out.println("MYSQL: Joueur déjà existant");
  77.         }
  78.         catch(SQLException e)
  79.         {
  80.             e.printStackTrace();
  81.         }
  82.     }
  83.    
  84.     //Ajouter un véhicule à la fourrière
  85.     public static void addVehicle(String uuid, String vehicle)
  86.     {
  87.         try {
  88.             PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET VEHICULES = concat(VEHICULES, ',', ?) WHERE UUID=?");
  89.             statement.setString(1, vehicle);
  90.             statement.setString(2, uuid);
  91.             statement.executeUpdate();
  92.         }
  93.         catch(SQLException e)
  94.         {
  95.             e.printStackTrace();
  96.         }
  97.     }
  98.    
  99.     //Récupérer les véhicules à ajouter à l'extProp du joueur
  100.     public static String getVehicles(String uuid)
  101.     {
  102.         try {
  103.             PreparedStatement statement = connection.prepareStatement("SELECT * FROM " +table+ " WHERE UUID=?");
  104.             statement.setString(1, uuid);
  105.             ResultSet results = statement.executeQuery();
  106.             results.next();
  107.            
  108.             return results.getString("VEHICULES");
  109.         }
  110.         catch(SQLException e)
  111.         {
  112.             e.printStackTrace();
  113.         }
  114.        
  115.         return null;
  116.     }
  117.    
  118.     //Supprime tous les véhicules du joueur
  119.     public static void deleteVehicles(UUID uuid)
  120.     {
  121.         try {
  122.             PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET VEHICULES='' WHERE UUID=?");
  123.             statement.setString(1, uuid.toString());
  124.             statement.executeUpdate();
  125.         }
  126.         catch(SQLException e)
  127.         {
  128.             e.printStackTrace();
  129.         }
  130.     }
  131.    
  132.     //Ajouter un gang au joueur
  133.     public static boolean joinGang(UUID uuid, String nomGang)
  134.     {
  135.         try {
  136.             PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET GANG=? WHERE UUID=?");
  137.             statement.setString(1, nomGang);
  138.             statement.setString(2, uuid.toString());
  139.             statement.executeUpdate();
  140.            
  141.             return true;
  142.         }
  143.         catch(SQLException e)
  144.         {
  145.             e.printStackTrace();
  146.         }
  147.        
  148.         return false;
  149.     }
  150.    
  151.     //Enlève le joueur du gang
  152.     public static void quitGang(UUID uuid)
  153.     {
  154.         try {
  155.             PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET GANG='' WHERE UUID=?");
  156.             statement.setString(1, uuid.toString());
  157.             statement.executeUpdate();
  158.         }
  159.         catch(SQLException e)
  160.         {
  161.             e.printStackTrace();
  162.         }
  163.     }
  164.    
  165.     //Définit le rôle du joueur dans le gang
  166.     public static void setRole(String name, String role)
  167.     {
  168.         try {
  169.             PreparedStatement statement = connection.prepareStatement("UPDATE " +table+ " SET ROLE=? WHERE NAME=?");
  170.             statement.setString(1, role);
  171.             statement.setString(2, name);
  172.             statement.executeUpdate();
  173.         }
  174.         catch(SQLException e)
  175.         {
  176.             e.printStackTrace();
  177.         }
  178.     }
  179.    
  180.     //Retourne le rôle du joueur
  181.     public static String getRole(String name)
  182.     {
  183.         try {
  184.             PreparedStatement statement = connection.prepareStatement("SELECT ROLE FROM " +table+ " WHERE NAME=?");
  185.             statement.setString(1, name);
  186.             ResultSet results = statement.executeQuery();
  187.             results.next();
  188.            
  189.             return results.getString("ROLE");
  190.         }
  191.         catch(SQLException e)
  192.         {
  193.             e.printStackTrace();
  194.         }
  195.        
  196.         return null;
  197.     }
  198.    
  199.     //Récupérer la liste de tous les membres d'un gang
  200.     public static ArrayList<String> getGangMembers(String nomGang)
  201.     {
  202.         try {
  203.             PreparedStatement statement = connection.prepareStatement("SELECT NAME FROM " +table+ " WHERE GANG=?");
  204.             statement.setString(1, nomGang);
  205.             ResultSet results = statement.executeQuery();
  206.            
  207.             ArrayList<String> gangMembers = new ArrayList<String>();
  208.  
  209.             while(results.next())
  210.             {
  211.                 String member = results.getString("NAME");
  212.                 gangMembers.add(member);
  213.             }
  214.            
  215.             return gangMembers;
  216.         }
  217.         catch(SQLException e)
  218.         {
  219.             e.printStackTrace();
  220.         }
  221.        
  222.         return null;
  223.     }
  224.    
  225.     //Vérifie si le joueur appartient à un gang
  226.     public static boolean isInGang(UUID uuid)
  227.     {
  228.         try {
  229.             PreparedStatement statement = connection.prepareStatement("SELECT GANG FROM " +table+ " WHERE UUID=?");
  230.             statement.setString(1, uuid.toString());
  231.             ResultSet results = statement.executeQuery();
  232.             results.next();
  233.            
  234.             //Si le field GANG est vide
  235.             if(results.getString("GANG").isEmpty())
  236.                 return false;
  237.         }
  238.         catch(SQLException e)
  239.         {
  240.             e.printStackTrace();
  241.         }
  242.        
  243.         return true;
  244.     }
  245.    
  246.     //Retourne si le gang à ce nom existe ou non
  247.     public static boolean gangExists(String nomGang)
  248.     {
  249.         int size = 0;
  250.        
  251.         try {
  252.             PreparedStatement statement = connection.prepareStatement("SELECT GANG FROM " +table);
  253.             ResultSet results = statement.executeQuery();
  254.            
  255.             results.last();
  256.             size = results.getRow();
  257.             results.beforeFirst();
  258.             results.next();
  259.            
  260.            
  261.             for(int i = 0; i < size; i++)
  262.             {
  263.                 if(results.getString("GANG").matches(nomGang))
  264.                     return true;
  265.                
  266.                 results.next();
  267.             }
  268.         }
  269.         catch(SQLException e)
  270.         {
  271.             e.printStackTrace();
  272.         }
  273.        
  274.         return false;
  275.     }
  276.    
  277.     //Retourne le nom du gang auquel appartient le joueur
  278.     public static String getGangName(UUID uuid)
  279.     {
  280.         try {
  281.             PreparedStatement statement = connection.prepareStatement("SELECT GANG FROM " +table+ " WHERE UUID=?");
  282.             statement.setString(1, uuid.toString());
  283.             ResultSet results = statement.executeQuery();
  284.             results.next();
  285.            
  286.             return results.getString("GANG");
  287.         }
  288.         catch(SQLException e)
  289.         {
  290.             e.printStackTrace();
  291.         }
  292.        
  293.         return null;
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement