Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.murder.loots;
- 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 org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- public class MySQL {
- private static String dburl, username, password;
- private static Connection conn;
- public static boolean init(String host, String database, String newusername, String newpassword, int port) {
- dburl = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
- username = newusername;
- password = newpassword;
- return connect();
- }
- public static boolean connect() {
- conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(dburl, username, password);
- if(conn != null) Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[Loots] MySQL service enabled");
- 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));");
- return true;
- } catch(SQLException e) {
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to enable MySQL service : " + e.getMessage());
- } catch (ClassNotFoundException e) {
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to enable MySQL service : could not find database driver class.");
- }
- return false;
- }
- public static boolean test() {
- try {
- if(conn != null && !conn.isClosed()) {
- conn.createStatement().execute("SELECT 1");
- return true;
- }
- } catch (SQLException e) { }
- return false;
- }
- private static void execute(String query, Object... args) {
- try {
- if (!test()) connect();
- PreparedStatement statement = conn.prepareStatement(query);
- for(int i=0; i<args.length; i++) statement.setObject(i+1, args[i]);
- statement.executeUpdate();
- } catch (SQLException e) {
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to execute MySQL statement : " + e.getMessage());
- }
- }
- private static ResultSet get(String query, Object... args) {
- try {
- if (!test()) connect();
- PreparedStatement statement = conn.prepareStatement(query);
- for(int i=0; i<args.length; i++) statement.setObject(i+1, args[i]);
- return statement.executeQuery();
- } catch (SQLException e) {
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Failed to execute MySQL statement : " + e.getMessage());
- }
- return null;
- }
- public static boolean isLoot(UUID uuid) {
- try {
- ResultSet result = get("SELECT * FROM `loots` WHERE `UUID`='" + uuid.toString() + "'");
- if(result.next()) return true;
- } catch (SQLException e) {
- Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Loots] Exception while checking player existence : " + e.getMessage());
- }
- return false;
- }
- public static UUID addLoot(String to, String id, String name, String lore, String from) {
- UUID uuid = UUID.randomUUID();
- while (isLoot(uuid)) uuid = UUID.randomUUID();
- execute("INSERT INTO `loots` (`UUID`,`OWNER`,`ID`,`NAME`,`LORE`,`STACK`,`FROM`) VALUES ('" + uuid + "','" + to + "','" + id + "','" + name + "','" + lore + "','" + from + "'");
- return uuid;
- }
- public static void removeLoot(UUID uuid) {
- execute("DELETE FROM `loots` WHERE `UUID`='" + uuid.toString() + "'");
- }
- public static ArrayList<Item> getLoots(String owner) {
- ArrayList<Item> items = new ArrayList<Item>();
- try {
- ResultSet result = get("SELECT * FROM `loots` WHERE `OWNER`='" + owner + "'");
- 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")));
- } catch (SQLException e) { }
- return items;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement