Skip to content

Commit

Permalink
VariableSelector uses buttonRangeKey for displaying VariableValue
Browse files Browse the repository at this point in the history
  • Loading branch information
anjoismysign committed Jul 24, 2024
1 parent f392fb4 commit 4b97634
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@NotNull String dataType,
@Nullable String dataType,
@NotNull Supplier<List<T>> selectorList,
@NotNull Consumer<T> onSelect,
@Nullable Function<T, ItemStack> display,
Expand All @@ -445,9 +445,9 @@ public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
BlobInventory inventory = buildInventory(blobInventoryKey, player);
BlobSelector<T> selector = BlobSelector.build(inventory, player.getUniqueId(),
dataType, selectorList.get(), onReturn);
selector.setButtonRangeKey(buttonRangeKey);
selector.setItemsPerPage(selector.getSlots(buttonRangeKey)
== null ? 1 : selector.getSlots(buttonRangeKey).size());
selector.setWhiteBackgroundName(buttonRangeKey);
if (display != null)
selector.selectElement(player,
onSelect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class VariableSelector<T> extends BlobInventory {
@Nullable
private Supplier<Collection<T>> collectionSupplier;
@Nullable
private String whiteBackgroundName;
private String buttonRangeKey;
private int page;
private int itemsPerPage;

Expand Down Expand Up @@ -78,34 +78,38 @@ public VariableSelector(BlobInventory blobInventory, UUID builderId,
/**
* creates a new VariableSelector
*
* @param blobInventory the inventory to use
* @param builderId the id of the builder
* @param dataType the data type
* @param filler the filler to use
* @param returnAction the action to run when the return button is clicked
* @param loadFunction the function to use to convert the list to an itemstack
*/
public VariableSelector(BlobInventory blobInventory, UUID builderId,
String dataType, VariableFiller<T> filler,
* @param blobInventory the inventory to use
* @param builderId the id of the builder
* @param dataType the data type
* @param filler the filler to use
* @param returnAction the action to run when the return button is clicked
* @param loadFunction the function to use to convert the list to an itemstack
* @param collectionSupplier the collection supplier
* @param buttonRangeKey the button range key
*/
public VariableSelector(BlobInventory blobInventory,
UUID builderId,
@Nullable String dataType,
VariableFiller<T> filler,
@Nullable Consumer<Player> returnAction,
@Nullable Function<T, ItemStack> loadFunction,
@Nullable Supplier<Collection<T>> collectionSupplier,
@Nullable String whiteBackgroundName) {
@Nullable String buttonRangeKey) {
super(blobInventory.getTitle(), blobInventory.getSize(), blobInventory.getButtonManager());
this.returnAction = returnAction == null ? HumanEntity::closeInventory : returnAction;
this.loadFunction = loadFunction;
this.collectionSupplier = collectionSupplier;
this.whiteBackgroundName = whiteBackgroundName;
this.buttonRangeKey = buttonRangeKey;
this.filler = filler;
this.builderId = builderId;
this.values = new HashMap<>();
this.dataType = dataType.toUpperCase();
this.dataType = dataType;
if (dataType != null)
setTitle(blobInventory.getTitle().replace("%variable%", dataType));
buildInventory();
this.page = 1;
this.itemsPerPage = 1;
Set<Integer> slots = getSlots(getWhiteBackgroundName());
Set<Integer> slots = getSlots(getButtonRangeKey());
if (slots != null)
setItemsPerPage(slots.size());
loadFirstPage();
Expand All @@ -131,7 +135,7 @@ public void loadPage(int page, boolean whiteBackgroundRefill) {
return;
}
if (whiteBackgroundRefill)
refillButton(getWhiteBackgroundName());
refillButton(getButtonRangeKey());
values.clear();
List<VariableValue<T>> values = filler.page(page, itemsPerPage);
for (int i = 0; i < values.size(); i++) {
Expand All @@ -154,7 +158,7 @@ public void loadCustomPage(int page, boolean refill, List<T> list, Function<T, I
return;
}
if (refill)
refillButton(getWhiteBackgroundName());
refillButton(getButtonRangeKey());
values.clear();
List<VariableValue<T>> values = filler.customPage(page, itemsPerPage, list, function);
for (int i = 0; i < values.size(); i++) {
Expand All @@ -177,11 +181,19 @@ public void loadCustomPage(int page, boolean refill, Function<T, ItemStack> func
return;
}
if (refill)
refillButton(getWhiteBackgroundName());
refillButton(getButtonRangeKey());
clearValues();
InventoryButton inventoryButton = Objects.requireNonNull(getButton(buttonRangeKey), "buttonRangeKey is not an InventoryButton");
List<Integer> slots = inventoryButton.getSlots().stream()
.sorted(Comparator.comparingInt(Integer::intValue))
.toList();
List<VariableValue<T>> values = this.customPage(page, getItemsPerPage(), function);
for (int i = 0; i < values.size(); i++) {
setValue(i, values.get(i));
Iterator<Integer> iterator = slots.iterator();
for (VariableValue<T> value : values) {
if (!iterator.hasNext())
break;
int next = iterator.next();
setValue(next, value);
}
}

Expand Down Expand Up @@ -295,6 +307,7 @@ public boolean isNextPageButton(int slot) {
/**
* @return the datatype that was specified in the constructor
*/
@Nullable
public String getDataType() {
return dataType;
}
Expand Down Expand Up @@ -459,13 +472,13 @@ public void setCollectionSupplier(@NotNull Supplier<Collection<T>> collectionSup
this.collectionSupplier = collectionSupplier;
}

public void setWhiteBackgroundName(@NotNull String whiteBackgroundName) {
Objects.requireNonNull(whiteBackgroundName, "whiteBackgroundName cannot be null");
this.whiteBackgroundName = whiteBackgroundName;
public void setButtonRangeKey(@NotNull String buttonRangeKey) {
Objects.requireNonNull(buttonRangeKey, "whiteBackgroundName cannot be null");
this.buttonRangeKey = buttonRangeKey;
}

@NotNull
private String getWhiteBackgroundName() {
return whiteBackgroundName == null ? "White-Background" : whiteBackgroundName;
private String getButtonRangeKey() {
return buttonRangeKey == null ? "White-Background" : buttonRangeKey;
}
}

0 comments on commit 4b97634

Please sign in to comment.