From 8f84583e4e91b0235ef055dc40e6f581e0c8530c Mon Sep 17 00:00:00 2001 From: Paldiu Date: Mon, 30 Jan 2023 21:16:50 -0600 Subject: [PATCH] 75% completion --- .../unraveled/reforged/command/HaltCMD.java | 4 + .../unraveled/reforged/command/JailCMD.java | 4 + .../unraveled/reforged/command/LockCMD.java | 4 + .../reforged/command/base/PlayerDataCMD.java | 196 ++++++++++++++++++ .../mc/unraveled/reforged/config/Yaml.java | 14 ++ .../reforged/config/YamlManager.java | 8 +- .../reforged/data/CustomLoginData.java | 11 - .../unraveled/reforged/data/DataManager.java | 8 + .../mc/unraveled/reforged/data/LoginInfo.java | 14 ++ .../unraveled/reforged/data/LoginManager.java | 31 ++- .../mc/unraveled/reforged/economy/Coin.java | 25 --- .../reforged/economy/EconomyManager.java | 3 - .../listening/PlayerDataListener.java | 1 - .../unraveled/reforged/plugin/Traverse.java | 30 +++ .../mc/unraveled/reforged/storage/DBUser.java | 103 ++++++++- src/main/resources/config.yml | 0 16 files changed, 401 insertions(+), 55 deletions(-) create mode 100644 src/main/java/mc/unraveled/reforged/command/HaltCMD.java create mode 100644 src/main/java/mc/unraveled/reforged/command/JailCMD.java create mode 100644 src/main/java/mc/unraveled/reforged/command/LockCMD.java create mode 100644 src/main/java/mc/unraveled/reforged/command/base/PlayerDataCMD.java delete mode 100644 src/main/java/mc/unraveled/reforged/data/CustomLoginData.java create mode 100644 src/main/java/mc/unraveled/reforged/data/LoginInfo.java delete mode 100644 src/main/java/mc/unraveled/reforged/economy/Coin.java create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/mc/unraveled/reforged/command/HaltCMD.java b/src/main/java/mc/unraveled/reforged/command/HaltCMD.java new file mode 100644 index 0000000..dd526b1 --- /dev/null +++ b/src/main/java/mc/unraveled/reforged/command/HaltCMD.java @@ -0,0 +1,4 @@ +package mc.unraveled.reforged.command; + +public class HaltCMD { +} diff --git a/src/main/java/mc/unraveled/reforged/command/JailCMD.java b/src/main/java/mc/unraveled/reforged/command/JailCMD.java new file mode 100644 index 0000000..b55cbeb --- /dev/null +++ b/src/main/java/mc/unraveled/reforged/command/JailCMD.java @@ -0,0 +1,4 @@ +package mc.unraveled.reforged.command; + +public class JailCMD { +} diff --git a/src/main/java/mc/unraveled/reforged/command/LockCMD.java b/src/main/java/mc/unraveled/reforged/command/LockCMD.java new file mode 100644 index 0000000..e37200f --- /dev/null +++ b/src/main/java/mc/unraveled/reforged/command/LockCMD.java @@ -0,0 +1,4 @@ +package mc.unraveled.reforged.command; + +public class LockCMD { +} diff --git a/src/main/java/mc/unraveled/reforged/command/base/PlayerDataCMD.java b/src/main/java/mc/unraveled/reforged/command/base/PlayerDataCMD.java new file mode 100644 index 0000000..7af4860 --- /dev/null +++ b/src/main/java/mc/unraveled/reforged/command/base/PlayerDataCMD.java @@ -0,0 +1,196 @@ +package mc.unraveled.reforged.command.base; + +import mc.unraveled.reforged.api.annotations.CommandInfo; +import mc.unraveled.reforged.data.InfractionData; +import mc.unraveled.reforged.data.PlayerData; +import mc.unraveled.reforged.permission.Rank; +import mc.unraveled.reforged.plugin.Traverse; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +@CommandInfo( + name = "playerdata", + description = "Checks your player data. Also can be used to check and modify other players' data.", + usage = "/ [ | set ]", + aliases = {"pd", "userdata", "ud"} +) +public class PlayerDataCMD extends AbstractCommandBase { + public PlayerDataCMD(Traverse plugin) { + super(plugin, "playerdata"); + } + + @Override + public Component cmd(CommandSender sender, String[] args) { + switch (args.length) { + case 0: + if (sender instanceof Player player) { + PlayerData data = getPlugin().getDataManager().getPlayerData(player.getUniqueId()); + + if (data == null) { + return Component.text("Player data not found!").color(NamedTextColor.RED); + } + + String newLine = "\n"; + + String sb = "Player Data:" + newLine + + "UUID: " + data.getUuid() + newLine + + "Username: " + data.getUserName() + newLine + + "Rank: " + data.getRank() + newLine + + "Play Time: " + data.getPlaytime() + newLine + + "Balance: " + data.getCoins() + newLine + + "Infractions: " + data.getInfractionData().getInfractions() + newLine + + "Last Login: " + data.getLastLogin() + newLine + + "Login Message: " + data.getLoginMessage(); + + return Component.text(sb); + } else return Component.text("This command can only be used by players!"); + + case 2: + if (args[1].equalsIgnoreCase("info")) { + Player target = getPlugin().getServer().getPlayer(args[0]); + if (target == null) return MessageDefaults.MSG_NOT_FOUND; + + PlayerData data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) { + return Component.text("Player data not found!").color(NamedTextColor.RED); + } + + String newLine = "\n"; + + String sb = "Player Data:" + newLine + + "UUID: " + data.getUuid() + newLine + + "Username: " + data.getUserName() + newLine + + "Rank: " + data.getRank() + newLine + + "Play Time: " + data.getPlaytime() + newLine + + "Balance: " + data.getCoins() + newLine + + "Infractions: " + data.getInfractionData().getInfractions() + newLine + + "Last Login: " + data.getLastLogin() + newLine + + "Login Message: " + data.getLoginMessage(); + + return Component.text(sb); + } else { + return usage(); + } + case 3: + if (args[1].equalsIgnoreCase("reset")) { + Player target = getPlugin().getServer().getPlayer(args[0]); + if (target == null) return MessageDefaults.MSG_NOT_FOUND; + + switch (args[2]) { + case "rank": + PlayerData data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + data.setRank(Rank.NON_OP); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Reset " + target.getName() + "'s rank to " + Rank.NON_OP); + case "playtime": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + data.setPlaytime(0); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Reset " + target.getName() + "'s playtime to 0"); + case "balance": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + data.setCoins(0); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Reset " + target.getName() + "'s balance to 0"); + case "infractions": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + InfractionData inf = data.getInfractionData(); + while (inf.getInfractions() > 0) { + inf.decrement(); + } + + inf.setFrozen(false); + inf.setJailed(false); + inf.setMuted(false); + inf.setLocked(false); + + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Reset " + target.getName() + "'s infractions to 0"); + case "login": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + data.setLoginMessage(""); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Reset " + target.getName() + "'s login message to empty"); + default: + return Component.text("Invalid data type!").color(NamedTextColor.RED); + } + } + case 4: + Player target = getPlugin().getServer().getPlayer(args[0]); + if (target == null) return MessageDefaults.MSG_NOT_FOUND; + + if (args[1].equalsIgnoreCase("set")) { + switch (args[2]) { + case "rank": + PlayerData data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + Rank rank = Rank.valueOf(args[3].toUpperCase()); + data.setRank(rank); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Set " + target.getName() + "'s rank to " + rank); + case "playtime": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + int playtime = Integer.parseInt(args[3]); + data.setPlaytime(playtime); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Set " + target.getName() + "'s playtime to " + playtime); + case "balance": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + int balance = Integer.parseInt(args[3]); + data.setCoins(balance); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Set " + target.getName() + "'s balance to " + balance); + case "infractions": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + InfractionData inf = data.getInfractionData(); + int infractions = Integer.parseInt(args[3]); + while (inf.getInfractions() > infractions) { + inf.decrement(); + } + + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Set " + target.getName() + "'s infractions to " + infractions); + case "login": + data = getPlugin().getDataManager().getPlayerData(target.getUniqueId()); + if (data == null) return MessageDefaults.MSG_NOT_FOUND; + + String login = args[3]; + data.setLoginMessage(login); + getPlugin().getDataManager().updatePlayer(target.getUniqueId(), data); + return Component.text("Set " + target.getName() + "'s login message to " + login); + default: + return Component.text("Invalid data type!").color(NamedTextColor.RED); + } + } else { + return usage(); + } + default: + return usage(); + } + } + + private Component usage() { + return Component + .text("/ [ | set ]") + .color(NamedTextColor.GRAY); + } +} diff --git a/src/main/java/mc/unraveled/reforged/config/Yaml.java b/src/main/java/mc/unraveled/reforged/config/Yaml.java index b376af6..d578983 100644 --- a/src/main/java/mc/unraveled/reforged/config/Yaml.java +++ b/src/main/java/mc/unraveled/reforged/config/Yaml.java @@ -72,4 +72,18 @@ public abstract class Yaml extends YamlConfiguration implements Baker { getPlugin().getLogger().warning("Failed to unbake " + getFileName()); } } + + @SneakyThrows + public void saveToFile() { + unbake(); + super.save(yamlFile); + bake(); + } + + @SneakyThrows + public void loadFromFile() { + unbake(); + super.load(yamlFile); + bake(); + } } diff --git a/src/main/java/mc/unraveled/reforged/config/YamlManager.java b/src/main/java/mc/unraveled/reforged/config/YamlManager.java index ddcc1ba..c8e3969 100644 --- a/src/main/java/mc/unraveled/reforged/config/YamlManager.java +++ b/src/main/java/mc/unraveled/reforged/config/YamlManager.java @@ -63,9 +63,10 @@ public class YamlManager { } }); } - public static class Builder { + public static class Builder { private final Traverse plugin; + private String fileName; private File dataFolder; private boolean copyDefaults; @@ -76,6 +77,8 @@ public class YamlManager { public Builder fileName(String fileName) { this.fileName = fileName; + this.dataFolder = plugin.getDataFolder(); + this.copyDefaults = false; return this; } @@ -90,7 +93,8 @@ public class YamlManager { } public Yaml build() { - return new Yaml(plugin, fileName, dataFolder, copyDefaults){}; + return new Yaml(plugin, fileName, dataFolder, copyDefaults) { + }; } } } diff --git a/src/main/java/mc/unraveled/reforged/data/CustomLoginData.java b/src/main/java/mc/unraveled/reforged/data/CustomLoginData.java deleted file mode 100644 index 96a31ca..0000000 --- a/src/main/java/mc/unraveled/reforged/data/CustomLoginData.java +++ /dev/null @@ -1,11 +0,0 @@ -package mc.unraveled.reforged.data; - -import lombok.Data; -import net.kyori.adventure.text.Component; -import org.bukkit.entity.Player; - -@Data -public class CustomLoginData { - private final Player player; - private final Component loginMessage; -} diff --git a/src/main/java/mc/unraveled/reforged/data/DataManager.java b/src/main/java/mc/unraveled/reforged/data/DataManager.java index da73472..47148a2 100644 --- a/src/main/java/mc/unraveled/reforged/data/DataManager.java +++ b/src/main/java/mc/unraveled/reforged/data/DataManager.java @@ -5,6 +5,7 @@ import mc.unraveled.reforged.api.Baker; import mc.unraveled.reforged.api.Locker; import mc.unraveled.reforged.plugin.Traverse; import mc.unraveled.reforged.storage.DBUser; +import org.bukkit.entity.Player; import org.jetbrains.annotations.Nullable; import java.util.HashSet; @@ -42,6 +43,13 @@ public final class DataManager implements Baker, Locker { .orElse(null); } + public void updatePlayer(UUID uuid, PlayerData data) { + if (baked) throw new IllegalStateException("Cannot update player data while the data manager is baked."); + + playerDataCache.removeIf(playerData -> playerData.getUuid().equals(uuid)); + playerDataCache.add(data); + } + public void addPlayerData(PlayerData data) { if (baked) throw new IllegalStateException("Cannot add player data while the data manager is baked."); diff --git a/src/main/java/mc/unraveled/reforged/data/LoginInfo.java b/src/main/java/mc/unraveled/reforged/data/LoginInfo.java new file mode 100644 index 0000000..0ad2249 --- /dev/null +++ b/src/main/java/mc/unraveled/reforged/data/LoginInfo.java @@ -0,0 +1,14 @@ +package mc.unraveled.reforged.data; + +import lombok.Data; +import lombok.NoArgsConstructor; +import net.kyori.adventure.text.Component; + +/** + * Login Message Information Bean + */ +@Data +@NoArgsConstructor +public class LoginInfo { + private Component loginMessage; +} diff --git a/src/main/java/mc/unraveled/reforged/data/LoginManager.java b/src/main/java/mc/unraveled/reforged/data/LoginManager.java index 04b03ff..170c81a 100644 --- a/src/main/java/mc/unraveled/reforged/data/LoginManager.java +++ b/src/main/java/mc/unraveled/reforged/data/LoginManager.java @@ -6,8 +6,11 @@ import mc.unraveled.reforged.listening.AbstractListener; import mc.unraveled.reforged.plugin.Traverse; import mc.unraveled.reforged.storage.DBUser; import mc.unraveled.reforged.util.Pair; +import net.kyori.adventure.text.Component; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerLoginEvent; import org.jetbrains.annotations.Nullable; @@ -18,25 +21,31 @@ import java.util.stream.Collectors; @Getter public class LoginManager implements Baker { private final Traverse plugin; - private Set> dataSet = new HashSet<>(); // VALUE ONLY MODIFIED BY BAKER + private Set> dataSet = new HashSet<>(); // VALUE ONLY MODIFIED BY BAKER private boolean baked = false; public LoginManager(Traverse plugin) { this.plugin = plugin; + DBUser user = new DBUser(plugin.getSQLManager().establish()); + user.getLoginMessages().forEach((oPlayer, info) -> dataSet.add(new Pair<>(oPlayer, info))); + user.close(); + + bake(); + new LoginListener(plugin); } - public void add(Player player, CustomLoginData data) { + public void add(OfflinePlayer player, LoginInfo data) { dataSet.add(new Pair<>(player, data)); } - public void remove(Player player) { + public void remove(OfflinePlayer player) { dataSet.removeIf(pair -> pair.getFirst().equals(player)); } @Nullable - public CustomLoginData get(Player player) { + public LoginInfo get(OfflinePlayer player) { return dataSet.stream() .filter(pair -> pair.getFirst().equals(player)) .map(Pair::getSecond) @@ -44,7 +53,7 @@ public class LoginManager implements Baker { .orElse(null); } - public void set(Player player, CustomLoginData data) { + public void set(OfflinePlayer player, LoginInfo data) { remove(player); add(player, data); } @@ -54,8 +63,8 @@ public class LoginManager implements Baker { if (baked) return; dataSet.forEach(pair -> { - Player player = pair.getFirst(); - CustomLoginData data = pair.getSecond(); + OfflinePlayer player = pair.getFirst(); + LoginInfo data = pair.getSecond(); DBUser user = new DBUser(plugin.getSQLManager().establish()); user.setLoginMessage(player.getUniqueId().toString(), data.getLoginMessage().toString()); @@ -74,14 +83,18 @@ public class LoginManager implements Baker { this.baked = false; } - private final class LoginListener extends AbstractListener { + private static final class LoginListener extends AbstractListener { public LoginListener(Traverse plugin) { super(plugin); } @EventHandler - public void onLogin(PlayerLoginEvent event) { + public void onLogin(PlayerJoinEvent event) { Player player = event.getPlayer(); + DBUser user = new DBUser(getPlugin().getSQLManager().establish()); + LoginInfo info = user.getLoginInfo(player.getUniqueId().toString()); + user.close(); + event.joinMessage(info.getLoginMessage()); } } } diff --git a/src/main/java/mc/unraveled/reforged/economy/Coin.java b/src/main/java/mc/unraveled/reforged/economy/Coin.java deleted file mode 100644 index 0fad0ac..0000000 --- a/src/main/java/mc/unraveled/reforged/economy/Coin.java +++ /dev/null @@ -1,25 +0,0 @@ -package mc.unraveled.reforged.economy; - -public class Coin { - private final String name; - private final String symbol; - private final double value; - - public Coin(String name, String symbol, double value) { - this.name = name; - this.symbol = symbol; - this.value = value; - } - - public String getName() { - return name; - } - - public String getSymbol() { - return symbol; - } - - public double getValue() { - return value; - } -} diff --git a/src/main/java/mc/unraveled/reforged/economy/EconomyManager.java b/src/main/java/mc/unraveled/reforged/economy/EconomyManager.java index 0a99de1..1e14b29 100644 --- a/src/main/java/mc/unraveled/reforged/economy/EconomyManager.java +++ b/src/main/java/mc/unraveled/reforged/economy/EconomyManager.java @@ -19,7 +19,6 @@ public class EconomyManager { private final RegisteredServiceProvider rsp; private final Map> requests = new HashMap<>(); // This is NOT persistent. private final Economy economy; - private final Coin coin; public EconomyManager(@NotNull Traverse plugin) { if (plugin.getServer().getPluginManager().getPlugin("Vault") == null) @@ -30,8 +29,6 @@ public class EconomyManager { if (rsp == null) throw new MissingResourceException("Economy not found", Economy.class.getName(), "Economy"); economy = rsp.getProvider(); - - coin = new Coin("Sheccies", "$", 1); } public EconomyResponse newAccount(OfflinePlayer player) { diff --git a/src/main/java/mc/unraveled/reforged/listening/PlayerDataListener.java b/src/main/java/mc/unraveled/reforged/listening/PlayerDataListener.java index b019b05..015b1e7 100644 --- a/src/main/java/mc/unraveled/reforged/listening/PlayerDataListener.java +++ b/src/main/java/mc/unraveled/reforged/listening/PlayerDataListener.java @@ -6,7 +6,6 @@ import mc.unraveled.reforged.permission.Rank; import mc.unraveled.reforged.plugin.Traverse; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import java.time.Instant; diff --git a/src/main/java/mc/unraveled/reforged/plugin/Traverse.java b/src/main/java/mc/unraveled/reforged/plugin/Traverse.java index e34219c..6797b00 100644 --- a/src/main/java/mc/unraveled/reforged/plugin/Traverse.java +++ b/src/main/java/mc/unraveled/reforged/plugin/Traverse.java @@ -6,6 +6,7 @@ import mc.unraveled.reforged.api.Locker; import mc.unraveled.reforged.banning.BanManager; import mc.unraveled.reforged.command.*; import mc.unraveled.reforged.command.base.CommandLoader; +import mc.unraveled.reforged.config.Yaml; import mc.unraveled.reforged.config.YamlManager; import mc.unraveled.reforged.data.DataManager; import mc.unraveled.reforged.data.LoginManager; @@ -18,8 +19,13 @@ import mc.unraveled.reforged.storage.DBConnectionHandler; import mc.unraveled.reforged.storage.DBProperties; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; + public final class Traverse extends JavaPlugin implements Locker { + // Secondary variable declaration. + private final String CONFIG_FILE = "config.yml"; + private final File CONFIG = new File(getDataFolder(), CONFIG_FILE); // Primary variable declaration. @Getter private DBConnectionHandler SQLManager; @@ -41,6 +47,8 @@ public final class Traverse extends JavaPlugin implements Locker { private LoginManager loginManager; @Getter private YamlManager yamlManager; + @Getter + private Yaml yamlConfig; @Override @SneakyThrows @@ -66,6 +74,7 @@ public final class Traverse extends JavaPlugin implements Locker { this.dataManager.saveCacheToDB(); this.PIPELINE.recycle(); this.rankManager.save(); + this.yamlConfig.saveToFile(); // Plugin shutdown logic } @@ -92,4 +101,25 @@ public final class Traverse extends JavaPlugin implements Locker { public void registerListeners() { new InfractionListener(this); } + + @SneakyThrows + public void registerConfig() { + Yaml yaml; + if (CONFIG.createNewFile()) { + yaml = getYamlManager().bldr() + .fileName(CONFIG_FILE) + .dataFolder(getDataFolder()) + .copyDefaults(true) + .build(); + } else { + yaml = getYamlManager().bldr() + .fileName(CONFIG_FILE) + .dataFolder(getDataFolder()) + .copyDefaults(false) + .build(); + } + this.yamlConfig = yaml; + + getYamlConfig().loadFromFile(); + } } diff --git a/src/main/java/mc/unraveled/reforged/storage/DBUser.java b/src/main/java/mc/unraveled/reforged/storage/DBUser.java index 79b7bac..0995417 100644 --- a/src/main/java/mc/unraveled/reforged/storage/DBUser.java +++ b/src/main/java/mc/unraveled/reforged/storage/DBUser.java @@ -3,18 +3,18 @@ package mc.unraveled.reforged.storage; import lombok.Getter; import lombok.SneakyThrows; import mc.unraveled.reforged.data.InfractionData; +import mc.unraveled.reforged.data.LoginInfo; import mc.unraveled.reforged.data.PlayerData; import mc.unraveled.reforged.permission.Rank; +import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; +import java.util.*; public class DBUser { @Getter @@ -68,6 +68,72 @@ public class DBUser { statement.executeUpdate(); } + @SneakyThrows + public void setLastLogin(String uuid, long lastLogin) { + PreparedStatement statement = getConnection().prepareStatement("UPDATE users SET last_login = ? WHERE uuid = ?;"); + statement.setLong(1, lastLogin); + statement.setString(2, uuid); + statement.executeUpdate(); + } + + @SneakyThrows + public Date getLastLogin(String uuid) { + PreparedStatement statement = getConnection().prepareStatement("SELECT last_login FROM users WHERE uuid = ?;"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + long lastLogin = 0; + + while (resultSet.next()) { + lastLogin = resultSet.getLong("last_login"); + } + + return new Date(lastLogin); + } + + @SneakyThrows + public void setCoins(String uuid, int coins) { + PreparedStatement statement = getConnection().prepareStatement("UPDATE users SET coins = ? WHERE uuid = ?;"); + statement.setInt(1, coins); + statement.setString(2, uuid); + statement.executeUpdate(); + } + + @SneakyThrows + public int getCoins(String uuid) { + PreparedStatement statement = getConnection().prepareStatement("SELECT coins FROM users WHERE uuid = ?;"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + int coins = 0; + + while (resultSet.next()) { + coins = resultSet.getInt("coins"); + } + + return coins; + } + + @SneakyThrows + public void setRank(String uuid, @NotNull Rank rank) { + PreparedStatement statement = getConnection().prepareStatement("UPDATE users SET rank = ? WHERE uuid = ?;"); + statement.setString(1, rank.name()); + statement.setString(2, uuid); + statement.executeUpdate(); + } + + @SneakyThrows + public Rank getRank(String uuid) { + PreparedStatement statement = getConnection().prepareStatement("SELECT rank FROM users WHERE uuid = ?;"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + Rank rank = Rank.NON_OP; + + while (resultSet.next()) { + rank = Rank.valueOf(resultSet.getString("rank")); + } + + return rank; + } + @SneakyThrows public void setLoginMessage(String uuid, String message) { PreparedStatement statement = getConnection().prepareStatement("UPDATE users SET login_message = ? WHERE uuid = ?;"); @@ -76,6 +142,35 @@ public class DBUser { statement.executeUpdate(); } + @SneakyThrows + public Map getLoginMessages() { + PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM users;"); + ResultSet resultSet = statement.executeQuery(); + Map loginInfoMap = new HashMap<>(); + + while (resultSet.next()) { + OfflinePlayer player = Bukkit.getPlayer(UUID.fromString(resultSet.getString("uuid"))); + LoginInfo loginInfo = new LoginInfo(); + loginInfoMap.put(player, loginInfo); + } + return loginInfoMap; + } + + @SneakyThrows + public LoginInfo getLoginInfo(String uuid) { + PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM users WHERE uuid = ?;"); + statement.setString(1, uuid); + ResultSet resultSet = statement.executeQuery(); + LoginInfo loginInfo = new LoginInfo(); + + while (resultSet.next()) { + Component c = Component.text(resultSet.getString("login_message")); + loginInfo.setLoginMessage(c); + } + + return loginInfo; + } + @SneakyThrows public List all() { PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM users;"); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..e69de29