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

113
.gitignore vendored Normal file
View File

@@ -0,0 +1,113 @@
# User-specific stuff
.idea/
*.iml
*.ipr
*.iws
# IntelliJ
out/
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml
# Common working directory
run/

95
pom.xml Normal file
View File

@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.knarcraft</groupId>
<artifactId>crateregistry</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CrateRegistry</name>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>org.jetbrains.annotations</pattern>
<shadedPattern>net.knarcraft.crateregistry.lib.annotations</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>org.jetbrains:annotations</artifact>
<includes>
<include>org/jetbrains/annotations/**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

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

View File

@@ -0,0 +1,15 @@
name: CrateRegistry
version: '1.0-SNAPSHOT'
main: net.knarcraft.crateregistry.CrateRegistry
api-version: '1.20'
commands:
cr:
description: "The main command for the CrateRegistry plugin"
usage: |
/<command> claim
/<command> giveKey <player> <amount>
permissions:
crateregistry.give:
description: Gives permission to the /cr give command
default: false