From edb0ac7bcf6c1533d8a4e1bde6c6f0cbddd78003 Mon Sep 17 00:00:00 2001 From: lbenav8095 Date: Thu, 1 Aug 2024 13:16:40 -0400 Subject: [PATCH] Should allow adding custom buttons to VariableSelector and BlobEditor --- .../bloblib/api/BlobLibInventoryAPI.java | 34 ------------- .../entities/inventory/BlobInventory.java | 27 ++++++++-- .../entities/inventory/InventoryBuilder.java | 14 ++++++ .../inventory/InventoryBuilderCarrier.java | 2 +- .../entities/inventory/MetaBlobInventory.java | 29 +++++++++-- .../entities/inventory/SharableInventory.java | 13 ++++- .../entities/inventory/VariableSelector.java | 2 +- .../managers/VariableSelectorManager.java | 50 +++++++++++++++---- 8 files changed, 115 insertions(+), 56 deletions(-) diff --git a/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java b/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java index a3a3bb22..c77c7b2f 100644 --- a/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java +++ b/src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java @@ -484,20 +484,6 @@ public BlobSelector customSelector(@NotNull String blobInventoryKey, return selector; } - /** - * 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. - * Will use default VariableSelector inventory. - * - * @param player the player - * @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 the type of the selector - * @return the selector - */ @Nullable public BlobSelector selector(@NotNull Player player, @NotNull String dataType, @@ -538,26 +524,6 @@ public BlobEditor customEditor(@NotNull String blobInventoryKey, null); } - /** - * 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. - * Does nothing when the player closes the editor. - * - * @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 onReturn what's consumed when the player returns the editor - * @param the type of the editor - * @return the editor - */ @Nullable public BlobEditor customEditor(@NotNull String blobInventoryKey, @NotNull Player player, diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventory.java b/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventory.java index 4e8efaa5..2beba798 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventory.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/BlobInventory.java @@ -1,6 +1,7 @@ package us.mytheria.bloblib.entities.inventory; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -8,11 +9,24 @@ public class BlobInventory extends SharableInventory { public static BlobInventory fromInventoryBuilderCarrier(InventoryBuilderCarrier carrier) { String title = Objects.requireNonNull(carrier.title(), "title cannot be null"); - return new BlobInventory(title, carrier.size(), - carrier.buttonManager().copy()); + return new BlobInventory( + title, + carrier.size(), + carrier.buttonManager().copy(), + carrier.reference(), + carrier.locale()); } - public BlobInventory(@NotNull String title, int size, + public BlobInventory(@NotNull String title, + int size, + @NotNull ButtonManager buttonManager, + @Nullable String reference, + @Nullable String locale) { + super(title, size, buttonManager, reference, locale); + } + + public BlobInventory(@NotNull String title, + int size, @NotNull ButtonManager buttonManager) { super(title, size, buttonManager); } @@ -20,6 +34,11 @@ public BlobInventory(@NotNull String title, int size, @Override @NotNull public BlobInventory copy() { - return new BlobInventory(getTitle(), getSize(), getButtonManager().copy()); + return new BlobInventory( + getTitle(), + getSize(), + getButtonManager().copy(), + getReference(), + getLocale()); } } diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilder.java b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilder.java index 9e232353..9e3bdc61 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilder.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilder.java @@ -12,6 +12,10 @@ public abstract class InventoryBuilder { private String title; private int size; private ButtonManager buttonManager; + @Nullable + protected String reference; + @Nullable + protected String locale; public String getTitle() { return title; @@ -82,4 +86,14 @@ public boolean handleAll(String key, Player player) { } return button.handleAll(player); } + + @Nullable + public String getLocale() { + return locale; + } + + @Nullable + public String getReference() { + return reference; + } } diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java index 9fb39eb9..6b9c2c79 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/InventoryBuilderCarrier.java @@ -11,7 +11,7 @@ import java.io.File; import java.util.Objects; -public record InventoryBuilderCarrier(@Nullable String title, +public record InventoryBuilderCarrier(@NotNull String title, int size, @NotNull ButtonManager buttonManager, @Nullable String type, diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/MetaBlobInventory.java b/src/main/java/us/mytheria/bloblib/entities/inventory/MetaBlobInventory.java index ed018ac5..b38f94e2 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/MetaBlobInventory.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/MetaBlobInventory.java @@ -2,6 +2,7 @@ import me.anjoismysign.anjo.entities.Result; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Objects; @@ -9,8 +10,23 @@ public class MetaBlobInventory extends SharableInventory { private final String type; public static MetaBlobInventory fromInventoryBuilderCarrier(InventoryBuilderCarrier carrier) { - return new MetaBlobInventory(carrier.title(), carrier.size(), - carrier.buttonManager().copy(), carrier.type()); + return new MetaBlobInventory( + carrier.title(), + carrier.size(), + carrier.buttonManager().copy(), + carrier.type(), + carrier.reference(), + carrier.locale()); + } + + public MetaBlobInventory(@NotNull String title, + int size, + @NotNull ButtonManager buttonManager, + @Nullable String type, + @Nullable String reference, + @Nullable String locale) { + super(title, size, buttonManager, reference, locale); + this.type = Objects.requireNonNull(type, "'type' cannot be null!"); } public MetaBlobInventory(@NotNull String title, int size, @@ -23,8 +39,13 @@ public MetaBlobInventory(@NotNull String title, int size, @Override @NotNull public MetaBlobInventory copy() { - return new MetaBlobInventory(getTitle(), getSize(), - getButtonManager().copy(), getType()); + return new MetaBlobInventory( + getTitle(), + getSize(), + getButtonManager().copy(), + getType(), + getReference(), + getLocale()); } /** diff --git a/src/main/java/us/mytheria/bloblib/entities/inventory/SharableInventory.java b/src/main/java/us/mytheria/bloblib/entities/inventory/SharableInventory.java index 5f412b58..2c1f66a6 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/SharableInventory.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/SharableInventory.java @@ -32,8 +32,17 @@ public class SharableInventory extends InventoryBuild * @param size The size of the inventory. * @param buttonManager The ButtonManager that will be used to manage the buttons. */ - protected SharableInventory(@NotNull String title, int size, + protected SharableInventory(@NotNull String title, + int size, @NotNull ButtonManager buttonManager) { + this(title, size, buttonManager, null, null); + } + + protected SharableInventory(@NotNull String title, + int size, + @NotNull ButtonManager buttonManager, + @Nullable String reference, + @Nullable String locale) { this.setTitle(Objects.requireNonNull(title, "'title' cannot be null!")); this.setSize(size); @@ -41,6 +50,8 @@ protected SharableInventory(@NotNull String title, int size, "'buttonManager' cannot be null!")); this.buildInventory(); this.loadDefaultButtons(); + this.reference = reference; + this.locale = locale; } /** 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 3b570920..4b61e425 100644 --- a/src/main/java/us/mytheria/bloblib/entities/inventory/VariableSelector.java +++ b/src/main/java/us/mytheria/bloblib/entities/inventory/VariableSelector.java @@ -94,7 +94,7 @@ public VariableSelector(BlobInventory blobInventory, @Nullable Function loadFunction, @Nullable Supplier> collectionSupplier, @Nullable String buttonRangeKey) { - super(blobInventory.getTitle(), blobInventory.getSize(), blobInventory.getButtonManager()); + super(blobInventory.getTitle(), blobInventory.getSize(), blobInventory.getButtonManager(), blobInventory.getReference(), blobInventory.getLocale()); this.returnAction = returnAction == null ? HumanEntity::closeInventory : returnAction; this.loadFunction = loadFunction; this.collectionSupplier = collectionSupplier; diff --git a/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java b/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java index aec34171..884d76b9 100644 --- a/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/VariableSelectorManager.java @@ -8,14 +8,18 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerQuitEvent; import us.mytheria.bloblib.BlobLib; +import us.mytheria.bloblib.api.BlobLibInventoryAPI; import us.mytheria.bloblib.api.BlobLibSoundAPI; import us.mytheria.bloblib.entities.BlobEditor; +import us.mytheria.bloblib.entities.inventory.ClickEventProcessor; +import us.mytheria.bloblib.entities.inventory.InventoryDataRegistry; import us.mytheria.bloblib.entities.inventory.VariableSelector; import us.mytheria.bloblib.entities.listeners.EditorListener; import us.mytheria.bloblib.entities.listeners.SelectorListener; import us.mytheria.bloblib.entities.message.BlobSound; import java.util.HashMap; +import java.util.Objects; public class VariableSelectorManager implements Listener { private final BlobLib main; @@ -42,6 +46,8 @@ public void onEditorClick(InventoryClickEvent event) { } event.setCancelled(true); int slot = event.getRawSlot(); + String reference = Objects.requireNonNull(blobEditor.getReference(), "'blobEditor' was dynamically constructed by BlobLib without providing a reference"); + InventoryDataRegistry registry = BlobLibInventoryAPI.getInstance().getInventoryDataRegistry(reference); BlobSound clickSound = BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click"); if (blobEditor.isNextPageButton(slot)) { blobEditor.nextPage(); @@ -65,23 +71,35 @@ public void onEditorClick(InventoryClickEvent event) { blobEditor.processReturn(); return; } - if (!listener.setInputFromSlot(blobEditor, event.getRawSlot())) + if (!listener.setInputFromSlot(blobEditor, event.getRawSlot())) { + blobEditor.getKeys().forEach(key -> { + var button = blobEditor.getButton(key); + if (!button.containsSlot(slot)) + return; + if (!button.handleAll((Player) event.getWhoClicked())) + return; + registry.processSingleClickEvent(key, event); + button.accept(ClickEventProcessor.of(event, registry)); + }); return; + } clickSound.handle(player); } @EventHandler - public void onSelectorClick(InventoryClickEvent e) { - Player player = (Player) e.getWhoClicked(); + public void onSelectorClick(InventoryClickEvent event) { + Player player = (Player) event.getWhoClicked(); VariableSelector variableSelector = variableSelectors.get(player.getName()); 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(); + event.setCancelled(true); + String reference = Objects.requireNonNull(variableSelector.getReference(), "'variableSelector' was dynamically constructed by BlobLib without providing a reference"); + InventoryDataRegistry registry = BlobLibInventoryAPI.getInstance().getInventoryDataRegistry(reference); + int slot = event.getRawSlot(); + BlobSound clickSound = BlobLibSoundAPI.getInstance().getSound("Builder.Button-Click"); if (variableSelector.isNextPageButton(slot)) { variableSelector.nextPage(); clickSound.handle(player); @@ -97,14 +115,24 @@ public void onSelectorClick(InventoryClickEvent e) { clickSound.handle(player); return; } - if (!listener.setInputFromSlot(variableSelector, slot)) + if (!listener.setInputFromSlot(variableSelector, slot)) { + variableSelector.getKeys().forEach(key -> { + var button = variableSelector.getButton(key); + if (!button.containsSlot(slot)) + return; + if (!button.handleAll((Player) event.getWhoClicked())) + return; + registry.processSingleClickEvent(key, event); + button.accept(ClickEventProcessor.of(event, registry)); + }); return; + } listener.getClickSound().handle(player); } @EventHandler - public void onSelectorClose(InventoryCloseEvent e) { - Player player = (Player) e.getPlayer(); + public void onSelectorClose(InventoryCloseEvent event) { + Player player = (Player) event.getPlayer(); if (!variableSelectors.containsKey(player.getName())) return; SelectorListener listener = main.getSelectorManager().getSelectorListener(player); @@ -114,8 +142,8 @@ public void onSelectorClose(InventoryCloseEvent e) { } @EventHandler - public void onEditorClose(InventoryCloseEvent e) { - Player player = (Player) e.getPlayer(); + public void onEditorClose(InventoryCloseEvent event) { + Player player = (Player) event.getPlayer(); if (!blobEditors.containsKey(player.getName())) return; EditorListener listener = main.getSelectorManager().getEditorListener(player);