From 3c34707944afcda3743750a2d7bc7c846c1990a4 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Fri, 4 Oct 2024 20:53:54 +0200 Subject: [PATCH] Add system to track keys given and related command for players Introduces `KeyStorage` for handling keys and their persistence. Added `Key` subcommand to allow players to receive their keys. Updated configuration files and messages to support the new feature. --- .../com/alttd/playerutils/PlayerUtils.java | 13 ++- .../commands/PlayerUtilsCommand.java | 5 +- .../commands/playerutils_subcommands/Key.java | 84 +++++++++++++++++++ .../com/alttd/playerutils/config/Config.java | 26 ++++++ .../alttd/playerutils/config/KeyStorage.java | 69 +++++++++++++++ .../alttd/playerutils/config/Messages.java | 19 +++++ 6 files changed, 211 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/alttd/playerutils/commands/playerutils_subcommands/Key.java create mode 100644 src/main/java/com/alttd/playerutils/config/KeyStorage.java diff --git a/src/main/java/com/alttd/playerutils/PlayerUtils.java b/src/main/java/com/alttd/playerutils/PlayerUtils.java index 4e46dc7..a184fe6 100644 --- a/src/main/java/com/alttd/playerutils/PlayerUtils.java +++ b/src/main/java/com/alttd/playerutils/PlayerUtils.java @@ -3,18 +3,18 @@ package com.alttd.playerutils; import com.alttd.playerutils.commands.PlayerUtilsCommand; import com.alttd.playerutils.commands.playerutils_subcommands.RotateBlock; import com.alttd.playerutils.config.Config; +import com.alttd.playerutils.config.KeyStorage; import com.alttd.playerutils.config.Messages; import com.alttd.playerutils.event_listeners.GoatHornEvent; import com.alttd.playerutils.event_listeners.RotateBlockEvent; import com.alttd.playerutils.event_listeners.TeleportEvent; import com.alttd.playerutils.event_listeners.XpBottleEvent; import com.alttd.playerutils.util.Logger; +import org.bukkit.Bukkit; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; +import java.util.concurrent.TimeUnit; public final class PlayerUtils extends JavaPlugin { @@ -27,6 +27,7 @@ public final class PlayerUtils extends JavaPlugin { registerCommands(); registerEvents(); reloadConfigs(); + registerSchedulers(); } @Override @@ -51,5 +52,11 @@ public final class PlayerUtils extends JavaPlugin { public void reloadConfigs() { Config.reload(logger); Messages.reload(logger); + KeyStorage.reload(logger); + } + + private void registerSchedulers() { + Bukkit.getScheduler().runTaskTimerAsynchronously(this, KeyStorage.STORAGE::save, + TimeUnit.MINUTES.toSeconds(5) * 20, TimeUnit.MINUTES.toSeconds(5) * 20); } } diff --git a/src/main/java/com/alttd/playerutils/commands/PlayerUtilsCommand.java b/src/main/java/com/alttd/playerutils/commands/PlayerUtilsCommand.java index 60ca410..0220570 100644 --- a/src/main/java/com/alttd/playerutils/commands/PlayerUtilsCommand.java +++ b/src/main/java/com/alttd/playerutils/commands/PlayerUtilsCommand.java @@ -22,7 +22,7 @@ public class PlayerUtilsCommand implements CommandExecutor, TabExecutor { PluginCommand command = playerUtils.getCommand("playerutils"); if (command == null) { subCommands = null; - logger.severe("Unable to find transfer command."); + logger.severe("Unable to find playerutils command."); return; } command.setExecutor(this); @@ -33,7 +33,8 @@ public class PlayerUtilsCommand implements CommandExecutor, TabExecutor { new Glow(logger), new XPCheque(playerUtils), new XPCalc(), - new Reload(playerUtils)) + new Reload(playerUtils), + new Key(logger)) ); } diff --git a/src/main/java/com/alttd/playerutils/commands/playerutils_subcommands/Key.java b/src/main/java/com/alttd/playerutils/commands/playerutils_subcommands/Key.java new file mode 100644 index 0000000..2214cdd --- /dev/null +++ b/src/main/java/com/alttd/playerutils/commands/playerutils_subcommands/Key.java @@ -0,0 +1,84 @@ +package com.alttd.playerutils.commands.playerutils_subcommands; + +import com.alttd.playerutils.commands.SubCommand; +import com.alttd.playerutils.config.Config; +import com.alttd.playerutils.config.KeyStorage; +import com.alttd.playerutils.config.Messages; +import com.alttd.playerutils.util.Logger; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.List; +import java.util.UUID; + +public class Key extends SubCommand { + + private final Logger logger; + + public Key(Logger logger) { + this.logger = logger; + } + + @Override + public boolean onCommand(CommandSender commandSender, String[] args) { + if (!(commandSender instanceof Player player)) { + commandSender.sendMiniMessage(Messages.GENERIC.PLAYER_ONLY, null); + return true; + } + + if (args.length != 2) { + return false; + } + + String crate = args[1].toLowerCase(); + if (!Config.KEY.CRATES.containsKey(crate)) { + commandSender.sendMiniMessage(Messages.KEY.CRATE_NOT_EXIST, Placeholder.parsed("crate", crate)); + return true; + } + + UUID uuid = player.getUniqueId(); + Object2IntOpenHashMap crateMap = KeyStorage.STORAGE.KEYS.get(crate); + int totalKeys = Config.KEY.CRATES.getOrDefault(crate, 0); + int keys = crateMap.getOrDefault(uuid, 0); + if (keys >= totalKeys) { + commandSender.sendMiniMessage(Messages.KEY.RETRIEVED_ALL_KEYS, TagResolver.resolver( + Placeholder.parsed("crate", crate), + Placeholder.parsed("keys", String.valueOf(keys)))); + return true; + } + + crateMap.addTo(uuid, 1); + logger.info(String.format("Gave %s one key for %s", player.getName(), crate)); + commandSender.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format("crate give v %s 1 %s", crate, player.getName())); + if (keys + 1 == totalKeys) { + commandSender.sendMiniMessage(Messages.KEY.GAVE_FINAL_KEY, TagResolver.resolver( + Placeholder.parsed("crate", crate), + Placeholder.parsed("keys", String.valueOf(keys + 1)))); + } else { + commandSender.sendMiniMessage(Messages.KEY.GAVE_KEY, TagResolver.resolver( + Placeholder.parsed("crate", crate), + Placeholder.parsed("keys", String.valueOf(keys + 1)), + Placeholder.parsed("total_keys", String.valueOf(totalKeys)))); + } + return true; + } + + @Override + public String getName() { + return "key"; + } + + @Override + public List getTabComplete(CommandSender commandSender, String[] args) { + return Config.KEY.CRATES.keySet().stream().toList(); + } + + @Override + public String getHelpMessage() { + return Messages.HELP.KEY; + } +} diff --git a/src/main/java/com/alttd/playerutils/config/Config.java b/src/main/java/com/alttd/playerutils/config/Config.java index feca623..b481454 100644 --- a/src/main/java/com/alttd/playerutils/config/Config.java +++ b/src/main/java/com/alttd/playerutils/config/Config.java @@ -1,8 +1,12 @@ package com.alttd.playerutils.config; import com.alttd.playerutils.util.Logger; +import org.bukkit.configuration.ConfigurationSection; import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Set; public class Config extends AbstractConfig{ @@ -36,4 +40,26 @@ public class Config extends AbstractConfig{ WARNINGS = config.getBoolean(prefix, "warnings", WARNINGS); } } + + public static class KEY { + private static final String prefix = "key."; + public static HashMap CRATES = new HashMap<>(); + + @SuppressWarnings("unused") + private static void load() { + CRATES.clear(); + ConfigurationSection configurationSection = config.getConfigurationSection(prefix.substring(0, prefix.length() - 1)); + if (configurationSection == null) { + config.logger.warning("No keys configured, adding default"); + config.set(prefix, "dailyvotecrate", 0); + config.set(prefix, "weeklyvotecrate", 0); + config.set(prefix, "questcrate", 0); + return; + } + Set keys = configurationSection.getKeys(false); + for (String key : keys) { + CRATES.put(key, config.getInt(prefix, key, 0)); + } + } + } } diff --git a/src/main/java/com/alttd/playerutils/config/KeyStorage.java b/src/main/java/com/alttd/playerutils/config/KeyStorage.java new file mode 100644 index 0000000..ddbcf7c --- /dev/null +++ b/src/main/java/com/alttd/playerutils/config/KeyStorage.java @@ -0,0 +1,69 @@ +package com.alttd.playerutils.config; + +import com.alttd.playerutils.util.Logger; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import org.bukkit.configuration.ConfigurationSection; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +public class KeyStorage extends AbstractConfig { + + static KeyStorage config; + private final Logger logger; + + public KeyStorage(Logger logger) { + super( + new File(File.separator + + "mnt" + File.separator + + "configs" + File.separator + + "PlayerUtils"), + "messages.yml", logger); + this.logger = logger; + } + + public static void reload(Logger logger) { + logger.info("Reloading key storage"); + config = new KeyStorage(logger); + config.readConfig(KeyStorage.class, null); + } + + public static class STORAGE { + private static final String prefix = "storage."; + + public static HashMap> KEYS = new HashMap<>(); + + @SuppressWarnings("unused") + private static void load() { + save(); + KEYS.clear(); + for (String crate : Config.KEY.CRATES.keySet()) { + Object2IntOpenHashMap count = new Object2IntOpenHashMap<>(); + ConfigurationSection configurationSection = config.getConfigurationSection(prefix + crate); + if (configurationSection == null) { + config.logger.info(String.format("No section yet for crate %s", crate)); + continue; + } + List uuids = configurationSection.getKeys(false).stream().map(UUID::fromString).toList(); + if (uuids.isEmpty()) { + config.logger.info(String.format("No keys yet for crate %s", crate)); + continue; + } + for (UUID uuid : uuids) { + int keys = config.getInt(prefix, String.format("%s.%s", crate, uuid.toString()), 0); + count.put(uuid, keys); + } + KEYS.put(crate, count); + } + } + + public synchronized static void save() { + config.logger.info("Saving KeyStorage"); + KEYS.keySet() + .forEach(crate -> KEYS.get(crate) + .forEach((uuid, keys) -> config.set(prefix + crate, uuid.toString(), keys))); + } + } +} diff --git a/src/main/java/com/alttd/playerutils/config/Messages.java b/src/main/java/com/alttd/playerutils/config/Messages.java index 9564752..211aaf1 100644 --- a/src/main/java/com/alttd/playerutils/config/Messages.java +++ b/src/main/java/com/alttd/playerutils/config/Messages.java @@ -1,6 +1,7 @@ package com.alttd.playerutils.config; import com.alttd.playerutils.util.Logger; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.List; @@ -34,6 +35,7 @@ public class Messages extends AbstractConfig { public static String XP_CALC = "Calculate the amount of XP between levels: /pu xpcalc "; public static String RELOAD = "Reload the configs for PlayerUtils: /pu reload"; public static String ROTATE_BLOCK = "Enable rotating blocks with a blaze rod: /pu rotateblock"; + public static String KEY = "Receive a key that you are owed: /pu key"; @SuppressWarnings("unused") private static void load() { @@ -138,4 +140,21 @@ public class Messages extends AbstractConfig { DISABLED = config.getString(prefix, "disabled", DISABLED); } } + + public static class KEY { + private static final String prefix = "pu-command.key."; + + public static String CRATE_NOT_EXIST = "There is no crate called "; + public static String RETRIEVED_ALL_KEYS = "You already retrieved all keys for the ."; + public static String GAVE_KEY = "You received a key. You have received out of keys. Make sure to use your key before requesting another"; + public static String GAVE_FINAL_KEY = "You received your final key. You have received a total of keys for this crate."; + + @SuppressWarnings("unused") + private static void load() { + CRATE_NOT_EXIST = config.getString(prefix, "crate-not-exist", CRATE_NOT_EXIST); + RETRIEVED_ALL_KEYS = config.getString(prefix, "retrieved-all-keys", RETRIEVED_ALL_KEYS); + GAVE_KEY = config.getString(prefix, "gave-key", GAVE_KEY); + GAVE_FINAL_KEY = config.getString(prefix, "gave-final-key", GAVE_FINAL_KEY); + } + } }