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.
This commit is contained in:
parent
524a657abd
commit
3c34707944
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<UUID> 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<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
return Config.KEY.CRATES.keySet().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Messages.HELP.KEY;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String, Integer> 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<String> keys = configurationSection.getKeys(false);
|
||||
for (String key : keys) {
|
||||
CRATES.put(key, config.getInt(prefix, key, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
69
src/main/java/com/alttd/playerutils/config/KeyStorage.java
Normal file
69
src/main/java/com/alttd/playerutils/config/KeyStorage.java
Normal file
|
|
@ -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<String, Object2IntOpenHashMap<UUID>> KEYS = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static void load() {
|
||||
save();
|
||||
KEYS.clear();
|
||||
for (String crate : Config.KEY.CRATES.keySet()) {
|
||||
Object2IntOpenHashMap<UUID> 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<UUID> 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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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 = "<green>Calculate the amount of XP between levels: <gold>/pu xpcalc <from> <to></gold></green>";
|
||||
public static String RELOAD = "<green>Reload the configs for PlayerUtils: <gold>/pu reload</gold></green>";
|
||||
public static String ROTATE_BLOCK = "<green>Enable rotating blocks with a blaze rod: <gold>/pu rotateblock</gold></green>";
|
||||
public static String KEY = "<green>Receive a key that you are owed: <gold>/pu key</gold></green>";
|
||||
|
||||
@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 = "<red>There is no crate called <crate></green>";
|
||||
public static String RETRIEVED_ALL_KEYS = "<red>You already retrieved all <keys> keys for the <crate>.</red>";
|
||||
public static String GAVE_KEY = "<green>You received a <crate> key. You have received <keys> out of <total_keys> keys. Make sure to use your key before requesting another</green>";
|
||||
public static String GAVE_FINAL_KEY = "<green>You received your final <crate> key. You have received a total of <keys> keys for this crate.</green>";
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user