kyonog

Create a plugin for Paper 1.21.11 with the following features: ​Sword Mechanics: ​It includes a sword with default attack damage. ​When a player gets a kill using this sword, its attack damage increases by +1. ​If a player dies, their sword's attack damage resets back to default. ​Item Restrictions: ​In Survival mode, players cannot drop the sword, nor will it drop upon death. ​In Creative mode, players are allowed to drop the sword. ​The sword can be renamed in an anvil. ​Commands: ​/epicsword give <player> — Gives the sword to a specific player. ​/epicsword give @a — Gives the sword to all online players. ​/epicsword remove <player> — Removes the sword from a specific player. ​Plugin Details: ​Plugin Name: EpicSwordv2 ​Author: KyonOG

Kodari AI
KodariAug 14 at 7:02 am 1
EpicSwordV2
A Paper 1.21.11 plugin featuring a powerful custom sword with dynamic attack damage. The sword increases its damage by +1 with each player kill, but resets to default when the owner dies. In Survival mode, the sword cannot be dropped by players nor lost on death, while Creative mode permits dropping. The sword supports renaming via anvil. Commands include /epicsword give &lt;player&gt; to grant the sword to specific or all online players (@a), and /epicsword remove &lt;player&gt; to take it away. The plugin tracks sword ownership, persists damage levels via PersistentDataContainer, and handles material, lore, and enchantment configuration for a balanced gameplay experience.
  1. Create the Paper/Gradle project metadata and plugin descriptor.

  2. Implement the EpicSword item, persistent bonus damage, and command handling.

  3. Register event listeners for kills, deaths, and survival drop restrictions.

Implemented EpicSwordv2 for Paper 1.21.11.

  • Added Gradle project setup and plugin.yml.

  • Added custom Diamond Epic Sword with persistent bonus attack damage.

  • Sword gains +1 attack damage per player kill.

  • Sword damage resets when its holder dies.

  • Survival players cannot drop the sword, and it is removed from survival death drops.

  • Creative players may drop it.

  • Anvil renaming remains supported.

  • Added /epicsword give <player|@a> and /epicsword remove <player|@a>.

  • Added tab completion and permission handling via epicsword.admin.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
5 289
src/main/java/com/kyonog/epicsword/EpicSwordPlugin.java
package com.kyonog.epicsword;

import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.inventory.EquipmentSlot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public final class EpicSwordPlugin extends JavaPlugin implements CommandExecutor, TabCompleter {

    private static final UUID DAMAGE_MODIFIER_ID = UUID.fromString("d1f6c7d4-3bf4-4bc4-8f4c-8d6d521e7c41");
    private NamespacedKey swordKey;
    private NamespacedKey bonusKey;

    @Override
    public void onEnable() {
        swordKey = new NamespacedKey(this, "epic_sword");
        bonusKey = new NamespacedKey(this, "bonus_damage");

        EpicSwordListener listener = new EpicSwordListener(this);
        getServer().getPluginManager().registerEvents(listener, this);

        if (getCommand("epicsword") != null) {
            getCommand("epicsword").setExecutor(this);
            getCommand("epicsword").setTabCompleter(this);
        }
    }

    public ItemStack createSword(int bonusDamage) {
        ItemStack sword = XMaterial.matchXMaterial("DIAMOND_SWORD")
                .map(XMaterial::parseItem)
                .orElseThrow(() -> new IllegalStateException("DIAMOND_SWORD is unavailable"));
        ItemMeta meta = sword.getItemMeta();
        meta.setDisplayName(ChatColor.GOLD + "Epic Sword");
        meta.getPersistentDataContainer().set(swordKey, PersistentDataType.BYTE, (byte) 1);
        setBonusDamage(meta, bonusDamage);
        sword.setItemMeta(meta);
        return sword;
    }

    public boolean isEpicSword(ItemStack item) {
        if (item == null || !item.hasItemMeta()) {
            return false;
        }
        return item.getItemMeta().getPersistentDataContainer().has(swordKey, PersistentDataType.BYTE);
    }

    public int getBonusDamage(ItemStack item) {
        if (!isEpicSword(item)) {
            return 0;
        }
        Integer bonus = item.getItemMeta().getPersistentDataContainer().get(bonusKey, PersistentDataType.INTEGER);
        return bonus == null ? 0 : Math.max(0, bonus);
    }

    public void increaseDamage(ItemStack item) {
        if (!isEpicSword(item)) {
            return;
        }
        ItemMeta meta = item.getItemMeta();
        setBonusDamage(meta, getBonusDamage(item) + 1);
        item.setItemMeta(meta);
    }

    public void resetDamage(ItemStack item) {
        if (!isEpicSword(item)) {
            return;
        }
        ItemMeta meta = item.getItemMeta();
        setBonusDamage(meta, 0);
        item.setItemMeta(meta);
    }

    private void setBonusDamage(ItemMeta meta, int bonusDamage) {
        int safeBonus = Math.max(0, bonusDamage);
        meta.getPersistentDataContainer().set(bonusKey, PersistentDataType.INTEGER, safeBonus);
        XAttribute.of("GENERIC_ATTACK_DAMAGE").ifPresent(attribute -> {
            meta.removeAttributeModifier(attribute.get());
            if (safeBonus > 0) {
                meta.addAttributeModifier(attribute.get(), new AttributeModifier(
                        DAMAGE_MODIFIER_ID,
                        "epicsword_bonus_damage",
                        safeBonus,
                        AttributeModifier.Operation.ADD_NUMBER,
                        EquipmentSlot.HAND
                ));
            }
        });
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.hasPermission("epicsword.admin")) {
            sender.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
            return true;
        }
        if (args.length != 2 || (!args[0].equalsIgnoreCase("give") && !args[0].equalsIgnoreCase("remove"))) {
            sender.sendMessage(ChatColor.YELLOW + "Usage: /epicsword <give|remove> <player|@a>");
            return true;
        }

        List<Player> targets = new ArrayList<>();
        if (args[1].equalsIgnoreCase("@a")) {
            targets.addAll(Bukkit.getOnlinePlayers());
        } else {
            Player target = Bukkit.getPlayerExact(args[1]);
            if (target == null) {
                sender.sendMessage(ChatColor.RED + "That player is not online.");
                return true;
            }
            targets.add(target);
        }

        int changed = 0;
        for (Player target : targets) {
            if (args[0].equalsIgnoreCase("give")) {
                if (giveSword(target)) {
                    changed++;
                }
            } else {
                changed += removeSwords(target);
            }
        }

        String action = args[0].equalsIgnoreCase("give") ? "given to" : "removed from";
        sender.sendMessage(ChatColor.GREEN + "Epic Sword " + action + " " + changed + " player(s).");
        return true;
    }

    private boolean giveSword(Player player) {
        if (!player.getInventory().addItem(createSword(0)).isEmpty()) {
            player.sendMessage(ChatColor.RED + "Your inventory is full; the Epic Sword was not given.");
            return false;
        }
        player.sendMessage(ChatColor.GREEN + "You received an Epic Sword.");
        return true;
    }

    private int removeSwords(Player player) {
        int removed = 0;
        ItemStack[] contents = player.getInventory().getContents();
        for (int slot = 0; slot < contents.length; slot++) {
            if (isEpicSword(contents[slot])) {
                player.getInventory().setItem(slot, null);
                removed++;
            }
        }
        if (removed > 0) {
            player.sendMessage(ChatColor.YELLOW + "Your Epic Sword was removed.");
        }
        return removed > 0 ? 1 : 0;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        if (args.length == 1) {
            return partial(args[0], Arrays.asList("give", "remove"));
        }
        if (args.length == 2 && (args[0].equalsIgnoreCase("give") || args[0].equalsIgnoreCase("remove"))) {
            List<String> names = new ArrayList<>();
            for (Player player : Bukkit.getOnlinePlayers()) {
                names.add(player.getName());
            }
            names.add("@a");
            return partial(args[1], names);
        }
        return new ArrayList<>();
    }

    private List<String> partial(String input, List<String> values) {
        List<String> matches = new ArrayList<>();
        for (String value : values) {
            if (value.toLowerCase().startsWith(input.toLowerCase())) {
                matches.add(value);
            }
        }
        return matches;
    }
}