Added PlayerChatEvent#getDefaultChatFormat

This commit is contained in:
TheMode 2021-04-07 02:47:19 +02:00
parent 093cce7242
commit 2606f2b291
2 changed files with 21 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Called every time a {@link Player} write and send something in the chat.
@ -19,14 +20,18 @@ import java.util.function.Function;
public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
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 String message) {
public PlayerChatEvent(@NotNull Player player, @NotNull Collection<Player> recipients,
@NotNull Supplier<Component> defaultChatFormat,
@NotNull String message) {
super(player);
this.recipients = new ArrayList<>(recipients);
this.defaultChatFormat = defaultChatFormat;
this.message = message;
}
@ -57,8 +62,7 @@ public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
*
* @return a modifiable list of message targets
*/
@NotNull
public Collection<Player> getRecipients() {
public @NotNull Collection<Player> getRecipients() {
return recipients;
}
@ -67,8 +71,7 @@ public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
*
* @return the sender's message
*/
@NotNull
public String getMessage() {
public @NotNull String getMessage() {
return message;
}
@ -88,11 +91,14 @@ public class PlayerChatEvent extends PlayerEvent implements CancellableEvent {
*
* @return the chat format which will be used, null if this is the default one
*/
@Nullable
public Function<PlayerChatEvent, Component> getChatFormatFunction() {
public @Nullable Function<@NotNull PlayerChatEvent, @NotNull Component> getChatFormatFunction() {
return chatFormat;
}
public @NotNull Supplier<@NotNull Component> getDefaultChatFormat() {
return defaultChatFormat;
}
@Override
public boolean isCancelled() {
return cancelled;

View File

@ -10,6 +10,7 @@ import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
import net.minestom.server.network.packet.server.play.ChatMessagePacket;
import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.function.Function;
@ -34,7 +35,8 @@ public class ChatMessageListener {
}
final Collection<Player> players = CONNECTION_MANAGER.getOnlinePlayers();
PlayerChatEvent playerChatEvent = new PlayerChatEvent(player, players, message);
String finalMessage = message;
PlayerChatEvent playerChatEvent = new PlayerChatEvent(player, players, () -> buildDefaultChatMessage(player, finalMessage), message);
// Call the event
player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> {
@ -48,7 +50,7 @@ public class ChatMessageListener {
textObject = formatFunction.apply(playerChatEvent);
} else {
// Default format
textObject = buildDefaultChatMessage(playerChatEvent);
textObject = playerChatEvent.getDefaultChatFormat().get();
}
final Collection<Player> recipients = playerChatEvent.getRecipients();
@ -64,15 +66,14 @@ public class ChatMessageListener {
}
private static Component buildDefaultChatMessage(PlayerChatEvent chatEvent) {
final String username = chatEvent.getPlayer().getUsername();
private static @NotNull Component buildDefaultChatMessage(@NotNull Player player, @NotNull String message) {
final String username = player.getUsername();
return Component.translatable("chat.type.text")
.args(Component.text(username)
.insertion(username)
.clickEvent(ClickEvent.suggestCommand("/msg " + username + " "))
.hoverEvent(chatEvent.getPlayer()),
Component.text(chatEvent.getMessage())
.hoverEvent(player),
Component.text(message)
);
}