Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.uhc.api.objects;
- import me.uhc.api.Main;
- import me.uhc.api.managers.ConfigManager;
- import me.uhc.api.managers.MySQLManager;
- import me.uhc.api.managers.UserManager;
- import me.uhc.api.utils.BukkitSerializationUtils;
- import me.uhc.api.utils.Utils;
- import me.uhc.guilds.managers.GuildManager;
- import me.uhc.guilds.objects.Guild;
- import org.bukkit.Bukkit;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.plugin.Plugin;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.PreparedStatement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- /**
- * Created by mikigal on 2016-03-30.
- */
- public class MySQL {
- private String host;
- private int port;
- private String user;
- private String pass;
- private String database;
- private String url;
- private Connection conn;
- public MySQL(String host, int port, String user, String pass, String database){
- this.host = host;
- this.port = port;
- this.user = user;
- this.pass = pass;
- this.database = database;
- this.buildURL();
- this.connect();
- }
- public void buildURL(){
- this.url = "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database;
- }
- public boolean isConnected(){
- if(conn == null){
- return false;
- }
- return true;
- }
- public boolean connect(){
- try {
- Utils.sendMsgToConsole(Main.getInst(), "&aUsing url to connect: " + this.url);
- conn = DriverManager.getConnection(this.url, this.user, this.pass);
- Utils.sendMsgToConsole(Main.getInst(), "&aPolaczono z baza danych MySQL!");
- return true;
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return false;
- }
- public boolean disconnect(){
- if(isConnected()){
- try {
- this.conn.close();
- return true;
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- }
- return false;
- }
- public void send(String sql){
- if(!isConnected()){
- connect();
- }
- Bukkit.getScheduler().runTaskAsynchronously(Main.getInst(), () -> {
- try {
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.execute();
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- e.printStackTrace();
- }
- });
- }
- public ResultSet query(String sql){
- if(!isConnected()){
- connect();
- }
- try {
- return this.conn.createStatement().executeQuery(sql);
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return null;
- }
- public void createUsersFromDatabase(){
- try{
- int i = 0;
- ResultSet rs = query("SELECT * FROM `tools`");
- while(rs.next()){
- UUID uuid = UUID.fromString(rs.getString("uuid"));
- int kills = (int) get("pvp_rank", "kills", uuid);
- int deaths = (int) get("pvp_rank", "deaths", uuid);
- double kd = (double) get("pvp_rank", "kd", uuid);
- int rank = (int) get("pvp_rank", "rank", uuid);
- String killHistory = (String) get("pvp_rank", "killHistory", uuid);
- long firstJoinTime = (long) get("tools", "firstJoinTime", uuid);
- String password = (String) get("auth", "password", uuid);
- List<UUID> ignored = new ArrayList<>();
- for(String s : rs.getString("ignored").split("/")){
- if(s.length() > 2){
- ignored.add(UUID.fromString(s));
- }
- }
- int blockAmount = rs.getInt("blockAmount");
- int dropLevel = rs.getInt("dropLevel");
- String invBackup = rs.getString("invBackup");
- String armorBackup = rs.getString("armorBackup");
- long timeOnServer = (long) get("stats", "timeOnServer", uuid);
- int kox = (int) get("stats", "kox", uuid);
- int ref = (int) get("stats", "ref", uuid);
- int pearl = (int) get("stats", "pearl", uuid);
- int obsidian = (int) get("stats", "obsidian", uuid);
- int meat = (int) get("stats", "meat", uuid);
- int arrow = (int) get("stats", "arrow", uuid);
- String registerIp = (String) get("auth", "ip", uuid);
- User u = UserManager.createUser(uuid, kills, deaths, kd, rank, killHistory, firstJoinTime, registerIp, password, ignored, invBackup, armorBackup, blockAmount, dropLevel, timeOnServer, kox, ref, pearl, obsidian, meat, arrow, true);
- i++;
- }
- Utils.sendMsgToConsole(Main.getInst(), "&aZaladowany: &c" + i + " &agraczy z bazy danych!");
- Main.getMySQL().createBansFromDatabase();
- }catch(SQLException e){
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- }
- public void createBansFromDatabase(){
- try{
- int i = 0;
- ResultSet rs = query("SELECT * FROM `bans`");
- while(rs.next()){
- UUID uuid = UUID.fromString(rs.getString("uuid"));
- String admin = rs.getString("admin");
- String reason = rs.getString("reason");
- long banDate = rs.getLong("banDate");
- long time = rs.getLong("time");
- User u = UserManager.getUser(uuid);
- Ban ban = new Ban(uuid, admin, reason, banDate, time);
- u.setBan(ban);
- i++;
- }
- Utils.sendMsgToConsole(Main.getInst(), "&aZaladowany: &c" + i + " &abanow z bazy danych!");
- }catch(SQLException e){
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- }
- public User createUserFromDatabase(UUID uuid){
- int kills = (int) get("pvp_rank", "kills", uuid);
- int deaths = (int) get("pvp_rank", "deaths", uuid);
- double kd = (double) get("pvp_rank", "kd", uuid);
- int rank = (int) get("pvp_rank", "rank", uuid);
- String killHistory = (String) get("pvp_rank", "killHistory", uuid);
- long firstJoinTime = (long) get("tools", "firstJoinTime", uuid);
- String password = (String) get("auth", "password", uuid);
- List<UUID> ignored = new ArrayList<>();
- String rawIgnored = (String) get("tools", "ignored", uuid);
- for(String s : rawIgnored.split("/")){
- if(s.length() > 2){
- ignored.add(UUID.fromString(s));
- }
- }
- int blockAmount = (int) get("tools", "blockAmount", uuid);
- int dropLevel = (int) get("tools", "dropLevel", uuid);
- String invBackup = (String) get("tools", "invBackup", uuid);
- String armorBackup = (String) get("tools", "armorBackup", uuid);
- long timeOnServer = (long) get("stats", "timeOnServer", uuid);
- int kox = (int) get("stats", "kox", uuid);
- int ref = (int) get("stats", "ref", uuid);
- int pearl = (int) get("stats", "pearl", uuid);
- int obsidian = (int) get("stats", "obsidian", uuid);
- int meat = (int) get("stats", "meat", uuid);
- int arrow = (int) get("stats", "arrow", uuid);
- String registerIp = (String) get("auth", "ip", uuid);
- return UserManager.createUser(uuid, kills, deaths, kd, rank, killHistory, firstJoinTime, registerIp, password, ignored, invBackup, armorBackup, blockAmount, dropLevel, timeOnServer, kox, ref, pearl, obsidian, meat, arrow, true);
- }
- public Object get(String tableName, String columnName, UUID uuid){
- try {
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM `" + tableName + "` WHERE uuid = ?");
- ps.setString(1, uuid.toString());
- ResultSet rs = ps.executeQuery();
- rs.last();
- if (rs.getRow() != 0) {
- rs.first();
- return rs.getObject(columnName);
- }
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return null;
- }
- public Object get(String tableName, String columnName, String whereColumn, Object whereValue){
- try {
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM `" + tableName + "` WHERE " + whereColumn + " = ?");
- ps.setObject(1, whereValue);
- ResultSet rs = ps.executeQuery();
- rs.last();
- if (rs.getRow() != 0) {
- rs.first();
- return rs.getObject(columnName);
- }
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return null;
- }
- public void set(String tableName, String columnName, Object newValue, UUID uuid){
- Bukkit.getScheduler().runTaskAsynchronously(Main.getInst(), () -> {
- try {
- PreparedStatement ps = conn.prepareStatement("UPDATE `" + tableName + "` SET `" + columnName + "`='" + newValue + "' WHERE uuid = ?");
- ps.setString(1, uuid.toString());
- ps.executeUpdate();
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- });
- }
- public void setFromTag(String tableName, String columnName, Object newValue, String tag){
- Bukkit.getScheduler().runTaskAsynchronously(Main.getInst(), () -> {
- try {
- PreparedStatement ps = conn.prepareStatement("UPDATE `" + tableName + "` SET `" + columnName + "`='" + newValue + "' WHERE tag = ?");
- ps.setString(1, tag);
- ps.executeUpdate();
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- });
- }
- public void saveDefaultData(User u){
- boolean newPlayer = get("tools", "name", u.getUuid()) == null;
- if(newPlayer){
- this.send("INSERT INTO `tools` VALUES ('" + String.valueOf(u.getUuid()) + "','" + u.getName() + "','NO_BACKUP','NO_BACKUP','" + System.currentTimeMillis() + "',' ', '0', '0')");
- this.send("INSERT INTO `pvp_rank` VALUES ('" + String.valueOf(u.getUuid()) + "','0','0','1.0','1000','');");
- this.send("INSERT INTO `auth` VALUES ('" + u.getUuid() + "','xxx','" + u.getRegisterIp() + "');");
- this.send("INSERT INTO `stats` VALUES ('" + u.getUuid() + "','0','0','0','0','0','0','0');");
- }
- }
- public void ban(User u, Ban b){
- this.send("INSERT INTO `bans` VALUES ('" + u.getUuid() + "','" + b.getAdmin() + "','" + b.getReason() + "','" + b.getBanDate() + "','" + b.getTime() + "');");
- }
- public void unban(User user) {
- Bukkit.getScheduler().runTaskAsynchronously(Main.getInst(), () -> {
- try {
- PreparedStatement ps = conn.prepareStatement("DELETE FROM `bans` WHERE uuid = ?");
- ps.setString(1, user.getUuid().toString());
- ps.executeUpdate();
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- });
- }
- public List<UUID> getTopRanks(){
- List<UUID> uu = new ArrayList<UUID>();
- UUID uuid;
- try{
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM pvp_rank ORDER BY rank DESC LIMIT 15;");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- uuid = UUID.fromString(rs.getString("uuid"));
- uu.add(uuid);
- }
- }catch(SQLException e){
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return uu;
- }
- public List<UUID> getTopDrops(){
- List<UUID> uu = new ArrayList<UUID>();
- UUID uuid;
- try{
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM tools ORDER BY blockAmount DESC LIMIT 10;");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- uuid = UUID.fromString(rs.getString("uuid"));
- uu.add(uuid);
- }
- }catch(SQLException e){
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return uu;
- }
- public List<Guild> getTopGuilds(){
- List<Guild> gg = new ArrayList<>();
- try{
- PreparedStatement ps = conn.prepareStatement("SELECT * FROM guilds_guilds ORDER BY points DESC LIMIT 15;");
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- gg.add(GuildManager.getGuild(rs.getString("tag")));
- }
- }catch(SQLException e){
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- return gg;
- }
- public void reconnect(){
- try {
- this.conn.prepareStatement("SELECT * FROM `tools`").executeQuery();
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- }
- public void sendOnMainThread(String sql){
- try {
- if(!isConnected()){
- connect();
- }
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.execute();
- } catch (SQLException e) {
- Utils.sendMsgToConsole(Main.getInst(), "&c" + e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment