Add generics to SpellContainer.stream

This commit is contained in:
Sollace 2022-10-01 18:21:08 +02:00
parent 2b00977e7f
commit 8c65adeea2
2 changed files with 14 additions and 7 deletions

View file

@ -62,9 +62,16 @@ public interface SpellContainer {
*/ */
boolean forEach(Function<Spell, Operation> action, boolean update); boolean forEach(Function<Spell, Operation> action, boolean update);
/**
* Gets all active effects for this caster updating it if needed.
*/
Stream<Spell> stream(boolean update); Stream<Spell> stream(boolean update);
Stream<Spell> stream(@Nullable SpellPredicate<?> type, boolean update); /**
* Gets all active effects for this caster that match the given type updating it if needed.
*/
<T extends Spell> Stream<T> stream(@Nullable SpellPredicate<T> type, boolean update);
/** /**
* Removes all effects currently active in this slot. * Removes all effects currently active in this slot.

View file

@ -51,9 +51,8 @@ public class EffectSync implements SpellContainer {
} }
@Override @Override
@SuppressWarnings("unchecked")
public <T extends Spell> Optional<T> get(@Nullable SpellPredicate<T> type, boolean update) { public <T extends Spell> Optional<T> get(@Nullable SpellPredicate<T> type, boolean update) {
return (Optional<T>)(read(type, update, true).findFirst()); return read(type, update, true).findFirst();
} }
@Override @Override
@ -95,7 +94,7 @@ public class EffectSync implements SpellContainer {
} }
@Override @Override
public Stream<Spell> stream(@Nullable SpellPredicate<?> type, boolean update) { public <T extends Spell> Stream<T> stream(@Nullable SpellPredicate<T> type, boolean update) {
return read(type, update, true); return read(type, update, true);
} }
@ -111,15 +110,16 @@ public class EffectSync implements SpellContainer {
return false; return false;
} }
private Stream<Spell> read(@Nullable SpellPredicate<?> type, boolean synchronize, boolean sendUpdate) { @SuppressWarnings("unchecked")
private <T extends Spell> Stream<T> read(@Nullable SpellPredicate<T> type, boolean synchronize, boolean sendUpdate) {
if (synchronize && spells.fromNbt(owner.getEntity().getDataTracker().get(param)) && sendUpdate) { if (synchronize && spells.fromNbt(owner.getEntity().getDataTracker().get(param)) && sendUpdate) {
owner.getEntity().getDataTracker().set(param, spells.toNbt()); owner.getEntity().getDataTracker().set(param, spells.toNbt());
} }
if (type == null) { if (type == null) {
return spells.getReferences(); return (Stream<T>)spells.getReferences();
} }
return spells.getReferences().flatMap(s -> s.findMatches(type)); return (Stream<T>)spells.getReferences().flatMap(s -> s.findMatches(type));
} }
public boolean reduce(Alteration alteration) { public boolean reduce(Alteration alteration) {