components - Hazebyte/base GitHub Wiki

Components

Base is composed of reusable components.

Menu

To create a reusable menu, one would do...

/**
 * Creates a inventory that lists players
 */
public class PlayerListPage extends Base {
    public PlayerListPage(JavaPlugin plugin, String title, Size size) {
        super(plugin, title, size);

        for (Player player: Bukkit.getOnlinePlayers()) {
            // Create a new item that shows the player name
            ItemStack item = new ItemBuilder(Material.SKULL_ITEM)
                    .displayName(player.getName())
                    .lore(String.format("&fHealth: %d", player.getHealth()))
                    .asItemStack();

            // Create a new button with the item stack
            Button button = new Button(item);

            // Add the item to this menu
            // Handles pagination automatically!
            this.addIcon(button);
        }
        // TODO: Things to think about
        // What if new players join?
        // What if players leave
    }
}

To use the component

    PlayerListPage page = new PlayerListPage(plugin, "Players", Size.from(27));
    page.open(player);

Button

To create a reusable button, one would do

public class TeleportButton extends Button {

    private static final ItemStack DEFAULT = new ItemBuilder(Material.BOOKSHELF)
            .displayName("&fTeleport!")
            .asItemStack();

    public TeleportButton(Location location) {
        this(DEFAULT, location);
    }

    /**
     * Creates a button with a item icon.
     *
     * @param item
     * @throws NullPointerException if the item is null
     */
    public TeleportButton(ItemStack item, Location location) {
        super(item);

        setAction(event -> event.getEntity().teleport(location));
    }
}

To use this component,

Location location = player.getLocation();
TeleportButton button = new TeleportButton(location);
menu.addIcon(button);