GithubHelp home page GithubHelp logo

spigui's Introduction

SpiGUI

A comprehensive inventory menu API for Spigot with pages support. Supports Bukkit/Spigot 1.7 - 1.20 (see Version Notes) (Future versions ought to work just fine too!).

License No Dependencies

JitPack JavaDoc




The code for this example can be found in the library test class.



Version Notes

IMPORTANT! If you have an opinion on how backwards compatibility should be achieved with new versions, please feel free to drop a reply to this open discussion.

  • I don't see a reason it shouldn't work in Spigot 1.7 or any version of Bukkit from 1.8 - 1.20 but it hasn't been tested on each individual version.
  • This library has been tested on Spigot 1.8, Spigot 1.16, PaperSpigot 1.19, Spigot 1.20 and is expected to work on every versions in-between for most, if not all, forks of Spigot.
  • The ItemBuilder API should work for all versions of Bukkit/Spigot unless you use the ItemDataColor (or data value) which relies on pre-1.13 item data values. (Though you can just use the relevant Material instead - e.g., instead of using Material.WOOL and ItemDataColor.BLUE, just use Material.BLUE_WOOL.)

Installation

You can very easily install SpiGUI using JitPack. (The JitPack page contains instructions for Gradle, Maven, sbt, etc.)

Instructions for Gradle

Just add the following to your build.gradle file:

repositories {
    // ...
    maven { url 'https://jitpack.io' }
}

dependencies {
    // ...
    implementation 'com.samjakob:SpiGUI:<insert latest version here>'
}

For distribution, you can just shade the library into your plugin JAR. On Gradle, this can be done by adding the following to the end of your build.gradle:

jar {
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

If you aren't using a build system, you can just download the latest JAR and add it to your project's classpath (just make sure the SpiGUI classes are included in your JAR when you build it).


Quick Start

Step 1: Create an instance of the SpiGUI library in your plugin

class MyPlugin extends JavaPlugin {

  public static SpiGUI spiGUI;
  
  @Override
  public void onEnable() {
    // (IMPORTANT!) Registers SpiGUI event handlers (and stores plugin-wide settings for SpiGUI.)
    spiGUI = new SpiGUI(this);
  }
  
}

Step 2: Use the library

public void openMyAwesomeMenu(Player player) {

  // Create a GUI with 3 rows (27 slots)
  SGMenu myAwesomeMenu = MyPlugin.spiGUI.create("&cMy Awesome Menu", 3);

  // Create a button
  SGButton myAwesomeButton = new SGButton(
    // Includes an ItemBuilder class with chainable methods to easily
    // create menu items.
    new ItemBuilder(Material.WOOD).build()
  ).withListener((InventoryClickEvent event) -> {
    // Events are cancelled automatically, unless you turn it off
    // for your plugin or for this inventory.
    event.getWhoClicked().sendMessage("Hello, world!");
  });
  
  // Add the button to your GUI
  myAwesomeMenu.addButton(myAwesomeButton);
  
  // Show the GUI
  player.openInventory(myAwesomeMenu.getInventory());

}

Step 3: Profit!


Why?

Chest Inventory Menus (commonly referred to as GUIs) are the ubiquitous way to display menus, execute actions and even manage configuration in Spigot plugins. However, the Inventory API leveraged to achieve this in Spigot is not designed for menus, making code far more verbose and far less maintainable than it needs to be.

SpiGUI is a rewrite of my outdated SpigotPaginatedGUI API including improvements and features I've added whilst using the API in my own software that aims to provide a highly readable and concise API for menus.

spigui's People

Contributors

mcreeper12731 avatar samjakob avatar stanley-dam avatar stijnb1234 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spigui's Issues

SGMenuListener does not handle InventoryDragEvent

Currently this library does not handle the InventoryDragEvent which means that one could modify an otherwise protected inventory by dragging instead of clicking.

Also "clicks" could go missing and annoy players as even moving the mouse a single pixel while clicking will be counted as a drag, not click event.

Block unrealistic interactions to avoid getting items from gui

Currently, if we use any interaction not including LEFT and RIGHT click, we can essentially get items from the inventory. This probably isn't an intended behavior, but it would be good to add that as a boolean nonetheless.

private boolean blockDefaultInteractions = true;

Such as: blockUnrealisticInteractions(?), which defaults to true, since 99% of interactions will be handled with either left or right click.

Than it's probably just a case of:

if(clickedGui.areUnrealisticInteractionsBlocked()){
    event.setCancelled(true);
}

You can already achieve the same behavior without that change, but it would be helpful nonetheless:

...
).withListener((InventoryClickEvent e) -> {
    if(e.getClick() == ClickType.LEFT){
        //left click stuff
    } else if (e.getClick() == ClickType.RIGHT) {
        //right click stuff
    } else {
        e.setCancelled(true);
    }
})

Would've made a PR but I would rather give that someone who actually knows what they're doing lmao

InventoryClickEvent error with auto Pagination

Could not pass event InventoryClickEvent to CombatData v1
org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2277) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:32) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:941) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:934) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:918) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:850) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
Caused by: java.lang.NullPointerException
        at com.samjakob.spigui.SGMenu.nextPage(SGMenu.java:443) ~[?:?]
        at com.samjakob.spigui.SpiGUI.lambda$null$2(SpiGUI.java:94) ~[?:?]
        at com.samjakob.spigui.menu.SGMenuListener.onInventoryClick(SGMenuListener.java:64) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot.jar:git-Spigot-37d799b-3eb7236]
        ... 18 more

This error occurs when moving pages with auto-pagination. Weird because the buttons actually work, and cycle pages properly and the inventory setup also is fine.

I also had a quick performance question:

Right now I'm creating a new SGMenu for every player whenever I need one. I feel as though this could cause performance issues, so I'm curious as to whether or not after the inventory is closed the menu is removed from memory. If I'm just creating a bunch of instances over and over I'd rather just store each SGMenu in a map so I only need one instance for player and can update it as I go.

Plugin does not check if the clicked inventory is the actual GUI

There is a known issue in Spigot when checking the inventory's clicked slot number because of the slot numbering system (picture included)
image
I fixed this issue by adding an additional fix
if (event.getClickedInventory() == null || event.getClickedInventory().getType() == InventoryType.PLAYER) return;
which checks if the inventory that was clicked was actually the upper part of the UI, otherwise the listener also let's through clicks that were done in the player inventory and act as if they were done in the GUI. But adding this everytime is a bit tedious. Sorry if the explanation is a bit confusing, I can go over certain parts again if you don't understand the problem

Refresh inventory.

Good night, I have a question. Which is the easiest way to refresh the inventory every X seconds? (Items and its lore)
I only want to refresh the contents because it could be that a new item be created or removed with a command that my plugin has.

Greetings.

Bug in Toolbar

I created a pagable ui with a Toolbar.
But it seens like the first slot of the toolbar is still used by the page-content.
The Item is not displayed, but its clickable, so one item in the page-content is lost!

No items in close event

Hi.

This code:

addingMenu.setOnClose(event ->
{
    p.getInventory().setContents(event.getInventory().getContents());
});

clears my inventory, because getInventory in this event is empty...
Can i somehow get it contents? Or maybe this is just wrong?

Add or change some PaginationButtons

There is the possibility of being able to modify the buttons of PaginationButtons? I'm trying to change icons 'next' and 'prev' page but i can't found any solution.

Shift + click not firing Listener

Hey there, I've been using SpiGUI and it has been a great experience, but I've ran into an issue!

image

I want to use isShiftClick(), but it never fires the event when the player uses shift click...
Could this be fixed? :)

null error

I get this error when clicking the arrows

        at spigui.SGMenu.nextPage(SGMenu.java:444) ~[?:?]
        at spigui.SpiGUI.lambda$new$2(SpiGUI.java:96) ~[?:?]
        at spigui.menu.SGMenuListener.onInventoryClick(SGMenuListener.java:64) ~[?:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor846.execute(Unknown Source) ~[?:?]```

Server version
 This server is running Paper version git-Paper-385 (MC: 1.15.2) (Implementing API version 1.15.2-R0.1-SNAPSHOT)

refreshInventory bug

Hi, to summarize I found a bug on an if statement in the refreshInventory() method.

// If the new size is different, we'll need to open a new inventory.
if (viewer.getOpenInventory().getTopInventory().getSize() != getPageSize() + (getMaxPage() > 0 ? 9 : 0)) {
    viewer.openInventory(getInventory());
    return;
}

My code:

SGMenu menu = Main.spiGUI.create("&eInventory", 2);
menu.setAutomaticPaginationEnabled(false);
//Buttons stuff
BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
BukkitTask task = scheduler.runTaskTimer(plugin, new Runnable() {
    @Override
    public void run() {
        menu.clearAllButStickiedSlots();
        //Set buttons stuff again
        menu.refreshInventory(p);
    }
}, 0L, 20L);
menu.setOnClose(inventory -> {
    if (task != null) scheduler.cancelTask(task.getTaskId());
});

The problem is that when I use menu.setAutomaticPaginationEnabled(false); the previous if statement is called and the scheduler is cancelled because it opens a new inventory and that must not happen because I am not modifying the inventory size after opening the inventory.

Maven?

Can you add this to maven?

Cant Create Custom Menu Buttons

I am using your API for a skin setting purpose. But I found myself unable to add a clear skin button along with the autopager next and back button row.

Dependencies

I am currently writing a Plugin and I would apprechiate, if you could add a github package for me to include in my project.

I don't work with gradle that much so I don't know, if there is a difference between maven and gradle packages. If there is, please also add a maven-package.

If that is not possible just close this issue.

Thank you for taking your time to actually code this!

Kind regards,
Trqhxrd

ArrayIndexOutOfBoundsException

Hey i get the following error when i try to use your example code at step 2 (https://github.com/SamJakob/SpiGUI)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 27 at java.util.Arrays$ArrayList.set(Unknown Source) ~[?:1.8.0_261] at net.minecraft.server.v1_16_R1.NonNullList.set(SourceFile:53) ~[patched_1.16.1.jar:git-Purpur-710] at org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventoryCustom$MinecraftInventory.setItem(CraftInventoryCustom.java:108) ~[patched_1.16.1.jar:git-Purpur-710] at org.bukkit.craftbukkit.v1_16_R1.inventory.CraftInventory.setItem(CraftInventory.java:99) ~[patched_1.16.1.jar:git-Purpur-710] at com.samjakob.spigui.SGMenu.getInventory(SGMenu.java:659) ~[?:?]

Bottom inventory event

Hello, i would like to add an event to the bottom inventory (player inventory).

I'm adding an item to the player's inventory when the item is clicked in the spiGUI inventory. Additionally, I want the item to be removed from the bottom inventory when it is clicked IN the bottom inventory.

Is there a way to do something like that ?

Add ChatComponent support

What do I want?
I want to add Adventure API support. Or at least bungeecord component builder
Is there any current method of doing this?
Set the name for the inventory from gui.getInventory()
What is adventure API?
Adventure API - created by Kyori - is a rich component builder API which allows you to build components (Paper)
What about bungeecord/spigot API for components?
https://www.spigotmc.org/wiki/the-chat-component-api/

InventoryClickEvent click types

Is there a way to handle all inventory click event click types inside the SGButton withListener?
The event will allow only LEFT and RIGHT click types here but i want to also handle SHIFT_LEFT or SHIFT_RIGHT.
I tried to override the SGMenuListener onInventoryClick but it was firing both events

Help with some things

Hi, your API is awesome but I have some questions about the pagination system.

First, only when there are two items created, the pagination row and items are showed, as you can see, but is there a way to make that the row and its items always appear even though there are less than two items?
image
image

Second, is there a way to change the slot of the pagination items? (the nametag and arrows).

Thirth and finally, is there a way to add more items to the pagination row? for example glass panels to decorate.

That is all, thank you.

Customizing Toolbar (Pagination) buttons

Currently as far as I am aware, the Buttons for automatic pages cannot be localised or changed since their lore and names are hardcoded.

Hence a feature I would love to have is the ability to change the lore and names of the buttons.

[QUESTION TO USERS] What Minecraft versions are this library being used with?

This is an open discussion on the below points. Feel free to drop a reply with your thoughts.

  • Is anyone having issues with the current approach to compatibility with this library?
  • Is anyone still using this plugin with Minecraft server versions below 1.13 (i.e., with legacy support)?
    • Only the server version matters, e.g., if you're using ViaVersion/ViaBackwards for additional client support, dealing with other client versions would be done by that plugin.
  • How should version compatibility be handled?
    • Assuming 1.12 and below are not needed, support for those versions can be dropped and api-version can be used instead.
    • Alternatively, does anyone have suggestions on how backwards compatibility should be achieved?

Please Write Docs!

Please do us a favor and write good documentation.
I wasted literally 3 hours trying to figure out how to make custom Toolbars!
Not everyone knows your thoughts and your concepts!

I really like your effort and the result, don't get me wrong. But I would appreciate a good documentation :)

Toolbar Button and Menu Slots

I created a multi-line menu that displayed items and gave them upon clicking. Then, I added a toolbar. An item from the toolbar that was in slot 0 (on the toolbar) had the click properties of an item located on the next page in slot 0. Please fix this.

So far, I haven't found any bugs, but I have more to add.

  1. Add the ability to choose the type of inventory so that it can be set to not only 9 * (number of rows).
  2. Add automatic row calculation to avoid extra rows. I think this would be convenient and very useful for someone. It would eliminate the need to specify rows in the constructor.

ClassNotFound error with Gradle

I'm having an issue where when I try to load the plugin on a server I get this error "java.lang.NoClassDefFoundError: com/samjakob/spigui/SpiGUI" Here is the implementation in the build.gradle file:

repositories {
    mavenCentral()
    maven {
        name = "spigotmc-repo"
        url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
    }
    maven {
        name = "sonatype"
        url = "https://oss.sonatype.org/content/groups/public/"
    }
    maven { url 'https://jitpack.io' }
}

dependencies {
    compileOnly "org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT"
    compileOnly "org.xerial:sqlite-jdbc:3.8.11.2"
    compileOnly 'com.zaxxer:HikariCP:2.3.2'
    compileOnly "com.samjakob:SpiGUI:v1.3"
}

Any help would be lovely :D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.