Skip to content

Commit

Permalink
Add ability to clear collection
Browse files Browse the repository at this point in the history
  • Loading branch information
instrumentalityi committed Nov 10, 2024
1 parent 74c0f77 commit 4b38af1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion adventure/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archivesBaseName = 'zaiko-adventure'
version = '2.1.2-SNAPSHOT' // < change this version
version = '2.1.3-SNAPSHOT' // < change this version
description = 'Adventure support for Zaiko titles'

dependencies {
Expand Down
8 changes: 8 additions & 0 deletions api/src/main/java/gg/saki/zaiko/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ public void insertItem(int slot, ItemStack item) {
this.unstableInventory.setItem(slot, item);
}

public void removeItem(int slot) {
if (this.unstableInventory == null) return;

if (this.containsPlaceable(slot)) return;

this.unstableInventory.setItem(slot, null);
}

/**
* Gets whether the specified slot contains a {@link Placeable}.
*
Expand Down
20 changes: 15 additions & 5 deletions api/src/main/java/gg/saki/zaiko/populators/impl/Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

import gg.saki.zaiko.Menu;
import gg.saki.zaiko.placeables.Placeable;
import gg.saki.zaiko.placeables.impl.Icon;
import gg.saki.zaiko.populators.Populator;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -40,11 +40,11 @@ public class Collector implements Populator {

private int @NotNull [] dataSlots;

private @NotNull List<ItemStack> data;
private List<ItemStack> data;

private final Predicate<ItemStack> acceptable;

public Collector(int @NotNull [] dataSlots, @NotNull List<ItemStack> data, Predicate<ItemStack> acceptable) {
public Collector(int @NotNull [] dataSlots, @Nullable List<ItemStack> data, Predicate<ItemStack> acceptable) {
this.dataSlots = dataSlots;
this.data = data;
this.acceptable = acceptable;
Expand All @@ -55,19 +55,29 @@ public List<ItemStack> collect(@NotNull Menu menu) {

for (int slot : this.dataSlots) {
Placeable placeable = menu.getPlaceable(slot);
if(placeable == null) continue;
if (placeable == null) continue;

ItemStack item = placeable.getItem();
if(!this.acceptable.test(item)) continue;
if (!this.acceptable.test(item)) continue;

this.data.add(item);
}

return this.data;
}

public void clear(@NotNull Menu menu) {
for (int slot : this.dataSlots) {
menu.removeItem(slot);
}

this.data = null;
}

@Override
public void populate(@NotNull Menu menu) {
if(this.data == null) return;

Iterator<ItemStack> iterator = this.data.iterator();

for (int slot : this.dataSlots) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public CollectorMenu(@NotNull Zaiko zaiko) {
public void build(@NotNull Player player) {
this.place(4, 3, Button.builder().item(new ItemStack(Material.REDSTONE)).action((p, e) -> {
List<ItemStack> items = this.collector.collect(this);
this.collector.clear(this);

p.sendMessage("Size: " + items.size());

Expand Down

0 comments on commit 4b38af1

Please sign in to comment.