Skip to content

DigitalityDev/DigitalGUI

Repository files navigation

DigitalGUI

Modern GUI system for Developers

If you like our project, please join our Discord!

Table of Contents

0. Features

  • Easy to use
  • Capable of creating complex GUIs
  • Maintainer has (unfortunately) a lot of free time
  • Pagination (TODO!)

1. Installation

Maven

<repositories>
    <repository>
        <id>digitality-repo-releases</id>
        <url>https://repo.digitality.dev/releases</url>
    </repository>
</repositories>

<dependencies>
  <dependency>
      <groupId>dev.digitality</groupId>
      <artifactId>digitalgui</artifactId>
      <version>1.0.1</version>
  </dependency>
</dependencies>

Gradle

repositories {
    maven {
        name "digitalityRepoReleases"
        url "https://repo.digitality.dev/releases"
    }
}

dependencies {
    implementation "dev.digitality:digitalgui:1.0.1"
}

2. Shading + Relocating (Very important!)

Maven

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <configuration>
                <relocations>
                    <relocation>
                        <pattern>dev.digitality.digitalgui</pattern>
                        <shadedPattern>your.package.here.digitalgui</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </plugin>
    </plugins>
</build>

Gradle

plugins {
    id "com.github.johnrengelman.shadow" version "8.1.1"
}

shadowJar {
    relocate "dev.digitality.digitalgui", "your.package.here.digitalgui"
}

3. GUI Usage

This isn't what exactly makes the GUI interactive, please check the section below.

3.0 Registering the API

To register the API, you need to call the DigitalGUI.register() method. You can (and should) do this in your onEnable() method. You need to do this only once.

@Override
public void onEnable() {
    DigitalGUI.register(this);
}

3.1 Creating a GUI

To create a GUI you need to make a separate class implementing the IGUI interface and getInventory() method. If you wish to pass anything, such as a player, you can do so by adding parameters to the constructor.

public class ExampleGUI implements IGUI {
    private final Player player;

    public ExampleGUI(Player player) {
        this.player = player;
    }

    @Override
    public Inventory getInventory() {
        Inventory inventory = Bukkit.createInventory(this, 9, "Example GUI"); // Don't forget the "this"!
        inventory.setItem(0, new ItemStack(Material.DIAMOND));
        
        return inventory;
    }
}

3.2 Opening a GUI

Opening a GUI as simple as calling the getInventory() method.

player.openInventory(new ExampleGUI(player).getInventory());

4. InteractiveItem Usage

4.0 Registering the API

To register the API, you need to call the DigitalGUI.register() method. You can (and should) do this in your onEnable() method. You need to do this only once.

@Override
public void onEnable() {
    DigitalGUI.register(this);
}

4.1 What is an InteractiveItem?

InteractiveItem serves as a clickable drop-in replacement for ItemStack. This means, that everything you can do with ItemStacks works with InteractiveItems as well. InteractiveItem also works with both InventoryClickEvent and PlayerInteractEvent. This means, that you can use InteractiveItems in both GUIs and in the world.

This includes, but is not limited to:

  • Setting the item's name with one line
  • Setting the item's lore with one line
  • Setting the item's glow with one line
  • Setting left/right click actions

4.2 Creating an InteractiveItem

Creating an InteractiveItem is as simple as creating a new instance of the class. You can optionally specify the slot, the item's name and the item's lore. See the source code for more.

InteractiveItem item = new InteractiveItem(Material.DIAMOND, 0, "§aDiamond", "§7This is a diamond.");

4.3 Setting the click handler

To set the click handler, you need to call the onClick() method. This method takes a function as a parameter, with player and click type as parameters.

item.onClick((player, clickType) -> {
    player.sendMessage("You clicked the diamond!");
});

You can also use onLeftClick and onRightClick methods, which take a function with player as a parameter.

item.onLeftClick(player -> {
    player.sendMessage("You left clicked the diamond!");
});

4.4 Setting the glow

To set the glow, you need to call the setGlow() method. This method takes a boolean as a parameter.

item.setGlow(true);

5. Pagination Usage

TODO!

6. Utilities

6.1 Creating borders and filling

You can use the fillInventory() method to fill an inventory with an item and create a frame around it.

Inventory inventory = Bukkit.createInventory(this, 9, "Example GUI"); // Don't forget the "this"!
ItemStack fillerItem = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
ItemStack borderItem = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
DigitalGUI.fillInventory(inventory, fillerItem, borderItem);

7. Putting it all together

Here is a final example which you can use as a reference.

public class ExampleGUI implements IGUI {
    private final Player player;

    public ExampleGUI(Player player) {
        this.player = player;
    }

    @Override
    public Inventory getInventory() {
        Inventory inventory = Bukkit.createInventory(this, 9, "Example GUI"); // Don't forget the "this"!
        DigitalGUI.fillInventory(inventory, new InteractiveItem(Material.GRAY_STAINED_GLASS_PANE, 0, "§7"), new InteractiveItem(Material.BLACK_STAINED_GLASS_PANE, 0, "§7"));
        InteractiveItem item = new InteractiveItem(Material.DIAMOND, 0, "§aDiamond", "§7This is a diamond.")
              .onClick((player, clickType) -> { // This will run for any click action
                player.sendMessage("You clicked the diamond!");
              })
              .onLeftClick(player -> { // This will run on left click, regardless of whether it was InventoryClickEvent or PlayerInteractEvent
                player.sendMessage("You left clicked the diamond!");
              })
              .onRightClick(player -> { // This will run on right click, regardless of whether it was InventoryClickEvent or PlayerInteractEvent
                player.sendMessage("You right clicked the diamond!");
              });
        inventory.setItem(item.getSlot(), item);
        
        return inventory;
    }
}