Files
src
build.gradle
manifest.kod
pom.xml
settings.gradle
Code
10 994
src/main/java/com/kodari/voidspear/VoidSpear.java
package com.kodari.voidspear;
import com.kodari.voidspear.command.VoidSpearCommand;
import com.kodari.voidspear.item.VoidSpearItem;
import com.kodari.voidspear.listener.VoidSpearListener;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
public final class VoidSpear extends JavaPlugin {
private final Map<UUID, Map<String, Long>> cooldowns = new HashMap<>();
private NamespacedKey spearKey;
private VoidSpearListener listener;
private BukkitTask cooldownCleanupTask;
@Override
public void onEnable() {
saveDefaultConfig();
spearKey = new NamespacedKey(this, "voidspear_id");
listener = new VoidSpearListener(this);
Bukkit.getPluginManager().registerEvents(listener, this);
listener.start();
VoidSpearCommand command = new VoidSpearCommand(this);
Objects.requireNonNull(getCommand("voidspear"), "voidspear command missing from plugin.yml")
.setExecutor(command);
Objects.requireNonNull(getCommand("voidspear"), "voidspear command missing from plugin.yml")
.setTabCompleter(command);
cooldownCleanupTask = Bukkit.getScheduler().runTaskTimer(this, this::cleanCooldowns, 1200L, 1200L);
getLogger().info("VoidSpear enabled.");
}
@Override
public void onDisable() {
if (cooldownCleanupTask != null) {
cooldownCleanupTask.cancel();
}
if (listener != null) {
listener.shutdown();
}
cooldowns.clear();
}
public NamespacedKey getSpearKey() {
return spearKey;
}
public ItemStack createSpear() {
return VoidSpearItem.create(this);
}
public boolean isVoidSpear(ItemStack item) {
if (item == null || item.getType().isAir() || !item.hasItemMeta()) {
return false;
}
String id = item.getItemMeta().getPersistentDataContainer()
.get(spearKey, PersistentDataType.STRING);
return "voidspear".equals(id);
}
public boolean activateAbility(Player player, String ability) {
String permission = getConfig().getString("permissions." + ability, "voidspear.use");
if (permission == null || !player.hasPermission(permission)) {
sendActionbar(player, getConfig().getString("messages.no-permission", "&cNo permission."));
return false;
}
String path = "abilities." + ability;
if (!getConfig().getBoolean(path + ".enabled", true)) {
sendActionbar(player, getConfig().getString("messages.disabled", "&cThat ability is disabled."));
return false;
}
long now = System.currentTimeMillis();
Map<String, Long> playerCooldowns = cooldowns.computeIfAbsent(player.getUniqueId(), ignored -> new HashMap<>());
long expiresAt = playerCooldowns.getOrDefault(ability, 0L);
if (expiresAt > now) {
long seconds = (expiresAt - now + 999L) / 1000L;
sendActionbar(player, getConfig().getString("messages.cooldown", "&5{ability} &7cooldown: {seconds}s")
.replace("{ability}", displayAbility(ability))
.replace("{seconds}", Long.toString(seconds)));
return false;
}
long cooldownSeconds = Math.max(0L, getConfig().getLong(path + ".cooldown-seconds", 0L));
playerCooldowns.put(ability, now + cooldownSeconds * 1000L);
return true;
}
public void sendActivation(Player player, String ability) {
String fallback = "&5" + displayAbility(ability) + " activated!";
sendActionbar(player, getConfig().getString("messages.activated." + ability, fallback));
}
public void sendActionbar(Player player, String message) {
if (message != null && !message.isBlank() && player.isOnline()) {
player.sendActionBar(LegacyComponentSerializer.legacyAmpersand().deserialize(message));
}
}
public static String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text == null ? "" : text);
}
private void cleanCooldowns() {
long now = System.currentTimeMillis();
Iterator<Map.Entry<UUID, Map<String, Long>>> players = cooldowns.entrySet().iterator();
while (players.hasNext()) {
Map<String, Long> values = players.next().getValue();
values.values().removeIf(expiry -> expiry <= now);
if (values.isEmpty()) {
players.remove();
}
}
}
private String displayAbility(String ability) {
String[] words = ability.split("-");
StringBuilder result = new StringBuilder();
for (String word : words) {
if (!result.isEmpty()) {
result.append(' ');
}
result.append(Character.toUpperCase(word.charAt(0))).append(word.substring(1));
}
return result.toString();
}
}