Skip to content

About

Lighweight Inventory API

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

ScrollInventoryAPI

Scroll through Paper inventory contents with the mouse wheel.

Scrolling inventory demo

Minecraft does not send an event when a player hovers an inventory slot and scrolls. The only client signal for that is the bundle selection packet. This library puts items into a carrier that the client treats as a bundle, reads ServerboundSelectBundleItemPacket through PacketEvents, and calls your handler with ScrollDirection.PREVIOUS or ScrollDirection.NEXT.

The player still sees the original item. The carrier copies ITEM_MODEL and ITEM_NAME from it, so there is no resource pack and no GUI framework involved.

Requirements

  • Paper 1.21.4+
  • Java 21
  • PacketEvents 2.7+ installed on the server

Install

repositories {
    maven("https://jitpack.io")
}

dependencies {
    implementation("com.github.00re3se.ScrollInventoryAPI:scrollinventoryapi:<tag>")
}

PacketEvents stays a runtime plugin. A Paper plugin needs its classes on the classpath:

dependencies:
  server:
    packetevents:
      load: BEFORE
      required: true
      join-classpath: true

Setup

Register the datapack that ships with the library from your bootstrapper:

public final class MyBootstrap implements PluginBootstrap {
    @Override
    public void bootstrap(BootstrapContext context) {
        ScrollInventoryDatapack.register(context);
    }
}
bootstrapper: com.example.MyBootstrap

Enable scrolling once in onEnable:

if (!ScrollInventory.init(this)) {
    getLogger().warning("Scrolling requires Paper 1.21.4+ and PacketEvents.");
    return;
}

Then build a menu. Wrapping a slot is all it takes to make that slot scrollable:

public final class ShopMenu implements InventoryHolder, ScrollableInventory {

    private final List<ItemStack> items = new ArrayList<>();
    private final Inventory inventory = Bukkit.createInventory(this, 54, Component.text("Shop"));
    private final ScrollHandler handler = this::scroll;

    private int offset;

    @Override
    public Inventory getInventory() {
        return inventory;
    }

    @Override
    public ScrollHandler getScrollHandler() {
        return handler;
    }

    private boolean scroll(ScrollContext context) {
        int step = context.direction() == ScrollDirection.NEXT ? 9 : -9;
        offset = Math.max(0, Math.min(offset + step, maxOffset()));
        render();
        return true;
    }

    private void render() {
        for (int slot = 0; slot < 45; slot++) {
            int index = offset + slot;
            inventory.setItem(slot, index < items.size()
                    ? ScrollMenu.createScrollable(items.get(index))
                    : ScrollMenu.emptySlot());
        }
    }

    private int maxOffset() {
        return Math.max(0, items.size() - 45);
    }
}

Slots you do not wrap render normally and ignore the wheel. If your holder cannot implement ScrollableInventory, register the handler yourself:

ScrollInventory.register(inventory, handler);

How it works

  1. ScrollMenu.createScrollable(item) returns a carrier in the #minecraft:bundles item tag. Its BUNDLE_CONTENTS holds tagged dummy items, and ITEM_MODEL/ITEM_NAME are copied from the original item so the client draws it normally.
  2. Scrolling over the slot moves the bundle's selected index and the client sends ServerboundSelectBundleItemPacket.
  3. The library compares the index with the previous one and calls your handler with PREVIOUS or NEXT.

The datapack adds minecraft:paper to #minecraft:bundles, so the default carrier can be PAPER instead of a real bundle. That is what removes the bundle fullness bar. If the datapack is not installed, the default falls back to Material.BUNDLE; scrolling still works but the bar is visible.

ScrollMenu.emptySlot() uses the element-less minecraft:air model, so filler slots render nothing but still capture the wheel.

API

Package Type Purpose
dev.reese.scrollinventoryapi ScrollInventory init, shutdown, register, unregister, isSupported
...event ScrollableInventory Implement on your InventoryHolder
...event ScrollHandler Receives ScrollContext, returns whether the view changed
...event ScrollContext Player, inventory, slot and ScrollDirection
...event ScrollDirection PREVIOUS / NEXT
...menu ScrollMenu Create and inspect scrollable items
...menu ScrollMenuConfig Carrier material, dummy count, tooltip and preview options
...datapack ScrollInventoryDatapack Registers the bundled #bundles tag datapack

Build

./gradlew build

About

Lighweight Inventory API

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages