mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-19 13:51:22 +01:00
Simplify Chat Event (#2085)
This commit is contained in:
parent
0c9527118a
commit
9a0d61755e
@ -20,27 +20,26 @@ public class PlayerChatEvent implements PlayerInstanceEvent, CancellableEvent {
|
|||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final Collection<Player> recipients;
|
private final Collection<Player> recipients;
|
||||||
private final Supplier<Component> defaultChatFormat;
|
|
||||||
private String message;
|
private String message;
|
||||||
private Function<PlayerChatEvent, Component> chatFormat;
|
private Function<PlayerChatEvent, Component> chatFormat;
|
||||||
|
|
||||||
private boolean cancelled;
|
private boolean cancelled;
|
||||||
|
|
||||||
public PlayerChatEvent(@NotNull Player player, @NotNull Collection<Player> recipients,
|
public PlayerChatEvent(@NotNull Player player, @NotNull Collection<Player> recipients,
|
||||||
@NotNull Supplier<Component> defaultChatFormat,
|
@NotNull Function<PlayerChatEvent, Component> defaultChatFormat,
|
||||||
@NotNull String message) {
|
@NotNull String message) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.recipients = new ArrayList<>(recipients);
|
this.recipients = new ArrayList<>(recipients);
|
||||||
this.defaultChatFormat = defaultChatFormat;
|
this.chatFormat = defaultChatFormat;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes the chat format.
|
* 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;
|
this.chatFormat = chatFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,18 +75,13 @@ public class PlayerChatEvent implements PlayerInstanceEvent, CancellableEvent {
|
|||||||
/**
|
/**
|
||||||
* Used to retrieve the chat format for this message.
|
* Used to retrieve the chat format for this message.
|
||||||
* <p>
|
* <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;
|
return chatFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Supplier<@NotNull Component> getDefaultChatFormat() {
|
|
||||||
return defaultChatFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCancelled() {
|
public boolean isCancelled() {
|
||||||
return cancelled;
|
return cancelled;
|
||||||
|
@ -40,21 +40,12 @@ public class ChatMessageListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Collection<Player> players = CONNECTION_MANAGER.getOnlinePlayers();
|
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
|
// Call the event
|
||||||
EventDispatcher.callCancellable(playerChatEvent, () -> {
|
EventDispatcher.callCancellable(playerChatEvent, () -> {
|
||||||
final Function<PlayerChatEvent, Component> formatFunction = playerChatEvent.getChatFormatFunction();
|
final Function<PlayerChatEvent, Component> formatFunction = playerChatEvent.getChatFormatFunction();
|
||||||
|
Component textObject = formatFunction.apply(playerChatEvent);
|
||||||
Component textObject;
|
|
||||||
|
|
||||||
if (formatFunction != null) {
|
|
||||||
// Custom format
|
|
||||||
textObject = formatFunction.apply(playerChatEvent);
|
|
||||||
} else {
|
|
||||||
// Default format
|
|
||||||
textObject = playerChatEvent.getDefaultChatFormat().get();
|
|
||||||
}
|
|
||||||
|
|
||||||
final Collection<Player> recipients = playerChatEvent.getRecipients();
|
final Collection<Player> recipients = playerChatEvent.getRecipients();
|
||||||
if (!recipients.isEmpty()) {
|
if (!recipients.isEmpty()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user