Provide access to AudienceRegistry and rename CollectionAudienceProvider to reflect return types

This commit is contained in:
Kieran Wallbanks 2021-03-25 14:35:27 +00:00
parent 1f52761c39
commit 0caca00dda
3 changed files with 26 additions and 10 deletions

View File

@ -82,4 +82,10 @@ public interface AudienceProvider<A> {
* @return all matching audience members * @return all matching audience members
*/ */
@NotNull A of(@NotNull Predicate<Audience> filter); @NotNull A of(@NotNull Predicate<Audience> filter);
/**
* Gets the audience registry used to register custom audiences.
* @return the registry
*/
@NotNull AudienceRegistry registry();
} }

View File

@ -12,7 +12,7 @@ import java.util.function.Predicate;
* Utility class to access Adventure audiences. * Utility class to access Adventure audiences.
*/ */
public class Audiences implements AudienceProvider<Audience> { public class Audiences implements AudienceProvider<Audience> {
private final CollectionAudienceProvider collection = new CollectionAudienceProvider(); private final IterableAudienceProvider collection = new IterableAudienceProvider();
/** /**
* Short-hand method for {@link MinecraftServer#getAudiences()}. * Short-hand method for {@link MinecraftServer#getAudiences()}.
@ -23,10 +23,10 @@ public class Audiences implements AudienceProvider<Audience> {
} }
/** /**
* Gets the {@link CollectionAudienceProvider} instance. * Gets the {@link IterableAudienceProvider} instance.
* @return the instance * @return the instance
*/ */
public @NotNull CollectionAudienceProvider collection() { public @NotNull IterableAudienceProvider iterable() {
return this.collection; return this.collection;
} }
@ -37,12 +37,12 @@ public class Audiences implements AudienceProvider<Audience> {
@Override @Override
public @NotNull Audience players() { public @NotNull Audience players() {
return PacketGroupingAudience.of(this.collection().players()); return PacketGroupingAudience.of(this.iterable().players());
} }
@Override @Override
public @NotNull Audience players(@NotNull Predicate<Player> filter) { public @NotNull Audience players(@NotNull Predicate<Player> filter) {
return PacketGroupingAudience.of(this.collection().players(filter)); return PacketGroupingAudience.of(this.iterable().players(filter));
} }
@Override @Override
@ -57,21 +57,26 @@ public class Audiences implements AudienceProvider<Audience> {
@Override @Override
public @NotNull Audience custom() { public @NotNull Audience custom() {
return Audience.audience(this.collection().custom()); return Audience.audience(this.iterable().custom());
} }
@Override @Override
public @NotNull Audience custom(@NotNull Key key) { public @NotNull Audience custom(@NotNull Key key) {
return Audience.audience(this.collection().custom(key)); return Audience.audience(this.iterable().custom(key));
} }
@Override @Override
public @NotNull Audience custom(@NotNull Predicate<Audience> filter) { public @NotNull Audience custom(@NotNull Predicate<Audience> filter) {
return Audience.audience(this.collection().custom(filter)); return Audience.audience(this.iterable().custom(filter));
} }
@Override @Override
public @NotNull Audience of(@NotNull Predicate<Audience> filter) { public @NotNull Audience of(@NotNull Predicate<Audience> filter) {
return Audience.audience(this.collection().of(filter)); return Audience.audience(this.iterable().of(filter));
}
@Override
public @NotNull AudienceRegistry registry() {
return this.iterable().registry();
} }
} }

View File

@ -19,7 +19,7 @@ import java.util.stream.StreamSupport;
/** /**
* A provider of iterable audiences. * A provider of iterable audiences.
*/ */
public final class CollectionAudienceProvider implements AudienceProvider<Iterable<? extends Audience>> { public final class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Audience>> {
private final Collection<ConsoleSender> console = Collections.singleton(MinecraftServer.getCommandManager().getConsoleSender()); private final Collection<ConsoleSender> console = Collections.singleton(MinecraftServer.getCommandManager().getConsoleSender());
private final AudienceRegistry registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new); private final AudienceRegistry registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new);
@ -67,4 +67,9 @@ public final class CollectionAudienceProvider implements AudienceProvider<Iterab
public @NotNull Iterable<? extends Audience> of(@NotNull Predicate<Audience> filter) { public @NotNull Iterable<? extends Audience> of(@NotNull Predicate<Audience> filter) {
return StreamSupport.stream(this.all().spliterator(), false).filter(filter).collect(Collectors.toList()); return StreamSupport.stream(this.all().spliterator(), false).filter(filter).collect(Collectors.toList());
} }
@Override
public @NotNull AudienceRegistry registry() {
return this.registry;
}
} }