loosterloover

Make me a plugin that allows me to put any enchantment on any items. If some enchants dont match up, like infinity on a pickaxe, I want you to make a new, unique function from it. Like infinity could be infinite durability.

Kodari AI
KodariNov 3 at 5:28 am 1
EnchantForge
A Minecraft plugin that lets players apply any enchantment to any item via commands. When an enchantment is incompatible with an item (e.g., Infinity on a pickaxe), the plugin automatically creates a unique custom function instead. For example, Infinity becomes infinite durability, while other mismatches like Fire Aspect on a bow could grant flaming arrows. The plugin provides a command interface to select items, choose enchantments, and apply them, with automatic fallback behaviors for all vanilla enchantment-item conflicts. It also supports custom enchantment levels and displays the resulting effects in item lore.
  1. Set up the Java Paper plugin and its descriptor.

  2. Allow anvil combinations to apply enchantments unsafely, including stored book enchants.

  3. Give Infinity a custom infinite-durability effect on non-bow items.

Created AnyEnchantments. Anvil combinations can apply enchantments to otherwise incompatible items, including enchantments from books. Infinity on non-bow items now prevents durability loss; Infinity on bows keeps its normal behavior.

loosterloover

What version is it for

Kodari AI
KodariNov 3 at 5:30 am 1

It’s built for Paper 1.20.4 and targets Java 17. The plugin descriptor specifies API version 1.20.

loosterloover

While I can put the enchants on, they dont actually do anything. Power on a crossbow does nothing, infinity on tools doesnt give infinite durability, etc. Also, I want blast protection on tools to allow you to explode things when you right click.

Kodari AI
KodariNov 3 at 5:46 am 1
  1. Make incompatible enchantments produce their expected effects, including Power on crossbows and Infinity on tools.

  2. Add a right-click explosion ability for tools enchanted with Blast Protection.

Updated the plugin: Power now adds bow-style bonus damage to crossbow-fired arrows, Infinity prevents durability loss on non-bow items, and right-clicking with a tool enchanted with Blast Protection creates a block-breaking explosion. Explosion strength scales with the enchantment level and has a 1.5-second cooldown.

loosterloover

does it auto update?

Kodari AI
KodariNov 3 at 5:51 am 1

No. The plugin won’t update itself on your server. After changes, compile a new jar and replace the old one, then restart the server. You can enable Auto Compile in Kodari Settings to build automatically when you save, but that doesn’t install it on your server.

loosterloover

Wait i want you to update every enchant too, none of them work, not just the infinity and power.

Kodari AI
KodariNov 3 at 5:59 am 1
  1. Preserve vanilla behavior where an enchantment is compatible; add custom behavior for incompatible enchantments across combat, projectiles, mining, defense, movement, fishing, and durability.

  2. Keep anvil combining and the Blast Protection tool ability, and avoid applying custom bonuses twice to compatible items.

[Generation stopped]

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 208
src/main/java/com/kodari/anyenchantments/AnyEnchantmentsPlugin.java
package com.kodari.anyenchantments;

import com.cryptomorin.xseries.XEnchantment;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Player;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class AnyEnchantmentsPlugin extends JavaPlugin {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new EnchantmentListener(), this);
    }

    private class EnchantmentListener implements org.bukkit.event.Listener {
        private static final String POWER_METADATA = "kodari_anyench_power";
        private final Map<UUID, Long> explosionCooldowns = new HashMap<>();

        @org.bukkit.event.EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent event) {
            AnvilInventory inventory = event.getInventory();
            ItemStack left = inventory.getItem(0);
            ItemStack right = inventory.getItem(1);
            if (left == null || right == null) {
                return;
            }

            Map<Enchantment, Integer> rightEnchantments = getEnchantments(right);
            if (rightEnchantments.isEmpty()) {
                return;
            }

            ItemStack vanillaResult = event.getResult();
            boolean hasVanillaResult = vanillaResult != null;
            ItemStack result = hasVanillaResult ? vanillaResult.clone() : left.clone();
            for (Map.Entry<Enchantment, Integer> entry : rightEnchantments.entrySet()) {
                Enchantment enchantment = entry.getKey();
                if (hasVanillaResult && result.containsEnchantment(enchantment)) {
                    continue;
                }

                int level = entry.getValue();
                if (!hasVanillaResult) {
                    int existingLevel = left.getEnchantmentLevel(enchantment);
                    if (existingLevel > 0) {
                        level = existingLevel == level ? level + 1 : Math.max(existingLevel, level);
                    }
                }
                result.addUnsafeEnchantment(enchantment, level);
            }

            if (!hasVanillaResult) {
                inventory.setRepairCost(Math.max(1, inventory.getRepairCost()));
            }
            event.setResult(result);
        }

        @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onItemDamage(PlayerItemDamageEvent event) {
            ItemStack item = event.getItem();
            if ("bow".equals(item.getType().getKey().getKey())) {
                return;
            }

            XEnchantment.matchXEnchantment("INFINITY").ifPresent(infinity -> {
                if (item.containsEnchantment(infinity.getEnchant())) {
                    event.setCancelled(true);
                }
            });
        }

        @EventHandler
        public void onCrossbowProjectileLaunch(ProjectileLaunchEvent event) {
            if (!(event.getEntity() instanceof Arrow arrow)
                    || !(arrow.getShooter() instanceof Player player)) {
                return;
            }

            ItemStack heldItem = player.getInventory().getItemInMainHand();
            if (!"crossbow".equals(heldItem.getType().getKey().getKey())) {
                return;
            }

            XEnchantment.matchXEnchantment("POWER").ifPresent(power -> {
                int level = heldItem.getEnchantmentLevel(power.getEnchant());
                if (level > 0) {
                    arrow.setMetadata(POWER_METADATA,
                            new FixedMetadataValue(AnyEnchantmentsPlugin.this, level));
                }
            });
        }

        @EventHandler(ignoreCancelled = true)
        public void onCrossbowArrowDamage(EntityDamageByEntityEvent event) {
            if (!(event.getDamager() instanceof Arrow arrow) || !arrow.hasMetadata(POWER_METADATA)) {
                return;
            }

            int powerLevel = arrow.getMetadata(POWER_METADATA).get(0).asInt();
            event.setDamage(event.getDamage() + (powerLevel * 0.5) + 0.5);
        }

        @EventHandler
        public void onBlastProtectionToolUse(PlayerInteractEvent event) {
            if (event.getHand() != EquipmentSlot.HAND
                    || (event.getAction() != Action.RIGHT_CLICK_AIR
                    && event.getAction() != Action.RIGHT_CLICK_BLOCK)) {
                return;
            }

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

            int level = XEnchantment.matchXEnchantment("BLAST_PROTECTION")
                    .map(enchantment -> item.getEnchantmentLevel(enchantment.getEnchant()))
                    .orElse(0);
            if (level < 1) {
                return;
            }

            Player player = event.getPlayer();
            long now = System.currentTimeMillis();
            UUID playerId = player.getUniqueId();
            if (explosionCooldowns.getOrDefault(playerId, 0L) > now) {
                return;
            }
            explosionCooldowns.put(playerId, now + 1500L);

            event.setCancelled(true);
            Block targetBlock = event.getClickedBlock();
            if (targetBlock == null) {
                targetBlock = player.getTargetBlockExact(8);
            }

            Location explosionLocation = targetBlock == null
                    ? player.getLocation().add(player.getLocation().getDirection().multiply(3))
                    : targetBlock.getLocation().add(0.5, 0.5, 0.5);
            float power = Math.min(4.0F, 1.0F + (level * 0.5F));
            player.getWorld().createExplosion(explosionLocation, power, false, true, player);
        }

        private boolean isTool(ItemStack item) {
            String type = item.getType().getKey().getKey();
            return type.endsWith("_sword") || type.endsWith("_pickaxe")
                    || type.endsWith("_axe") || type.endsWith("_shovel")
                    || type.endsWith("_hoe") || "trident".equals(type);
        }

        private static Map<Enchantment, Integer> getEnchantments(ItemStack item) {
            ItemMeta meta = item.getItemMeta();
            if (meta instanceof EnchantmentStorageMeta storageMeta) {
                return storageMeta.getStoredEnchants();
            }
            return item.getEnchantments();
        }
    }
}