Skip to content

Commit

Permalink
Allows Selectors and Editors to consume player on close
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed May 11, 2024
1 parent 777d207 commit f557042
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 44 deletions.
85 changes: 47 additions & 38 deletions src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,21 +372,10 @@ public MetaBlobInventory buildMetaInventory(@NotNull String key,
}

/**
* Will make Player to select from a list of elements.
* The selector will be placed in the inventory at the specified buttonRangeKey.
* The inventory will open automatically.
* Clicking the return button will close the selector inventory.
*
* @param blobInventoryKey the key of the BlobInventory
* @param player the player
* @param buttonRangeKey the button from the BlobInventory which contains the slots (per page) into which the selector will place the elements
* @param dataType the data type of the selector
* @param selectorList the list of elements to select from
* @param onSelect what's consumed when an element is selected
* @param display the function to display an element, needs to return the ItemStack to display
* @param <T> the type of the selector
* @return the selector
* @deprecated Use {@link #customSelector(String, Player, String, String, Supplier, Consumer, Function, Consumer, Consumer)} instead.
*/
@Deprecated
@Nullable
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
Expand All @@ -403,6 +392,29 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
display, null);
}

/**
* @deprecated Use {@link #customSelector(String, Player, String, String, Supplier, Consumer, Function, Consumer, Consumer)} instead.
*/
@Deprecated
@Nullable
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@NotNull String dataType,
@NotNull Supplier<List<T>> selectorList,
@NotNull Consumer<T> onSelect,
@Nullable Function<T, ItemStack> display,
@Nullable Consumer<Player> onReturn) {
return customSelector(blobInventoryKey,
player,
buttonRangeKey,
dataType,
selectorList,
onSelect,
display,
onReturn, null);
}

/**
* Will make Player to select from a list of elements.
* The selector will be placed in the inventory at the specified buttonRangeKey.
Expand All @@ -416,17 +428,20 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
* @param onSelect what's consumed when an element is selected
* @param display the function to display an element, needs to return the ItemStack to display
* @param onReturn what's consumed when the player returns the selector
* @param onClose what's consumed when the player closes the selector
* @param <T> the type of the selector
* @return the selector
*/
@Nullable
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@NotNull String dataType,
@NotNull Supplier<List<T>> selectorList,
@NotNull Consumer<T> onSelect,
@Nullable Function<T, ItemStack> display,
@Nullable Consumer<Player> onReturn) {
@Nullable Consumer<Player> onReturn,
@Nullable Consumer<Player> onClose) {
BlobInventory inventory = buildInventory(blobInventoryKey, player);
BlobSelector<T> selector = BlobSelector.build(inventory, player.getUniqueId(),
dataType, selectorList.get(), onReturn);
Expand All @@ -438,7 +453,8 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
onSelect,
null,
display,
selectorList::get);
selectorList::get,
onClose);
else
selector.selectElement(player,
onSelect,
Expand Down Expand Up @@ -476,24 +492,9 @@ public <T> BlobSelector<T> selector(@NotNull Player player,
}

/**
* Will allow player to edit a collection of elements.
* The editor will be placed in the inventory at the specified buttonRangeKey.
* The inventory will open automatically.
* Clicking the return button will close the editor inventory.
*
* @param blobInventoryKey the key of the BlobInventory
* @param player the player
* @param buttonRangeKey the button from the BlobInventory which contains the slots (per page) into which the editor will place the elements
* @param dataType the data type of the editor
* @param addCollection the collection of elements to add to
* @param onAdd what's consumed when an element is added
* @param addDisplay the function to display an element, needs to return the ItemStack to display
* @param viewCollection the collection of elements to view
* @param removeDisplay the function to display an element, needs to return the ItemStack to display
* @param onRemove what's consumed when an element is removed
* @param <T> the type of the editor
* @return the editor
* @deprecated Use {@link #customEditor(String, Player, String, String, Supplier, Consumer, Function, Supplier, Function, Consumer, Consumer, Consumer)} instead.
*/
@Deprecated
@Nullable
public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
@NotNull Player player,
Expand All @@ -514,7 +515,9 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
addDisplay,
viewCollection,
removeDisplay,
onRemove, null);
onRemove,
null,
null);
}

/**
Expand All @@ -533,6 +536,7 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
* @param removeDisplay the function to display an element, needs to return the ItemStack to display
* @param onRemove what's consumed when an element is removed
* @param onReturn what's consumed when the player returns the editor
* @param onClose what's consumed when the player closes the editor
* @param <T> the type of the editor
* @return the editor
*/
Expand All @@ -547,7 +551,8 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
@NotNull Supplier<Collection<T>> viewCollection,
@NotNull Function<T, ItemStack> removeDisplay,
@NotNull Consumer<T> onRemove,
@Nullable Consumer<Player> onReturn) {
@Nullable Consumer<Player> onReturn,
@Nullable Consumer<Player> onClose) {
BlobInventory inventory = buildInventory(blobInventoryKey, player);
Uber<BlobEditor<T>> uber = Uber.fly();
uber.talk(BlobEditor.build(inventory, player.getUniqueId(),
Expand All @@ -564,15 +569,17 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
viewCollection,
removeDisplay,
onRemove,
onReturn);
onReturn,
onClose);
});
playerSelector.setItemsPerPage(playerSelector.getSlots(buttonRangeKey)
== null ? 1 : playerSelector.getSlots(buttonRangeKey).size());
playerSelector.selectElement(player,
onAdd,
null,
addDisplay,
viewCollection);
viewCollection,
onClose);
}, viewCollection.get(), onReturn));
BlobEditor<T> editor = uber.thanks();
editor.setItemsPerPage(editor.getSlots(buttonRangeKey) == null
Expand Down Expand Up @@ -618,6 +625,8 @@ public <T> BlobEditor<T> editor(@NotNull Player player,
addDisplay,
viewCollection,
removeDisplay,
onRemove, null);
onRemove,
null,
null);
}
}
22 changes: 19 additions & 3 deletions src/main/java/us/mytheria/bloblib/entities/BlobSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,33 @@ public void selectElement(Player player, Consumer<T> consumer, String timerMessa
loadPage(getPage(), true);
selectorManager.addSelectorListener(player, BlobSelectorListener.wise(player,
consumer, timerMessageKey,
this));
this, null));
}

public void selectElement(Player player, Consumer<T> consumer, String timerMessageKey, Function<T, ItemStack> function,
/**
* @deprecated use {@link #selectElement(Player, Consumer, String, Function, Supplier, Consumer)}
*/
@Deprecated
public void selectElement(Player player,
Consumer<T> consumer,
String timerMessageKey,
Function<T, ItemStack> function,
Supplier<Collection<T>> selectorList) {
selectElement(player, consumer, timerMessageKey, function, selectorList, null);
}

public void selectElement(Player player,
Consumer<T> consumer,
String timerMessageKey,
Function<T, ItemStack> function,
Supplier<Collection<T>> selectorList,
@Nullable Consumer<Player> onClose) {
loadCustomPage(getPage(), true, function);
setLoadFunction(function);
setCollectionSupplier(selectorList);
selectorManager.addSelectorListener(player, BlobSelectorListener.wise(player,
consumer, timerMessageKey,
this));
this, onClose));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class BlobSelectorListener<T> extends SelectorListener<T> {
* @param owner The owner of the SelectorListener
* @param inputRunnable The runnable to run when the SelectorListener receives input
* @param messages The messages to send to the player
* @deprecated Use {@link #wise(Player, Consumer, String, VariableSelector)} instead.
* @deprecated Use {@link #wise(Player, Consumer, String, VariableSelector, Consumer)} instead.
*/
@Deprecated
public static <T> BlobSelectorListener<T> build(Player owner, Runnable inputRunnable
Expand All @@ -45,7 +45,7 @@ public static <T> BlobSelectorListener<T> build(Player owner, Runnable inputRunn
* @param selector The selector to use
* @param <T> The type of the input
* @return The SelectorListener
* @deprecated Use {@link #wise(Player, Consumer, String, VariableSelector)} instead.
* @deprecated Use {@link #wise(Player, Consumer, String, VariableSelector, Consumer)} instead.
*/
@Deprecated
public static <T> BlobSelectorListener<T> smart(Player player, Consumer<T> consumer,
Expand Down Expand Up @@ -81,9 +81,33 @@ public static <T> BlobSelectorListener<T> smart(Player player, Consumer<T> consu
* @param <T> The type of the input
* @return The SelectorListener
*/
public static <T> BlobSelectorListener<T> wise(Player player, Consumer<T> consumer,
public static <T> BlobSelectorListener<T> wise(Player player,
Consumer<T> consumer,
@Nullable String timerMessageKey,
VariableSelector<T> selector) {
return wise(player, consumer, timerMessageKey, selector, null);
}

/**
* Will run a SelectorListener which will send messages to player every 10 ticks asynchronously.
* Will check if input is null. If so, will close player's inventory preventing
* dupe exploits and will also return, not running the consumer.
* Note that if not null, player's inventory won't be closed, so you need to make sure
* to close it if you need to, preferably in the consumer.
*
* @param player The player to send messages to
* @param consumer The consumer to run when the SelectorListener receives input
* @param timerMessageKey The key of the message to send to the player
* @param selector The selector to use
* @param onClose The consumer to run when the SelectorListener closes
* @param <T> The type of the input
* @return The SelectorListener
*/
public static <T> BlobSelectorListener<T> wise(Player player,
Consumer<T> consumer,
@Nullable String timerMessageKey,
VariableSelector<T> selector,
@Nullable Consumer<Player> onClose) {
BlobLib main = BlobLib.getInstance();
SelectorListenerManager selectorManager = main.getSelectorManager();
Optional<BlobMessage> timerMessage = Optional.empty();
Expand All @@ -95,6 +119,8 @@ public static <T> BlobSelectorListener<T> wise(Player player, Consumer<T> consum
selectorManager.removeSelectorListener(player);
if (input == null) {
player.closeInventory();
if (onClose != null)
onClose.accept(player);
return;
}
Bukkit.getScheduler().runTask(main, () -> {
Expand Down

0 comments on commit f557042

Please sign in to comment.