Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

88 Commits

Repository files navigation

FastInv

JitPack

Lightweight and easy-to-use inventory API for Bukkit plugins.

Features

  • Lightweight (around 500 lines of code with the Javadoc) and no dependencies
  • Compatible with all Minecraft versions starting with 1.7.10
  • Adventure components support
  • Supports custom inventories (size, title and type)
  • Built-in support for paginated inventories
  • Easy to use
  • Option to prevent a player from closing the inventory
  • The Bukkit inventory can still be directly used

Installation

Maven

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<relocations>
<relocation>
<pattern>fr.mrmicky.fastinv</pattern>
<!-- Replace 'com.yourpackae' with the package of your plugin! -->
<shadedPattern>com.yourpackage.fastinv</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>fr.mrmicky</groupId>
<artifactId>fastinv</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>

Gradle

plugins {
id 'com.gradleup.shadow' version '8.3.0'
}
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'fr.mrmicky:fastinv:3.1.2'
}
shadowJar {
// Replace 'com.yourpackage' with the package of your plugin 
relocate 'fr.mrmicky.fastinv', 'com.yourpackage.fastinv'
}

Manual

Simply copy FastInv.java and FastInvManager.java in your plugin. You can also add ItemBuilder.java if you need.

Usage

Register FastInv

Before creating inventories, register your plugin by adding FastInvManager.register(this); in the onEnable() method of your plugin:

@OverridepublicvoidonEnable() {
FastInvManager.register(this);
}

Creating an inventory class

Now you can create an inventory by creating a class that extends FastInv, and adding items in the constructor. You can also override onClick, onClose and onOpen if you need.

Basic example inventory:

packagefr.mrmicky.fastinv.test;
importfr.mrmicky.fastinv.FastInv;
importfr.mrmicky.fastinv.ItemBuilder;
importorg.bukkit.ChatColor;
importorg.bukkit.Material;
importorg.bukkit.event.inventory.InventoryClickEvent;
importorg.bukkit.event.inventory.InventoryCloseEvent;
importorg.bukkit.event.inventory.InventoryOpenEvent;
importorg.bukkit.inventory.ItemStack;
publicclassExampleInventoryextendsFastInv {
privatebooleanpreventClose = false;
publicExampleInventory() {
super(45, ChatColor.GOLD + "Example inventory");
// Add a random itemsetItem(22, newItemStack(Material.IRON_SWORD), e -> e.getWhoClicked().sendMessage("You clicked on the sword"));
// Add some blocks to the borderssetItems(getBorders(), newItemBuilder(Material.LAPIS_BLOCK).name(" ").build());
// Add a simple item to prevent closing the inventorysetItem(34, newItemBuilder(Material.BARRIER).name(ChatColor.RED + "Prevent close").build(), e -> {
this.preventClose = !this.preventClose;
});
// Prevent from closing when preventClose is truesetCloseFilter(p -> this.preventClose);
}
@OverridepublicvoidonOpen(InventoryOpenEventevent) {
event.getPlayer().sendMessage(ChatColor.GOLD + "You opened the inventory");
}
@OverridepublicvoidonClose(InventoryCloseEventevent) {
event.getPlayer().sendMessage(ChatColor.GOLD + "You closed the inventory");
}
@OverridepublicvoidonClick(InventoryClickEventevent) {
// do something
}
}

The inventory can be opened with the open(player) method:

newExampleInventory().open(player);

Paginated inventory

FastInv also supports paginated inventories, which can be created by using PaginatedFastInv instead of FastInv.

Content can be added to the inventory using addContent or setContent, and pagination items can be added with previousPageItem and nextPageItem.

You can also use onPageChange to execute code when the page changes, and the #currentPage(), #lastPage(), #isFirstPage() and #isLastPage() methods to get information about the current page.

importorg.bukkit.Material;
importorg.bukkit.inventory.ItemStack;
publicclassExamplePaginatedInventoryextendsPaginatedFastInv {
privatestaticfinalInventorySchemeSCHEME = newInventoryScheme()
.mask(" 1111111 ")
.mask(" 1111111 ")
.bindPagination('1');
publicExamplePaginatedInventory() {
super(27, "Example paginated inventory");
// Add pagination items to the inventory// These items are automatically updated when the page changes and displayed only if neededpreviousPageItem(20, p -> newItemBuilder(Material.ARROW).name("Page " + p + "/" + lastPage()).build());
nextPageItem(24, p -> newItemBuilder(Material.ARROW).name("Page " + p + "/" + lastPage()).build());
// Add some paginated content to the inventoryfor (inti = 1; i < 64; i++) {
addContent(newItemStack(Material.GOLDEN_APPLE, i),
e -> e.getWhoClicked().sendMessage("You clicked on paginated item"));
}
// The setContent method can also be used to set the index of a specific item in the paginated contentsetContent(42, newItemStack(Material.APPLE, 42));
// Non-paginated items can also still be added to the inventory if neededsetItem(26, newItemBuilder(Material.BARRIER).name("Close").build(),
e -> e.getWhoClicked().closeInventory());
// The pagination layout can also be customized with a mask instead of the default oneSCHEME.apply(this);
}
@Override// Optional method to handle the page change event if neededprotectedvoidonPageChange(intpage) {
// Called after the page changesetItem(18, newItemBuilder(Material.PAPER).name("Current page " + page).build());
}
}

Like a normal inventory, you can open the paginated inventory with open(player):

newExamplePaginatedInventory().open(player);

Creating a 'compact' inventory

Instead of creating a new class for each inventory, a 'compact' inventory can be created directly:

FastInvinv = newFastInv(InventoryType.DISPENSER, "Example compact inventory");
inv.setItem(4, newItemStack(Material.NAME_TAG), e -> e.getWhoClicked().sendMessage("You clicked on the name tag"));
inv.addClickHandler(e -> player.sendMessage("You clicked on slot " + e.getSlot()));
inv.addCloseHandler(e -> player.sendMessage(ChatColor.YELLOW + "Inventory closed"));
inv.open(player);

In the same way, you can also create a 'compact' paginated inventory.

Get the FastInv instance

You can get the FastInv instance from a Bukkit inventory with the holder:

if (inventory.getHolder() instanceofFastInv) {
FastInvfastInv = (FastInv) inventory.getHolder();
}

Adventure components support

FastInv supports Adventure components for inventory titles on PaperMC servers:

Componenttitle = Component.text("Hello World");
FastInvinv = newFastInv(owner -> Bukkit.createInventory(owner, 27, title));

About

Lightweight and easy-to-use inventory API for Bukkit plugins.

Topics

Resources

Stars

142 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages