Simplify Chat Event (#2085)

This commit is contained in:
iam 2024-04-06 16:33:55 -04:00 committed by GitHub
parent 0c9527118a
commit 9a0d61755e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 23 deletions

View File

@ -20,27 +20,26 @@ public class PlayerChatEvent implements PlayerInstanceEvent, CancellableEvent {
private final Player player;
private final Collection<Player> recipients;
private final Supplier<Component> defaultChatFormat;
private String message;
private Function<PlayerChatEvent, Component> chatFormat;
private boolean cancelled;
public PlayerChatEvent(@NotNull Player player, @NotNull Collection<Player> recipients,
@NotNull Supplier<Component> defaultChatFormat,
@NotNull Function<PlayerChatEvent, Component> defaultChatFormat,
@NotNull String message) {
this.player = player;
this.recipients = new ArrayList<>(recipients);
this.defaultChatFormat = defaultChatFormat;
this.chatFormat = defaultChatFormat;
this.message = message;
}
/**
* Changes the chat format.
*
* @param chatFormat the custom chat format, null to use the default one
* @param chatFormat the custom chat format
*/
public void setChatFormat(@Nullable Function<PlayerChatEvent, Component> chatFormat) {
public void setChatFormat(@NotNull Function<PlayerChatEvent, Component> chatFormat) {
this.chatFormat = chatFormat;
}
@ -76,18 +75,13 @@ public class PlayerChatEvent implements PlayerInstanceEvent, CancellableEvent {
/**
* Used to retrieve the chat format for this message.
* <p>
* If null, the default format will be used.
*
* @return the chat format which will be used, null if this is the default one
* @return the chat format which will be used
*/
public @Nullable Function<@NotNull PlayerChatEvent, @NotNull Component> getChatFormatFunction() {
public @NotNull Function<@NotNull PlayerChatEvent, @NotNull Component> getChatFormatFunction() {
return chatFormat;
}
public @NotNull Supplier<@NotNull Component> getDefaultChatFormat() {
return defaultChatFormat;
}
@Override
public boolean isCancelled() {
return cancelled;

View File

@ -40,21 +40,12 @@ public class ChatMessageListener {
}
final Collection<Player> players = CONNECTION_MANAGER.getOnlinePlayers();
PlayerChatEvent playerChatEvent = new PlayerChatEvent(player, players, () -> buildDefaultChatMessage(player, message), message);
PlayerChatEvent playerChatEvent = new PlayerChatEvent(player, players, (e) -> buildDefaultChatMessage(e.getPlayer(), e.getMessage()), message);
// Call the event
EventDispatcher.callCancellable(playerChatEvent, () -> {
final Function<PlayerChatEvent, Component> formatFunction = playerChatEvent.getChatFormatFunction();
Component textObject;
if (formatFunction != null) {
// Custom format
textObject = formatFunction.apply(playerChatEvent);
} else {
// Default format
textObject = playerChatEvent.getDefaultChatFormat().get();
}
Component textObject = formatFunction.apply(playerChatEvent);
final Collection<Player> recipients = playerChatEvent.getRecipients();
if (!recipients.isEmpty()) {