diff --git a/pom.xml b/pom.xml index 32635f4..973da95 100644 --- a/pom.xml +++ b/pom.xml @@ -102,7 +102,7 @@ me.anjoismysign.anjo anjo - 0.3.13 + 0.3.14 compile diff --git a/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java b/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java index fd3760b..a3a3bb2 100644 --- a/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java +++ b/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java @@ -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 BlobSelector customSelector(@NotNull String blobInventoryKey, @@ -389,12 +386,10 @@ public BlobSelector 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 BlobSelector customSelector(@NotNull String blobInventoryKey, @@ -412,7 +407,32 @@ public BlobSelector customSelector(@NotNull String blobInventoryKey, selectorList, onSelect, display, - onReturn, null); + onReturn, + null, + null); + } + + @Deprecated + @Nullable + public BlobSelector customSelector(@NotNull String blobInventoryKey, + @NotNull Player player, + @NotNull String buttonRangeKey, + @Nullable String dataType, + @NotNull Supplier> selectorList, + @NotNull Consumer onSelect, + @Nullable Function display, + @Nullable Consumer onReturn, + @Nullable Consumer onClose) { + return customSelector(blobInventoryKey, + player, + buttonRangeKey, + dataType, + selectorList, + onSelect, + display, + onReturn, + onClose, + null); } /** @@ -441,7 +461,8 @@ public BlobSelector customSelector(@NotNull String blobInventoryKey, @NotNull Consumer onSelect, @Nullable Function display, @Nullable Consumer onReturn, - @Nullable Consumer onClose) { + @Nullable Consumer onClose, + @Nullable String clickSound) { BlobInventory inventory = buildInventory(blobInventoryKey, player); BlobSelector selector = BlobSelector.build(inventory, player.getUniqueId(), dataType, selectorList.get(), onReturn); @@ -454,7 +475,8 @@ public BlobSelector customSelector(@NotNull String blobInventoryKey, null, display, selectorList::get, - onClose); + onClose, + clickSound); else selector.selectElement(player, onSelect, @@ -491,9 +513,6 @@ public BlobSelector 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 BlobEditor customEditor(@NotNull String blobInventoryKey, @@ -516,7 +535,6 @@ public BlobEditor customEditor(@NotNull String blobInventoryKey, viewCollection, removeDisplay, onRemove, - null, null); } @@ -566,6 +584,34 @@ public BlobEditor customEditor(@NotNull String blobInventoryKey, null); } + @Nullable + public BlobEditor customEditor(@NotNull String blobInventoryKey, + @NotNull Player player, + @NotNull String buttonRangeKey, + @NotNull String dataType, + @NotNull Supplier> addCollection, + @NotNull Consumer onAdd, + @Nullable Function addDisplay, + @NotNull Supplier> viewCollection, + @NotNull Function removeDisplay, + @NotNull Consumer onRemove, + @Nullable Consumer onReturn, + @Nullable Consumer 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. @@ -598,7 +644,8 @@ public BlobEditor customEditor(@NotNull String blobInventoryKey, @NotNull Function removeDisplay, @NotNull Consumer onRemove, @Nullable Consumer onReturn, - @Nullable Consumer onClose) { + @Nullable Consumer onClose, + @Nullable String clickSound) { BlobInventory inventory = buildInventory(blobInventoryKey, player); Uber> uber = Uber.fly(); uber.talk(BlobEditor.build(inventory, player.getUniqueId(), @@ -616,7 +663,8 @@ public BlobEditor customEditor(@NotNull String blobInventoryKey, removeDisplay, onRemove, onReturn, - onClose); + onClose, + clickSound); }); playerSelector.setItemsPerPage(playerSelector.getSlots(buttonRangeKey) == null ? 1 : playerSelector.getSlots(buttonRangeKey).size()); @@ -625,7 +673,8 @@ public BlobEditor customEditor(@NotNull String blobInventoryKey, null, addDisplay, viewCollection, - onClose); + onClose, + clickSound); }, viewCollection.get(), onReturn)); BlobEditor editor = uber.thanks(); editor.setItemsPerPage(editor.getSlots(buttonRangeKey) == null diff --git a/src/main/java/us/mytheria/bloblib/entities/BlobSelector.java b/src/main/java/us/mytheria/bloblib/entities/BlobSelector.java index 23bf7a3..44e1b0d 100644 --- a/src/main/java/us/mytheria/bloblib/entities/BlobSelector.java +++ b/src/main/java/us/mytheria/bloblib/entities/BlobSelector.java @@ -180,7 +180,7 @@ public void selectElement(Player player, Consumer 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, @@ -188,7 +188,7 @@ public void selectElement(Player player, String timerMessageKey, Function function, Supplier> selectorList) { - selectElement(player, consumer, timerMessageKey, function, selectorList, null); + selectElement(player, consumer, timerMessageKey, function, selectorList, null, null); } public void selectElement(Player player, @@ -196,13 +196,17 @@ public void selectElement(Player player, String timerMessageKey, Function function, Supplier> selectorList, - @Nullable Consumer onClose) { + @Nullable Consumer onClose, + @Nullable String clickSound) { loadCustomPage(getPage(), true, function); setLoadFunction(function); setCollectionSupplier(selectorList); - selectorManager.addSelectorListener(player, BlobSelectorListener.wise(player, + BlobSelectorListener listener = BlobSelectorListener.wise(player, consumer, timerMessageKey, - this, onClose)); + this, onClose); + if (clickSound != null) + listener.setClickSoundReference(clickSound); + selectorManager.addSelectorListener(player, listener); } /** diff --git a/src/main/java/us/mytheria/bloblib/entities/BlobSerializableManager.java b/src/main/java/us/mytheria/bloblib/entities/BlobSerializableManager.java index f047818..4af9f89 100644 --- a/src/main/java/us/mytheria/bloblib/entities/BlobSerializableManager.java +++ b/src/main/java/us/mytheria/bloblib/entities/BlobSerializableManager.java @@ -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; @@ -50,14 +49,16 @@ protected BlobSerializableManager(ManagerDirector managerDirector, Function joinEvent, @Nullable Function 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; diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventoryClickEvent.java b/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventoryClickEvent.java new file mode 100644 index 0000000..173b4f1 --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventoryClickEvent.java @@ -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()); + } +} diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryDataRegistry.java b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryDataRegistry.java index 3387db9..b867643 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryDataRegistry.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryDataRegistry.java @@ -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; @@ -21,10 +22,10 @@ public class InventoryDataRegistry { @NotNull private final String defaultLocale, key; private final Map> carriers; - private final Map> singleClickEvents; + private final Map> singleClickEvents; private final Map>> closeEvents; private final Map> clickEvents; - private final Map>> playerInventoryClickEvents; + private final Map>> playerInventoryClickEvents; /** * Will instantiate a new InventoryDataRegistry with the specified default locale. @@ -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 event) { + public void onClick(String button, Consumer event) { this.singleClickEvents.put(button, event); } @@ -123,10 +124,14 @@ public void onClick(String key, BiConsumer event) { * @param event the click event */ public void processSingleClickEvent(String button, InventoryClickEvent event) { - Consumer clickEvent = this.singleClickEvents.get(button); + Consumer 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); } /** @@ -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> event) { + public void onPlayerInventoryClick(String key, BiConsumer> event) { this.playerInventoryClickEvents.put(key, event); } @@ -184,10 +189,15 @@ public void onPlayerInventoryClick(String key, BiConsumer 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); } } diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/VariableSelector.java b/src/main/java/us/mytheria/bloblib/entities/inventory/VariableSelector.java index 287c09d..3b57092 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/VariableSelector.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/VariableSelector.java @@ -7,7 +7,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import us.mytheria.bloblib.api.BlobLibInventoryAPI; -import us.mytheria.bloblib.api.BlobLibSoundAPI; import us.mytheria.bloblib.entities.VariableFiller; import us.mytheria.bloblib.entities.VariableValue; @@ -248,6 +247,7 @@ public void clearValues() { * @param slot the slot that was clicked * @return the value that was stored in the slot */ + @Nullable public T getValue(int slot) { return values.get(slot); } @@ -370,8 +370,6 @@ public void setPage(int page) { */ public void nextPage() { setPage(page + 1); - Player player = getPlayer(); - BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click").handle(player); } /** @@ -379,8 +377,6 @@ public void nextPage() { */ public void previousPage() { setPage(page - 1); - Player player = getPlayer(); - BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click").handle(player); } /** @@ -389,7 +385,6 @@ public void previousPage() { public void processReturn() { Player player = getPlayer(); returnAction.accept(player); - BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click").handle(player); } /** diff --git a/src/main/java/us/mytheria/bloblib/entities/listeners/EditorListener.java b/src/main/java/us/mytheria/bloblib/entities/listeners/EditorListener.java index c510695..99c6bcb 100644 --- a/src/main/java/us/mytheria/bloblib/entities/listeners/EditorListener.java +++ b/src/main/java/us/mytheria/bloblib/entities/listeners/EditorListener.java @@ -1,5 +1,6 @@ package us.mytheria.bloblib.entities.listeners; +import org.jetbrains.annotations.Nullable; import us.mytheria.bloblib.BlobLib; import us.mytheria.bloblib.entities.BlobEditor; import us.mytheria.bloblib.entities.inventory.VariableSelector; @@ -45,11 +46,24 @@ public void setInput(T input) { inputConsumer.accept(this); } + /** + * Sets the input from the editor slot + * + * @param selector the selector + * @param slot the slot + * @return true if successful, false otherwise + */ @SuppressWarnings("unchecked") - public void setInputFromSlot(VariableSelector selector, int slot) { + public boolean setInputFromSlot(VariableSelector selector, int slot) { if (!getEditor().equals(this.editor)) - return; - setInput((T) selector.getValue(slot)); + return false; + @Nullable T input = (T) selector.getValue(slot); + if (input == null) { + setInput(null); + return false; + } + setInput(input); + return true; } public BlobEditor getEditor() { diff --git a/src/main/java/us/mytheria/bloblib/entities/listeners/SelectorListener.java b/src/main/java/us/mytheria/bloblib/entities/listeners/SelectorListener.java index cd52ccb..d6216bc 100644 --- a/src/main/java/us/mytheria/bloblib/entities/listeners/SelectorListener.java +++ b/src/main/java/us/mytheria/bloblib/entities/listeners/SelectorListener.java @@ -1,13 +1,20 @@ package us.mytheria.bloblib.entities.listeners; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import us.mytheria.bloblib.BlobLib; +import us.mytheria.bloblib.api.BlobLibSoundAPI; import us.mytheria.bloblib.entities.inventory.VariableSelector; +import us.mytheria.bloblib.entities.message.BlobSound; +import java.util.Objects; import java.util.function.Consumer; public class SelectorListener extends InputListener { private T input; private final VariableSelector selector; + @NotNull + private String clickSoundReference; public SelectorListener(String owner, Runnable inputRunnable, VariableSelector selector) { super(owner, inputListener -> { @@ -16,6 +23,7 @@ public SelectorListener(String owner, Runnable inputRunnable, VariableSelector> inputConsume this.selector = selector; register(); selector.open(); + clickSoundReference = "Builder.Button-Click"; } private void register() { @@ -44,14 +53,44 @@ public void setInput(T input) { inputConsumer.accept(this); } + /** + * Sets the input from the specified slot in the {@link VariableSelector}. + *

+ * This method will only succeed if the provided selector matches the one associated with this listener. + * If no value is available at the specified slot, or if setting the new input fails for any reason, + * the method returns false. + * + * @param selector the selector from which to retrieve the input + * @param slot the index of the slot containing the desired input + * @return true if the input was successfully set, or false otherwise + */ @SuppressWarnings("unchecked") - public void setInputFromSlot(VariableSelector selector, int slot) { + public boolean setInputFromSlot(VariableSelector selector, int slot) { if (!getSelector().equals(this.selector)) - return; - setInput((T) selector.getValue(slot)); + return false; + @Nullable T input = (T) selector.getValue(slot); + if (input == null) + return false; + setInput(input); + return true; } public VariableSelector getSelector() { return selector; } + + @NotNull + public String getClickSoundReference() { + return clickSoundReference; + } + + public void setClickSoundReference(@NotNull String clickSoundReference) { + Objects.requireNonNull(clickSoundReference, "'blobSoundReference' cannot be null"); + this.clickSoundReference = clickSoundReference; + } + + @NotNull + public BlobSound getClickSound() { + return Objects.requireNonNull(BlobLibSoundAPI.getInstance().getSound(clickSoundReference), "Not a valid BlobSound: " + clickSoundReference); + } } \ No newline at end of file diff --git a/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java b/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java index 95e4de1..aec3417 100644 --- a/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java @@ -43,32 +43,30 @@ public void onEditorClick(InventoryClickEvent event) { event.setCancelled(true); int slot = event.getRawSlot(); BlobSound clickSound = BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click"); - if (slot > blobEditor.valuesSize() - 1) { - if (blobEditor.isNextPageButton(slot)) { - blobEditor.nextPage(); - return; - } - if (blobEditor.isPreviousPageButton(slot)) { - blobEditor.previousPage(); - return; - } - if (blobEditor.isAddElementButton(slot)) { - clickSound.handle(player); - blobEditor.addElement(player); - return; - } - if (blobEditor.isRemoveElementButton(slot)) { - clickSound.handle(player); - blobEditor.removeElement(player); - return; - } - if (blobEditor.isReturnButton(slot)) { - blobEditor.processReturn(); - return; - } + if (blobEditor.isNextPageButton(slot)) { + blobEditor.nextPage(); return; } - listener.setInputFromSlot(blobEditor, event.getRawSlot()); + if (blobEditor.isPreviousPageButton(slot)) { + blobEditor.previousPage(); + return; + } + if (blobEditor.isAddElementButton(slot)) { + clickSound.handle(player); + blobEditor.addElement(player); + return; + } + if (blobEditor.isRemoveElementButton(slot)) { + clickSound.handle(player); + blobEditor.removeElement(player); + return; + } + if (blobEditor.isReturnButton(slot)) { + blobEditor.processReturn(); + return; + } + if (!listener.setInputFromSlot(blobEditor, event.getRawSlot())) + return; clickSound.handle(player); } @@ -79,27 +77,29 @@ public void onSelectorClick(InventoryClickEvent e) { if (variableSelector == null) return; SelectorListener listener = main.getSelectorManager().getSelectorListener(player); + BlobSound clickSound = BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click"); if (listener == null) return; e.setCancelled(true); int slot = e.getRawSlot(); - if (slot > variableSelector.valuesSize() - 1) { - if (variableSelector.isNextPageButton(slot)) { - variableSelector.nextPage(); - return; - } - if (variableSelector.isPreviousPageButton(slot)) { - variableSelector.previousPage(); - return; - } - if (variableSelector.isReturnButton(slot)) { - variableSelector.processReturn(); - return; - } + if (variableSelector.isNextPageButton(slot)) { + variableSelector.nextPage(); + clickSound.handle(player); + return; + } + if (variableSelector.isPreviousPageButton(slot)) { + variableSelector.previousPage(); + clickSound.handle(player); return; } - listener.setInputFromSlot(variableSelector, slot); - BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click").handle(player); + if (variableSelector.isReturnButton(slot)) { + variableSelector.processReturn(); + clickSound.handle(player); + return; + } + if (!listener.setInputFromSlot(variableSelector, slot)) + return; + listener.getClickSound().handle(player); } @EventHandler