daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Jun 29th, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fr.murder.loots;
  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 org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13.  
  14. public class MySQL {
  15.    
  16.     private static String dburl, username, password;
  17.     private static Connection conn;
  18.    
  19.     public static boolean init(String host, String database, String newusername, String newpassword, int port) {
  20.         dburl = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
  21.        
  22.         username = newusername;
  23.         password = newpassword;
  24.         return connect();
  25.     }
  26.    
  27.     public static boolean connect() {
  28.         conn = null;
  29.        
  30.         try {
  31.             Class.forName("com.mysql.jdbc.Driver");
  32.             conn = DriverManager.getConnection(dburl, username, password);
  33.             if(conn != null) Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[Loots] MySQL service enabled");
  34.             conn.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS `loots` (`UUID` VARCHAR(255) NOT NULL, `OWNER` VARCHAR(255), `ID` VARCHAR(255), `NAME` VARCHAR(255), `LORE` VARCHAR(255), `STACK` VARCHAR(255), `FROM` VARCHAR(255));");
  35.             return true;
  36.         } catch(SQLException e) {
  37.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to enable MySQL service : " + e.getMessage());
  38.         } catch (ClassNotFoundException e) {
  39.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to enable MySQL service : could not find database driver class.");
  40.         }
  41.         return false;
  42.     }
  43.    
  44.     public static boolean test() {
  45.         try {
  46.             if(conn != null && !conn.isClosed()) {
  47.                 conn.createStatement().execute("SELECT 1");
  48.                 return true;
  49.             }
  50.         } catch (SQLException e) { }
  51.         return false;
  52.     }
  53.    
  54.     private static void execute(String query, Object... args) {
  55.         try {
  56.             if (!test()) connect();
  57.            
  58.             PreparedStatement statement = conn.prepareStatement(query);
  59.             for(int i=0; i<args.length; i++) statement.setObject(i+1, args[i]);
  60.             statement.executeUpdate();
  61.         } catch (SQLException e) {
  62.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to execute MySQL statement : " + e.getMessage());
  63.         }
  64.     }
  65.    
  66.     private static ResultSet get(String query, Object... args) {
  67.         try {
  68.             if (!test()) connect();
  69.            
  70.             PreparedStatement statement = conn.prepareStatement(query);
  71.             for(int i=0; i<args.length; i++) statement.setObject(i+1, args[i]);
  72.             return statement.executeQuery();
  73.         } catch (SQLException e) {
  74.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to execute MySQL statement : " + e.getMessage());
  75.         }
  76.         return null;
  77.     }
  78.    
  79.     public static boolean isLoot(UUID uuid) {
  80.         try {  
  81.             ResultSet result = get("SELECT * FROM `loots` WHERE `UUID`='" + uuid.toString() + "'");
  82.             if(result.next()) return true;
  83.         } catch (SQLException e) {
  84.             Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Exception while checking player existence : " + e.getMessage());
  85.         }
  86.         return false;
  87.     }
  88.    
  89.     public static UUID addLoot(String to, String id, String name, String lore, String from) {
  90.         UUID uuid = UUID.randomUUID();
  91.         while (isLoot(uuid)) uuid = UUID.randomUUID();
  92.         execute("INSERT INTO `loots` (`UUID`,`OWNER`,`ID`,`NAME`,`LORE`,`STACK`,`FROM`) VALUES ('" + uuid + "','" + to + "','" + id + "','" + name + "','" + lore + "','" + from + "'");
  93.         return uuid;
  94.     }
  95.    
  96.     public static void removeLoot(UUID uuid) {
  97.         execute("DELETE FROM `loots` WHERE `UUID`='" + uuid.toString() + "'");
  98.     }
  99.    
  100.     public static ArrayList<Item> getLoots(String owner) {
  101.         ArrayList<Item> items = new ArrayList<Item>(); 
  102.         try {
  103.             ResultSet result = get("SELECT * FROM `loots` WHERE `OWNER`='" + owner + "'");
  104.             while(result.next()) items.add(new Item(result.getString("uuid"), result.getString("owner"), result.getInt("id"), result.getString("name"), result.getString("lore"), result.getInt("stack"), result.getString("from")));
  105.         } catch (SQLException e) { }
  106.         return items;
  107.     }
  108.  
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top