Skip to content

Commit

Permalink
Adds BlobInventoryClickEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Jul 30, 2024
1 parent 4b97634 commit e9995a4
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 86 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<dependency>
<groupId>me.anjoismysign.anjo</groupId>
<artifactId>anjo</artifactId>
<version>0.3.13</version>
<version>0.3.14</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
83 changes: 66 additions & 17 deletions src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,6 @@ public MetaBlobInventory buildMetaInventory(@NotNull String key,
return buildMetaInventory(key, player.getLocale());
}

/**
* @deprecated Use {@link #customSelector(String, Player, String, String, Supplier, Consumer, Function, Consumer, Consumer)} instead.
*/
@Deprecated
@Nullable
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
Expand All @@ -389,12 +386,10 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
dataType,
selectorList,
onSelect,
display, null);
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,
Expand All @@ -412,7 +407,32 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
selectorList,
onSelect,
display,
onReturn, null);
onReturn,
null,
null);
}

@Deprecated
@Nullable
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@Nullable String dataType,
@NotNull Supplier<List<T>> selectorList,
@NotNull Consumer<T> onSelect,
@Nullable Function<T, ItemStack> display,
@Nullable Consumer<Player> onReturn,
@Nullable Consumer<Player> onClose) {
return customSelector(blobInventoryKey,
player,
buttonRangeKey,
dataType,
selectorList,
onSelect,
display,
onReturn,
onClose,
null);
}

/**
Expand Down Expand Up @@ -441,7 +461,8 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Consumer<T> onSelect,
@Nullable Function<T, ItemStack> display,
@Nullable Consumer<Player> onReturn,
@Nullable Consumer<Player> onClose) {
@Nullable Consumer<Player> onClose,
@Nullable String clickSound) {
BlobInventory inventory = buildInventory(blobInventoryKey, player);
BlobSelector<T> selector = BlobSelector.build(inventory, player.getUniqueId(),
dataType, selectorList.get(), onReturn);
Expand All @@ -454,7 +475,8 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
null,
display,
selectorList::get,
onClose);
onClose,
clickSound);
else
selector.selectElement(player,
onSelect,
Expand Down Expand Up @@ -491,9 +513,6 @@ public <T> BlobSelector<T> selector(@NotNull Player player,
display, null);
}

/**
* @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,
Expand All @@ -516,7 +535,6 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
viewCollection,
removeDisplay,
onRemove,
null,
null);
}

Expand Down Expand Up @@ -566,6 +584,34 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
null);
}

@Nullable
public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@NotNull String dataType,
@NotNull Supplier<Collection<T>> addCollection,
@NotNull Consumer<T> onAdd,
@Nullable Function<T, ItemStack> addDisplay,
@NotNull Supplier<Collection<T>> viewCollection,
@NotNull Function<T, ItemStack> removeDisplay,
@NotNull Consumer<T> onRemove,
@Nullable Consumer<Player> onReturn,
@Nullable Consumer<Player> onClose) {
return customEditor(blobInventoryKey,
player,
buttonRangeKey,
dataType,
addCollection,
onAdd,
addDisplay,
viewCollection,
removeDisplay,
onRemove,
onReturn,
onClose,
null);
}

/**
* Will allow player to edit a collection of elements.
* The editor will be placed in the inventory at the specified buttonRangeKey.
Expand Down Expand Up @@ -598,7 +644,8 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
@NotNull Function<T, ItemStack> removeDisplay,
@NotNull Consumer<T> onRemove,
@Nullable Consumer<Player> onReturn,
@Nullable Consumer<Player> onClose) {
@Nullable Consumer<Player> onClose,
@Nullable String clickSound) {
BlobInventory inventory = buildInventory(blobInventoryKey, player);
Uber<BlobEditor<T>> uber = Uber.fly();
uber.talk(BlobEditor.build(inventory, player.getUniqueId(),
Expand All @@ -616,7 +663,8 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
removeDisplay,
onRemove,
onReturn,
onClose);
onClose,
clickSound);
});
playerSelector.setItemsPerPage(playerSelector.getSlots(buttonRangeKey)
== null ? 1 : playerSelector.getSlots(buttonRangeKey).size());
Expand All @@ -625,7 +673,8 @@ public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
null,
addDisplay,
viewCollection,
onClose);
onClose,
clickSound);
}, viewCollection.get(), onReturn));
BlobEditor<T> editor = uber.thanks();
editor.setItemsPerPage(editor.getSlots(buttonRangeKey) == null
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/us/mytheria/bloblib/entities/BlobSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,33 @@ public void selectElement(Player player, Consumer<T> consumer, String timerMessa
}

/**
* @deprecated use {@link #selectElement(Player, Consumer, String, Function, Supplier, Consumer)}
* @deprecated use {@link #selectElement(Player, Consumer, String, Function, Supplier, Consumer, String)}
*/
@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);
selectElement(player, consumer, timerMessageKey, function, selectorList, null, null);
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.managers.BlobPlugin;
import us.mytheria.bloblib.managers.Manager;
Expand Down Expand Up @@ -50,14 +49,16 @@ protected BlobSerializableManager(ManagerDirector managerDirector, Function<Blob
String crudableName, boolean logActivity,
@Nullable Function<T, Event> joinEvent,
@Nullable Function<T, Event> quitEvent,
@NotNull EventPriority joinPriority,
@NotNull EventPriority quitPriority) {
@Nullable EventPriority joinPriority,
@Nullable EventPriority quitPriority) {
super(managerDirector);
plugin = managerDirector.getPlugin();
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(this, plugin);
registerJoinListener(pluginManager, joinPriority);
registerQuitListener(pluginManager, quitPriority);
if (joinPriority != null)
registerJoinListener(pluginManager, joinPriority);
if (quitPriority != null)
registerQuitListener(pluginManager, quitPriority);
serializables = new HashMap<>();
autoSave = new HashMap<>();
this.generator = generator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package us.mytheria.bloblib.entities.inventory;

import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryView;
import org.jetbrains.annotations.NotNull;
import us.mytheria.bloblib.api.BlobLibSoundAPI;
import us.mytheria.bloblib.entities.message.BlobSound;

import java.util.Objects;

public class BlobInventoryClickEvent extends InventoryClickEvent {
@NotNull
private String clickSound;
private boolean playClickSound;

public static BlobInventoryClickEvent of(@NotNull InventoryClickEvent event) {
Objects.requireNonNull(event, "'event' cannot be null");
return new BlobInventoryClickEvent(event.getView(), event.getSlotType(), event.getRawSlot(), event.getClick(), event.getAction());
}

private BlobInventoryClickEvent(@NotNull InventoryView view, @NotNull InventoryType.SlotType type, int slot, @NotNull ClickType click, @NotNull InventoryAction action) {
super(view, type, slot, click, action);
this.clickSound = "Builder.Button-Click";
this.playClickSound = true;
}

public boolean playClickSound() {
return playClickSound;
}

public void setPlayClickSound(boolean playClickSound) {
this.playClickSound = playClickSound;
}

@NotNull
public String getClickSound() {
return clickSound;
}

public void setClickSound(@NotNull String clickSound) {
Objects.requireNonNull(clickSound, "'clickSound' cannot be null");
this.clickSound = clickSound;
}

@NotNull
public BlobSound getClickBlobSound() {
return Objects.requireNonNull(BlobLibSoundAPI.getInstance().getSound(getClickSound()), "Not a valid BlobSound: " + getClickSound());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.mytheria.bloblib.entities.inventory;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.jetbrains.annotations.NotNull;
Expand All @@ -21,10 +22,10 @@ public class InventoryDataRegistry<T extends InventoryButton> {
@NotNull
private final String defaultLocale, key;
private final Map<String, InventoryBuilderCarrier<T>> carriers;
private final Map<String, Consumer<InventoryClickEvent>> singleClickEvents;
private final Map<String, Consumer<BlobInventoryClickEvent>> singleClickEvents;
private final Map<String, BiConsumer<InventoryCloseEvent, SharableInventory<?>>> closeEvents;
private final Map<String, BiConsumer<InventoryClickEvent, T>> clickEvents;
private final Map<String, BiConsumer<InventoryClickEvent, SharableInventory<?>>> playerInventoryClickEvents;
private final Map<String, BiConsumer<BlobInventoryClickEvent, SharableInventory<?>>> playerInventoryClickEvents;

/**
* Will instantiate a new InventoryDataRegistry with the specified default locale.
Expand Down Expand Up @@ -100,7 +101,7 @@ public String getKey() {
* @param button the button to add the click event for
* @param event the click event
*/
public void onClick(String button, Consumer<InventoryClickEvent> event) {
public void onClick(String button, Consumer<BlobInventoryClickEvent> event) {
this.singleClickEvents.put(button, event);
}

Expand All @@ -123,10 +124,14 @@ public void onClick(String key, BiConsumer<InventoryClickEvent, T> event) {
* @param event the click event
*/
public void processSingleClickEvent(String button, InventoryClickEvent event) {
Consumer<InventoryClickEvent> clickEvent = this.singleClickEvents.get(button);
Consumer<BlobInventoryClickEvent> clickEvent = this.singleClickEvents.get(button);
if (clickEvent == null)
return;
clickEvent.accept(event);
BlobInventoryClickEvent blobInventoryClickEvent = BlobInventoryClickEvent.of(event);
clickEvent.accept(blobInventoryClickEvent);
if (blobInventoryClickEvent.playClickSound() &&
blobInventoryClickEvent.getWhoClicked() instanceof Player player)
blobInventoryClickEvent.getClickBlobSound().handle(player);
}

/**
Expand Down Expand Up @@ -173,7 +178,7 @@ public void processCloseEvents(InventoryCloseEvent event, SharableInventory<?> i
* @param key the key to add the click event for
* @param event the click event
*/
public void onPlayerInventoryClick(String key, BiConsumer<InventoryClickEvent, SharableInventory<?>> event) {
public void onPlayerInventoryClick(String key, BiConsumer<BlobInventoryClickEvent, SharableInventory<?>> event) {
this.playerInventoryClickEvents.put(key, event);
}

Expand All @@ -184,10 +189,15 @@ public void onPlayerInventoryClick(String key, BiConsumer<InventoryClickEvent, S
* @param inventory the inventory
*/
public void processPlayerInventoryClickEvent(InventoryClickEvent event, SharableInventory<?> inventory) {
BlobInventoryClickEvent blobInventoryClickEvent = BlobInventoryClickEvent.of(event);
blobInventoryClickEvent.setPlayClickSound(false);
playerInventoryClickEvents.values().forEach(playerInventoryClickEvent -> {
if (playerInventoryClickEvent == null)
return;
playerInventoryClickEvent.accept(event, inventory);
playerInventoryClickEvent.accept(blobInventoryClickEvent, inventory);
});
if (blobInventoryClickEvent.playClickSound() &&
blobInventoryClickEvent.getWhoClicked() instanceof Player player)
blobInventoryClickEvent.getClickBlobSound().handle(player);
}
}
Loading

0 comments on commit e9995a4

Please sign in to comment.