Skip to content

Commit

Permalink
Should allow adding custom buttons to VariableSelector and BlobEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Aug 1, 2024
1 parent b6efe17 commit edb0ac7
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 56 deletions.
34 changes: 0 additions & 34 deletions src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,6 @@ public <T> BlobSelector<T> 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 <T> the type of the selector
* @return the selector
*/
@Nullable
public <T> BlobSelector<T> selector(@NotNull Player player,
@NotNull String dataType,
Expand Down Expand Up @@ -538,26 +524,6 @@ public <T> BlobEditor<T> 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 <T> the type of the editor
* @return the editor
*/
@Nullable
public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
@NotNull Player player,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
package us.mytheria.bloblib.entities.inventory;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class BlobInventory extends SharableInventory<InventoryButton> {

public static BlobInventory fromInventoryBuilderCarrier(InventoryBuilderCarrier<InventoryButton> 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<InventoryButton> buttonManager,
@Nullable String reference,
@Nullable String locale) {
super(title, size, buttonManager, reference, locale);
}

public BlobInventory(@NotNull String title,
int size,
@NotNull ButtonManager<InventoryButton> buttonManager) {
super(title, size, buttonManager);
}

@Override
@NotNull
public BlobInventory copy() {
return new BlobInventory(getTitle(), getSize(), getButtonManager().copy());
return new BlobInventory(
getTitle(),
getSize(),
getButtonManager().copy(),
getReference(),
getLocale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public abstract class InventoryBuilder<T extends InventoryButton> {
private String title;
private int size;
private ButtonManager<T> buttonManager;
@Nullable
protected String reference;
@Nullable
protected String locale;

public String getTitle() {
return title;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.io.File;
import java.util.Objects;

public record InventoryBuilderCarrier<T extends InventoryButton>(@Nullable String title,
public record InventoryBuilderCarrier<T extends InventoryButton>(@NotNull String title,
int size,
@NotNull ButtonManager<T> buttonManager,
@Nullable String type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

import me.anjoismysign.anjo.entities.Result;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class MetaBlobInventory extends SharableInventory<MetaInventoryButton> {
private final String type;

public static MetaBlobInventory fromInventoryBuilderCarrier(InventoryBuilderCarrier<MetaInventoryButton> 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<MetaInventoryButton> 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,
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,26 @@ public class SharableInventory<T extends InventoryButton> 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<T> buttonManager) {
this(title, size, buttonManager, null, null);
}

protected SharableInventory(@NotNull String title,
int size,
@NotNull ButtonManager<T> buttonManager,
@Nullable String reference,
@Nullable String locale) {
this.setTitle(Objects.requireNonNull(title,
"'title' cannot be null!"));
this.setSize(size);
this.setButtonManager(Objects.requireNonNull(buttonManager,
"'buttonManager' cannot be null!"));
this.buildInventory();
this.loadDefaultButtons();
this.reference = reference;
this.locale = locale;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public VariableSelector(BlobInventory blobInventory,
@Nullable Function<T, ItemStack> loadFunction,
@Nullable Supplier<Collection<T>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit edb0ac7

Please sign in to comment.