Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some more features to (multi page) guis #278

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/minevalley/core/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import minevalley.core.api.discord.Webhook;
import minevalley.core.api.economy.BankAccount;
import minevalley.core.api.enums.DebugType;
import minevalley.core.api.gui.FillItem;
import minevalley.core.api.gui.InventoryGui;
import minevalley.core.api.gui.MultiPageGui;
import minevalley.core.api.npc.NPC;
Expand Down Expand Up @@ -547,14 +548,15 @@ public static InventoryGui createGui(@Nonnull Component title, @Nonnegative int
* <br>
* {@code %o%} will be replaced with the amount of pages
*
* @param title title of the inventory
* @param size size of the inventory
* @param title title of the inventory
* @param size size of the inventory
* @param fillItems items to fill the inventory with
* @return new gui-builder
* @throws IllegalArgumentException if the size is invalid (negative, higher than 54 or not a multiple of 9 while being higher than 6)
*/
@Nonnull
public static MultiPageGui createMultiPageGui(@Nonnull Component title, @Nonnegative int size) throws IllegalArgumentException {
return server.createMultiPageGui(size);
public static MultiPageGui createMultiPageGui(@Nonnull Component title, @Nonnegative int size, @Nonnull List<FillItem> fillItems) throws IllegalArgumentException {
return server.createMultiPageGui(title, size, fillItems);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/minevalley/core/api/CoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import minevalley.core.api.discord.Webhook;
import minevalley.core.api.economy.BankAccount;
import minevalley.core.api.enums.DebugType;
import minevalley.core.api.gui.FillItem;
import minevalley.core.api.gui.InventoryGui;
import minevalley.core.api.gui.MultiPageGui;
import minevalley.core.api.npc.NPC;
Expand Down Expand Up @@ -154,7 +155,7 @@ public interface CoreServer {
InventoryGui createGUI(@Nonnull Component title, @Nonnegative int size) throws IllegalArgumentException;

@Nonnull
MultiPageGui createMultiPageGui(@Nonnegative int size) throws IllegalArgumentException;
MultiPageGui createMultiPageGui(@Nonnull Component title, @Nonnegative int size, @Nonnull List<FillItem> fillItems) throws IllegalArgumentException;

@Nonnull
World getMainWorld();
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/minevalley/core/api/gui/FillItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package minevalley.core.api.gui;

import minevalley.core.api.Core;
import minevalley.core.api.users.OnlineUser;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public record FillItem(@Nonnull ItemStack itemStack,
@Nullable Consumer<OnlineUser> callback,
@Nullable BiConsumer<OnlineUser, InventoryClickEvent> advancedCallback) {

public FillItem {
if (itemStack == null) throw new IllegalArgumentException("ItemStack cannot be null");
}

@SuppressWarnings("unused")
public void accept(final @Nonnull InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) return;
final OnlineUser user = Core.getOnlineUser(player);
if (callback != null) callback.accept(user);
if (advancedCallback != null) advancedCallback.accept(user, event);
}
}
10 changes: 10 additions & 0 deletions src/main/java/minevalley/core/api/gui/InventoryGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item,
@Nonnull
InventoryGui addCloser();

/**
* Empties the specified slot.
*
* @param slot the slot to empty
* @return this GUI
* @throws IllegalArgumentException if the slot is out of bounds
*/
@Nonnull
InventoryGui empty(@Nonnegative int slot) throws IllegalArgumentException;

/**
* Gets the item in the specified slot.
*
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/minevalley/core/api/gui/MultiPageGui.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package minevalley.core.api.gui;

import minevalley.core.api.users.OnlineUser;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -116,18 +115,6 @@ MultiPageGui setPageDependantItem(@Nonnegative int slot, @Nonnull Function<Integ
return getInventory(0);
}

/**
* Updating the title of GUIs with multiple pages is not supported.
*
* @param title ignored
* @return nothing
*/
@Override
@Contract("_ -> fail")
default @Nonnull InventoryGui updateTitle(@Nonnull Component title) {
throw new UnsupportedOperationException("This GUI does not support updating the title.");
}

/**
* Opens this GUI for the specified player on the first page.
*
Expand Down
Loading