### Install Anvil Input Feature in Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Installation This Java code shows how to install the Anvil Input feature within the Inventory Framework view. This enables the feature for specific views, allowing for controlled integration. ```java import static me.devnatan.inventoryframework.AnvilInputFeature.AnvilInput; viewFrame.install(AnvilInput); ``` -------------------------------- ### Configure Repositories for Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Installation This snippet shows how to configure your build script to use Maven Central for releases and optionally Maven Central Snapshots for development builds. This is necessary to download the Inventory Framework and its related components. ```groovy repositories { mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } // optional } ``` -------------------------------- ### Add Paper Platform Dependency for Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Installation This code adds the Paper platform implementation of the Inventory Framework. This version includes support for Kyori's Adventure Components and Folia since v3.4.0. ```groovy implementation 'me.devnatan:inventory-framework-platform-paper:' ``` -------------------------------- ### Add Bukkit Platform Dependency for Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Installation This code adds the Bukkit platform implementation of the Inventory Framework as a project dependency. This is required if you are developing for a Bukkit-based server environment. ```groovy dependencies { implementation 'me.devnatan:inventory-framework-platform-bukkit:' } ``` -------------------------------- ### Add Minestom Platform Dependency for Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Installation This code adds the Minestom platform implementation of the Inventory Framework, available since version v3.3.0. Ensure your project is compatible with Minestom before using this dependency. ```groovy implementation 'me.devnatan:inventory-framework-platform-minestom:' ``` -------------------------------- ### Open View for Player (Return to A) with Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Navigating-Between-Views Illustrates returning to a previous view ('A.class') by clicking a REDSTONE item. This example showcases basic navigation back to a known view using `openForPlayer`. ```java // B.java @Override public void onInit(ViewConfigBuilder config) { config.title("B"); } @Override public void onFirstRender(RenderContext render) { // Moves player to back to "A" view on click render.firstSlot(new ItemStack(Material.REDSTONE)) .onClick(click -> click.openForPlayer(A.class)); } ``` -------------------------------- ### Add Anvil Input Feature Dependency Source: https://github.com/devnatan/inventory-framework/wiki/Installation This Java code snippet demonstrates how to include the Anvil Input feature as a dependency in your project. This allows you to integrate advanced input functionalities into your inventory views. ```java dependencies { implementation 'me.devnatan:inventory-framework-anvil-input:' } ``` -------------------------------- ### Install EventBus Feature (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Event-Bus This Java code demonstrates how to install the EventBus feature within the inventory framework. It requires importing the EventBusFeature and calling the install method. ```java import me.devnatan.inventoryframework.feature.eventbus.EventBusFeature.EventBus; install(EventBus); ``` -------------------------------- ### Declare Inventory Framework as Plugin Dependency Source: https://github.com/devnatan/inventory-framework/wiki/Installation This YAML snippet shows how to declare the Inventory Framework as a dependency for a Spigot/Bukkit plugin. This is part of the process to prevent version conflicts when the framework is used across multiple plugins. ```yaml depend: [ InventoryFramework ] ``` -------------------------------- ### Lazy State Example in Java Source: https://github.com/devnatan/inventory-framework/wiki/State-Management Illustrates the concept of a lazy state in Java. The value of a lazy state is computed only on the first call to `get()` and subsequent calls return the same computed value. This example uses a random number generator. ```java State intState = lazyState(ThreadLocalRandom.current()::nextInt); randomIntState.get(...); // 54 - from initial computation of random integer ^^ randomIntState.get(...); // 54 - previously defined by the initial computation ``` -------------------------------- ### Example: Update View Every Second (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Scheduled-Updates Provides a concrete example of scheduling a view update every 1 second, which translates to 20 ticks. This snippet illustrates the practical application of `scheduleUpdate` for time-based view refreshes. ```java public void onInit(ViewConfigBuilder config) { config.scheduleUpdate(20L /* ticks */); } ``` -------------------------------- ### Layout Definition Before and After in Java Source: https://github.com/devnatan/inventory-framework/wiki/Migrating-to-version-3 Demonstrates how layout and pagination sources were defined in older versions (onRender) compared to the current approach using modifyConfig() in onOpen. This change simplifies configuration and leverages state management for pagination. ```java public final class PetShopView extends PaginatedView<...> { @Override public void onRender(ViewContext context) { context.setLayout("..."); context.paginated().setSource(...); } } ``` ```java public final class PetShopView extends View { @Override public void onOpen(OpenContext open) { open.modifyConfig().layout("..."); } } ``` -------------------------------- ### View Initialization and Rendering Lifecycle in Java (After v3.0) Source: https://github.com/devnatan/inventory-framework/wiki/Migrating-to-version-3 Demonstrates the new approach to configuring views in version 3.0. Root options are set in `onInit` using `ViewConfigBuilder`, and context-specific options are modified in `onOpen` via `modifyConfig()`. Item rendering is now exclusively done in `onFirstRender`. ```java public final class PetShopView extends View { @Override public void onInit(ViewConfigBuilder config) { config.title("Pet Shop").size(4).cancelOnClick().layout(...); } @Override public void onOpen(OpenContext open) { // context-specific open.modifyConfig().title("Hello World!"); } } ``` -------------------------------- ### Computed State Example in Java Source: https://github.com/devnatan/inventory-framework/wiki/State-Management Demonstrates the creation and usage of a computed state in Java. A computed state's value is recalculated each time it is accessed, based on a provided supplier function. This example shows generating random numbers. ```java State randomNumberState = computedState(ThreadLocalRandom.current()::nextInt); randomNumberState.get(host); // some random number randomNumberState.get(host); // another random number ``` ```java private final State randomNumberState = computedState(() -> ThreadLocalRandom.current().nextInt(1, 64)); @Override public void onFirstRender(RenderContext render) { render.firstSlot() .onRender(slotRender -> slotRender(new ItemStack( /* type */ Material.DIAMOND, /* amount */ randomNumberState.get(slotRender) ))) .onClick(IFContext::updateSlot); ``` -------------------------------- ### Install Anvil Input Feature in ViewFrame (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Anvil-Input Installs the Anvil Input feature into your ViewFrame, making it available for use within your views. This step is necessary after adding the dependency. ```java viewFrame.install(AnvilInputFeature.AnvilInput); ``` -------------------------------- ### Iterate and Render Items to Available Slots (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Basic-Usage Shows how to iterate through data and render items to available slots in the inventory. The example places an item in a specific slot first, then uses a loop with `availableSlot` to add multiple items, respecting slot limits and pagination. ```java @Override public void onFirstRender(RenderContext render) { // will not be ignored in the iteration below render.slot(1, new ItemStack(Material.EGG)); for (int i = 1; i <= 5; i++) { render.availableSlot(new ItemStack(Material.DIAMOND, i)); } } ``` -------------------------------- ### View Initialization and Rendering Lifecycle in Java (Before v3.0) Source: https://github.com/devnatan/inventory-framework/wiki/Migrating-to-version-3 Illustrates the older method of defining inventory properties and rendering items within the `Inventory Framework`. This version used constructors and the `onRender` method for initialization and item display. ```java public final class PetShopView extends View { public PetShopView() { super(4, "Pet shop"); setCancelOnClick(true); setLayout(...); } @Override public void onOpen(OpenViewContext context) { // context-specific context.setContainerTitle("Hello world!"); } } ``` -------------------------------- ### Open a View for a Player Source: https://github.com/devnatan/inventory-framework/wiki/Basic-Usage Shows how to open a registered view for a specific player. This example utilizes an event handler to open the 'MyView' whenever a player sends a chat message. It requires the ViewFrame to be created and registered previously. ```java import me.devnatan.inventoryframework.ViewFrame; import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.CloseContext; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.plugin.java.JavaPlugin; final class MyPlugin extends JavaPlugin implements Listener { // Move to a final value to use it later private final ViewFrame viewFrame = ViewFrame.create(this).with(new MyView()); @Override public void onEnable() { viewFrame.register(); getServer().getPluginManager().registerEvents(this, this); } @EventHandler public void onChat(AsyncPlayerChatEvent event) { // Opens 'MyView' for the player who sent the chat message. viewFrame.open(MyView.class, event.getPlayer()); } } // Placeholder for MyView if not defined elsewhere class MyView extends View { // View implementation details... } ``` -------------------------------- ### Install Anvil Input Feature (Groovy) Source: https://github.com/devnatan/inventory-framework/wiki/Anvil-Input Includes the Anvil Input external feature dependency in your project's build file. This is a prerequisite for using the feature. ```groovy implementation 'me.devnatan:inventory-framework-anvil-input:' ``` -------------------------------- ### Configure Inventory View Size in Java Source: https://github.com/devnatan/inventory-framework/wiki/Pagination This Java code configures the size of an inventory view. It's a basic setup for the `onInit` method, defining the dimensions of the inventory grid. No external dependencies beyond the framework itself are shown. ```java @Override public void onInit(ViewConfigBuilder config) { config.size(3); } ``` -------------------------------- ### Set Inventory Size in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Configures the size of the inventory, either by specifying the number of lines (each line has 9 slots) or an absolute number of slots. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; @Override public void onInit(ViewConfigBuilder config) { config.size(4); // 4 lines (36 slots) config.size(45); // explicit number of slots } ``` -------------------------------- ### Interact with Mutable State in Java Source: https://github.com/devnatan/inventory-framework/wiki/State-Management Shows how to interact with a mutable state by setting and getting its value in Java. It includes setting a new value and then retrieving it, demonstrating state modification and access. ```java MutableState textState = mutableState(""); textState.get(context); // "" textState.set("abc", context); textState.get(context); // "abc" ``` -------------------------------- ### Item Rendering Before and After in Java Source: https://github.com/devnatan/inventory-framework/wiki/Migrating-to-version-3 Illustrates the evolution of item rendering in the Inventory Framework. Previously, items could be defined in the constructor and onRender. Now, all item definitions are consolidated within the onFirstRender method, simplifying the rendering logic. ```java public final class PetShopView extends View { public PetShopView() { slot(4, 3, new ItemStack(Material.GOLD_INGOT)).closeOnClick(); firstSlot().onRender(render -> render.setItem(new ItemStack(Material.DIAMOND))); } @Override public void onRender(ViewContext context) { context.availableSlot(new ItemStack(Material.GOLDEN_AXE)); } } ``` ```java public final class PetShopView extends View { @Override public void onFirstRender(RenderContext render) { render.slot(4, 3, new ItemStack(Material.GOLD_INGOT)).closeOnClick(); render.firstSlot().rendered(() -> new ItemStack(Material.DIAMOND))); render.availableSlot(new ItemStack(Material.GOLDEN_AXE)); } } ``` -------------------------------- ### Set Initial Inventory Title with Adventure Component in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Sets the initial title of an inventory using an Adventure Text Component, allowing for rich text formatting. Requires the `inventory-framework-platform-paper` module on the classpath. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @Override public void onInit(ViewConfigBuilder config) { config.title(Component.text("Write something") .color(NamedTextColor.BLUE)); } ``` -------------------------------- ### Lombok Compatible API Design in Java Source: https://github.com/devnatan/inventory-framework/wiki/Migrating-to-version-3 Demonstrates how to use Lombok annotations with the Inventory Framework's View class. This change allows for more concise view definitions by leveraging constructors and property initializers. ```java @RequiredArgsConstructor private final class PetShopView extends View { private final PetsManager petsManager; } ``` -------------------------------- ### Implement Counter UI with Java in Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Code-Samples This Java code implements a counter UI. It initializes a mutable integer state, sets up the UI layout with title and buttons, and defines how the item count is displayed and updated. It uses the `MutableIntState` for state management and `ItemStack` for UI elements. ```java private final MutableIntState counterState = mutableState(0); @Override public void onInit(ViewConfigBuilder config) { config.title("Counter") .cancelOnClick() .layout(" - # + "); } @Override public void onFirstRender(RenderContext render) { // Current count item render.layoutSlot('#') // The `watch` here updates the item when `counterState` updates .watch(counterState) .renderWith(() -> new ItemStack( /* type = */ Material.GOLD_INGOT, /* amount = */ counterState.get(render) )); // Decrement button render.layoutSlot('-', new ItemStack(Material.ARROW)) .onClick(counterState::decrement); // Increment button render.layoutSlot('+', new ItemStack(Material.ARROW)) .onClick(counterState::increment); } ``` -------------------------------- ### Apply Layout to Inventory View in Java Source: https://github.com/devnatan/inventory-framework/wiki/Pagination This Java snippet shows how to apply a custom layout to an inventory view using character-based definitions. The layout guides the placement of items within the inventory grid, demonstrated here with 'O' characters. ```java @Override public void onInit(ViewConfigBuilder config) { config.layout( " ", " OOOOO ", " " ); } ``` -------------------------------- ### Display 'Welcome Back' Message on Resume in Java Source: https://github.com/devnatan/inventory-framework/wiki/Navigating-Between-Views The onResume method is called when a player navigates back to a view. It receives origin and target contexts, allowing you to update the current view's title, for example, to display a 'Welcome back!' message. ```java // A.java @Override public void onResume(Context origin, Context target) { // Use `target` here since its the current context target.updateTitleForPlayer("Welcome back!"); } ``` -------------------------------- ### Add Proxy Inventory Dependency to Gradle Build File Source: https://github.com/devnatan/inventory-framework/wiki/Proxy-Inventory This snippet shows how to include the Proxy Inventory optional feature in your project. You need to add the specified implementation dependency to your build.gradle file. Replace '' with the actual version of the inventory-framework-proxy-inventory library you wish to use. ```groovy // build.gradle implementation 'me.devnatan:inventory-framework-proxy-inventory:' ``` -------------------------------- ### Anvil Input Feature for Player Text Input (Java) Source: https://context7.com/devnatan/inventory-framework/llms.txt Implements text input from players using the anvil inventory type. Requires the `inventory-framework-anvil-input` module and installation of the `AnvilInputFeature`. It allows capturing player text input, typically for search or naming functionalities. ```gradle // build.gradle // implementation 'me.devnatan:inventory-framework-anvil-input:' ``` ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.AnvilInput; import me.devnatan.inventoryframework.AnvilInputFeature; // Install feature in ViewFrame: // viewFrame.install(AnvilInputFeature.AnvilInput); public class SearchView extends View { private final AnvilInput anvilInput = AnvilInput.createAnvilInput(); @Override public void onInit(ViewConfigBuilder config) { config.title("Search Players"); config.use(anvilInput); // Apply anvil input as config modifier } @Override public void onFirstRender(RenderContext render) { // Left slot - input item with default text render.slot(0, createInputItem("Type player name...")); // Result slot - confirms the search render.slot(2, new ItemStack(Material.PAPER)) .onClick(click -> { String searchText = anvilInput.get(click).getText(); click.getPlayer().sendMessage("Searching for: " + searchText); click.closeForPlayer(); }); } private ItemStack createInputItem(String defaultText) { ItemStack item = new ItemStack(Material.NAME_TAG); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(defaultText); item.setItemMeta(meta); return item; } } ``` -------------------------------- ### Configure View Type to Hopper in Java Source: https://github.com/devnatan/inventory-framework/wiki/Basic-Usage This example demonstrates how to change the inventory type to `HOPPER` using the `onInit` method and the `ViewConfigBuilder`. The `type()` method accepts predefined `ViewType` constants. Note that some types have limitations or cannot have their titles modified due to Minecraft protocol constraints. ```java @Override public void onInit(ViewConfigBuilder config) { config.type(ViewType.HOPPER); } ``` -------------------------------- ### Dynamic View Configuration with onOpen Source: https://context7.com/devnatan/inventory-framework/llms.txt Illustrates how to dynamically configure views when they are opened using the `onOpen` method. This allows for per-player customization of properties like the inventory title and size based on player attributes or permissions. ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.ViewConfigBuilder; import me.devnatan.inventoryframework.context.OpenContext; import me.devnatan.inventoryframework.context.RenderContext; public class PersonalizedView extends View { @Override public void onInit(ViewConfigBuilder config) { config.size(6); config.cancelOnClick(); } @Override public void onOpen(OpenContext open) { Player player = open.getPlayer(); // Dynamically set title based on player open.modifyConfig() .title("Welcome, " + player.getName() + "!"); // Conditionally change size based on permissions if (player.hasPermission("shop.vip")) { open.modifyConfig().size(6); } else { open.modifyConfig().size(3); } } @Override public void onFirstRender(RenderContext render) { render.slot(4, new ItemStack(Material.NETHER_STAR)) .onClick(click -> click.getPlayer().sendMessage("Hello!")); } } ``` -------------------------------- ### Create a Basic Inventory View Source: https://context7.com/devnatan/inventory-framework/llms.txt Demonstrates how to create a custom inventory view by extending the `View` class. It covers setting configuration options like title, size, and click cancellation, as well as rendering items and handling clicks. ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.ViewConfigBuilder; import me.devnatan.inventoryframework.context.RenderContext; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; public class MyShopView extends View { @Override public void onInit(ViewConfigBuilder config) { config.title("Item Shop"); config.size(3); // 3 rows = 27 slots config.cancelOnClick(); // Prevent item stealing } @Override public void onFirstRender(RenderContext render) { // Place a diamond at slot 13 (center) render.slot(13, new ItemStack(Material.DIAMOND)) .onClick(click -> { click.getPlayer().sendMessage("You clicked the diamond!"); }); // Place items at first and last slots render.firstSlot(new ItemStack(Material.EMERALD)); render.lastSlot(new ItemStack(Material.GOLD_INGOT)); } } ``` -------------------------------- ### Create Executable Plugin with Gradle Source: https://github.com/devnatan/inventory-framework/wiki/Home Builds an executable plugin for all supported platforms using Gradle's shadowJar task. This is useful for creating a distributable version of your project. ```bash ./gradlew shadowJar ``` -------------------------------- ### Dynamic Title Update with Adventure Components (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Dynamic-Title-Update Shows how to use Adventure Text Components for dynamic inventory titles, starting from v3.7.0. This example updates the title with a counter and a randomly generated text color, ensuring the `inventory-framework-platform-paper` module is included. The title is updated for the specific player associated with the context. ```java private final MutableIntState countState = mutableState(0); @Override public void onInit(ViewConfigBuilder config) { config.title("&4Auto update") .scheduleUpdate(10L /* intervalInTicks */); } @Override public void onUpdate(Context update) { int count = countState.increment(update); Random random = ThreadLocalRandom.current(); TextColor textColor = TextColor.color( random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255) ); Component newTitle = Component.text( String.format("Auto update (%d)", count), textColor ); update.updateTitleForPlayer(newTitle); } ``` -------------------------------- ### Navigate Between Views Using OpenForPlayer and Back in Java Source: https://context7.com/devnatan/inventory-framework/llms.txt This Java code illustrates how to manage navigation between different inventory views. It uses `openForPlayer` to transition to new views, optionally passing data, and `back()` to return to the previous view. The `onResume` method handles actions when returning to a view. Dependencies include the Inventory Framework library and Guava's ImmutableMap. ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.context.Context; import me.devnatan.inventoryframework.context.RenderContext; // Main menu view public class MainMenuView extends View { @Override public void onInit(ViewConfigBuilder config) { config.title("Main Menu"); config.size(3); config.cancelOnClick(); } @Override public void onFirstRender(RenderContext render) { render.slot(11, createMenuItem(Material.DIAMOND, "Shop")) .onClick(click -> { // Navigate to shop view click.openForPlayer(ShopView.class); }); render.slot(13, createMenuItem(Material.COMPASS, "Settings")) .onClick(click -> { // Navigate with data click.openForPlayer(SettingsView.class, ImmutableMap.of( "theme", "dark", "volume", 50 )); }); render.slot(15, createMenuItem(Material.BOOK, "Help")) .onClick(click -> click.openForPlayer(HelpView.class)); } @Override public void onResume(Context origin, Context target) { // Called when player returns via back() target.updateTitleForPlayer("Welcome back!"); } private ItemStack createMenuItem(Material material, String name) { ItemStack item = new ItemStack(material); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(ChatColor.AQUA + name); item.setItemMeta(meta); return item; } } // Sub-view with back navigation public class ShopView extends View { @Override public void onInit(ViewConfigBuilder config) { config.title("Shop"); config.size(3); config.cancelOnClick(); } @Override public void onFirstRender(RenderContext render) { // Back button render.slot(0, new ItemStack(Material.ARROW)) .onClick(click -> click.back()); // Returns to previous view // Shop items render.slot(13, new ItemStack(Material.DIAMOND_SWORD)) .onClick(click -> click.getPlayer().sendMessage("Sword purchased!")); } } ``` -------------------------------- ### Render Static Items with Adaptive Slots (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Basic-Usage Demonstrates rendering static items to the first and last slots while also configuring the inventory type. The onFirstRender method is used to place items, and onInit configures the inventory's view type, ensuring adaptive item placement. ```java @Override public void onInit(ViewConfigBuilder config) { config.type(ViewType.HOPPER); } @Override public void onFirstRender(RenderContext render) { render.firstSlot(new ItemStack(Material.EGG)); render.lastSlot(new ItemStack(Material.EGG)); } ``` -------------------------------- ### Set Initial Inventory Title in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Sets the initial title of an inventory using a String. This configuration is applied during the initialization phase of the view. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; @Override public void onInit(ViewConfigBuilder config) { config.title("Write something"); } ``` -------------------------------- ### Basic Layout Configuration and Item Assignment in Java Source: https://github.com/devnatan/inventory-framework/wiki/Layouts Demonstrates how to define a basic inventory layout and assign items to specific slots using characters. This is configured in `onInit` and rendered in `onFirstRender`. ```java ```java @Override public void onInit(@NotNull ViewConfigBuilder config) { config.layout("### A ###"); } @Override public void onFirstRender(@NotNull RenderContext render) { render.layoutSlot('A', new ItemStack(Material.DIAMOND)); render.layoutSlot('#') .withItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE)); } ``` ``` -------------------------------- ### Navigate Back to Previous View with Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Navigating-Between-Views Shows how to implement a 'back' navigation using the `back()` method in Inventory Framework. This is useful for returning to the previous view without explicitly referencing it, providing a browser-like history behavior. ```java // B.java @Override public void onInit(ViewConfigBuilder config) { config.title("B"); } @Override public void onFirstRender(RenderContext render) { // Moves player to back to the previous view on click render.firstSlot(new ItemStack(Material.GLOWSTONE_DUST)) .onClick(click -> click.back()); } ``` -------------------------------- ### Inventory Framework State Management in Java Source: https://context7.com/devnatan/inventory-framework/llms.txt Demonstrates how to use mutable, initial, and computed states for managing data within an inventory view. It includes setting up the view, rendering slots, and handling state changes. Dependencies include the inventory framework API. ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.state.MutableIntState; import me.devnatan.inventoryframework.state.State; import java.util.concurrent.ThreadLocalRandom; public class CounterView extends View { // Mutable state that tracks a counter value private final MutableIntState counterState = mutableIntState(0); // Initial state from opening data private final State playerNameState = initialState("playerName"); // Computed state - generates new value each access private final State randomState = computedState(() -> ThreadLocalRandom.current().nextInt(1, 64)); @Override public void onInit(ViewConfigBuilder config) { config.title("Counter Demo"); config.size(3); config.cancelOnClick(); } @Override public void onFirstRender(RenderContext render) { // Increment button render.slot(3, new ItemStack(Material.LIME_WOOL)) .onClick(click -> { counterState.increment(click); click.getPlayer().sendMessage("Count: " + counterState.get(click)); }); // Display current count - updates automatically when state changes render.slot(4) .watch(counterState) .renderWith(() -> { int count = counterState.get(render); ItemStack item = new ItemStack(Material.DIAMOND, Math.max(1, count)); ItemMeta meta = item.getItemMeta(); meta.setDisplayName("Count: " + count); item.setItemMeta(meta); return item; }); // Decrement button render.slot(5, new ItemStack(Material.RED_WOOL)) .onClick(click -> { counterState.decrement(click); }); } } // Opening with initial state data: // viewFrame.open(CounterView.class, player, ImmutableMap.of("playerName", player.getName())); ``` -------------------------------- ### Cancel Pickup Interactions in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Disables the ability for players to pick up items into their inventory while the custom inventory is open. This complements `cancelOnDrop` for complete interaction control. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; @Override public void onInit(ViewConfigBuilder config) { config.cancelOnPickup(); } ``` -------------------------------- ### Cancel Drop Interactions in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Prevents players from dropping items from their inventory while the custom inventory is open. This is useful for enforcing specific item management rules. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; @Override public void onInit(ViewConfigBuilder config) { config.cancelOnDrop(); } ``` -------------------------------- ### Register Views with ViewFrame Source: https://context7.com/devnatan/inventory-framework/llms.txt Shows how to register custom inventory views using the `ViewFrame`. This includes creating a `ViewFrame` instance, registering multiple views, and opening views for players, with and without initial data. ```java import me.devnatan.inventoryframework.ViewFrame; import org.bukkit.plugin.java.JavaPlugin; public class MyPlugin extends JavaPlugin { private ViewFrame viewFrame; @Override public void onEnable() { // Create and register ViewFrame with all your views viewFrame = ViewFrame.create(this) .with(new MyShopView()) .with(new SettingsView()) .with(new InventoryView()) .register(); } // Open a view for a player public void openShop(Player player) { viewFrame.open(MyShopView.class, player); } // Open with initial data public void openShopWithData(Player player, String category) { viewFrame.open(MyShopView.class, player, ImmutableMap.of("category", category)); } } ``` -------------------------------- ### Set Inventory Type in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Defines the specific type of the underlying container for the inventory, such as CHEST, HOPPER, or SHULKER_BOX. This affects the visual representation and interaction capabilities. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; import me.devnatan.inventoryframework.ViewType; @Override public void onInit(ViewConfigBuilder config) { config.type(ViewType.SHULKER_BOX); } ``` -------------------------------- ### Set Maximum Inventory Size in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Automatically adjusts the inventory to the largest possible size allowed by its container type. This is useful when the exact size is not critical but should be maximized. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; @Override public void onInit(ViewConfigBuilder config) { config.maxSize(); } ``` -------------------------------- ### Publish Inventory Framework to Local Maven Repository Source: https://github.com/devnatan/inventory-framework/wiki/Home Publishes the Inventory Framework to your local Maven repository using Gradle. This allows you to use the framework in other local projects. ```bash ./gradlew publishToMavenLocal ``` -------------------------------- ### Cancel Drag Interactions in Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Disables the ability for players to drag items across slots within the inventory. This prevents actions like shifting items or rapid item placement. ```java import me.devnatan.inventoryframework.ViewConfigBuilder; @Override public void onInit(ViewConfigBuilder config) { config.cancelOnDrag(); } ``` -------------------------------- ### Clone Inventory Framework Repository Source: https://github.com/devnatan/inventory-framework/wiki/Home Clones the Inventory Framework project from its GitHub repository. This is the first step for local development or contributing to the project. ```bash git clone https://github.com/DevNatan/inventory-framework ``` -------------------------------- ### PaginatedView to View Migration in Java Source: https://github.com/devnatan/inventory-framework/wiki/Migrating-to-version-3 Shows the refactoring required for pagination changes in version 3.0. The `PaginatedView` class has been removed, and pagination is now handled through states within a regular `View`. ```diff - public final class PetShopView extends PaginatedView + public final class PetShopView extends View ``` -------------------------------- ### Open View for Player with Inventory Framework Source: https://github.com/devnatan/inventory-framework/wiki/Navigating-Between-Views Demonstrates opening a specific view ('B.class') for the player who clicks on a DIAMOND item. This is a fundamental navigation pattern in the Inventory Framework. ```java // A.java @Override public void onInit(ViewConfigBuilder config) { config.title("A"); } @Override public void onFirstRender(RenderContext render) { // Moves player to "B" view on click render.firstSlot(new ItemStack(Material.DIAMOND)) .onClick(click -> click.openForPlayer(B.class)); } ``` -------------------------------- ### Create and Register ViewFrame Instance Source: https://github.com/devnatan/inventory-framework/wiki/Basic-Usage Demonstrates the basic steps to create a ViewFrame instance, add a custom view to it, and register the ViewFrame. This is typically done in the `onEnable` method of a Bukkit plugin. The `ViewFrame` is made available through Bukkit's ServicesManager upon registration. ```java import me.devnatan.inventoryframework.ViewFrame; import org.bukkit.plugin.java.JavaPlugin; final class MyPlugin extends JavaPlugin { @Override public void onEnable() { // Create a new ViewFrame instance, add a view, and register it. ViewFrame viewFrame = ViewFrame.create(this) .with(new MyView()) // add view to ViewFrame instance .register(); // registers this ViewFrame instance } } ``` -------------------------------- ### Implement View Close Logic Source: https://github.com/devnatan/inventory-framework/wiki/Basic-Usage Provides examples of how to handle the closing of a view. The `onClose` method in a custom `View` class is called when the inventory is closed. You can prevent closure by canceling the event. ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.CloseContext; class CoolView extends View { @Override public void onClose(CloseContext close) { // This method is called when the view's inventory is closed. // You can add custom logic here, e.g., saving data or performing cleanup. System.out.println("View closed for: " + close.getPlayer().getName()); } } ``` ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.CloseContext; class PreventCloseView extends View { @Override public void onClose(CloseContext close) { // Prevent the inventory from being closed by canceling the context. close.setCancelled(true); System.out.println("Attempted to close view, but it was cancelled."); } } ``` -------------------------------- ### Dynamic Title Update with Adventure Text Components (v3.7.0+) Source: https://github.com/devnatan/inventory-framework/wiki/Dynamic-Title-Update Illustrates using Adventure Text Components for dynamic inventory titles starting from version 3.7.0. This allows for richer text formatting and colorization. ```APIDOC ## POST /api/views/title/update/adventure ### Description Updates the title of an inventory view using Adventure Text Components, available from v3.7.0. This example shows a title that includes a counter and dynamically changes color. ### Method POST ### Endpoint `/api/views/title/update/adventure` (Conceptual - actual implementation within the framework) ### Parameters #### Query Parameters - **intervalInTicks** (long) - Optional - The interval in ticks at which the title should be updated. #### Request Body This endpoint conceptually modifies the state managed by the Inventory Framework. The actual request is made through the framework's API calls within your Java code. Requires the `inventory-framework-platform-paper` module. ### Request Example ```java // Conceptual example within Java code: private final MutableIntState countState = mutableState(0); @Override public void onInit(ViewConfigBuilder config) { config.title("&4Auto update") .scheduleUpdate(10L /* intervalInTicks */); } @Override public void onUpdate(Context update) { int count = countState.increment(update); Random random = ThreadLocalRandom.current(); TextColor textColor = TextColor.color( random.nextInt(0, 255), random.nextInt(0, 255), random.nextInt(0, 255) ); Component newTitle = Component.text( String.format("Auto update (%d)", count), textColor ); update.updateTitleForPlayer(newTitle); } ``` ### Response #### Success Response (200) This action modifies the view state directly. Success is indicated by the updated, styled title appearing on the player's inventory view. #### Response Example N/A ``` -------------------------------- ### Create Static Pagination Source: https://github.com/devnatan/inventory-framework/wiki/Pagination Demonstrates how to create a static pagination state using `paginationState`. This method requires a `sourceProvider` for the data and an `elementFactory` to define how each item is rendered. The data is loaded once when the state is created. ```java final State paginationState = paginationState(sourceProvider, elementFactory); ``` ```java final State paginationState = paginationState( Arrays.asList("Notch", "Natan", "Michael"), (...) ); ``` ```java final State paginationState = paginationState( Arrays.asList("Notch", "Natan", "Michael"), (ctx, builder, index, value) -> (...) ); ``` -------------------------------- ### Implement Lazy Pagination with Item Factory in Java Source: https://context7.com/devnatan/inventory-framework/llms.txt Demonstrates creating a paginated view in Java. It uses lazy pagination to load data only when the inventory opens and an item factory to define how each item is displayed and interacted with. Dependencies include the Inventory Framework API. ```java import me.devnatan.inventoryframework.View; import me.devnatan.inventoryframework.component.Pagination; import me.devnatan.inventoryframework.state.State; import java.util.Arrays; import java.util.List; public class PlayerListView extends View { // Lazy pagination - data loaded when inventory opens private final State paginationState = lazyPaginationState( // Data source - list of player names () -> Arrays.asList("Notch", "jeb_", "Dinnerbone", "Marc", "Nathan", "Player1", "Player2", "Player3", "Player4", "Player5"), // Item factory - creates display item for each element (context, builder, index, playerName) -> { ItemStack head = new ItemStack(Material.PLAYER_HEAD); ItemMeta meta = head.getItemMeta(); meta.setDisplayName(ChatColor.GREEN + playerName); meta.setLore(Arrays.asList("Index: " + index, "Click to select")); head.setItemMeta(meta); builder.withItem(head); builder.onClick(click -> { click.getPlayer().sendMessage("Selected: " + playerName); }); } ); @Override public void onInit(ViewConfigBuilder config) { config.title("Player List"); config.cancelOnClick(); // Layout defines pagination area (O) and navigation slots config.layout( "#########", "#OOOOOOO#", "#OOOOOOO#", "#OOOOOOO#", "#P N#" ); } @Override public void onFirstRender(RenderContext render) { Pagination pagination = paginationState.get(render); // Border decoration render.layoutSlot('#', new ItemStack(Material.BLACK_STAINED_GLASS_PANE)); // Previous page button render.layoutSlot('P', createNavItem("Previous Page")) .displayIf(pagination::canBack) .updateOnStateChange(paginationState) .onClick(click -> { pagination.back(); click.updateTitleForPlayer("Page " + (pagination.currentPage() + 1)); }); // Next page button render.layoutSlot('N', createNavItem("Next Page")) .displayIf(pagination::canAdvance) .updateOnStateChange(paginationState) .onClick(click -> { pagination.advance(); click.updateTitleForPlayer("Page " + (pagination.currentPage() + 1)); }); } private ItemStack createNavItem(String name) { ItemStack item = new ItemStack(Material.ARROW); ItemMeta meta = item.getItemMeta(); meta.setDisplayName(ChatColor.YELLOW + name); item.setItemMeta(meta); return item; } } ``` -------------------------------- ### Apply AnvilInput to View Configuration (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Anvil-Input Applies the created AnvilInput instance as a configuration modifier to your view. This integrates the anvil input handling into the view's setup, often within the onInit method. ```java @Override public void onInit(ViewConfigBuilder config) { config.title("Hello world") .use(anvilInput); // <-- Look here } ``` -------------------------------- ### Run Linters and Build Checks with Gradle Source: https://github.com/devnatan/inventory-framework/wiki/Home Executes code formatting checks (lint) and comprehensive build checks using Gradle. It's recommended to run these before creating a pull request. ```bash ./gradlew lint build ``` -------------------------------- ### Creating Inventory Borders with Layouts in Java Source: https://github.com/devnatan/inventory-framework/wiki/Layouts Illustrates how to define a multi-row layout string array to create a visual border around an inventory, using a specific item for the border. ```java ```java @Override public void onInit(@NotNull ViewConfigBuilder config) { config.layout( "#########", "# #", "# #", "# #", "# #", "#########" ); } @Override public void onFirstRender(@NotNull RenderContext render) { render.layoutSlot('#') .withItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE)); } ``` ``` -------------------------------- ### Configure Transitive Initial Data - Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Controls whether initial data is passed when navigating between inventory views. When set to `true`, data from the current view is carried over to the next. Defaults to `false`, where data does not transfer. ```java @Override public void onInit(ViewConfigBuilder config) { config.transitiveInitialData(true); } ``` -------------------------------- ### Add Interaction Delay - Java Source: https://github.com/devnatan/inventory-framework/wiki/Configuration-Guide Adds a delay before any interaction becomes valid. Actions performed before the delay ends are cancelled. This is useful for preventing accidental rapid interactions. The delay is specified using a `Duration` object. ```java @Override public void onInit(ViewConfigBuilder config) { config.interactionDelay(Duration.ofSeconds(3)); } ``` -------------------------------- ### Create and Open Shared Context (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Shared-Contexts Demonstrates how to open a new view for players, which can result in a shared context if multiple players are involved. It shows how to obtain the context ID and how to open an already active context for another player. ```java String contextId = viewFrame.open(SomeView.class, [...]); ``` ```java viewFrame.openActive(SomeView.class, contextId, [...]); ``` -------------------------------- ### Create AnvilInput Instance (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Anvil-Input Creates a new instance of the AnvilInput class, which is the entry point for utilizing the anvil input functionality. This instance will be applied to your view configuration. ```java final AnvilInput anvilInput = AnvilInput.createAnvilInput(); ``` -------------------------------- ### Close Shared Context Action (Java) Source: https://github.com/devnatan/inventory-framework/wiki/Shared-Contexts Illustrates how to perform actions within a shared context that affect either a specific player or all players in the context. The example shows closing an action for everyone in the shared context. ```java click.closeForEveryone(); ```