Skip to content
Ntdi edited this page Dec 15, 2022 · 1 revision

Introduction into the GUI Framework

NR-Core's GUI framework is really simple to comprehend and blazing quick!

Basic Usage

First, in order to start using NR-Core's GUI framework, you must make a new class that extends GUI

MyGUI.java

public class MyGUI extends GUI {

}

We extend GUI as taht is the framework needed to make GUIs


Now, we need to give our GUI some basic information for it to work.

GUI has two constructors you can use,

  • super(String name, int rows); This takes the name of the GUI and how many rows you want, the rest is handled for you!
  • super(Inventory inventory); If you need more customization, you can use this to supply it with Bukkit.createInventory() - But I won't get into that.

Let's give our MyGUI a name of "&cWow!" and 5 rows.

MyGUI.java

public class MyGUI extends GUI {
    public MyGUI() {
        super("&cWow!", 5);
    }
}

And there we go! We made our first blank GUI. Now let's get into how to add buttons.


GUI Has a second baby called Button, these buttons allow us to provide functionality to our GUI!

We can create a static button with no functionality like this.

MyGUI.java

public class MyGUI extends GUI {
    public MyGUI() {
        super("&cWow!", 5);

        Button btn = Button.createBasic(new ItemStack(Material.STONE));
        addButton(btn, 23);

        update();
    }
}

Great! We've created a button using Button.createBasic(); which just makes a simple button that does nothing. Then we added it to the 23rd slot of the GUI with addButton();. Lastly, we call update() to update the GUI with new information.


If you thought that was all the GUI framework could do, you'd be so wrong.

What if you wanted your button to do something! We can use #create(); instead.

MyGUI.java

public class MyGUI extends GUI {
    public MyGUI() {
        super("&cWow!", 5);

        Button btn = Button.create(new ItemStack(Material.STONE), (e) -> {
            Bukkit.getLogger(e.getWhoClicked().getName());
        });

        addButton(btn, 23);
        update();
    }
}

Now we use #create(); to create a button that takes has functionality. The first argument is still the same ItemStack but the second argument is a consumer. The provided consumer action is the InventoryClickEvent. With that event we get the person who clicked and log them to the console!


What if you need access to the button that was clicked in your consumer? We have a method for that with a BiConsumer<InventoryClickEvent, Button>!

MyGUI.java

public class MyGUI extends GUI {
    public MyGUI() {
        super("&cWow!", 5);

        Button btn = Button.create(new ItemStack(Material.STONE), (e, b) -> {
            ItemStack buttonItem = b.getItem();
            Bukkit.getLogger(e.getWhoClicked().getName());
        });

        addButton(btn, 23);
        update();
    }
}

Now in this we can access both the inventory click event and the button. (e and b)

Congratualations! You've created your first GUI. Scroll down to see how to show it to the player and some good formatting tips.

In-depth GUI creation

Tips and Tricks

GUI contains a method called open() which opens the said GUI to the passed in player! We can take a player variable in our constuctor then open the GUI to the given player.

MyGUI.java

public class MyGUI extends GUI {
    public MyGUI(Player p) {
        super("&cWow!", 5);

        Button btn = Button.createBasic(new ItemStack(Material.STONE));
        addButton(btn, 23);

        update();

        open(p);
    }
}

Simple!


Now for some good tips, when making GUIs, code can often get messy! Let's fix that by making a seperate function for all our GUI items, then calling that function in the Constructor

public class MyGUI extends GUI {
    public MyGUI(Player p) {
        super("&cWow!", 5);

        addItems();

        open(p);
    }

    private void addItems() {
        Button btn = Button.createBasic(new ItemStack(Material.STONE));
        addButton(btn, 23);

        update();
    }
}

Notice how all the code stays the same, it's just now easier to read and mantain it!

Method explanations

GUI

addButton(); Add a button to a GUI with a given slot.

removeButton(); Remove a button from the GUI.

fill(); Fill a GUI with a certain ItemStack from a slot to another slot.

getButtons(); Get a list of all the buttons in a GUI.

clearSlot(); Remove a button or other item from a slot.

update(); Update a GUI with the newest information.

open(); Open a GUI to a player.

openSlot(); Open a slot for a user to put an item into.

openSlots(); Open multiple slots for a user to to put items into.

closeSlot(); Close a slot for a user to put an item into.

closeSlots(); Close multiple slots for a user to put items into.

getOpenSlots(); Get a list of all the open slots for a user to put items into.

setReturnsItems(); Set if the GUI should return the item's put into the GUI by the user.

setDestroyOnClose(); Set if the GUI should destory itself when it's closed.

setOnDestroy(); What should happen when a GUI is destoryed. Takes a Runnable.

setOnClickOpenSlot(); Set what happens when an open slot is clicked.

setOnDragOpenSlot(); Set what happens when an item is dragged to an open slot.

destory(); Destorys the GUI.

clear(); Clears the GUI of all its items.

Button

createBasic(); Create a static button that has no functionality.

create(); Create a button with a consumer.

create(); Create a button with a BiConsumer.

setSlot(); Should not be used see above GUI functions instead.

getSlot(); Get the slot the button is in.

getItem(); Get the ItemStack stored in the button.

setButton(); Should not be used see above GUI functions instead.

Clone this wiki locally