Skip to content

Commit

Permalink
improve PlayerChatEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed May 15, 2021
1 parent 2979759 commit 8ecd355
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
30 changes: 30 additions & 0 deletions src/main/java/org/spongepowered/api/adventure/Audiences.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
package org.spongepowered.api.adventure;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import org.spongepowered.api.Sponge;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

/**
* {@link Audience}s.
Expand Down Expand Up @@ -64,6 +69,31 @@ public static Audience withPermission(final String permission) {
return Audiences.factory().withPermission(permission);
}

/**
* Filters an audience with given predicate.
* <p>For {@link net.kyori.adventure.audience.ForwardingAudience}s the predicate is tested on each {@link ForwardingAudience#audiences()}</p>
*
* @param predicate The predicate
*
* @return The filtered audience.
*/
public static Optional<Audience> filtered(final Audience audience, final Predicate<Audience> predicate) {
if (!(audience instanceof ForwardingAudience)) {
if (predicate.test(audience)) {
return Optional.of(audience);
}
return Optional.empty();
}
final List<Audience> list = new ArrayList<>();
for (Audience subAudience : ((ForwardingAudience) audience).audiences()) {
Audiences.filtered(subAudience, predicate).ifPresent(list::add);
}
if (list.isEmpty()) {
return Optional.empty();
}
return Optional.of((ForwardingAudience) () -> list);
}

/**
* Gets an {@link Audience} that targets the system, such as a server console.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
package org.spongepowered.api.entity.living.player;

import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component;
import org.spongepowered.api.entity.living.player.server.ServerPlayer;

import java.util.Optional;

/**
* A chat router.
* A player chat formatter.
*/
public interface PlayerChatRouter {
static PlayerChatRouter toAudience(final Audience audience) {
return (player, message) -> audience.sendMessage(player, message, MessageType.CHAT);
}
public interface PlayerChatFormatter {

void chat(final Player player, final Component message);
Optional<Component> format(final ServerPlayer player, final Audience target, final Component message, final Component originalMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.player.CooldownTracker;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.PlayerChatRouter;
import org.spongepowered.api.entity.living.player.PlayerChatFormatter;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.entity.living.player.chat.ChatVisibility;
import org.spongepowered.api.entity.living.player.tab.TabList;
Expand Down Expand Up @@ -348,12 +348,12 @@ default Optional<Value.Mutable<Entity>> spectatorTarget() {
*
* @return The chat router
*/
PlayerChatRouter chatRouter();
PlayerChatFormatter chatFormatter();

/**
* Sets the chat router.
*
* @param router the chat router
*/
void setChatRouter(final PlayerChatRouter router);
void setChatFormatter(final PlayerChatFormatter router);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
*/
package org.spongepowered.api.event.message;

import com.google.common.collect.Streams;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.adventure.Audiences;
import org.spongepowered.api.util.annotation.eventgen.GenerateFactoryMethod;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Describes events when a involving a {@link Component} message and {@link Audience}s.
Expand Down Expand Up @@ -58,4 +64,15 @@ public interface MessageChannelEvent extends MessageEvent {
*/
void setAudience(@Nullable Audience audience);

/**
* Filters the current audience with given predicate.
*
* @param predicate the predicate
*/
default void filterAudience(Predicate<Audience> predicate) {
if (this.audience().isPresent()) {
this.setAudience(Audiences.filtered(this.audience().get(), predicate).orElse(null));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,43 @@
*/
package org.spongepowered.api.event.message;

import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.entity.living.player.PlayerChatRouter;
import org.spongepowered.api.entity.living.player.PlayerChatFormatter;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Event;

import java.util.Optional;

/**
* Fired when the {@link Component} being sent to a {@link PlayerChatRouter} was
* Fired when the {@link Component} being sent to a {@link PlayerChatFormatter} was
* due to chatting.
*/
public interface PlayerChatEvent extends Event, Cancellable {
public interface PlayerChatEvent extends MessageChannelEvent, Cancellable {

/**
* Gets the original router that this message will be sent to.
* Gets the original formatter that this message will be sent through.
*
* @return The original router to send to
* @return The original formatter used
*/
PlayerChatRouter originalChatRouter();
PlayerChatFormatter originalChatFormatter();

/**
* Gets the current router that this message will be sent to.
* Gets the current formatter that this message will be sent through.
*
* @return The router the message in this event will be sent to
* @return The formatter the message in this event will be sent through
*/
Optional<PlayerChatRouter> chatRouter();
Optional<PlayerChatFormatter> chatFormatter();

/**
* Sets the router for this message to go to.
* Sets the formatter for this message to go trough.
* <p>If the target audience is a {@link net.kyori.adventure.audience.ForwardingAudience}
* the {@link PlayerChatFormatter} will be applied to each of its {@link ForwardingAudience#audiences()}</p>
*
* @param router The router to set
*/
void setChatRouter(@Nullable PlayerChatRouter router);
void setChatFormatter(@Nullable PlayerChatFormatter router);

/**
* Gets the original chat message.
Expand Down

0 comments on commit 8ecd355

Please sign in to comment.