diff --git a/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/Audience.java b/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/Audience.java index 97278a1d..b844fbde 100644 --- a/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/Audience.java +++ b/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/Audience.java @@ -28,8 +28,17 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +/** + * A universal interface for sending Mindustry content to receivers. + */ public interface Audience { + /** + * Creates an audience that forwards its content to the given audiences. + * + * @param audiences the audiences + * @return the forwarding audience + */ static Audience of(final Audience... audiences) { if (audiences.length == 0) { return Audience.empty(); @@ -40,66 +49,130 @@ static Audience of(final Audience... audiences) { } } + /** + * Creates an audience that forwards its content to the given audiences. + * + * @param audiences the audiences + * @return the forwarding audience + */ static Audience of(final Iterable audiences) { return ((ForwardingAudience) () -> audiences); } + /** + * Returns an empty audience that does nothing. + */ static Audience empty() { return EmptyAudience.INSTANCE; } + /** + * Returns a collector, combining a stream of audiences into a single forwarding audience. + */ static Collector collectToAudience() { return Collectors.collectingAndThen(Collectors.toList(), Audience::of); } - default void sendMessage(final String message) {} - + /** + * Sends a message to this audience. + * + * @param component the message + */ default void sendMessage(final ComponentLike component) {} - default void sendMessage(final String message, final String unformatted, final Audience sender) {} - + /** + * Sends a message to this audience. + * + * @param component the message + * @param unformatted the unformatted message + * @param sender the sender + */ default void sendMessage(final ComponentLike component, final ComponentLike unformatted, final Audience sender) {} - default void sendWarning(final String message) {} - + /** + * Sends a warning to this audience. + * + * @param component the warning + */ default void sendWarning(final ComponentLike component) {} - default void showHUDText(final String message) {} - + /** + * Shows a HUD text to this audience. + * + * @param component the HUD text + */ default void showHUDText(final ComponentLike component) {} + /** + * Hides the HUD text of this audience. + */ default void hideHUDText() {} - default void sendNotification(final String message, final char icon) {} - + /** + * Sends a notification to this audience. + * + * @param component the notification + * @param icon the icon + */ default void sendNotification(final ComponentLike component, final char icon) {} - default void sendAnnouncement(final String message) {} - + /** + * Sends an announcement to this audience. + * + * @param component the announcement + */ default void sendAnnouncement(final ComponentLike component) {} + /** + * Sends an uri to open to this audience. + * + * @param uri the uri + */ default void openURI(final URI uri) {} - default void showLabel(final String label, final float x, final float y, final Duration duration) {} - + /** + * Shows a label to this audience. + * + * @param label the label + * @param x the x position in world coordinates + * @param y the y position in world coordinates + * @param duration the duration + */ default void showLabel(final ComponentLike label, final float x, final float y, final Duration duration) {} - default void kick(final String reason, final Duration duration, final boolean silent) {} - + /** + * Kicks this audience from the server. + * + * @param reason the reason + * @param duration the duration + * @param silent whether the kick should be logged + */ default void kick(final ComponentLike reason, final Duration duration, final boolean silent) {} + /** + * Returns the metadata of this audience. + */ default MetadataContainer getMetadata() { return MetadataContainer.empty(); } + /** + * Returns the audiences this audience forwards to. + */ default Iterable getAudiences() { return List.of(this); } + /** + * Returns the audiences this audience forwards to as a stream. + */ default Stream asStream() { return Stream.of(this); } + /** + * Returns the permissions of this audience. + */ default PermissionContainer getPermissions() { return PermissionContainer.empty(); } diff --git a/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/EmptyAudience.java b/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/EmptyAudience.java index 4bcdc457..ecf2f147 100644 --- a/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/EmptyAudience.java +++ b/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/EmptyAudience.java @@ -21,11 +21,8 @@ import java.util.List; import java.util.stream.Stream; -final class EmptyAudience implements Audience { - - static final Audience INSTANCE = new EmptyAudience(); - - private EmptyAudience() {} +enum EmptyAudience implements Audience { + INSTANCE; @Override public Iterable getAudiences() { diff --git a/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/ForwardingAudience.java b/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/ForwardingAudience.java index c0b20ebb..bdda4029 100644 --- a/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/ForwardingAudience.java +++ b/distributor-common-api/src/main/java/com/xpdustry/distributor/api/audience/ForwardingAudience.java @@ -28,6 +28,9 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; +/** + * An audience that forwards all operations to a collection of sub audiences. + */ @FunctionalInterface public interface ForwardingAudience extends Audience { @@ -50,13 +53,6 @@ default Stream asStream() { return stream.flatMap(Audience::asStream); } - @Override - default void sendMessage(final String message) { - for (final var audience : getAudiences()) { - audience.sendMessage(message); - } - } - @Override default void sendMessage(final ComponentLike component) { for (final var audience : getAudiences()) { @@ -64,13 +60,6 @@ default void sendMessage(final ComponentLike component) { } } - @Override - default void sendMessage(final String message, final String unformatted, final Audience sender) { - for (final var audience : getAudiences()) { - audience.sendMessage(message, unformatted, sender); - } - } - @Override default void sendMessage(final ComponentLike component, final ComponentLike unformatted, final Audience sender) { for (final var audience : getAudiences()) { @@ -78,13 +67,6 @@ default void sendMessage(final ComponentLike component, final ComponentLike unfo } } - @Override - default void sendWarning(final String message) { - for (final var audience : getAudiences()) { - audience.sendWarning(message); - } - } - @Override default void sendWarning(final ComponentLike component) { for (final var audience : getAudiences()) { @@ -92,13 +74,6 @@ default void sendWarning(final ComponentLike component) { } } - @Override - default void showHUDText(final String message) { - for (final var audience : getAudiences()) { - audience.showHUDText(message); - } - } - @Override default void showHUDText(final ComponentLike component) { for (final var audience : getAudiences()) { @@ -113,13 +88,6 @@ default void hideHUDText() { } } - @Override - default void sendNotification(final String message, final char icon) { - for (final var audience : getAudiences()) { - audience.sendNotification(message, icon); - } - } - @Override default void sendNotification(final ComponentLike component, final char icon) { for (final var audience : getAudiences()) { @@ -127,13 +95,6 @@ default void sendNotification(final ComponentLike component, final char icon) { } } - @Override - default void sendAnnouncement(final String message) { - for (final var audience : getAudiences()) { - audience.sendAnnouncement(message); - } - } - @Override default void sendAnnouncement(final ComponentLike component) { for (final var audience : getAudiences()) { @@ -148,13 +109,6 @@ default void openURI(final URI uri) { } } - @Override - default void showLabel(final String label, final float x, final float y, final Duration duration) { - for (final var audience : getAudiences()) { - audience.showLabel(label, x, y, duration); - } - } - @Override default void showLabel(final ComponentLike label, final float x, final float y, final Duration duration) { for (final var audience : getAudiences()) { @@ -162,13 +116,6 @@ default void showLabel(final ComponentLike label, final float x, final float y, } } - @Override - default void kick(final String reason, final Duration duration, final boolean silent) { - for (final var audience : getAudiences()) { - audience.kick(reason, duration, silent); - } - } - @Override default void kick(final ComponentLike reason, final Duration duration, final boolean silent) { for (final var audience : getAudiences()) { diff --git a/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/PlayerAudience.java b/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/PlayerAudience.java index 64ffd930..f4c55515 100644 --- a/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/PlayerAudience.java +++ b/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/PlayerAudience.java @@ -59,25 +59,11 @@ public final class PlayerAudience implements Audience { .build(); } - @Override - public void sendMessage(final String message) { - player.sendMessage(message); - } - @Override public void sendMessage(final ComponentLike component) { player.sendMessage(render(component)); } - @Override - public void sendMessage(final String message, final String unformatted, final Audience sender) { - if (sender instanceof PlayerAudience other) { - player.sendMessage(message, other.player, unformatted); - } else { - player.sendMessage(message); - } - } - @Override public void sendMessage(final ComponentLike component, final ComponentLike unformatted, final Audience sender) { if (sender instanceof PlayerAudience other) { @@ -87,21 +73,11 @@ public void sendMessage(final ComponentLike component, final ComponentLike unfor } } - @Override - public void sendWarning(final String message) { - Call.announce(getConnection(), message); - } - @Override public void sendWarning(final ComponentLike component) { Call.announce(getConnection(), render(component)); } - @Override - public void showHUDText(final String message) { - Call.setHudText(getConnection(), message); - } - @Override public void showHUDText(final ComponentLike component) { Call.setHudText(getConnection(), render(component)); @@ -112,21 +88,11 @@ public void hideHUDText() { Call.hideHudText(getConnection()); } - @Override - public void sendNotification(final String message, final char icon) { - Call.warningToast(getConnection(), icon, message); - } - @Override public void sendNotification(final ComponentLike component, final char icon) { Call.warningToast(getConnection(), icon, render(component)); } - @Override - public void sendAnnouncement(final String message) { - Call.infoMessage(getConnection(), message); - } - @Override public void sendAnnouncement(final ComponentLike component) { Call.infoMessage(getConnection(), render(component)); @@ -137,43 +103,30 @@ public void openURI(final URI uri) { Call.openURI(getConnection(), uri.toString()); } - @Override - public void showLabel(final String label, final float x, final float y, final Duration duration) { - Call.label(getConnection(), label, duration.toMillis() / 1000F, x, y); - } - @Override public void showLabel(final ComponentLike label, final float x, final float y, final Duration duration) { Call.label(getConnection(), render(label), duration.toMillis() / 1000F, x, y); } - @Override - public void kick(final String reason, final Duration duration, final boolean silent) { - kick0(reason, duration, silent); - } - @Override public void kick(final ComponentLike reason, final Duration duration, final boolean silent) { - kick0(render(reason), duration, silent); - } - - private void kick0(final String reason, final Duration duration, final boolean silent) { final var connection = getConnection(); if (connection.kicked) return; + final var rendered = render(reason); LoggerFactory.getLogger("ROOT") .atLevel(silent ? Level.TRACE : Level.INFO) .setMessage("Kicking connection {} / {}; Reason: {}") .addArgument(connection.address) .addArgument(connection.uuid) - .addArgument(reason.replace("\n", " ")) + .addArgument(rendered.replace("\n", " ")) .log(); if (duration.toMillis() > 0L) { netServer.admins.handleKicked(connection.uuid, connection.address, duration.toMillis()); } - Call.kick(connection, reason); + Call.kick(connection, rendered); if (connection.uuid.startsWith("steam:")) { // run with a 2-frame delay so there is time to send the kick packet, steam handles this weirdly diff --git a/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/ServerAudience.java b/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/ServerAudience.java index da437e4d..d623387b 100644 --- a/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/ServerAudience.java +++ b/distributor-common/src/main/java/com/xpdustry/distributor/common/audience/ServerAudience.java @@ -37,31 +37,16 @@ public enum ServerAudience implements Audience { .putSupplier(StandardKeys.LOCALE, Locale::getDefault) .build(); - @Override - public void sendMessage(final String message) { - Log.info(message); - } - @Override public void sendMessage(final ComponentLike component) { Log.info(render(component)); } - @Override - public void sendMessage(final String message, final String unformatted, final Audience sender) { - Log.info(message); - } - @Override public void sendMessage(final ComponentLike component, final ComponentLike unformatted, final Audience sender) { Log.info(render(component)); } - @Override - public void sendWarning(final String message) { - Log.warn(message); - } - @Override public void sendWarning(final ComponentLike component) { Log.warn(render(component));