DeluxeGroups/src/main/java/xyz/etztech/deluxegroups/Database.java

229 lines
8.3 KiB
Java

package xyz.etztech.deluxegroups;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.core.db.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Database extends DataSource {
public Database(JavaPlugin plugin) {
super(plugin);
create();
}
private void create() {
try {
Connection connection = getConnection();
final String CREATE1 = "CREATE TABLE IF NOT EXISTS `groups` (`id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(50) NOT NULL, `password` VARCHAR(50), `permanent` BOOLEAN NOT NULL);";
PreparedStatement pst = connection.prepareStatement(CREATE1);
pst.execute();
final String CREATE2 = "CREATE TABLE IF NOT EXISTS `players` (`id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `group_id` INT NOT NULL);";
pst = connection.prepareStatement(CREATE2);
pst.execute();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public int addGroup(DeluxeGroup group) {
int id = 0;
try {
Connection connection = getConnection();
final String GROUP = "INSERT INTO `groups` (`name`, `password`, `permanent`) VALUES (?, ?, FALSE);";
PreparedStatement pst = connection.prepareStatement(GROUP, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, group.getName());
pst.setString(2, group.getPassword());
pst.executeUpdate();
ResultSet keys = pst.getGeneratedKeys();
if (keys.next()) {
id = keys.getInt("id");
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return id;
}
public void removeGroup(int groupId) {
try {
Connection connection = getConnection();
final String REMOVE = "DELETE FROM `groups` WHERE `id` = ?;";
PreparedStatement pst = connection.prepareStatement(REMOVE);
pst.setInt(1, groupId);
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void updateGroup(DeluxeGroup group) {
try {
Connection connection = getConnection();
final String UPDATE = "UPDATE `groups` SET `name` = ?, `password` = ?, `permanent` = ? WHERE `id` = ?;";
PreparedStatement pst = connection.prepareStatement(UPDATE);
pst.setString(1, group.getName());
pst.setString(2, group.getPassword());
pst.setBoolean(3, group.getPermanent());
pst.setInt(4, group.getId());
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void addPlayer(String uuid, int groupId) {
try {
Connection connection = getConnection();
final String PLAYER = "INSERT INTO `players` (`uuid`, `group_id`) VALUES (?, ?);";
PreparedStatement pst = connection.prepareStatement(PLAYER);
pst.setString(1, uuid);
pst.setInt(2, groupId);
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void removePlayer(String uuid) {
try {
Connection connection = getConnection();
final String REMOVE = "DELETE FROM `players` WHERE `uuid` = ?;";
PreparedStatement pst = connection.prepareStatement(REMOVE);
pst.setString(1, uuid);
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public DeluxeGroup getGroup(String groupName) {
try {
Connection connection = getConnection();
final String GROUP = "SELECT `id`, `name`, `password`, `permanent` FROM `groups` WHERE LOWER(`name`) = LOWER(?);";
PreparedStatement pst = connection.prepareStatement(GROUP);
pst.setString(1, groupName);
ResultSet rs = pst.executeQuery();
DeluxeGroup group = getDeluxeGroup(rs);
connection.close();
return group;
} catch (Exception ex) {
ex.printStackTrace();
}
return new DeluxeGroup();
}
public DeluxeGroup getPlayerGroup(String uuid) {
try {
Connection connection = getConnection();
final String GROUP = "SELECT `id`, `name`, `password`, `permanent` FROM `groups` WHERE `id` = (SELECT `group_id` FROM `players` WHERE `uuid` = ?);";
PreparedStatement pst = connection.prepareStatement(GROUP);
pst.setString(1, uuid);
ResultSet rs = pst.executeQuery();
DeluxeGroup group = getDeluxeGroup(rs);
connection.close();
return group;
} catch (Exception ex) {
ex.printStackTrace();
}
return new DeluxeGroup();
}
public List<DeluxeGroup> getGroups() {
List<DeluxeGroup> groups = new ArrayList<>();
try {
Connection connection = getConnection();
final String GROUPS = "SELECT `id`, `name`, `password`, `permanent` FROM `groups`;";
PreparedStatement pst = connection.prepareStatement(GROUPS);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
DeluxeGroup group = new DeluxeGroup();
group.setId(rs.getInt("id"));
group.setName(rs.getString("name"));
group.setPassword(rs.getString("password"));
group.setPermanent(rs.getBoolean("permanent"));
groups.add(group);
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return groups;
}
public List<OfflinePlayer> getPlayers(int groupId) {
List<OfflinePlayer> players = new ArrayList<>();
try {
Connection connection = getConnection();
final String PLAYERS = "SELECT `uuid` FROM `players` WHERE `group_id` = ?;";
PreparedStatement pst = connection.prepareStatement(PLAYERS);
pst.setInt(1, groupId);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("uuid")));
players.add(player);
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return players;
}
public boolean inGroup(String uuid) {
boolean inGroup = false;
try {
Connection connection = getConnection();
final String INGROUP = "SELECT `uuid` FROM `players` WHERE `uuid` = ?;";
PreparedStatement pst = connection.prepareStatement(INGROUP);
pst.setString(1, uuid);
ResultSet rs = pst.executeQuery();
inGroup = rs.next();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return inGroup;
}
public boolean groupExists(String groupName) {
boolean exists = false;
try {
Connection connection = getConnection();
final String EXISTS = "SELECT `id` FROM `groups` WHERE LOWER(`name`) = LOWER(?);";
PreparedStatement pst = connection.prepareStatement(EXISTS);
pst.setString(1, groupName);
ResultSet rs = pst.executeQuery();
exists = rs.next();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return exists;
}
private DeluxeGroup getDeluxeGroup(ResultSet rs) throws SQLException {
if (rs.next()) {
DeluxeGroup group = new DeluxeGroup();
group.setId(rs.getInt("id"));
group.setName(rs.getString("name"));
group.setPassword(rs.getString("password"));
group.setPermanent(rs.getBoolean("permanent"));
group.setGroupList(new ArrayList<>(getPlayers(group.getId())));
return group;
}
return new DeluxeGroup();
}
}