122 lines
4.2 KiB
Java
122 lines
4.2 KiB
Java
package eu.univento.core.api.player;
|
|
|
|
import eu.univento.core.Core;
|
|
import eu.univento.core.api.MySQL;
|
|
import eu.univento.core.api.utils.NameFetcher;
|
|
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import org.bukkit.OfflinePlayer;
|
|
import org.bukkit.entity.Player;
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public static void setBanned(Player p, String reason, Player who, int time)throws SQLException, ClassNotFoundException {
|
|
MySQL sql = Core.returnSQL();
|
|
sql.openConnection();
|
|
String uuid = p.getUniqueId().toString();
|
|
String whouuid = who.getUniqueId().toString();
|
|
|
|
Date date = new Date();
|
|
date.setTime(date.getTime() + (time + 24));
|
|
|
|
Core.returnSQL().getConnection().createStatement().execute("INSERT INTO bans (player_uuid, Reason, who, time) VALUES ('" + uuid + "', '" + reason + "', '" + whouuid + "', '" + date.getTime() + "');");
|
|
p.kickPlayer("§cDu wurdest gebannt. §6Grund: " + reason + "\nDauer: " + time);
|
|
who.sendMessage("§cDu hast den Spieler " + p.getName() + " für " + time + " Tage gebannt");
|
|
sql.closeConnection();
|
|
}
|
|
|
|
/**
|
|
* unbans a player
|
|
* @param p player
|
|
* @throws SQLException SQL server not available or throwing error
|
|
* @throws ClassNotFoundException class couldn't be found
|
|
*/
|
|
public static void unban(OfflinePlayer p)throws SQLException, ClassNotFoundException{
|
|
MySQL sql = Core.returnSQL();
|
|
sql.openConnection();
|
|
String uuid = p.getUniqueId().toString();
|
|
Core.returnSQL().getConnection().createStatement().executeUpdate("DELETE FROM bans WHERE player_uuid ='" + uuid + "';");
|
|
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
|
|
*/
|
|
public static boolean isBanned(OfflinePlayer p)throws SQLException, ClassNotFoundException {
|
|
MySQL sql = Core.returnSQL();
|
|
sql.openConnection();
|
|
String uuid = p.getUniqueId().toString();
|
|
ResultSet rs = Core.returnSQL().getConnection().createStatement().executeQuery("SELECT * FROM bans WHERE player_uuid='" + uuid + "';");
|
|
sql.closeConnection();
|
|
return rs.next();
|
|
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public static String getBanReason(OfflinePlayer p)throws SQLException, ClassNotFoundException {
|
|
MySQL sql = Core.returnSQL();
|
|
sql.openConnection();
|
|
String uuid = p.getUniqueId().toString();
|
|
ResultSet rs = Core.returnSQL().getConnection().createStatement().executeQuery("SELECT * FROM bans WHERE player_uuid='" + uuid + "';");
|
|
if (!rs.next()) {
|
|
sql.closeConnection();
|
|
return null;
|
|
}
|
|
sql.closeConnection();
|
|
return rs.getString("Reason");
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public static String getWhoBanned(OfflinePlayer p)throws SQLException, ClassNotFoundException {
|
|
MySQL sql = Core.returnSQL();
|
|
sql.openConnection();
|
|
String uuid = p.getUniqueId().toString();
|
|
ResultSet rs = Core.returnSQL().getConnection().createStatement().executeQuery("SELECT * FROM bans WHERE player_uuid='" + uuid + "';");
|
|
if (!rs.next()) {
|
|
sql.closeConnection();
|
|
return null;
|
|
}
|
|
String whouuid = rs.getString("who");
|
|
UUID who = UUID.fromString(whouuid);
|
|
List<UUID> list = new ArrayList<UUID>();
|
|
list.add(who);
|
|
NameFetcher name = new NameFetcher(list);
|
|
sql.closeConnection();
|
|
return name.toString();
|
|
}
|
|
} |