Skip to content

Commit

Permalink
New Slot.withEntriesListener
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Sep 17, 2024
1 parent bfe5865 commit ad4fd43
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import me.shedaniel.math.Point;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.common.entry.EntryStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

public abstract class Slot extends WidgetWithBounds {
public static final byte UN_MARKED = 0;
Expand Down Expand Up @@ -134,6 +136,10 @@ public final Slot disableBackground() {

public abstract Slot entries(Collection<? extends EntryStack<?>> stacks);

@ApiStatus.Experimental
@ApiStatus.Internal
public abstract Slot withEntriesListener(Consumer<Slot> listener);

public abstract EntryStack<?> getCurrentEntry();

public abstract List<EntryStack<?>> getEntries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -273,6 +274,12 @@ public EntryWidget entries(Collection<? extends EntryStack<?>> stacks) {
return this;
}

@Override
public Slot withEntriesListener(Consumer<Slot> listener) {
this.getCyclingEntries().addListener($ -> listener.accept(this));
return this;
}

public Slot entries(CyclingList<EntryStack<?>> stacks) {
this.getCyclingEntries().setBacking(stacks);
if (removeTagMatch) tagMatch = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@
import com.google.common.collect.Iterables;
import org.jetbrains.annotations.Nullable;

import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class OriginalRetainingCyclingList<T> implements CyclingList.Mutable<T> {
private final Supplier<T> empty;
@Nullable
private CyclingList<T> backing = null;
private List<Consumer<CyclingList<T>>> listeners = List.of();

public OriginalRetainingCyclingList(Supplier<T> empty) {
this.empty = empty;
Expand All @@ -56,7 +55,9 @@ public T peek() {
@Override
public T previous() {
if (this.backing == null) return empty.get();
return this.backing.previous();
T previous = this.backing.previous();
notifyListeners();
return previous;
}

@Override
Expand All @@ -72,7 +73,9 @@ public int previousIndex() {
@Override
public T next() {
if (this.backing == null) return empty.get();
return this.backing.next();
T next = this.backing.next();
notifyListeners();
return next;
}

@Override
Expand All @@ -86,11 +89,16 @@ public void add(T entry) {
mutable.add(entry);
this.backing = mutable;
}

notifyListeners();
}

@Override
public void resetToStart() {
if (this.backing != null) this.backing.resetToStart();
if (this.backing != null) {
this.backing.resetToStart();
notifyListeners();
};
}

@Override
Expand Down Expand Up @@ -118,6 +126,8 @@ public void addAll(Collection<? extends T> entries) {
mutable.addAll(entries);
this.backing = mutable;
}

notifyListeners();
}
}

Expand All @@ -128,10 +138,13 @@ public void clear() {
} else {
this.backing = null;
}

notifyListeners();
}

public void setBacking(@Nullable CyclingList<T> backing) {
this.backing = backing;
notifyListeners();
}

private static <T> AbstractList<T> getListFromCollection(Collection<? extends T> entries) {
Expand Down Expand Up @@ -173,4 +186,19 @@ public CyclingList<T> getBacking() {
if (this.backing == null) return CyclingList.of(this.empty);
return this.backing;
}

public void addListener(Consumer<CyclingList<T>> listener) {
if (this.listeners instanceof ArrayList<Consumer<CyclingList<T>>> list) {
list.add(listener);
} else {
this.listeners = new ArrayList<>(this.listeners);
this.listeners.add(listener);
}
}

private void notifyListeners() {
for (Consumer<CyclingList<T>> listener : this.listeners) {
listener.accept(this);
}
}
}

0 comments on commit ad4fd43

Please sign in to comment.