mirror of
https://github.com/plexusorg/Plex.git
synced 2025-07-13 14:48:34 +00:00
Revert API
This commit is contained in:
131
src/main/java/dev/plex/cache/DataUtils.java
vendored
Normal file
131
src/main/java/dev/plex/cache/DataUtils.java
vendored
Normal file
@ -0,0 +1,131 @@
|
||||
package dev.plex.cache;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.cache.player.PlayerCache;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.storage.StorageType;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Parent cache class
|
||||
*/
|
||||
public class DataUtils
|
||||
{
|
||||
/**
|
||||
* Checks if the player has been on the server before
|
||||
*
|
||||
* @param uuid The unique ID of the player
|
||||
* @return true if the player is registered in the database
|
||||
*/
|
||||
public static boolean hasPlayedBefore(UUID uuid)
|
||||
{
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
return Plex.get().getMongoPlayerData().exists(uuid);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Plex.get().getSqlPlayerData().exists(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a player from cache or from the database
|
||||
*
|
||||
* @param uuid The unique ID of the player
|
||||
* @return a PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public static PlexPlayer getPlayer(UUID uuid)
|
||||
{
|
||||
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
||||
{
|
||||
return PlayerCache.getPlexPlayerMap().get(uuid);
|
||||
}
|
||||
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
return Plex.get().getMongoPlayerData().getByUUID(uuid);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Plex.get().getSqlPlayerData().getByUUID(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public static PlexPlayer getPlayer(String username)
|
||||
{
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
return Plex.get().getMongoPlayerData().getByName(username);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Plex.get().getSqlPlayerData().getByName(username);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a player from cache or from the database
|
||||
*
|
||||
* @param ip The IP address of the player.
|
||||
* @return a PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public static PlexPlayer getPlayerByIP(String ip)
|
||||
{
|
||||
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
|
||||
if (player != null)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
return Plex.get().getMongoPlayerData().getByIP(ip);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Plex.get().getSqlPlayerData().getByIP(ip);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a player's information in the database
|
||||
*
|
||||
* @param plexPlayer The PlexPlayer to update
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public static void update(PlexPlayer plexPlayer)
|
||||
{
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
Plex.get().getMongoPlayerData().update(plexPlayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
Plex.get().getSqlPlayerData().update(plexPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a player's information in the database
|
||||
*
|
||||
* @param plexPlayer The PlexPlayer to insert
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public static void insert(PlexPlayer plexPlayer)
|
||||
{
|
||||
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
||||
{
|
||||
Plex.get().getMongoPlayerData().save(plexPlayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
Plex.get().getSqlPlayerData().insert(plexPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
/* REDIS METHODS AT ONE POINT FOR BANS, AND JSON METHODS FOR PUNISHMENTS */
|
||||
|
||||
}
|
40
src/main/java/dev/plex/cache/notes/PlayerNotes.java
vendored
Normal file
40
src/main/java/dev/plex/cache/notes/PlayerNotes.java
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
package dev.plex.cache.notes;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerNotes
|
||||
{
|
||||
private final String SELECT = "SELECT * FROM `notes` WHERE uuid=?";
|
||||
//private final String UPDATE = "UPDATE `notes` SET name=?, written_by=?, note=? WHERE uuid=?";
|
||||
private final String INSERT = "INSERT INTO `notes` (`uuid`, `name`, `written_by`, `note`) VALUES (?, ?, ?, ?);";
|
||||
|
||||
public PlexPlayer getByUUID(UUID uuid)
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
PlexPlayer plexPlayer = new PlexPlayer(uuid);
|
||||
while (set.next())
|
||||
{
|
||||
String name = set.getString("name");
|
||||
String writtenBy = set.getString("written_by");
|
||||
String note = set.getString("note");
|
||||
|
||||
}
|
||||
return plexPlayer;
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
139
src/main/java/dev/plex/cache/player/MongoPlayerData.java
vendored
Normal file
139
src/main/java/dev/plex/cache/player/MongoPlayerData.java
vendored
Normal file
@ -0,0 +1,139 @@
|
||||
package dev.plex.cache.player;
|
||||
|
||||
import dev.morphia.Datastore;
|
||||
import dev.morphia.query.Query;
|
||||
import dev.morphia.query.Update;
|
||||
import dev.morphia.query.experimental.filters.Filters;
|
||||
import dev.morphia.query.experimental.updates.UpdateOperators;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Mongo fetching utilities for players
|
||||
*/
|
||||
public class MongoPlayerData
|
||||
{
|
||||
/**
|
||||
* The datastore object / database
|
||||
*/
|
||||
private final Datastore datastore;
|
||||
|
||||
/**
|
||||
* Creates an instance of the player data
|
||||
*/
|
||||
public MongoPlayerData()
|
||||
{
|
||||
this.datastore = Plex.get().getMongoConnection().getDatastore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the player exists in mongo's database
|
||||
*
|
||||
* @param uuid The unique ID of the player
|
||||
* @return true if the player was found
|
||||
*/
|
||||
public boolean exists(UUID uuid)
|
||||
{
|
||||
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
|
||||
.filter(Filters.eq("uuid", uuid));
|
||||
|
||||
return query.first() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player from cache or from mongo's database
|
||||
*
|
||||
* @param uuid The unique ID of the player
|
||||
* @return a PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public PlexPlayer getByUUID(UUID uuid)
|
||||
{
|
||||
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
||||
{
|
||||
return PlayerCache.getPlexPlayerMap().get(uuid);
|
||||
}
|
||||
|
||||
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid));
|
||||
return query2.first();
|
||||
}
|
||||
|
||||
public PlexPlayer getByName(String username)
|
||||
{
|
||||
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
|
||||
if (player != null)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
|
||||
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.regex("name").caseInsensitive().pattern(username));
|
||||
return query2.first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player from cache or from mongo's database
|
||||
*
|
||||
* @param ip The IP address of the player.
|
||||
* @return a PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public PlexPlayer getByIP(String ip)
|
||||
{
|
||||
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
|
||||
if (player != null)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
|
||||
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.in("ips", Collections.singleton(ip)));
|
||||
return query2.first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a player's information in the mongo database
|
||||
*
|
||||
* @param player The PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public void update(PlexPlayer player)
|
||||
{
|
||||
Query<PlexPlayer> filter = datastore.find(PlexPlayer.class)
|
||||
.filter(Filters.eq("uuid", player.getUuid()));
|
||||
|
||||
Update<PlexPlayer> updateOps = filter
|
||||
.update(
|
||||
UpdateOperators.set("name", player.getName()),
|
||||
UpdateOperators.set("loginMessage", player.getLoginMessage()),
|
||||
UpdateOperators.set("prefix", player.getPrefix()),
|
||||
UpdateOperators.set("vanished", player.isVanished()),
|
||||
UpdateOperators.set("commandSpy", player.isCommandSpy()),
|
||||
UpdateOperators.set("adminActive", player.isAdminActive()),
|
||||
UpdateOperators.set("rank", player.getRank().toLowerCase()),
|
||||
UpdateOperators.set("ips", player.getIps()),
|
||||
UpdateOperators.set("coins", player.getCoins()),
|
||||
UpdateOperators.set("punishments", player.getPunishments()),
|
||||
UpdateOperators.set("notes", player.getNotes()));
|
||||
|
||||
updateOps.execute();
|
||||
}
|
||||
|
||||
public List<PlexPlayer> getPlayers()
|
||||
{
|
||||
return datastore.find(PlexPlayer.class).stream().toList();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves the player's information in the database
|
||||
*
|
||||
* @param plexPlayer The PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public void save(PlexPlayer plexPlayer)
|
||||
{
|
||||
datastore.save(plexPlayer);
|
||||
}
|
||||
}
|
46
src/main/java/dev/plex/cache/player/PlayerCache.java
vendored
Normal file
46
src/main/java/dev/plex/cache/player/PlayerCache.java
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
package dev.plex.cache.player;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Cache storage
|
||||
*/
|
||||
|
||||
public class PlayerCache
|
||||
{
|
||||
/**
|
||||
* A key/value pair where the key is the unique ID of the Plex Player
|
||||
*/
|
||||
private static final Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
|
||||
|
||||
/**
|
||||
* A key/value pair where the key is the unique ID of the Punished Player
|
||||
*/
|
||||
// private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
|
||||
|
||||
// public static Map<UUID, PunishedPlayer> getPunishedPlayerMap()
|
||||
// {
|
||||
// return punishedPlayerMap;
|
||||
// }
|
||||
public static Map<UUID, PlexPlayer> getPlexPlayerMap()
|
||||
{
|
||||
return plexPlayerMap;
|
||||
}
|
||||
|
||||
/*public static PunishedPlayer getPunishedPlayer(UUID uuid)
|
||||
{
|
||||
if (!getPunishedPlayerMap().containsKey(uuid))
|
||||
{
|
||||
getPunishedPlayerMap().put(uuid, new PunishedPlayer(uuid));
|
||||
}
|
||||
return getPunishedPlayerMap().get(uuid);
|
||||
}
|
||||
*/
|
||||
public static PlexPlayer getPlexPlayer(UUID uuid)
|
||||
{
|
||||
return getPlexPlayerMap().get(uuid);
|
||||
}
|
||||
}
|
256
src/main/java/dev/plex/cache/player/SQLPlayerData.java
vendored
Normal file
256
src/main/java/dev/plex/cache/player/SQLPlayerData.java
vendored
Normal file
@ -0,0 +1,256 @@
|
||||
package dev.plex.cache.player;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* SQL fetching utilities for players
|
||||
*/
|
||||
public class SQLPlayerData
|
||||
{
|
||||
private final String SELECT = "SELECT * FROM `players` WHERE uuid=?";
|
||||
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, adminActive=?, ips=?, coins=?, vanished=?, commandspy=? WHERE uuid=?";
|
||||
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `adminActive`, `ips`, `coins`, `vanished`, `commandspy`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
|
||||
|
||||
/**
|
||||
* Checks if a player exists in the SQL database
|
||||
*
|
||||
* @param uuid The unique ID of the player
|
||||
* @return true if the player was found in the database
|
||||
*/
|
||||
public boolean exists(UUID uuid)
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
return set.next();
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player from cache or from the SQL database
|
||||
*
|
||||
* @param uuid The unique ID of the player
|
||||
* @return a PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public PlexPlayer getByUUID(UUID uuid)
|
||||
{
|
||||
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
||||
{
|
||||
return PlayerCache.getPlexPlayerMap().get(uuid);
|
||||
}
|
||||
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
PlexPlayer plexPlayer = new PlexPlayer(uuid);
|
||||
while (set.next())
|
||||
{
|
||||
String name = set.getString("name");
|
||||
String loginMSG = set.getString("login_msg");
|
||||
String prefix = set.getString("prefix");
|
||||
String rankName = set.getString("rank").toUpperCase();
|
||||
boolean adminActive = set.getBoolean("adminActive");
|
||||
long coins = set.getLong("coins");
|
||||
boolean vanished = set.getBoolean("vanished");
|
||||
boolean commandspy = set.getBoolean("commandspy");
|
||||
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
|
||||
{
|
||||
}.getType());
|
||||
plexPlayer.setName(name);
|
||||
plexPlayer.setLoginMessage(loginMSG);
|
||||
plexPlayer.setPrefix(prefix);
|
||||
plexPlayer.setRank(rankName);
|
||||
plexPlayer.setAdminActive(adminActive);
|
||||
plexPlayer.setIps(ips);
|
||||
plexPlayer.setCoins(coins);
|
||||
plexPlayer.setVanished(vanished);
|
||||
plexPlayer.setCommandSpy(commandspy);
|
||||
}
|
||||
return plexPlayer;
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public PlexPlayer getByName(String username)
|
||||
{
|
||||
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
|
||||
if (player != null)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE name=? LIMIT 1");
|
||||
statement.setString(1, username);
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next())
|
||||
{
|
||||
PlexPlayer plexPlayer = new PlexPlayer(UUID.fromString(set.getString("uuid")));
|
||||
String loginMSG = set.getString("login_msg");
|
||||
String prefix = set.getString("prefix");
|
||||
String rankName = set.getString("rank").toUpperCase();
|
||||
boolean adminActive = set.getBoolean("adminActive");
|
||||
long coins = set.getLong("coins");
|
||||
boolean vanished = set.getBoolean("vanished");
|
||||
boolean commandspy = set.getBoolean("commandspy");
|
||||
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
|
||||
{
|
||||
}.getType());
|
||||
plexPlayer.setName(username);
|
||||
plexPlayer.setLoginMessage(loginMSG);
|
||||
plexPlayer.setPrefix(prefix);
|
||||
plexPlayer.setRank(rankName);
|
||||
plexPlayer.setAdminActive(adminActive);
|
||||
plexPlayer.setIps(ips);
|
||||
plexPlayer.setCoins(coins);
|
||||
plexPlayer.setVanished(vanished);
|
||||
plexPlayer.setCommandSpy(commandspy);
|
||||
return plexPlayer;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player from cache or from the SQL database
|
||||
*
|
||||
* @param ip The IP address of the player.
|
||||
* @return a PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public PlexPlayer getByIP(String ip)
|
||||
{
|
||||
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
|
||||
if (player != null)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement("select * from `players` where json_search(ips, ?, ?) IS NOT NULL LIMIT 1");
|
||||
statement.setString(1, "one");
|
||||
statement.setString(2, ip);
|
||||
ResultSet set = statement.executeQuery();
|
||||
|
||||
PlexPlayer plexPlayer = null;
|
||||
while (set.next())
|
||||
{
|
||||
String uuid = set.getString("uuid");
|
||||
String name = set.getString("name");
|
||||
String loginMSG = set.getString("login_msg");
|
||||
String prefix = set.getString("prefix");
|
||||
String rankName = set.getString("rank").toUpperCase();
|
||||
boolean adminActive = set.getBoolean("adminActive");
|
||||
long coins = set.getLong("coins");
|
||||
boolean vanished = set.getBoolean("vanished");
|
||||
boolean commandspy = set.getBoolean("commandspy");
|
||||
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
|
||||
{
|
||||
}.getType());
|
||||
plexPlayer = new PlexPlayer(UUID.fromString(uuid));
|
||||
plexPlayer.setName(name);
|
||||
plexPlayer.setLoginMessage(loginMSG);
|
||||
plexPlayer.setPrefix(prefix);
|
||||
plexPlayer.setRank(rankName);
|
||||
plexPlayer.setAdminActive(adminActive);
|
||||
plexPlayer.setIps(ips);
|
||||
plexPlayer.setCoins(coins);
|
||||
plexPlayer.setVanished(vanished);
|
||||
plexPlayer.setCommandSpy(commandspy);
|
||||
}
|
||||
return plexPlayer;
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a player's information in the SQL database
|
||||
*
|
||||
* @param player The PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public void update(PlexPlayer player)
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(UPDATE);
|
||||
statement.setString(1, player.getName());
|
||||
statement.setString(2, player.getLoginMessage());
|
||||
statement.setString(3, player.getPrefix());
|
||||
statement.setString(4, player.getRank().toLowerCase());
|
||||
statement.setBoolean(5, player.isAdminActive());
|
||||
statement.setString(6, new Gson().toJson(player.getIps()));
|
||||
statement.setLong(7, player.getCoins());
|
||||
statement.setBoolean(8, player.isVanished());
|
||||
statement.setBoolean(9, player.isCommandSpy());
|
||||
statement.setString(10, player.getUuid().toString());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the player's information in the database
|
||||
*
|
||||
* @param player The PlexPlayer object
|
||||
* @see PlexPlayer
|
||||
*/
|
||||
public void insert(PlexPlayer player)
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(INSERT);
|
||||
statement.setString(1, player.getUuid().toString());
|
||||
statement.setString(2, player.getName());
|
||||
statement.setString(3, player.getLoginMessage());
|
||||
statement.setString(4, player.getPrefix());
|
||||
statement.setString(5, player.getRank().toLowerCase());
|
||||
statement.setBoolean(6, player.isAdminActive());
|
||||
statement.setString(7, new Gson().toJson(player.getIps()));
|
||||
statement.setLong(8, player.getCoins());
|
||||
statement.setBoolean(9, player.isVanished());
|
||||
statement.setBoolean(10, player.isCommandSpy());
|
||||
statement.execute();
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
97
src/main/java/dev/plex/cache/sql/SQLNotes.java
vendored
Normal file
97
src/main/java/dev/plex/cache/sql/SQLNotes.java
vendored
Normal file
@ -0,0 +1,97 @@
|
||||
package dev.plex.cache.sql;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.punishment.extra.Note;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SQLNotes
|
||||
{
|
||||
private static final String SELECT = "SELECT * FROM `notes` WHERE uuid=?";
|
||||
private static final String INSERT = "INSERT INTO `notes` (`id`, `uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?, ?)";
|
||||
private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND id=?";
|
||||
|
||||
public CompletableFuture<List<Note>> getNotes(UUID uuid)
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
List<Note> notes = Lists.newArrayList();
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next())
|
||||
{
|
||||
Note note = new Note(
|
||||
uuid,
|
||||
set.getString("note"),
|
||||
UUID.fromString(set.getString("written_by")),
|
||||
LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("timestamp")), ZoneId.systemDefault())
|
||||
);
|
||||
note.setId(set.getInt("id"));
|
||||
notes.add(note);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return notes;
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> deleteNote(int id, UUID uuid)
|
||||
{
|
||||
return CompletableFuture.runAsync(() ->
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(DELETE);
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setInt(2, id);
|
||||
statement.execute();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> addNote(Note note)
|
||||
{
|
||||
return CompletableFuture.runAsync(() ->
|
||||
{
|
||||
getNotes(note.getUuid()).whenComplete((notes, throwable) ->
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(INSERT);
|
||||
statement.setInt(1, notes.size() + 1);
|
||||
statement.setString(2, note.getUuid().toString());
|
||||
statement.setString(3, note.getWrittenBy().toString());
|
||||
statement.setString(4, note.getNote());
|
||||
statement.setLong(5, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli());
|
||||
statement.execute();
|
||||
note.setId(notes.size());
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
133
src/main/java/dev/plex/cache/sql/SQLPunishment.java
vendored
Normal file
133
src/main/java/dev/plex/cache/sql/SQLPunishment.java
vendored
Normal file
@ -0,0 +1,133 @@
|
||||
package dev.plex.cache.sql;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.punishment.Punishment;
|
||||
import dev.plex.punishment.PunishmentType;
|
||||
import dev.plex.util.PlexLog;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SQLPunishment
|
||||
{
|
||||
private static final String SELECT = "SELECT * FROM `punishments` WHERE punished=?";
|
||||
private static final String SELECT_BY = "SELECT * FROM `punishments` WHERE punisher=?";
|
||||
|
||||
private static final String INSERT = "INSERT INTO `punishments` (`punished`, `punisher`, `punishedUsername`, `ip`, `type`, `reason`, `customTime`, `active`, `endDate`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||
private static final String UPDATE_BAN = "UPDATE `punishments` SET active=? WHERE active=? AND punished=? AND type=?";
|
||||
|
||||
public CompletableFuture<List<Punishment>> getPunishments()
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
List<Punishment> punishments = Lists.newArrayList();
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement("SELECT * FROM `punishments`");
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next())
|
||||
{
|
||||
Punishment punishment = new Punishment(UUID.fromString(set.getString("punished")), UUID.fromString(set.getString("punisher")));
|
||||
punishment.setActive(set.getBoolean("active"));
|
||||
punishment.setType(PunishmentType.valueOf(set.getString("type")));
|
||||
punishment.setCustomTime(set.getBoolean("customTime"));
|
||||
punishment.setPunishedUsername(set.getString("punishedUsername"));
|
||||
punishment.setEndDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("endDate")), ZoneId.systemDefault()));
|
||||
punishment.setReason(set.getString("reason"));
|
||||
punishment.setIp(set.getString("ip"));
|
||||
punishments.add(punishment);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return punishments;
|
||||
});
|
||||
}
|
||||
|
||||
public List<Punishment> getPunishments(UUID uuid)
|
||||
{
|
||||
List<Punishment> punishments = Lists.newArrayList();
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||
statement.setString(1, uuid.toString());
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next())
|
||||
{
|
||||
Punishment punishment = new Punishment(UUID.fromString(set.getString("punished")), set.getString("punisher") == null ? null : UUID.fromString(set.getString("punisher")));
|
||||
punishment.setActive(set.getBoolean("active"));
|
||||
punishment.setType(PunishmentType.valueOf(set.getString("type")));
|
||||
punishment.setCustomTime(set.getBoolean("customTime"));
|
||||
punishment.setPunishedUsername(set.getString("punishedUsername"));
|
||||
punishment.setEndDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("endDate")), ZoneId.systemDefault()));
|
||||
punishment.setReason(set.getString("reason"));
|
||||
punishment.setIp(set.getString("ip"));
|
||||
punishments.add(punishment);
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return punishments;
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> insertPunishment(Punishment punishment)
|
||||
{
|
||||
|
||||
return CompletableFuture.runAsync(() ->
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PlexLog.debug("Running execute punishment on " + punishment.getPunished().toString());
|
||||
PreparedStatement statement = con.prepareStatement(INSERT);
|
||||
statement.setString(1, punishment.getPunished().toString());
|
||||
statement.setString(2, punishment.getPunisher() == null ? null : punishment.getPunisher().toString());
|
||||
statement.setString(3, punishment.getPunishedUsername());
|
||||
statement.setString(4, punishment.getIp());
|
||||
statement.setString(5, punishment.getType().name());
|
||||
statement.setString(6, punishment.getReason());
|
||||
statement.setBoolean(7, punishment.isCustomTime());
|
||||
statement.setBoolean(8, punishment.isActive());
|
||||
statement.setLong(9, punishment.getEndDate().toInstant(ZoneOffset.UTC).toEpochMilli());
|
||||
PlexLog.debug("Executing punishment");
|
||||
statement.execute();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> removeBan(UUID uuid)
|
||||
{
|
||||
return CompletableFuture.runAsync(() ->
|
||||
{
|
||||
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||
{
|
||||
PreparedStatement statement = con.prepareStatement(UPDATE_BAN);
|
||||
statement.setBoolean(1, false);
|
||||
statement.setBoolean(2, true);
|
||||
statement.setString(3, uuid.toString());
|
||||
statement.setString(4, PunishmentType.BAN.name());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user