No chat to show

This project's files are all here to explore.

Files
src
pom.xml
README.md
Code
13 2,288
src/main/java/dev/loyola/legspear/LegSpear.java
package dev.loyola.legspear;

import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public final class LegSpear extends JavaPlugin {
    private static final MiniMessage MM = MiniMessage.miniMessage();

    private Items items;
    private StunManager stuns;
    private NamespacedKey recipeKey;
    private BukkitTask hudTask;
    private final Map<String, Map<UUID, Long>> cooldowns = new HashMap<>();
    private final Set<UUID> stunEnabled = new HashSet<>();
    private final Map<UUID, Long> fallImmune = new HashMap<>();
    private CannonListener cannon;
    private ExclusiveListener exclusive;
    private final Map<UUID, Long> cdTotals = new HashMap<>();
    private final Map<UUID, BossBar> huds = new HashMap<>();
    private final Map<String, Long> lastMsg = new HashMap<>();
    private final Set<UUID> hudToggled = new HashSet<>(); // players who flipped the default

    @Override
    public void onEnable() {
        saveDefaultConfig();
        items = new Items(this);
        stuns = new StunManager(this);
        registerRecipe();

        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(new StunListener(this), this);
        pm.registerEvents(new WeaponListener(this), this);
        cannon = new CannonListener(this);
        pm.registerEvents(cannon, this);
        exclusive = new ExclusiveListener(this);
        pm.registerEvents(exclusive, this);

        Commands cmds = new Commands(this);
        for (String n : List.of("legspear", "legspearhud", "givelegendaryspear", "givedashspear", "givestunsword", "givedashmace", "dashmace", "givecannonsword", "cannonsword", "lorelegspear")) {
            PluginCommand c = getCommand(n);
            if (c != null) {
                c.setExecutor(cmds);
                c.setTabCompleter(cmds);
            }
        }
        loadPlayers();
        startHud();
    }

    @Override
    public void onDisable() {
        if (hudTask != null) hudTask.cancel();
        for (UUID u : new ArrayList<>(huds.keySet())) hudRemove(u);
        if (stuns != null) stuns.shutdown();
        if (cannon != null) cannon.shutdown();
        if (recipeKey != null) Bukkit.removeRecipe(recipeKey);
    }

    public void reload() {
        reloadConfig();
        registerRecipe();
        startHud();
    }

    public Items items() { return items; }
    public StunManager stuns() { return stuns; }
    public ExclusiveListener exclusive() { return exclusive; }
    public Set<UUID> stunEnabled() { return stunEnabled; }
    public Map<UUID, Long> fallImmune() { return fallImmune; }

    // ---------- cooldowns ----------
    public long cdRemaining(UUID u, String id) {
        Map<UUID, Long> m = cooldowns.get(id);
        if (m == null) return 0;
        Long end = m.get(u);
        if (end == null) return 0;
        return Math.max(0, end - System.currentTimeMillis());
    }

    public void cdStart(UUID u, String id, long ms) {
        cooldowns.computeIfAbsent(id, k -> new HashMap<>()).put(u, System.currentTimeMillis() + ms);
        cdTotals.put(u, ms); // only used for the HUD bar fill
    }

    public long cdTotal(UUID u, String id) {
        return Math.max(1, cdTotals.getOrDefault(u, 1000L));
    }

    // ---------- messages ----------
    public Component text(String path, Object... kv) {
        String s = getConfig().getString("messages." + path, "");
        for (int i = 0; i + 1 < kv.length; i += 2) s = s.replace("{" + kv[i] + "}", String.valueOf(kv[i + 1]));
        return MM.deserialize(s);
    }

    /** Feedback message in chat. Throttled (same message max once per 1.5s) and silenced when the player turned the HUD off. */
    public void bar(Player p, String path, Object... kv) {
        String s = getConfig().getString("messages." + path, "");
        if (s.isBlank() || !hudEnabled(p.getUniqueId())) return;
        String k = p.getUniqueId() + path;
        long now = System.currentTimeMillis();
        Long last = lastMsg.get(k);
        if (last != null && now - last < 1500) return;
        lastMsg.put(k, now);
        p.sendMessage(text(path, kv));
    }

    // ---------- HUD toggle (per player, saved in players.yml) ----------
    public boolean hudEnabled(UUID u) {
        boolean def = getConfig().getBoolean("hud.default-enabled", true);
        return def != hudToggled.contains(u);
    }

    public boolean toggleHud(Player p) {
        UUID u = p.getUniqueId();
        if (!hudToggled.remove(u)) hudToggled.add(u);
        savePlayers();
        if (!hudEnabled(u)) hudRemove(u);
        return hudEnabled(u);
    }

    private void loadPlayers() {
        File f = new File(getDataFolder(), "players.yml");
        if (!f.exists()) return;
        for (String x : YamlConfiguration.loadConfiguration(f).getStringList("toggled")) {
            try {
                hudToggled.add(UUID.fromString(x));
            } catch (IllegalArgumentException ignored) {
            }
        }
    }

    private void savePlayers() {
        YamlConfiguration y = new YamlConfiguration();
        List<String> l = new ArrayList<>();
        for (UUID u : hudToggled) l.add(u.toString());
        y.set("toggled", l);
        try {
            y.save(new File(getDataFolder(), "players.yml"));
        } catch (IOException ex) {
            getLogger().warning("Could not save players.yml: " + ex.getMessage());
        }
    }

    public void hudRemove(UUID u) {
        BossBar b = huds.remove(u);
        Player p = Bukkit.getPlayer(u);
        if (b != null && p != null) p.hideBossBar(b);
    }

    // ---------- recipe ----------
    private void registerRecipe() {
        if (recipeKey != null) Bukkit.removeRecipe(recipeKey);
        recipeKey = new NamespacedKey(this, "legendary_spear");
        if (!getConfig().getBoolean("recipe.enabled", true)) return;
        if (Items.spearMaterial() == Material.TRIDENT) {
            getLogger().warning("This server has no NETHERITE_SPEAR (needs 1.21.11+), crafting recipe disabled.");
            return;
        }
        ShapedRecipe r = new ShapedRecipe(recipeKey, items.craftedSpear());
        r.shape(" N ", " T ", " T ");
        r.setIngredient('N', Material.NETHER_STAR);
        r.setIngredient('T', Material.TRIDENT);
        Bukkit.addRecipe(r);
    }

    // ---------- HUD (boss bar) ----------
    private void startHud() {
        if (hudTask != null) {
            hudTask.cancel();
            hudTask = null;
        }
        for (UUID u : new ArrayList<>(huds.keySet())) hudRemove(u);
        if (!getConfig().getBoolean("hud.enabled", true)) return;
        hudTask = Bukkit.getScheduler().runTaskTimer(this, () -> {
            for (Player p : Bukkit.getOnlinePlayers()) updateHud(p);
        }, 5L, 5L);
    }