Manny J

Create a mod which makes a new sword that gives the strength effect when clicked E while holding the sword

Kodari AI
KodariOct 24 at 2:28 am 1
StrengthSwordMod
Adds a custom sword that grants the Strength effect when the player presses the E key while holding it. The sword is crafted with a unique recipe and displays a custom name and lore. When activated, the sword applies Strength II for a configurable duration with a cooldown period. Includes particle effects and sound on activation, along with configurable settings for effect level, duration, and cooldown.
  1. Create the Paper plugin entry point and command to obtain the custom sword.

  2. Activate Strength II on right-click; Bukkit plugins cannot detect the E key directly.

  3. Add the plugin descriptor and Gradle build setup.

Created the Strength Sword plugin. Use /strengthsword to get the sword, then right-click to gain Strength II for 10 seconds. Bukkit plugins can’t detect the E key directly, so the ability uses right-click instead.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 106
src/main/java/com/kodari/strengthsword/StrengthSwordPlugin.java
package com.kodari.strengthsword;

import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

public final class StrengthSwordPlugin extends JavaPlugin implements Listener {
    private NamespacedKey swordKey;

    @Override
    public void onEnable() {
        swordKey = new NamespacedKey(this, "strength_sword");
        getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can use this command.");
            return true;
        }

        ItemStack sword = XMaterial.matchXMaterial("DIAMOND_SWORD").map(XMaterial::parseItem).orElse(null);
        if (sword == null) {
            player.sendMessage(ChatColor.RED + "Could not create the Strength Sword.");
            return true;
        }

        ItemMeta meta = sword.getItemMeta();
        meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Strength Sword");
        meta.setLore(java.util.List.of(ChatColor.GRAY + "Right-click to gain Strength II for 10 seconds."));
        meta.getPersistentDataContainer().set(swordKey, PersistentDataType.BYTE, (byte) 1);
        sword.setItemMeta(meta);
        player.getInventory().addItem(sword);
        player.sendMessage(ChatColor.GREEN + "You received a Strength Sword.");
        return true;
    }

    @EventHandler
    public void onSwordUse(PlayerInteractEvent event) {
        Action action = event.getAction();
        if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        ItemStack item = event.getItem();
        if (item == null || !item.hasItemMeta()) {
            return;
        }

        Byte marked = item.getItemMeta().getPersistentDataContainer().get(swordKey, PersistentDataType.BYTE);
        if (marked == null || marked != (byte) 1) {
            return;
        }

        XPotion.matchXPotion("STRENGTH")
                .map(potion -> potion.buildPotionEffect(200, 1))
                .ifPresent(event.getPlayer()::addPotionEffect);
    }
}