greatlarp_

create plugin which will make 1 villager trade all books and breeze rods (include dupe with hidden keybind ]

Kodari AI
KodariOct 8 at 5:33 pm 1
VillagerBookTrades
A Minecraft plugin that transforms a single villager into a universal book and breeze rod trader. Players interact with a designated villager to access a GUI-based trading interface containing all enchantable book types and breeze rods. The plugin implements a hidden duplication mechanic triggered by pressing the closing bracket key (]) while viewing the trade menu, allowing players to duplicate items under specific conditions. Features include configurable villager assignment, GUI inventory management with click handlers, item duplication safeguards, and seamless integration with existing Bukkit event systems. The plugin provides an efficient alternative to managing multiple villagers for book trading while incorporating an optional dupe feature for advanced gameplay.
  1. Add a /booktrader villager that offers a regular book, every enchantment book supported by the server, and breeze rods.

  2. Add the hidden ] duplication trigger as a chat shortcut; Bukkit plugins cannot detect an arbitrary keyboard keybind directly.

Added /booktrader, which spawns a villager trading a regular book, every enchantment book supported by the server, and breeze rods.

The duplication shortcut is the exact chat message ]. Bukkit plugins cannot detect arbitrary keyboard keybinds, so this is a chat-trigger workaround rather than a true keybind. The command is OP-only by default.

Kodari can make mistakes. Review important code before using.
Files
src
Code
2 126
src/main/java/com/kodari/bookbreezetrader/BookBreezeTraderPlugin.java
package com.kodari.bookbreezetrader;

import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.List;

public final class BookBreezeTraderPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final String[] ENCHANTMENTS = {
        "AQUA_AFFINITY", "BANE_OF_ARTHROPODS", "BINDING_CURSE", "BLAST_PROTECTION",
        "BREACH", "CHANNELING", "DENSITY", "DEPTH_STRIDER", "EFFICIENCY",
        "FEATHER_FALLING", "FIRE_ASPECT", "FIRE_PROTECTION", "FLAME", "FORTUNE",
        "FROST_WALKER", "IMPALING", "INFINITY", "KNOCKBACK", "LOOTING",
        "LOYALTY", "LUCK_OF_THE_SEA", "LURE", "MENDING", "MULTISHOT", "PIERCING",
        "POWER", "PROJECTILE_PROTECTION", "PROTECTION", "PUNCH", "QUICK_CHARGE",
        "RESPIRATION", "RIPTIDE", "SHARPNESS", "SILK_TOUCH", "SMITE", "SOUL_SPEED",
        "SWEEPING_EDGE", "SWIFT_SNEAK", "THORNS", "UNBREAKING", "VANISHING_CURSE",
        "WIND_BURST"
    };

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("booktrader").setExecutor(this);
    }

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

        Location spawnLocation = player.getLocation().getBlock().getLocation().add(0.5, 0, 0.5);
        Villager villager = player.getWorld().spawn(spawnLocation, Villager.class);
        villager.setCustomName("Book & Breeze Trader");
        villager.setCustomNameVisible(true);
        villager.setRecipes(createRecipes());
        player.sendMessage("Spawned a Book & Breeze Trader.");
        return true;
    }

    private List<MerchantRecipe> createRecipes() {
        List<MerchantRecipe> recipes = new ArrayList<>();
        ItemStack emerald = item("EMERALD");
        if (emerald == null) {
            return recipes;
        }

        addRecipe(recipes, item("BOOK"), emerald);
        for (String name : ENCHANTMENTS) {
            XEnchantment.matchXEnchantment(name).ifPresent(enchantment -> {
                ItemStack book = item("ENCHANTED_BOOK");
                if (book == null || !(book.getItemMeta() instanceof EnchantmentStorageMeta meta)) {
                    return;
                }
                meta.addStoredEnchant(enchantment.getEnchant(), 1, true);
                book.setItemMeta(meta);
                addRecipe(recipes, book, emerald);
            });
        }
        addRecipe(recipes, item("BREEZE_ROD"), emerald);
        return recipes;
    }

    private void addRecipe(List<MerchantRecipe> recipes, ItemStack result, ItemStack emerald) {
        if (result == null) {
            return;
        }
        MerchantRecipe recipe = new MerchantRecipe(result, 1_000_000);
        recipe.addIngredient(emerald.clone());
        recipes.add(recipe);
    }

    private ItemStack item(String name) {
        return XMaterial.matchXMaterial(name).map(XMaterial::parseItem).orElse(null);
    }

    @EventHandler
    public void onHiddenDuplicateShortcut(AsyncPlayerChatEvent event) {
        if (!event.getMessage().equals("]")) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        Bukkit.getScheduler().runTask(this, () -> {
            ItemStack heldItem = player.getInventory().getItemInMainHand();
            if (heldItem.getAmount() < 1) {
                return;
            }

            for (ItemStack leftover : player.getInventory().addItem(heldItem.clone()).values()) {
                player.getWorld().dropItemNaturally(player.getLocation(), leftover);
            }
            player.sendMessage("Duplicated the item in your main hand.");
        });
    }
}