75% completion

This commit is contained in:
Paldiu 2023-01-30 21:16:50 -06:00
parent 5a48fb7285
commit 8f84583e4e
16 changed files with 401 additions and 55 deletions

View File

@ -0,0 +1,4 @@
package mc.unraveled.reforged.command;
public class HaltCMD {
}

View File

@ -0,0 +1,4 @@
package mc.unraveled.reforged.command;
public class JailCMD {
}

View File

@ -0,0 +1,4 @@
package mc.unraveled.reforged.command;
public class LockCMD {
}

View File

@ -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 = "/<command> [<player> <info | reset <data> | set <data> <newValue>]",
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("/<command> [<player> <info | reset <data> | set <data> <newValue>]")
.color(NamedTextColor.GRAY);
}
}

View File

@ -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();
}
}

View File

@ -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) {
};
}
}
}

View File

@ -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;
}

View File

@ -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.");

View File

@ -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;
}

View File

@ -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<Pair<Player, CustomLoginData>> dataSet = new HashSet<>(); // VALUE ONLY MODIFIED BY BAKER
private Set<Pair<OfflinePlayer, LoginInfo>> 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());
}
}
}

View File

@ -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;
}
}

View File

@ -19,7 +19,6 @@ public class EconomyManager {
private final RegisteredServiceProvider<Economy> rsp;
private final Map<Player, List<EconomyRequest>> 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) {

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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<OfflinePlayer, LoginInfo> getLoginMessages() {
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM users;");
ResultSet resultSet = statement.executeQuery();
Map<OfflinePlayer, LoginInfo> 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<PlayerData> all() {
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM users;");

View File