No chat to show

This project's files are all here to explore.

Files
atheronpack
src
pom.xml
Code
23 2,720
src/main/java/com/atheronsmp/atheronweapons/AtheronWeapons.java
package com.atheronsmp.atheronweapons;

import com.atheronsmp.atheronweapons.commands.Ability1Command;
import com.atheronsmp.atheronweapons.commands.Ability2Command;
import com.atheronsmp.atheronweapons.commands.WeaponCommand;
import com.atheronsmp.atheronweapons.hooks.WorldGuardHook;
import com.atheronsmp.atheronweapons.listeners.WeaponListener;
import com.atheronsmp.atheronweapons.listeners.ShooterListener;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class AtheronWeapons extends JavaPlugin {

    private static AtheronWeapons instance;
    private static NamespacedKey weaponKey;

    private WorldGuardHook worldGuardHook;

    // AtheronBlade cooldown maps: UUID -> expiry timestamp (millis)
    // ConcurrentHashMap — safe for async cleanup task
    private final Map<UUID, Long> ability1Cooldowns = new ConcurrentHashMap<>();
    private final Map<UUID, Long> ability2Cooldowns = new ConcurrentHashMap<>();

    // AtheronTerminator cooldown maps
    private final Map<UUID, Long> terminatorAbility1Cooldowns = new ConcurrentHashMap<>();
    private final Map<UUID, Long> terminatorAbility2Cooldowns = new ConcurrentHashMap<>();

    // AtheronShooter cooldown maps
    private final Map<UUID, Long> shooterAbility1Cooldowns = new ConcurrentHashMap<>();
    private final Map<UUID, Long> shooterAbility2Cooldowns = new ConcurrentHashMap<>();

    // Primed players for AtheronBlade ability1 (The Statue)
    private final Set<UUID> primedPlayers = ConcurrentHashMap.newKeySet();

    // Primed players for AtheronBlade ability2 (Giant Pop)
    private final Set<UUID> primedAbility2Players = ConcurrentHashMap.newKeySet();

    // Primed players for AtheronShooter abilities (UUID → expiry timestamp)
    private final Map<UUID, Long> shooterPrimedAbility1 = new ConcurrentHashMap<>();
    private final Map<UUID, Long> shooterPrimedAbility2 = new ConcurrentHashMap<>();

    // Frozen entities for AtheronBlade ability1
    private final Set<UUID> frozenEntities = ConcurrentHashMap.newKeySet();

    @Override
    public void onEnable() {
        instance = this;
        weaponKey = new NamespacedKey(this, "atheron_blade");

        // Save default config
        saveDefaultConfig();

        // Hook into WorldGuard if present
        worldGuardHook = new WorldGuardHook(this);

        // Register listeners
        getServer().getPluginManager().registerEvents(new WeaponListener(this), this);
        getServer().getPluginManager().registerEvents(new ShooterListener(this), this);

        // Register commands
        WeaponCommand weaponCommand = new WeaponCommand(this);
        getCommand("weapon").setExecutor(weaponCommand);
        getCommand("weapon").setTabCompleter(weaponCommand);
        getCommand("ability1").setExecutor(new Ability1Command(this));
        getCommand("ability2").setExecutor(new Ability2Command(this));

        // Start async cleanup task to prevent memory leaks from inactive players
        getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
            long now = System.currentTimeMillis();
            ability1Cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);
            ability2Cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);
            terminatorAbility1Cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);
            terminatorAbility2Cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);
            shooterAbility1Cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);
            shooterAbility2Cooldowns.entrySet().removeIf(entry -> entry.getValue() <= now);
            shooterPrimedAbility1.entrySet().removeIf(entry -> entry.getValue() <= now);
            shooterPrimedAbility2.entrySet().removeIf(entry -> entry.getValue() <= now);
        }, 6000L, 6000L); // Run every 5 minutes (6000 ticks)

        getLogger().info("§6AtheronWeapons §7has been §aenabled§7!");
    }

    @Override
    public void onDisable() {
        // Cancel all running tasks to prevent memory leaks
        getServer().getScheduler().cancelTasks(this);

        // Clear all maps
        ability1Cooldowns.clear();
        ability2Cooldowns.clear();
        terminatorAbility1Cooldowns.clear();
        terminatorAbility2Cooldowns.clear();
        shooterAbility1Cooldowns.clear();
        shooterAbility2Cooldowns.clear();
        primedPlayers.clear();
        primedAbility2Players.clear();
        shooterPrimedAbility1.clear();
        shooterPrimedAbility2.clear();
        frozenEntities.clear();

        getLogger().info("§6AtheronWeapons §7has been §cdisabled§7!");
    }

    public static AtheronWeapons getInstance() {
        return instance;
    }

    public static NamespacedKey getWeaponKey() {
        return weaponKey;
    }

    public WorldGuardHook getWorldGuardHook() {
        return worldGuardHook;
    }

    // --- AtheronBlade Cooldowns ---
    public Map<UUID, Long> getAbility1Cooldowns() {
        return ability1Cooldowns;
    }

    public Map<UUID, Long> getAbility2Cooldowns() {
        return ability2Cooldowns;
    }

    // --- AtheronTerminator Cooldowns ---
    public Map<UUID, Long> getTerminatorAbility1Cooldowns() {
        return terminatorAbility1Cooldowns;
    }

    public Map<UUID, Long> getTerminatorAbility2Cooldowns() {
        return terminatorAbility2Cooldowns;
    }

    // --- AtheronBlade Primed Sets ---
    public Set<UUID> getPrimedPlayers() {
        return primedPlayers;
    }

    public Set<UUID> getPrimedAbility2Players() {
        return primedAbility2Players;
    }

    public Set<UUID> getFrozenEntities() {
        return frozenEntities;
    }

    // --- AtheronShooter Cooldowns ---
    public Map<UUID, Long> getShooterAbility1Cooldowns() {
        return shooterAbility1Cooldowns;
    }

    public Map<UUID, Long> getShooterAbility2Cooldowns() {
        return shooterAbility2Cooldowns;
    }

    // --- AtheronShooter Primed Maps ---
    public Map<UUID, Long> getShooterPrimedAbility1() {
        return shooterPrimedAbility1;
    }

    public Map<UUID, Long> getShooterPrimedAbility2() {
        return shooterPrimedAbility2;
    }

    /**
     * Gets a message from config with the prefix prepended.
     */
    public String getMessage(String path) {
        String prefix = colorize(getConfig().getString("prefix", "&6&l⚔ ᴀᴛʜᴇʀᴏɴ &8» &7"));
        String message = colorize(getConfig().getString("messages." + path, "&cMessage not found: " + path));
        return prefix + message;
    }

    /**
     * Gets a raw message from config without prefix.
     */
    public String getRawMessage(String path) {
        return colorize(getConfig().getString("messages." + path, "&cMessage not found: " + path));
    }

    private static final java.util.regex.Pattern HEX_PATTERN = java.util.regex.Pattern.compile("&#([A-Fa-f0-9]{6})");

    /**
     * Translates color codes including hex colors (&#RRGGBB format).
     */
    public static String colorize(String message) {
        if (message == null) return "";
        // Handle hex colors &#RRGGBB
        java.util.regex.Matcher matcher = HEX_PATTERN.matcher(message);
        StringBuilder buffer = new StringBuilder();
        while (matcher.find()) {
            StringBuilder replacement = new StringBuilder("§x");
            for (char c : matcher.group(1).toCharArray()) {
                replacement.append("§").append(c);
            }
            matcher.appendReplacement(buffer, replacement.toString());
        }