Initial commit

This commit is contained in:
2025-12-17 14:47:09 +01:00
commit 83aa504c30
9 changed files with 619 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
package net.knarcraft.crateregistry;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
/**
* A storage for players' unclaimed crates
*/
public class CrateKeyStorage {
private final Map<UUID, Integer> unclaimedCrateKeys;
private final FileConfiguration configuration;
/**
* Initializes an empty crate key storage
*
* @param configuration <p>The file configuration to use for saving and loading</p>
*/
public CrateKeyStorage(@NotNull FileConfiguration configuration) {
this.unclaimedCrateKeys = load(configuration);
this.configuration = configuration;
}
/**
* Checks whether the given player has unclaimed crate keys
*
* @param player <p>The player to check</p>
* @return <p>True if the player has unclaimed crate keys</p>
*/
public boolean hasStoredKeys(@NotNull Player player) {
return this.unclaimedCrateKeys.containsKey(player.getUniqueId());
}
/**
* Adds unclaimed crate keys for a player
*
* @param player <p>The player to store the crate keys for</p>
* @param amount <p>The amount to store</p>
*/
public void addCrateKeys(@NotNull Player player, int amount) {
int existing = this.unclaimedCrateKeys.getOrDefault(player.getUniqueId(), 0);
this.unclaimedCrateKeys.put(player.getUniqueId(), existing + amount);
save();
}
/**
* Removes a player's stored crate keys
*
* @param player <p>The player to remove crate keys for</p>
* @return <p>The amount of keys removed</p>
*/
public int removeKeys(@NotNull Player player) {
Integer amount = this.unclaimedCrateKeys.remove(player.getUniqueId());
int realAmount = Objects.requireNonNullElse(amount, 0);
if (realAmount > 64) {
this.unclaimedCrateKeys.put(player.getUniqueId(), realAmount - 64);
realAmount -= 64;
}
save();
return realAmount;
}
/**
* Saves all unclaimed crate keys to the configuration file
*/
private void save() {
this.configuration.set("playerKeys", null);
ConfigurationSection playerKeysSection = this.configuration.createSection("playerKeys");
for (Map.Entry<UUID, Integer> entry : this.unclaimedCrateKeys.entrySet()) {
playerKeysSection.set(entry.getKey().toString(), entry.getValue());
}
CrateRegistry.getInstance().saveConfig();
}
/**
* Loads crate keys from disk
*
* @param configuration <p>The file configuration to load values from</p>
* @return <p>The loaded values</p>
*/
@NotNull
private Map<UUID, Integer> load(@NotNull FileConfiguration configuration) {
ConfigurationSection playerKeysSection = configuration.getConfigurationSection("playerKeys");
if (playerKeysSection == null) {
return new HashMap<>();
}
Map<UUID, Integer> loaded = new HashMap<>();
Set<String> keys = playerKeysSection.getKeys(false);
for (String key : keys) {
loaded.put(UUID.fromString(key), playerKeysSection.getInt(key));
}
return loaded;
}
}

View File

@@ -0,0 +1,44 @@
package net.knarcraft.crateregistry;
import net.knarcraft.crateregistry.command.CrateRegistryCommand;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
/**
* The crate registry's main class
*/
public final class CrateRegistry extends JavaPlugin {
private static CrateRegistry instance;
private CrateKeyStorage crateKeyStorage;
@Override
public void onEnable() {
instance = this;
crateKeyStorage = new CrateKeyStorage(this.getConfig());
PluginCommand command = this.getCommand("cr");
if (command != null) {
command.setExecutor(new CrateRegistryCommand());
}
}
/**
* Gets an instance of this plugin
*
* @return <p>This plugin's main class</p>
*/
public static CrateRegistry getInstance() {
return instance;
}
/**
* Gets the crate key storage
*
* @return <p>The crate key storage</p>
*/
public CrateKeyStorage getCrateKeyStorage() {
return this.crateKeyStorage;
}
}

View File

@@ -0,0 +1,53 @@
package net.knarcraft.crateregistry.command;
import net.knarcraft.crateregistry.CrateKeyStorage;
import net.knarcraft.crateregistry.CrateRegistry;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* The command for claiming stored crate keys
*/
public class ClaimCommand extends PluginCommand {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be used by players");
return false;
}
CrateKeyStorage crateKeyStorage = CrateRegistry.getInstance().getCrateKeyStorage();
if (!crateKeyStorage.hasStoredKeys(player)) {
player.sendMessage("You have no unclaimed crate keys.");
return true;
}
if (player.getInventory().firstEmpty() == -1) {
player.sendMessage("You must have an empty slot in your inventory in order to claim crate keys.");
return true;
}
int amount = crateKeyStorage.removeKeys(player);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "crates giveKey vote_crate " + player.getName() + " " + amount);
player.sendMessage("Crate keys claimed");
if (crateKeyStorage.hasStoredKeys(player)) {
player.sendMessage("You still have more unclaimed crate keys remaining.");
}
return true;
}
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
return List.of();
}
}

View File

@@ -0,0 +1,56 @@
package net.knarcraft.crateregistry.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* The main command for the CrateRegistry plugin
*/
public class CrateRegistryCommand extends PluginCommand {
private final TabExecutor claimExecutor;
private final TabExecutor giveExecutor;
/**
* Instantiates a new crate registry command
*/
public CrateRegistryCommand() {
claimExecutor = new ClaimCommand();
giveExecutor = new GiveCommand();
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
return switch (arguments[0].toLowerCase()) {
case "givekey" -> giveExecutor.onCommand(sender, command, label, arguments);
case "claim" -> claimExecutor.onCommand(sender, command, label, arguments);
default -> false;
};
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
if (args.length == 1) {
if (sender.hasPermission("crateregistry.give")) {
return filterMatchingStartsWith(List.of("giveKey", "claim"), args[0]);
} else {
return filterMatchingStartsWith(List.of("claim"), args[0]);
}
} else {
return switch (args[0].toLowerCase()) {
case "givekey" -> giveExecutor.onTabComplete(sender, command, label, args);
case "claim" -> claimExecutor.onTabComplete(sender, command, label, args);
default -> List.of();
};
}
}
}

View File

@@ -0,0 +1,89 @@
package net.knarcraft.crateregistry.command;
import net.knarcraft.crateregistry.CrateRegistry;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* The command for giving or storing a crate key
*/
public class GiveCommand extends PluginCommand {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
if (arguments.length < 3) {
return false;
}
if (!sender.hasPermission("crateregistry.give")) {
sender.sendMessage("You don't have permission to use this command");
return false;
}
try {
return giveOrStoreCrateKeys(sender, arguments);
} catch (NumberFormatException exception) {
return false;
}
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) {
if (arguments.length == 2) {
return filterMatchingContains(getPlayerNames(), arguments[1]);
} else if (arguments.length == 3) {
return filterMatchingStartsWith(List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"), arguments[2]);
} else {
return List.of();
}
}
/**
* Gives crate keys to a player, or stores them if the player has no available space
*
* @param sender <p>The sender of the command</p>
* @param arguments <p>The arguments given</p>
* @return <p>True if this command was used correctly</p>
*/
private boolean giveOrStoreCrateKeys(@NotNull CommandSender sender, @NotNull String[] arguments) {
String userName = arguments[1];
int amount = Integer.parseInt(arguments[2]);
Player player = Bukkit.getPlayer(userName);
if (player == null) {
sender.sendMessage("No player " + userName + " exists!");
return false;
}
if (player.getInventory().firstEmpty() == -1) {
CrateRegistry.getInstance().getCrateKeyStorage().addCrateKeys(player, amount);
player.sendMessage("Your crate key was stored, as your inventory is full. Claim it with \"/cr claim\"");
} else {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "crates giveKey vote_crate " + player.getName() + " " + amount);
}
return true;
}
/**
* Gets all online player names
*
* @return <p>All online player names</p>
*/
private List<String> getPlayerNames() {
List<String> output = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
output.add(player.getName());
}
return output;
}
}

View File

@@ -0,0 +1,48 @@
package net.knarcraft.crateregistry.command;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* A class containing useful command-related methods
*/
public abstract class PluginCommand implements TabExecutor {
/**
* Finds tab complete values that contain the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that contain the player's typed text</p>
*/
protected static @NotNull List<String> filterMatchingContains(@NotNull List<String> values, @NotNull String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().contains(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
/**
* Finds tab complete values that match the start of the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that start with the player's typed text</p>
*/
protected static @NotNull List<String> filterMatchingStartsWith(@NotNull List<String> values, @NotNull String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().startsWith(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
}