Files
src
build.gradle
README.md
settings.gradle
Code
8 611
src/main/java/com/stringplugin/StringPlugin.java
package com.stringplugin;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.java.JavaPlugin;
public final class StringPlugin extends JavaPlugin implements CommandExecutor {
private static final int STRING_STACK_SIZE = 64;
private Material stringMaterial;
private CooldownStore cooldownStore;
private RestrictedGamemodeListener gamemodeListener;
@Override
public void onEnable() {
saveDefaultConfig();
stringMaterial = Material.matchMaterial("STRING");
if (stringMaterial == null) {
getLogger().severe("Could not resolve the STRING material; disabling StringPlugin.");
getServer().getPluginManager().disablePlugin(this);
return;
}
cooldownStore = new CooldownStore(this);
cooldownStore.load();
gamemodeListener = new RestrictedGamemodeListener(this);
getServer().getPluginManager().registerEvents(gamemodeListener, this);
gamemodeListener.reloadAllowlist();
getCommand("string").setExecutor(this);
getCommand("stringplugin").setExecutor(this);
}
@Override
public void onDisable() {
if (cooldownStore != null) {
cooldownStore.save();
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("string")) {
if (!sender.hasPermission("string.use")) {
sendConfiguredMessage(sender, "messages.no-permission");
return true;
}
if (!(sender instanceof Player player)) {
sendConfiguredMessage(sender, "messages.player-only");
return true;
}
if (args.length != 0) {
sendConfiguredMessage(sender, "messages.string-usage");
return true;
}
giveString(player);
return true;
}
if (!sender.hasPermission("string.admin")) {
sendConfiguredMessage(sender, "messages.no-permission");
return true;
}
if (args.length != 1) {
sendConfiguredMessage(sender, "messages.admin-usage");
return true;
}
if (args[0].equalsIgnoreCase("reload")) {
reloadPluginConfiguration();
sendConfiguredMessage(sender, "messages.reloaded");
return true;
}
if (args[0].equalsIgnoreCase("version")) {
sendConfiguredMessage(sender, "messages.version", "%version%", getDescription().getVersion());
return true;
}
sendConfiguredMessage(sender, "messages.admin-usage");
return true;
}
public void sendConfiguredMessage(CommandSender sender, String path) {
String message = getConfig().getString(path, "");
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
public void sendConfiguredMessage(CommandSender sender, String path, String placeholder, String value) {
String message = getConfig().getString(path, "").replace(placeholder, value);
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
private void giveString(Player player) {
long remaining = cooldownStore.getRemainingMillis(player.getUniqueId());
if (remaining > 0L) {
long seconds = remaining / 1000L + (remaining % 1000L == 0L ? 0L : 1L);
sendConfiguredMessage(player, "messages.cooldown", "%time%", Long.toString(seconds));
return;
}
int received = fillAvailableSlots(player);
if (received == 0) {
sendConfiguredMessage(player, "messages.inventory-full");
return;
}
long cooldownSeconds = Math.max(0L, getConfig().getLong("string.cooldown-seconds", 60L));
cooldownStore.start(player.getUniqueId(), cooldownDurationMillis(cooldownSeconds));
String message = getConfig().getString("messages.received", "")
.replace("%amount%", Integer.toString(received))
.replace("%cooldown%", Long.toString(cooldownSeconds));
player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
private int fillAvailableSlots(Player player) {
PlayerInventory inventory = player.getInventory();
ItemStack[] storage = copyContents(inventory.getStorageContents());
boolean includeArmor = getConfig().getBoolean("inventory.include-armor", false);
boolean includeOffhand = getConfig().getBoolean("inventory.include-offhand", false);
ItemStack[] armor = includeArmor ? copyContents(inventory.getArmorContents()) : null;
ItemStack offhand = includeOffhand ? copyItem(inventory.getItemInOffHand()) : null;
int maximumStackSize = Math.min(STRING_STACK_SIZE, stringMaterial.getMaxStackSize());
int capacity = capacity(storage, maximumStackSize);
if (includeArmor) {
capacity += capacity(armor, maximumStackSize);
}
if (includeOffhand) {
capacity += capacity(offhand, maximumStackSize);
}
if (capacity == 0) {
return 0;
}
int remaining = fill(storage, capacity, maximumStackSize);
if (includeArmor) {
remaining = fill(armor, remaining, maximumStackSize);
}
if (includeOffhand) {
SlotResult result = fill(offhand, remaining, maximumStackSize);
offhand = result.item;
remaining = result.remaining;
}
inventory.setStorageContents(storage);
if (includeArmor) {
inventory.setArmorContents(armor);
}
if (includeOffhand) {
inventory.setItemInOffHand(offhand);
}
return capacity - remaining;
}
private int capacity(ItemStack[] items, int maximumStackSize) {
int total = 0;
for (ItemStack item : items) {
total += capacity(item, maximumStackSize);
}
return total;
}
private int capacity(ItemStack item, int maximumStackSize) {
if (isEmpty(item)) {
return maximumStackSize;
}
if (item.getType() == stringMaterial && item.getAmount() < maximumStackSize) {
return maximumStackSize - item.getAmount();
}
return 0;
}
private int fill(ItemStack[] items, int amount, int maximumStackSize) {
int remaining = amount;
for (int slot = 0; slot < items.length && remaining > 0; slot++) {
ItemStack item = items[slot];
if (isEmpty(item)) {
int added = Math.min(remaining, maximumStackSize);
items[slot] = new ItemStack(stringMaterial, added);
remaining -= added;
} else if (item.getType() == stringMaterial && item.getAmount() < maximumStackSize) {
int added = Math.min(remaining, maximumStackSize - item.getAmount());
item.setAmount(item.getAmount() + added);
remaining -= added;
}
}
return remaining;
}
private SlotResult fill(ItemStack item, int amount, int maximumStackSize) {
int remaining = amount;
if (remaining == 0) {