Skip to content
Back to skills

Paper Menu Type Gui

ASecurity

Paper MenuType 原版介面:不需要方塊就開啟可攜式工作台、鐵砧、切石機、鍛造台、砂輪、織布機、製圖台與自訂交易介面;鐵砧花費上限、方塊實體類介面的暫存陷阱、關閉時歸還物品 / Paper MenuType vanilla menus: open portable crafting tables, anvils, stonecutters, smithing tables, grindstones, looms, cartography tables and custom merchant screens without a block; anvil cost limits, the temporary block-entity trap, returning items on close

  • 3 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 23, 2026
ai-agentsgojavaapi

Works with

  • cursor
  • cli
  • api

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add MrPippi/MJP-Paper-Skills --skill paper-menu-type-gui --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Paper Menu Type Gui?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Paper Menu Type Gui
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mrpippi-paper-menu-type-gui/badge)](https://www.skillsdirectory.com/skills/mrpippi-paper-menu-type-gui)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: paper-menu-type-gui
description: "Paper MenuType 原版介面:不需要方塊就開啟可攜式工作台、鐵砧、切石機、鍛造台、砂輪、織布機、製圖台與自訂交易介面;鐵砧花費上限、方塊實體類介面的暫存陷阱、關閉時歸還物品 / Paper MenuType vanilla menus: open portable crafting tables, anvils, stonecutters, smithing tables, grindstones, looms, cartography tables and custom merchant screens without a block; anvil cost limits, the temporary block-entity trap, returning items on close"
---

# Paper MenuType GUI

## Skill Name

`paper-menu-type-gui`

## Purpose

`MenuType` opens the **real vanilla screen** for a workstation without a block: crafting, anvil, stonecutter, smithing, grindstone, loom, cartography table, merchant and more. Unlike a chest GUI (`paper-chest-gui`) the client runs its normal logic (recipe book, anvil renaming, stonecutter list), and unlike `nms-custom-menu` no NMS is needed.

```java
MenuType.ANVIL.builder().title(Component.text("Portable Anvil")).build(player);   // returns an AnvilView
player.openInventory(view);
```

What the view builder does (read from the Paper 26.3 implementation):

| Builder call | Effect |
|------|------|
| none besides `title` | The menu is bound to the player's own position; **the right choice for portable menus** |
| `location(loc)` | Binds the menu to the block at `loc` (a real anvil there takes damage, a real furnace keeps smelting) |
| `checkReachable(true)` | The menu closes when the player walks out of reach of that position; the default is `false` |

**Two kinds of menus behave differently without a block:**

| Kind | Menus | Without `location` |
|------|------|------|
| Block menus | `CRAFTING`, `ANVIL`, `STONECUTTER`, `SMITHING`, `GRINDSTONE`, `LOOM`, `CARTOGRAPHY_TABLE`, `ENCHANTMENT` | Work normally; vanilla gives the input items back when the screen closes |
| Block-entity menus | `FURNACE`, `SMOKER`, `BLAST_FURNACE`, `BREWING_STAND`, `HOPPER`, `SHULKER_BOX`, `CRAFTER_3X3`, `GENERIC_9X3`, `GENERIC_9X6`, `BEACON`, `LECTERN` | Paper creates a **temporary block entity that is not in the world**: it does not tick (a portable furnace never smelts) and nothing saves its contents |

For a storage GUI use an `Inventory` (`paper-chest-gui`); use block-entity menu types only with `location(...)` pointing at a real block.

## Paper Version Requirements

- Paper 1.21.11 / 26.3 (`MenuType`, the view builders, `AnvilView`, `Bukkit.createMerchant` and `PlayerPurchaseEvent` have identical signatures on both; both compile-verified)
- `MerchantView#updateRecipes()` exists from Paper 26.3 **build 270**; the template marks that line `// @1.21.11:` and reopens the view on 1.21.11
- **Spigot**: Core portable — `MenuType.Typed#builder`, `AnvilView#setMaximumRepairCost` and `Bukkit.createMerchant` exist in Spigot-API 26.3; replace the Adventure titles (`title(Component)`) with Spigot's String titles

## Triggers

- 「MenuType」「可攜式工作台」「portable crafting table」「隨身鐵砧」「portable anvil」
- 「打開原版介面」「open vanilla menu」「切石機介面」「鍛造台介面」「交易介面」「custom merchant」
- 「AnvilView」「setMaximumRepairCost」「Too Expensive」「PrepareAnvilEvent」
- 「限量商店」「limited stock shop」「交易即時更新」「updateRecipes」「PlayerPurchaseEvent」

## Inputs

| Parameter | Example | Description |
|------|------|------|
| `package` | `com.example.workstations` | Output package |
| `menus` | crafting, anvil, stonecutter, merchant | Which screens to open |
| `anvil_max_cost` | `100` | Raise the vanilla 40-level limit for portable anvils |
| `trades` | 1 emerald → 4 bread | Offers of the custom merchant |
| `stock` | 20 per offer, restock daily | Shared stock for a `StockedShop` (optional) |

## Outputs

- `Workstations.java` — opens portable block menus with a title
- `PortableAnvil.java` — opens anvils, raises the cost limit for those anvils only, forgets them on close
- `TradeMenu.java` — a custom merchant screen with fixed trades
- `StockedShop.java` — a merchant with a stock shared by all players; open screens update when someone buys or the shop restocks

## Build Setup

See [`references/paper-api-platform.md`](references/paper-api-platform.md). Only `paper-api` (`compileOnly`) is required.

## Code Template

### `Workstations.java`

```java
package com.example.workstations;

import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.MenuType;

/**
 * Portable vanilla workstations. No {@code location(...)}: the menu follows the player, needs no block, and vanilla
 * returns the input items when the screen closes. Main thread only.
 */
public final class Workstations {

    private Workstations() {
    }

    public static void crafting(Player player) {
        open(player, MenuType.CRAFTING.builder().title(Component.text("Portable Crafting")).build(player));
    }

    public static void stonecutter(Player player) {
        open(player, MenuType.STONECUTTER.builder().title(Component.text("Stonecutter")).build(player));
    }

    public static void smithing(Player player) {
        open(player, MenuType.SMITHING.builder().title(Component.text("Smithing")).build(player));
    }

    public static void grindstone(Player player) {
        open(player, MenuType.GRINDSTONE.builder().title(Component.text("Grindstone")).build(player));
    }

    public static void loom(Player player) {
        open(player, MenuType.LOOM.builder().title(Component.text("Loom")).build(player));
    }

    public static void cartography(Player player) {
        open(player, MenuType.CARTOGRAPHY_TABLE.builder().title(Component.text("Cartography")).build(player));
    }

    private static void open(Player player, InventoryView view) {
        player.openInventory(view);
    }
}
```

### `PortableAnvil.java`

```java
package com.example.workstations;

import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.MenuType;
import org.bukkit.inventory.view.AnvilView;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * Portable anvil whose "Too Expensive!" limit is raised, without changing real anvils.
 * Players with a portable anvil open are tracked by UUID and dropped on close, so a real anvil opened later keeps vanilla limits.
 */
public final class PortableAnvil implements Listener {

    private final int maxCost;
    private final Set<UUID> open = new HashSet<>();

    public PortableAnvil(int maxCost) {
        this.maxCost = maxCost;
    }

    public void open(Player player) {
        AnvilView view = MenuType.ANVIL.builder()
                .title(Component.text("Portable Anvil"))
                .build(player);
        view.setMaximumRepairCost(maxCost);
        player.openInventory(view);
        open.add(player.getUniqueId());          // after openInventory: opening closes the previous screen first
    }

    /** Runs on every input change; the cost limit must be re-applied because vanilla recomputes the view. */
    @EventHandler
    public void onPrepare(PrepareAnvilEvent event) {
        if (open.contains(event.getView().getPlayer().getUniqueId())) {
            event.getView().setMaximumRepairCost(maxCost);
        }
    }

    @EventHandler
    public void onClose(InventoryCloseEvent event) {
        if (event.getView() instanceof AnvilView) {
            open.remove(event.getPlayer().getUniqueId());
        }
    }

}
```

### `TradeMenu.java`

```java
package com.example.workstations;

import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MenuType;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe;

import java.util.List;

/** A villager-style trade screen with fixed offers and no villager. */
public final class TradeMenu {

    private TradeMenu() {
    }

    public static void open(Player player) {
        Merchant merchant = Bukkit.createMerchant();
        merchant.setRecipes(List.of(
                offer(new ItemStack(Material.EMERALD), new ItemStack(Material.BREAD, 4)),
                offer(new ItemStack(Material.EMERALD, 3), new ItemStack(Material.ARROW, 16))));
        player.openInventory(MenuType.MERCHANT.builder()
                .merchant(merchant)
                .title(Component.text("Wandering Baker"))
                .build(player));
    }

    /** maxUses = Integer.MAX_VALUE: the offer never locks; no villager XP. */
    private static MerchantRecipe offer(ItemStack cost, ItemStack result) {
        MerchantRecipe recipe = new MerchantRecipe(result, 0, Integer.MAX_VALUE, false);
        recipe.addIngredient(cost);
        return recipe;
    }
}
```

### `StockedShop.java`

A merchant whose offers share one stock across every player. When someone buys, every open screen of the shop shows the new stock (or the red "out of stock" cross) at once.

Two server behaviors this relies on (read from the Paper 26.3 build 273 implementation):

- A trade in a merchant from `Bukkit.createMerchant()` fires `PlayerPurchaseEvent` before the items move. Cancelling it removes the result from the cursor and resends the offers to that player.
- Changing a merchant's offers (`setRecipes`, `MerchantRecipe#setUses`) does **not** reach a screen that is already open. `MerchantView#updateRecipes()` (Paper 26.3 build 270+) recalculates the result slot and resends the offers. 1.21.11 has no such method, so the template opens a fresh view instead.

```java
package com.example.workstations;

import io.papermc.paper.event.player.PlayerPurchaseEvent;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MenuType;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.inventory.view.MerchantView;
import org.bukkit.plugin.Plugin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.IntStream;

/**
 * A trade screen with a stock shared by every player. Main thread only (Paper); register it as a listener.
 * Each player gets an own Merchant (a custom merchant serves one trader); the stock lives here.
 */
public final class StockedShop implements Listener {

    /** One offer and how many times it can be bought before a restock. */
    public record Offer(ItemStack cost, ItemStack result, int maxStock) {
        public Offer {
            Objects.requireNonNull(cost, "cost");
            Objects.requireNonNull(result, "result");
            if (maxStock <= 0) throw new IllegalArgumentException("maxStock must be positive: " + maxStock);
            cost = cost.clone();
            result = result.clone();
        }
    }

    private final Plugin plugin;
    private final Component title;
    private final List<Offer> offers;
    private final int[] stock;
    private final Map<UUID, MerchantView> viewers = new HashMap<>();
    private boolean refreshQueued;

    public StockedShop(Plugin plugin, Component title, List<Offer> offers) {
        if (offers.isEmpty()) throw new IllegalArgumentException("a shop needs at least one offer");
        this.plugin = plugin;
        this.title = title;
        this.offers = List.copyOf(offers);
        this.stock = this.offers.stream().mapToInt(Offer::maxStock).toArray();
    }

    public void open(Player player) {
        Merchant merchant = Bukkit.createMerchant();
        merchant.setRecipes(recipes());
        MerchantView view = MenuType.MERCHANT.builder().merchant(merchant).title(title).build(player);
        player.openInventory(view);                  // closes the previous view first (its close event runs now)
        viewers.put(player.getUniqueId(), view);
    }

    /** Refills every offer and shows it on all open screens. */
    public void restock() {
        for (int i = 0; i < stock.length; i++) stock[i] = offers.get(i).maxStock();
        refreshAll();
    }

    public int remaining(int offerIndex) {
        return stock[offerIndex];
    }

    @EventHandler(ignoreCancelled = true)
    public void onPurchase(PlayerPurchaseEvent event) {
        MerchantView view = viewers.get(event.getPlayer().getUniqueId());
        if (view == null || view.getMerchant() != event.getMerchant()) return;    // another plugin's merchant
        int index = view.getTopInventory().getSelectedRecipeIndex();
        if (index < 0 || index >= stock.length) return;
        if (stock[index] <= 0) {
            event.setCancelled(true);                // sold out by someone else this tick; the server resends the offers
            return;
        }
        stock[index]--;
        queueRefresh();
    }

    @EventHandler
    public void onClose(InventoryCloseEvent event) {
        viewers.remove(event.getPlayer().getUniqueId(), event.getView());          // only if it is still our view
    }

    /** Next tick: the trade in progress has finished, and several purchases in one tick cause one refresh. */
    private void queueRefresh() {
        if (refreshQueued) return;
        refreshQueued = true;
        Bukkit.getScheduler().runTask(plugin, () -> {
            refreshQueued = false;
            refreshAll();
        });
    }

    private void refreshAll() {
        for (MerchantView view : List.copyOf(viewers.values())) {                 // reopen() changes the map on 1.21.11
            view.getMerchant().setRecipes(recipes());
            view.updateRecipes();                    // @1.21.11: reopen(view);
        }
    }

    /** 1.21.11 has no MerchantView#updateRecipes: open a fresh view; the old one gives its input items back on close. */
    private void reopen(MerchantView view) {
        if (view.getPlayer() instanceof Player player && player.getOpenInventory() == view) open(player);
    }

    /** New recipe objects every time: uses = sold so far, so a sold-out offer shows vanilla's red cross. */
    private List<MerchantRecipe> recipes() {
        return IntStream.range(0, offers.size()).mapToObj(i -> {
            Offer offer = offers.get(i);
            MerchantRecipe recipe = new MerchantRecipe(offer.result(), offer.maxStock() - stock[i], offer.maxStock(), false);
            recipe.addIngredient(offer.cost());
            return recipe;
        }).toList();
    }
}
```

## Recommended Directory Structure

```
src/main/java/com/example/workstations/
├── Workstations.java   <- portable block menus
├── PortableAnvil.java  <- anvil + cost limit (Listener)
├── TradeMenu.java      <- custom merchant
└── StockedShop.java    <- merchant with shared stock, live updates (Listener)
```

## Thread Safety

- `build(player)` and `openInventory` are main-thread only; open from a command executor or a scheduled task, never from an async callback
- `PortableAnvil` only touches its UUID set on the main thread (open, prepare and close all run there)
- `StockedShop` keeps its stock and viewers in plain fields: purchase, close and the scheduled refresh all run on the main thread. It is not for Folia, where players in different regions trade on different threads at the same time
- See [`references/paper-threading.md`](references/paper-threading.md)

### Key Techniques

| Topic | Approach |
|------|------|
| Portable vs. block-bound | Leave `location(...)` out for portable block menus; set it only to drive a real block |
| `checkReachable` | Default `false`. Set `true` with `location(...)` when the player must stay next to that block |
| Anvil cost | `AnvilView#setMaximumRepairCost` (the vanilla limit is 40). Re-apply it in `PrepareAnvilEvent`; `setRepairCost` sets the price of the current result |
| Telling your views apart | Track the players you opened a view for (UUID set, cleared on close), not the title text |
| Merchant | `Bukkit.createMerchant()` + `MerchantRecipe`; `MenuType.MERCHANT.builder().merchant(...)` |
| Live trade changes | Change the offers, then `MerchantView#updateRecipes()` (26.3 build 270+); on 1.21.11 open a fresh view |
| Shared stock | Own counter per offer, checked in `PlayerPurchaseEvent` (cancel when sold out); `uses` = sold so far shows vanilla's sold-out cross |
| Storage screens | Do not use block-entity menu types for storage; use `Bukkit.createInventory` with an `InventoryHolder` |

## Fallback

| Error | Cause | Fix |
|------|------|------|
| Portable furnace accepts fuel but never smelts | Block-entity menus without `location` use a temporary block entity that never ticks | Point `location(...)` at a real furnace, or do not offer it |
| Items vanish when a portable hopper / shulker / 9x3 menu closes | The temporary block entity is not saved | Use `Bukkit.createInventory` for storage |
| Anvil still says "Too Expensive!" | The limit was set once and vanilla recomputed the view | Re-apply `setMaximumRepairCost` in `PrepareAnvilEvent` |
| The menu closes immediately | `checkReachable(true)` with the player far from `location` | Leave `checkReachable` at `false` for portable menus |
| Real anvil gets damaged by a "portable" anvil | `location(...)` pointed at that anvil | Leave `location` out |
| `IllegalArgumentException: The given player must be ...` | `build` was called with a non-player `HumanEntity` | Build views only for online `Player`s |
| Changed trades do not show until the screen is reopened | Offers changed with `setRecipes` / `setUses` are not sent to an open view | Call `MerchantView#updateRecipes()` after the change (26.3 build 270+); on 1.21.11 reopen the menu |
| `NoSuchMethodError: MerchantView.updateRecipes()` | The server runs 26.3 older than build 270 | Update Paper, or use the 1.21.11 path (reopen) |
| Two players bought the last item | The stock was only shown, not checked at purchase time | Check and decrement the stock in `PlayerPurchaseEvent`; cancel when it is 0 |
| Every player has a separate stock | Each `Merchant` counts `uses` on its own offers | Keep the stock outside the merchant and rebuild the recipes from it |

Files in this skill

  • SKILL.md10.8 KB
  • examples.md3.6 KB
  • references/paper-api-platform.md7.9 KB
  • references/paper-threading.md6.9 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.