Skip to content

Commit

Permalink
introduction of GuiItemClick
Browse files Browse the repository at this point in the history
  • Loading branch information
Snabeldier committed Jan 18, 2025
1 parent c8eeb2e commit f2f2a0e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 53 deletions.
8 changes: 2 additions & 6 deletions src/main/java/minevalley/core/api/gui/FillItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@

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 record FillItem(@Nonnull ItemStack itemStack, @Nullable Consumer<GuiItemClick> callback) {

public FillItem {
if (itemStack == null) throw new IllegalArgumentException("ItemStack cannot be null");
Expand All @@ -23,7 +20,6 @@ public record FillItem(@Nonnull ItemStack itemStack,
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);
if (callback != null) callback.accept(new GuiItemClick(user, event.getClick(), event.getAction()));
}
}
12 changes: 12 additions & 0 deletions src/main/java/minevalley/core/api/gui/GuiItemClick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package minevalley.core.api.gui;

import minevalley.core.api.users.OnlineUser;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;

import javax.annotation.Nonnull;

public record GuiItemClick(@Nonnull OnlineUser user,
@Nonnull ClickType clickType,
@Nonnull InventoryAction inventoryAction) {
}
49 changes: 3 additions & 46 deletions src/main/java/minevalley/core/api/gui/InventoryGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import minevalley.core.api.users.OnlineUser;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Contract;

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

@SuppressWarnings("unused")
Expand Down Expand Up @@ -64,7 +62,7 @@ default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item)
*/
@Nonnull
@Contract("_, _, _ -> this")
InventoryGui setItem(@Nonnegative int slot, @Nonnull ItemStack item, @Nonnull Consumer<OnlineUser> onClick) throws IllegalArgumentException;
InventoryGui setItem(@Nonnegative int slot, @Nonnull ItemStack item, @Nonnull Consumer<GuiItemClick> onClick) throws IllegalArgumentException;

/**
* Sets the interface item in the specified slot.
Expand All @@ -77,35 +75,7 @@ default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item)
*/
@Nonnull
@Contract("_, _, _ -> this")
default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item, @Nonnull Consumer<OnlineUser> onClick) throws IllegalArgumentException {
return setItem(slot, item.toItemStack(), onClick);
}

/**
* Sets the item in the specified slot.
*
* @param slot the slot to set the item in
* @param item the item to set
* @param onClick the consumer to run when the item is clicked
* @return this GUI
* @throws IllegalArgumentException if the slot is out of bounds, or the item or onClick is null
*/
@Nonnull
@Contract("_, _, _ -> this")
InventoryGui setItem(@Nonnegative int slot, @Nonnull ItemStack item, @Nonnull BiConsumer<OnlineUser, InventoryClickEvent> onClick) throws IllegalArgumentException;

/**
* Sets the interface item in the specified slot.
*
* @param slot the slot to set the interface item in
* @param item the item to set
* @param onClick the consumer to run when the item is clicked
* @return this GUI
* @throws IllegalArgumentException if the slot is out of bounds, or the interface item or onClick is null
*/
@Nonnull
@Contract("_, _, _ -> this")
default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item, @Nonnull BiConsumer<OnlineUser, InventoryClickEvent> onClick) throws IllegalArgumentException {
default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item, @Nonnull Consumer<GuiItemClick> onClick) throws IllegalArgumentException {
return setItem(slot, item.toItemStack(), onClick);
}

Expand Down Expand Up @@ -174,20 +144,7 @@ default InventoryGui setItem(@Nonnegative int slot, @Nonnull InterfaceItem item,
* @throws IllegalArgumentException if the slot is out of bounds, or the onClick is null
*/
@Nonnull
InventoryGui onClickSlot(@Nonnegative int slot, @Nonnull Consumer<OnlineUser> onClick) throws IllegalArgumentException;

/**
* Sets a callback to be called when the specified slot is clicked.
* <p>
* <b>Note:</b> The callback can be overwritten by setting another callback for the same slot or an item on this slot.
*
* @param slot the slot to set the callback for
* @param onClick the callback to call
* @return this GUI
* @throws IllegalArgumentException if the slot is out of bounds, or the onClick is null
*/
@Nonnull
InventoryGui onClickSlot(@Nonnegative int slot, @Nonnull BiConsumer<OnlineUser, InventoryClickEvent> onClick) throws IllegalArgumentException;
InventoryGui onClickSlot(@Nonnegative int slot, @Nonnull Consumer<GuiItemClick> onClick) throws IllegalArgumentException;

/**
* Removes the callback for the specified slot.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/minevalley/core/api/gui/MultiPageGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface MultiPageGui extends InventoryGui {
@Nonnull
@Contract("_, _, _ -> this")
MultiPageGui setPageDependantItem(@Nonnegative int slot, @Nonnull Function<Integer, ItemStack> itemProvider,
@Nonnull Function<Integer, Consumer<OnlineUser>> callbackProvider) throws IllegalArgumentException;
@Nonnull Function<Integer, Consumer<GuiItemClick>> callbackProvider) throws IllegalArgumentException;

/**
* Get the amount of pages in this GUI.
Expand Down

0 comments on commit f2f2a0e

Please sign in to comment.