Core/src/eu/univento/core/api/player/PlayerSettings.java

144 lines
4.0 KiB
Java

package eu.univento.core.api.player;
import eu.univento.core.Core;
import eu.univento.core.api.database.MySQL;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
* server-wide settings
* @author joethei
* @version 0.1
*/
public class PlayerSettings {
/**
* returns if setting is set
* @param what what setting is set
* @param p CustomPlayer
* @return boolean
*/
static boolean isSet(String what, CustomPlayer p) {
int id = p.getID();
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement st = sql.getConnection().prepareStatement("SELECT * FROM PlayerSettings WHERE ID = '" + id + "';");
ResultSet rs = st.executeQuery();
if (rs.next()) {
boolean is = rs.getInt(what) == 1;
sql.closeConnection();
return is;
}
sql.closeConnection();
return false;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
/**
* sets setting
* @param p CustomPlayer
* @param bool boolean to set to
* @param what what to set
* @throws ClassNotFoundException class could not be found
* @throws SQLException SQL ServerPinger not available or throwing error
*/
static void set(boolean bool, String what, CustomPlayer p) throws SQLException, ClassNotFoundException {
int id = p.getID();
MySQL sql = Core.returnSQL();
sql.openConnection();
if (bool) {
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE PlayerSettings SET " + what + " = '1' WHERE ID = '" + id + "';");
st.execute();
}else {
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE PlayerSettings SET " + what + " = '0' WHERE ID = '" + id + "';");
st.execute();
}
sql.closeConnection();
}
/**
* gets setting from player
* @param what what to get
* @param p CustomPlayer
* @return String
*/
static String get(String what, CustomPlayer p) {
int id = p.getID();
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement st = sql.getConnection().prepareStatement("SELECT " + what + " FROM PlayerSettings WHERE ID='" + id + "';");
ResultSet rs = st.executeQuery();
if(rs.next()) {
String rt = rs.getString(what);
sql.closeConnection();
return rt;
}
return null;
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* sets settings for player
* @param set value to set
* @param what what to set
* @param p CustomPlayer
*/
static void set(String set, String what, CustomPlayer p) {
int id = p.getID();
MySQL sql = Core.returnSQL();
try {
sql.openConnection();
PreparedStatement st = sql.getConnection().prepareStatement("UPDATE PlayerSettings SET " + what + " = '" + set + "' WHERE ID = '" + id + "';");
st.execute();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* changes settings
* @param what what to set
* @param p CustomPlayer
*/
static void change(String what, CustomPlayer p) {
if(isSet(what, p)) {
try {
set(false, what, p);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}else {
try {
set(true, what, p);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
public static ArrayList<CustomPlayer> getAllPlayersWithEffectsEnabled() {
ArrayList<CustomPlayer> list = new ArrayList<>();
for(Player players : Bukkit.getOnlinePlayers()) {
CustomPlayer p = CustomPlayer.getPlayer(players);
if(isSet("effects", p)) {
list.add(p);
}
}
return list;
}
}