package eu.univento.core.api.player; import eu.univento.core.Core; import eu.univento.core.api.database.MySQL; import eu.univento.core.api.utils.NameFetcher; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; /** * bans and unbans players * @author joethei * @version 1.2 */ public class BanSystem{ /** * sets player banned * @param p player * @param reason reason * @param who who banned * @param time duration * @throws SQLException SQL server not available or throwing error * @throws ClassNotFoundException class couldn't be found */ static void setBanned(CustomPlayer p, String reason, CustomPlayer who, int time)throws SQLException, ClassNotFoundException { int id = p.getID(); MySQL sql = Core.returnSQL(); sql.openConnection(); Date date = new Date(); date.setTime(date.getTime() + (time * 24)); Timestamp timestamp = new Timestamp(date.getTime()); PreparedStatement st = sql.getConnection().prepareStatement("INSERT INTO bans (BID, Reason, until, BannerID) VALUES ('" + id + "', '" + reason + "', '" + who.getID() + "', '" + timestamp + "');"); st.execute(); p.kickPlayer("§cYou have been banned. §6Reason: " + reason + "\nUntil: " + timestamp); who.sendMessage("§cDu hast den Spieler " + p.getColor() + p.getName() + "§c für " + time + " Tage gebannt wegen " + reason); sql.closeConnection(); } /** * unbans a player * @param p player * @throws SQLException SQL server not available or throwing error * @throws ClassNotFoundException class couldn't be found */ static void removeAllBans(CustomPlayer p) throws SQLException, ClassNotFoundException{ int id = p.getID(); MySQL sql = Core.returnSQL(); sql.openConnection(); PreparedStatement st = sql.getConnection().prepareStatement("DELETE FROM bans WHERE ID ='" + id + "';"); st.execute(); sql.closeConnection(); } /** * checks if player is banned * @param p player * @return true /false * @throws SQLException SQL server not available or throwing error * @throws ClassNotFoundException class couldn't be found */ static boolean isBanned(CustomPlayer p) throws SQLException, ClassNotFoundException { int id = p.getID(); MySQL sql = Core.returnSQL(); PreparedStatement st = sql.getConnection().prepareStatement("SELECT * FROM bans WHERE ID='" + id + "';"); ResultSet rs = st.executeQuery(); if(rs.next()) { Date date = new Date(); Timestamp time = new Timestamp(date.getTime()); sql.closeConnection(); return time.before(getBanTime(p)); }else { sql.closeConnection(); return false; } } static Timestamp getBanTime(CustomPlayer p) throws SQLException { int id = p.getID(); MySQL sql = Core.returnSQL(); PreparedStatement st = sql.getConnection().prepareStatement("SELECT * FROM bans WHERE ID='" + id + "';"); ResultSet rs = st.executeQuery(); if(rs.next()) { Timestamp time = rs.getTimestamp("until"); sql.closeConnection(); return time; } sql.closeConnection(); return null; } /** * return reason for ban * @param p player * @return String * @throws SQLException SQL server not available or throwing error * @throws ClassNotFoundException class couldn't be found */ static String getBanReason(CustomPlayer p)throws SQLException, ClassNotFoundException { int id = p.getID(); MySQL sql = Core.returnSQL(); sql.openConnection(); PreparedStatement st = sql.getConnection().prepareStatement("SELECT * FROM bans WHERE ID='" + id + "';"); ResultSet rs = st.executeQuery(); if(rs.next()) { String reason = rs.getString("Reason"); sql.closeConnection(); return reason; }else { sql.closeConnection(); return null; } } /** * gets the name of the player who banned the player * @param p player * @return String * @throws SQLException SQL server not available or throwing error * @throws ClassNotFoundException class couldn't be found */ static String getWhoBanned(CustomPlayer p)throws SQLException, ClassNotFoundException { int id = p.getID(); MySQL sql = Core.returnSQL(); sql.openConnection(); ResultSet rs = Core.returnSQL().getConnection().createStatement().executeQuery("SELECT * FROM bans WHERE ID='" + id + "';"); if (!rs.next()) { sql.closeConnection(); return null; } String whouuid = rs.getString("who"); UUID who = UUID.fromString(whouuid); List list = new ArrayList<>(); list.add(who); NameFetcher name = new NameFetcher(list); sql.closeConnection(); return name.toString(); } }