Added support for 3x3 menus, improved Java Page API.

This commit is contained in:
Auxilor
2022-09-30 14:06:39 +01:00
parent e1c063d5f4
commit 21d933cb11
14 changed files with 226 additions and 69 deletions

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.core.gui;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.gui.menu.Menu;
import com.willfp.eco.core.gui.menu.MenuBuilder;
import com.willfp.eco.core.gui.menu.MenuType;
import com.willfp.eco.core.gui.slot.SlotBuilder;
import com.willfp.eco.core.gui.slot.functional.SlotProvider;
import org.bukkit.inventory.ItemStack;
@@ -29,10 +30,12 @@ public interface GUIFactory {
* Create menu builder.
*
* @param rows The amount of rows.
* @param type The type.
* @return The builder.
*/
@NotNull
MenuBuilder createMenuBuilder(int rows);
MenuBuilder createMenuBuilder(int rows,
@NotNull MenuType type);
/**
* Combine the state of two menus together.

View File

@@ -27,6 +27,15 @@ public interface Menu {
*/
int getRows();
/**
* Get the amount of columns.
*
* @return The amount of columns.
*/
default int getColumns() {
return 9;
}
/**
* Get a static slot at a given row and column.
* <p>
@@ -81,6 +90,21 @@ public interface Menu {
*/
List<ItemStack> getCaptiveItems(@NotNull Player player);
/**
* Get a captive item at a specific position.
*
* @param player The player.
* @param row The row.
* @param column The column.
* @return The captive item.
*/
@Nullable
default ItemStack getCaptiveItem(@NotNull final Player player,
final int row,
final int column) {
return null;
}
/**
* Add state for a player.
*
@@ -196,6 +220,19 @@ public interface Menu {
* @return The builder.
*/
static MenuBuilder builder(final int rows) {
return Eco.getHandler().getGUIFactory().createMenuBuilder(rows);
return Eco.getHandler().getGUIFactory().createMenuBuilder(
rows,
MenuType.NORMAL
);
}
/**
* Create a builder with a given type.
*
* @param type The menu type.
* @return The builder.
*/
static MenuBuilder builder(@NotNull final MenuType type) {
return Eco.getHandler().getGUIFactory().createMenuBuilder(type.getDefaultRows(), type);
}
}

View File

@@ -101,6 +101,22 @@ public interface MenuBuilder extends PageBuilder {
return this.addComponent(MenuLayer.TOP, 1, 1, page);
}
/**
* Add a page.
*
* @param pageNumber The page number.
* @param pageBuilder The page builder.
* @return The builder.
*/
default MenuBuilder addPage(final int pageNumber,
@NotNull final Consumer<PageBuilder> pageBuilder) {
MenuBuilder builder = Menu.builder(this.getRows());
pageBuilder.accept(builder);
Page page = new Page(pageNumber, builder.build());
return this.addPage(page);
}
/**
* Set the max pages.
*

View File

@@ -0,0 +1,56 @@
package com.willfp.eco.core.gui.menu;
/**
* The type of menu.
*/
public enum MenuType {
/**
* Normal menu (1x9, 2x9, 3x9, etc).
*/
NORMAL(9, 6),
/**
* Dispenser menu (3x3).
*/
DISPENSER(3, 3);
/**
* The amount of columns.
*/
private final int columns;
/**
* The default amount of rows.
*/
private final int defaultRows;
/**
* Create a new menu type.
*
* @param columns The number of columns.
* @param defaultRows The default number of rows.
*/
MenuType(final int columns,
final int defaultRows) {
this.columns = columns;
this.defaultRows = defaultRows;
}
/**
* Get the amount of columns.
*
* @return The columns.
*/
public int getColumns() {
return columns;
}
/**
* Get the default amount of rows.
*
* @return The default amount of rows.
*/
public int getDefaultRows() {
return defaultRows;
}
}

View File

@@ -3,14 +3,12 @@ package com.willfp.eco.core.gui.page;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.gui.component.GUIComponent;
import com.willfp.eco.core.gui.menu.Menu;
import com.willfp.eco.core.gui.menu.MenuBuilder;
import com.willfp.eco.core.gui.slot.Slot;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Consumer;
/**
* A page is a component representing another menu.
@@ -42,6 +40,16 @@ public final class Page implements GUIComponent {
*/
private Menu delegate = null;
/**
* The rows for the page to have.
*/
private int rows = 6;
/**
* The columns for the page to have.
*/
private int columns = 9;
/**
* Create a new page.
*
@@ -54,20 +62,6 @@ public final class Page implements GUIComponent {
this.page = page;
}
/**
* Create a new page.
*
* @param pageNumber The page number.
* @param page The base menu.
*/
public Page(final int pageNumber,
@NotNull final Consumer<PageBuilder> page) {
this.pageNumber = pageNumber;
MenuBuilder builder = Menu.builder(6);
page.accept(builder);
this.page = builder.build();
}
/**
* Get the current page number.
*
@@ -93,14 +87,21 @@ public final class Page implements GUIComponent {
return page.getSlot(row, column, player, delegate);
}
@Override
public void init(final int maxRows,
final int maxColumns) {
this.rows = maxRows;
this.columns = maxColumns;
}
@Override
public int getRows() {
return page.getRows();
return rows;
}
@Override
public int getColumns() {
return 9;
return columns;
}
/**

View File

@@ -1,19 +1,14 @@
package com.willfp.eco.core.gui.page;
import com.willfp.eco.core.gui.component.GUIComponent;
import com.willfp.eco.core.gui.menu.CloseHandler;
import com.willfp.eco.core.gui.menu.Menu;
import com.willfp.eco.core.gui.menu.MenuLayer;
import com.willfp.eco.core.gui.menu.OpenHandler;
import com.willfp.eco.core.gui.slot.FillerMask;
import com.willfp.eco.core.gui.slot.Slot;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.jetbrains.annotations.NotNull;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Builder to create pages.
@@ -26,6 +21,13 @@ public interface PageBuilder {
*/
int getRows();
/**
* Get the amount of columns.
*
* @return The amount of columns.
*/
int getColumns();
/**
* Set a slot.
*

View File

@@ -90,9 +90,6 @@ public class FillerMask implements GUIComponent {
for (String patternRow : pattern) {
int column = 0;
if (patternRow.length() != 9) {
throw new IllegalArgumentException("Invalid amount of columns in pattern!");
}
for (char c : patternRow.toCharArray()) {
if (c == '0') {
mask.get(row).set(column, null);

View File

@@ -9,6 +9,7 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;

View File

@@ -3,14 +3,10 @@ package com.willfp.eco.util;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.gui.menu.Menu;
import com.willfp.eco.core.tuples.Pair;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;
/**
* Utilities / API methods for menus.
*/
@@ -23,9 +19,7 @@ public final class MenuUtils {
*/
@NotNull
public static Pair<Integer, Integer> convertSlotToRowColumn(final int slot) {
int row = Math.floorDiv(slot, 9);
int column = slot - row * 9;
return new Pair<>(row + 1, column + 1);
return convertSlotToRowColumn(slot, 9);
}
/**
@@ -36,7 +30,36 @@ public final class MenuUtils {
* @return The slot.
*/
public static int rowColumnToSlot(final int row, final int column) {
return (column - 1) + ((row - 1) * 9);
return rowColumnToSlot(row, column, 9);
}
/**
* Convert 0-53 slot to row and column pair.
*
* @param slot The slot.
* @param columns The columns.
* @return The pair of row and columns.
*/
@NotNull
public static Pair<Integer, Integer> convertSlotToRowColumn(final int slot,
final int columns) {
int row = Math.floorDiv(slot, columns);
int column = slot - row * columns;
return new Pair<>(row + 1, column + 1);
}
/**
* Convert row and column to 0-53 slot.
*
* @param row The row.
* @param column The column.
* @param columns The columns in the menu.
* @return The slot.
*/
public static int rowColumnToSlot(final int row,
final int column,
final int columns) {
return (column - 1) + ((row - 1) * columns);
}
/**