mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 01:47:54 +01:00
Merge pull request #185 from kezz/make-audiences-static
Make Audiences static
This commit is contained in:
commit
d55e509944
@ -2,6 +2,7 @@ package net.minestom.server.adventure.audience;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.key.Keyed;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -12,88 +13,139 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* Utility class to access Adventure audiences.
|
||||
*/
|
||||
public class Audiences implements AudienceProvider<Audience> {
|
||||
private final IterableAudienceProvider collection;
|
||||
private final Audience players, server;
|
||||
public class Audiences {
|
||||
private static final SingleAudienceProvider audience = new SingleAudienceProvider();
|
||||
|
||||
/**
|
||||
* Creates a new audiences instance. <b>Do not</b> instantiate this class, instead
|
||||
* you can obtain an instance using {@link #audiences()}.
|
||||
*/
|
||||
public Audiences() {
|
||||
this.collection = new IterableAudienceProvider();
|
||||
this.players = PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers());
|
||||
this.server = Audience.audience(this.players, MinecraftServer.getCommandManager().getConsoleSender());
|
||||
}
|
||||
|
||||
/**
|
||||
* Short-hand method for {@link MinecraftServer#getAudiences()}.
|
||||
* @return the audiences instance
|
||||
*/
|
||||
public static @NotNull Audiences audiences() {
|
||||
return MinecraftServer.getAudiences();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link IterableAudienceProvider} instance.
|
||||
* Gets the {@link AudienceProvider} that provides forwarding audiences.
|
||||
* @return the instance
|
||||
*/
|
||||
public @NotNull IterableAudienceProvider iterable() {
|
||||
return this.collection;
|
||||
public static @NotNull AudienceProvider<Audience> single() {
|
||||
return audience;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience all() {
|
||||
return Audience.audience(this.server, this.customs());
|
||||
/**
|
||||
* Gets the {@link AudienceProvider} that provides iterables of audience members.
|
||||
* @return the instance
|
||||
*/
|
||||
public static @NotNull AudienceProvider<Iterable<? extends Audience>> iterable() {
|
||||
return audience.collection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience players() {
|
||||
return this.players;
|
||||
/**
|
||||
* Gets all audience members. This returns {@link #players()} combined with
|
||||
* {@link #customs()} and {@link #console()}. This can be a costly operation, so it
|
||||
* is often preferable to use {@link #server()} instead.
|
||||
* @return all audience members
|
||||
*/
|
||||
public static @NotNull Audience all() {
|
||||
return Audience.audience(audience.server, audience.customs());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience players(@NotNull Predicate<Player> filter) {
|
||||
/**
|
||||
* Gets all audience members that are of type {@link Player}.
|
||||
* @return all players
|
||||
*/
|
||||
public static @NotNull Audience players() {
|
||||
return audience.players;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all audience members that are of type {@link Player} and match the predicate.
|
||||
* @param filter the predicate
|
||||
* @return all players matching the predicate
|
||||
*/
|
||||
public static @NotNull Audience players(@NotNull Predicate<Player> filter) {
|
||||
return PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience console() {
|
||||
/**
|
||||
* Gets the console as an audience.
|
||||
* @return the console
|
||||
*/
|
||||
public static @NotNull Audience console() {
|
||||
return MinecraftServer.getCommandManager().getConsoleSender();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience server() {
|
||||
return this.server;
|
||||
/**
|
||||
* Gets the combination of {@link #players()} and {@link #console()}.
|
||||
* @return the audience of all players and the console
|
||||
*/
|
||||
public static @NotNull Audience server() {
|
||||
return audience.server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience customs() {
|
||||
return Audience.audience(this.iterable().customs());
|
||||
/**
|
||||
* Gets all custom audience members.
|
||||
* @return all custom audience members
|
||||
*/
|
||||
public static @NotNull Audience customs() {
|
||||
return Audience.audience(audience.iterable().customs());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience custom(@NotNull Key key) {
|
||||
return Audience.audience(this.iterable().custom(key));
|
||||
/**
|
||||
* Gets all custom audience members stored using the given keyed object.
|
||||
* @param keyed the keyed object
|
||||
* @return all custom audience members stored using the key of the object
|
||||
*/
|
||||
public static @NotNull Audience custom(@NotNull Keyed keyed) {
|
||||
return custom(keyed.key());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience custom(@NotNull Key key, Predicate<Audience> filter) {
|
||||
return Audience.audience(this.iterable().custom(key, filter));
|
||||
/**
|
||||
* Gets all custom audience members stored using the given key.
|
||||
* @param key the key
|
||||
* @return all custom audience members stored using the key
|
||||
*/
|
||||
public static @NotNull Audience custom(@NotNull Key key) {
|
||||
return Audience.audience(audience.iterable().custom(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience customs(@NotNull Predicate<Audience> filter) {
|
||||
return Audience.audience(this.iterable().customs(filter));
|
||||
/**
|
||||
* Gets all custom audience members stored using the given keyed object that match
|
||||
* the given predicate.
|
||||
* @param keyed the keyed object
|
||||
* @param filter the predicate
|
||||
* @return all custom audience members stored using the key
|
||||
*/
|
||||
public static @NotNull Audience custom(@NotNull Keyed keyed, Predicate<Audience> filter) {
|
||||
return custom(keyed.key(), filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience all(@NotNull Predicate<Audience> filter) {
|
||||
return Audience.audience(this.iterable().all(filter));
|
||||
/**
|
||||
* Gets all custom audience members stored using the given key that match the
|
||||
* given predicate.
|
||||
* @param key the key
|
||||
* @param filter the predicate
|
||||
* @return all custom audience members stored using the key
|
||||
*/
|
||||
public static @NotNull Audience custom(@NotNull Key key, Predicate<Audience> filter) {
|
||||
return Audience.audience(audience.iterable().custom(key, filter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull AudienceRegistry registry() {
|
||||
return this.iterable().registry();
|
||||
/**
|
||||
* Gets all custom audience members matching the given predicate.
|
||||
* @param filter the predicate
|
||||
* @return all matching custom audience members
|
||||
*/
|
||||
public static @NotNull Audience customs(@NotNull Predicate<Audience> filter) {
|
||||
return Audience.audience(audience.iterable().customs(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all audience members that match the given predicate.
|
||||
* @param filter the predicate
|
||||
* @return all matching audience members
|
||||
*/
|
||||
public static @NotNull Audience all(@NotNull Predicate<Audience> filter) {
|
||||
return Audience.audience(audience.iterable().all(filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the audience registry used to register custom audiences.
|
||||
* @return the registry
|
||||
*/
|
||||
public static @NotNull AudienceRegistry registry() {
|
||||
return audience.iterable().registry();
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,14 @@ import java.util.stream.StreamSupport;
|
||||
/**
|
||||
* A provider of iterable audiences.
|
||||
*/
|
||||
public final class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Audience>> {
|
||||
private final Collection<ConsoleSender> console = Collections.singleton(MinecraftServer.getCommandManager().getConsoleSender());
|
||||
private final AudienceRegistry registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new);
|
||||
class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Audience>> {
|
||||
private final Collection<ConsoleSender> console;
|
||||
private final AudienceRegistry registry;
|
||||
|
||||
IterableAudienceProvider() {
|
||||
this.console = Collections.singleton(MinecraftServer.getCommandManager().getConsoleSender());
|
||||
this.registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Iterable<? extends Audience> all() {
|
||||
|
@ -0,0 +1,88 @@
|
||||
package net.minestom.server.adventure.audience;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A provider of audiences. For complex returns, this instance is backed by
|
||||
* {@link IterableAudienceProvider}.
|
||||
*/
|
||||
class SingleAudienceProvider implements AudienceProvider<Audience> {
|
||||
final IterableAudienceProvider collection;
|
||||
final Audience players, server;
|
||||
|
||||
SingleAudienceProvider() {
|
||||
this.collection = new IterableAudienceProvider();
|
||||
this.players = PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers());
|
||||
this.server = Audience.audience(this.players, MinecraftServer.getCommandManager().getConsoleSender());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link IterableAudienceProvider} instance.
|
||||
* @return the instance
|
||||
*/
|
||||
public @NotNull IterableAudienceProvider iterable() {
|
||||
return this.collection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience all() {
|
||||
return Audience.audience(this.server, this.customs());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience players() {
|
||||
return this.players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience players(@NotNull Predicate<Player> filter) {
|
||||
return PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience console() {
|
||||
return MinecraftServer.getCommandManager().getConsoleSender();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience server() {
|
||||
return this.server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience customs() {
|
||||
return Audience.audience(this.iterable().customs());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience custom(@NotNull Key key) {
|
||||
return Audience.audience(this.iterable().custom(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience custom(@NotNull Key key, Predicate<Audience> filter) {
|
||||
return Audience.audience(this.iterable().custom(key, filter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience customs(@NotNull Predicate<Audience> filter) {
|
||||
return Audience.audience(this.iterable().customs(filter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience all(@NotNull Predicate<Audience> filter) {
|
||||
return Audience.audience(this.iterable().all(filter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull AudienceRegistry registry() {
|
||||
return this.iterable().registry();
|
||||
}
|
||||
}
|
@ -518,7 +518,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
|
||||
// #buildDeathMessage can return null, check here
|
||||
if (chatMessage != null) {
|
||||
Audiences.audiences().players().sendMessage(chatMessage);
|
||||
Audiences.players().sendMessage(chatMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -153,9 +153,9 @@ public final class ConnectionManager {
|
||||
@Deprecated
|
||||
public void broadcastMessage(@NotNull JsonMessage jsonMessage, @Nullable PlayerValidator condition) {
|
||||
if (condition == null) {
|
||||
Audiences.audiences().players().sendMessage(jsonMessage);
|
||||
Audiences.players().sendMessage(jsonMessage);
|
||||
} else {
|
||||
Audiences.audiences().players(condition).sendMessage(jsonMessage);
|
||||
Audiences.players(condition).sendMessage(jsonMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ public class PlayerInit {
|
||||
|
||||
final Component header = Component.text("RAM USAGE: " + ramUsage + " MB");
|
||||
final Component footer = benchmarkManager.getCpuMonitoringMessage();
|
||||
Audiences.audiences().players().sendPlayerListHeaderAndFooter(header, footer);
|
||||
Audiences.players().sendPlayerListHeaderAndFooter(header, footer);
|
||||
|
||||
}).repeat(10, TimeUnit.TICK).schedule();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user