package pl.jaqubiaq.bans.Utils; import pl.jaqubiaq.bans.Managers.DBManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.UUID; import static pl.jaqubiaq.bans.Managers.DBManager.*; public class DBUtil { // //BANS // public static void setBanned(String target_Name, UUID target, String executor, String reason) throws SQLException { if(DBManager.isConnected()){ if(target!=null){ PreparedStatement statement = getConnection() .prepareStatement("INSERT INTO " + table_bans + " (UUID,NAME,REASON,EXECUTOR) VALUES (?,?,?,?)"); statement.setString(1, target.toString()); statement.setString(2, target_Name); statement.setString(3, reason); statement.setString(4, executor); statement.executeUpdate(); } } } public static void setUnBanned(UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isBanned(uuid)){ PreparedStatement statement = getConnection() .prepareStatement("DELETE * FROM " + table_bans + " WHERE UUID=?"); statement.setString(1, uuid.toString()); statement.executeUpdate(); } } } public static boolean isBanned(UUID uuid) throws SQLException { if(DBManager.isConnected()){ PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + table_bans + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); return rs.next(); } return false; } // //MUTES // public static void setMuted(String target_Name, UUID target, String executor, String reason) throws SQLException { if(DBManager.isConnected()){ if(target!=null){ PreparedStatement statement = getConnection() .prepareStatement("INSERT INTO " + table_mutes + " (UUID,NAME,REASON,EXECUTOR) VALUES (?,?,?,?)"); statement.setString(1, target.toString()); statement.setString(2, target_Name); statement.setString(3, reason); statement.setString(4, executor); statement.executeUpdate(); } } } public static void setUnMuted(UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isMuted(uuid)){ PreparedStatement statement = getConnection() .prepareStatement("DELETE FROM " + table_mutes + " WHERE UUID=?"); statement.setString(1, uuid.toString()); statement.executeUpdate(); } } } public static boolean isMuted(UUID uuid) throws SQLException { if(DBManager.isConnected()){ PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + table_mutes + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); return rs.next(); } return false; } // //TEMPMUTES // public static void setTempMuted(String target_Name, UUID target, String executor, String reason, long time) throws SQLException { if(DBManager.isConnected()){ if(target!=null){ PreparedStatement statement = getConnection() .prepareStatement("INSERT INTO " + table_tempMutes + " (UUID,NAME,REASON,EXECUTOR,TIME_END) VALUES (?,?,?,?,?)"); statement.setString(1, target.toString()); statement.setString(2, target_Name); statement.setString(3, reason); statement.setString(4, executor); statement.setLong(5, time + System.currentTimeMillis()); statement.executeUpdate(); } } } public static void setUnTempMuted(UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isTempMuted(uuid)){ PreparedStatement statement = getConnection() .prepareStatement("DELETE FROM " + table_tempMutes + " WHERE UUID=?"); statement.setString(1, uuid.toString()); statement.executeUpdate(); } } } public static boolean isTempMuted(UUID uuid) throws SQLException { if(DBManager.isConnected()){ PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + table_tempMutes + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); return rs.next(); } return false; } // //TAMPBANS // public static void setTempBanned(String target_Name, UUID target, String executor, String reason, long time) throws SQLException { if(DBManager.isConnected()){ if(target!=null){ PreparedStatement statement = getConnection() .prepareStatement("INSERT INTO " + table_tempBans + " (UUID,NAME,REASON,EXECUTOR,TIME_END) VALUES (?,?,?,?,?)"); statement.setString(1, target.toString()); statement.setString(2, target_Name); statement.setString(3, reason); statement.setString(4, executor); statement.setLong(5, time + System.currentTimeMillis()); statement.executeUpdate(); } } } public static void setUnTempBanned(UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isTempBanned(uuid)){ PreparedStatement statement = getConnection() .prepareStatement("DELETE FROM " + table_tempBans + " WHERE UUID=?"); statement.setString(1, uuid.toString()); statement.executeUpdate(); } } } public static boolean isTempBanned(UUID uuid) throws SQLException { if(DBManager.isConnected()){ PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + table_tempBans + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); return rs.next(); } return false; } // // GET METHODS // public static String getReason(String tableName, UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isBanned(uuid) || isTempBanned(uuid)) { PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + tableName + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); if(rs.next()) { return rs.getString("REASON"); } } } return null; } public static String getExecutor(String tableName, UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isBanned(uuid) || isTempBanned(uuid)) { PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + tableName + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); if(rs.next()){ return rs.getString("EXECUTOR"); } } } return null; } public static Long getTimeLeft(String tableName, UUID uuid) throws SQLException { if(DBManager.isConnected()){ if(isTempBanned(uuid)) { PreparedStatement statement = getConnection() .prepareStatement("SELECT * FROM " + tableName + " WHERE UUID=?"); statement.setString(1, uuid.toString()); ResultSet rs = statement.executeQuery(); if(rs.next()){ return rs.getLong("TIME_END"); } } } return null; } }