From ac57726a983ce873b98b596bf31f2755f112964f Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Tue, 2 Mar 2021 13:42:34 +0000 Subject: [PATCH] Make more classes implement Audience --- .../net/minestom/server/MinecraftServer.java | 9 +++++- .../java/net/minestom/server/Viewable.java | 12 ++++++++ .../server/adventure/WrapperAudience.java | 28 +++++++++++++++++++ .../minestom/server/instance/Instance.java | 13 ++++++++- .../server/network/ConnectionManager.java | 10 ++++++- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/minestom/server/adventure/WrapperAudience.java diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index 24feecfd6..b3545e4ca 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -1,5 +1,7 @@ package net.minestom.server; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.ForwardingAudience; import net.minestom.server.advancements.AdvancementManager; import net.minestom.server.adventure.BossBarManager; import net.minestom.server.benchmark.BenchmarkManager; @@ -50,6 +52,7 @@ import net.minestom.server.utils.validate.Check; import net.minestom.server.world.Difficulty; import net.minestom.server.world.DimensionTypeManager; import net.minestom.server.world.biomes.BiomeManager; +import org.checkerframework.checker.nullness.qual.NonNull; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -63,7 +66,7 @@ import java.io.IOException; * The server needs to be initialized with {@link #init()} and started with {@link #start(String, int)}. * You should register all of your dimensions, biomes, commands, events, etc... in-between. */ -public final class MinecraftServer { +public final class MinecraftServer implements ForwardingAudience.Single { public final static Logger LOGGER = LoggerFactory.getLogger(MinecraftServer.class); @@ -816,4 +819,8 @@ public final class MinecraftServer { "if you are developing an extension be sure to retrieve them at least after Extension#preInitialize");*/ } + @Override + public @NonNull Audience audience() { + return getConnectionManager(); + } } diff --git a/src/main/java/net/minestom/server/Viewable.java b/src/main/java/net/minestom/server/Viewable.java index 8dd6f1f79..861930804 100644 --- a/src/main/java/net/minestom/server/Viewable.java +++ b/src/main/java/net/minestom/server/Viewable.java @@ -1,5 +1,8 @@ package net.minestom.server; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.ForwardingAudience; +import net.minestom.server.adventure.WrapperAudience; import net.minestom.server.entity.Player; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.utils.PacketUtils; @@ -82,4 +85,13 @@ public interface Viewable { default void sendPacketToViewersAndSelf(@NotNull ServerPacket packet) { sendPacketToViewers(packet); } + + /** + * Gets the result of {@link #getViewers()} as an Adventure Audience. + * + * @return the audience + */ + default @NotNull Audience asAudience() { + return new WrapperAudience(this.getViewers()); + } } diff --git a/src/main/java/net/minestom/server/adventure/WrapperAudience.java b/src/main/java/net/minestom/server/adventure/WrapperAudience.java new file mode 100644 index 000000000..52b2b3db0 --- /dev/null +++ b/src/main/java/net/minestom/server/adventure/WrapperAudience.java @@ -0,0 +1,28 @@ +package net.minestom.server.adventure; + +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.ForwardingAudience; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.jetbrains.annotations.NotNull; + +/** + * Implementation of {@link ForwardingAudience} that acts as a wrapper around another + * iterable collection of audiences. + */ +public class WrapperAudience implements ForwardingAudience { + private final Iterable audiences; + + /** + * Creates a new wrapper audience. + * + * @param audiences the audiences to wrap + */ + public WrapperAudience(@NotNull Iterable audiences) { + this.audiences = audiences; + } + + @Override + public @NonNull Iterable audiences() { + return this.audiences; + } +} diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 0e5982217..378fc2cf6 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -1,6 +1,11 @@ package net.minestom.server.instance; import com.google.common.collect.Queues; +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import it.unimi.dsi.fastutil.longs.Long2ObjectMaps; +import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.ForwardingAudience; import net.minestom.server.MinecraftServer; import net.minestom.server.UpdateManager; import net.minestom.server.data.Data; @@ -34,6 +39,7 @@ import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.validate.Check; import net.minestom.server.world.DimensionType; +import org.checkerframework.checker.nullness.qual.NonNull; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -53,7 +59,7 @@ import java.util.function.Consumer; * you need to be sure to signal the {@link UpdateManager} of the changes using * {@link UpdateManager#signalChunkLoad(Instance, int, int)} and {@link UpdateManager#signalChunkUnload(Instance, int, int)}. */ -public abstract class Instance implements BlockModifier, EventHandler, DataContainer { +public abstract class Instance implements BlockModifier, EventHandler, DataContainer, ForwardingAudience { protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager(); protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager(); @@ -1100,4 +1106,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta public PFInstanceSpace getInstanceSpace() { return instanceSpace; } + + @Override + public @NonNull Iterable audiences() { + return this.getPlayers(); + } } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index f4272b4e2..2abc58d28 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -1,6 +1,8 @@ package net.minestom.server.network; import io.netty.channel.Channel; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.ForwardingAudience; import net.minestom.server.MinecraftServer; import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ColoredText; @@ -24,6 +26,7 @@ import net.minestom.server.utils.async.AsyncUtils; import net.minestom.server.utils.callback.validator.PlayerValidator; import net.minestom.server.utils.validate.Check; import org.apache.commons.text.similarity.JaroWinklerDistance; +import org.checkerframework.checker.nullness.qual.NonNull; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -37,7 +40,7 @@ import java.util.function.Consumer; /** * Manages the connected clients. */ -public final class ConnectionManager { +public final class ConnectionManager implements ForwardingAudience { private static final long KEEP_ALIVE_DELAY = 10_000; private static final long KEEP_ALIVE_KICK = 30_000; @@ -528,4 +531,9 @@ public final class ConnectionManager { public void addWaitingPlayer(@NotNull Player player) { this.waitingPlayers.add(player); } + + @Override + public @NonNull Iterable audiences() { + return this.getOnlinePlayers(); + } }