Skip to content

fix(api): add UserChatEvent #isCancellable and #isModifiable #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,90 @@ public final class UserChatEvent extends AbstractCancellable implements UserEven

private final @NotNull User user;
private @NotNull String message;
private final boolean cancellable;
private final boolean modifiable;

/**
* User chat event constructor.
*
* @param user User that sent the message.
* @param message Message that the user attempted to send.
* @param cancelled Whether this event is cancelled.
* @param user User that sent the message.
* @param message Message that the user attempted to send.
* @param cancelled Whether this event is cancelled.
* @param cancellable Whether this event can be cancelled on this platform.
* @param modifiable Whether this event can be modified on this platform.
*/
public UserChatEvent(@NotNull User user, @NotNull String message, boolean cancelled) {
public UserChatEvent(@NotNull User user, @NotNull String message, boolean cancelled, boolean cancellable, boolean modifiable) {
super(cancelled);
this.user = user;
this.message = message;
this.cancellable = cancellable;
this.modifiable = modifiable;
}

/**
* Get the user who sent this message.
* Returns the user who sent this message.
*
* @return the user who sent this message.
* @return message sender.
*/
@Override
public @NotNull User getUser() {
return this.user;
}

/**
* Get the message that was sent.
* Returns the message that was sent.
*
* @return the message.
* @return message.
*/
public @NotNull String getMessage() {
return this.message;
}

/**
* Sets the message that was sent.
* <p>Due to recent Minecraft changes and the addition of signed message, this may not work
* properly on all platforms and versions.</p>
* Changes the chat message.
* <p>Due to changes in Minecraft 1.19.1+, some platforms no longer support modifying the chat
* message. If the current platform does not support changing the chat message, no changes will
* be made.</p>
*
* @param message New message that will be sent.
* @param message New message.
*/
public void setMessage(@NotNull String message) {
if (!this.modifiable) {
return;
}
this.message = message;
}

/**
* Returns whether this event can be cancelled.
* <p>Due to changes in Minecraft 1.19.1+, some platforms no longer support cancelling signed
* chat packets. If the current platform does not support cancelling this event, this will
* return {@code false}.</p>
*
* <p>If this method returns {@code false}, but the event is cancelled, the event will not be
* cancelled on the platform to prevent problems from occurring.</p>
*
* @return {@code true} if this chat event can be cancelled on the underlying platform,
* otherwise {@code false}.
*/
public boolean isCancellable() {
return this.cancellable;
}

/**
* Returns whether this event can be modified.
* <p>Due to changes in Minecraft 1.19.1+, some platforms no longer support modifying the chat
* message. If the current platform does not support changing the chat message for this event,
* this will return {@code false}.</p>
*
* <p>If this method returns {@code false}, but the chat message is modified, the event's chat
* message will not be changed on the platform to prevent problems from occurring.</p>
*
* @return {@code true} if this chat event can be modified on the underlying platform, otherwise
* {@code false}.
*/
public boolean isModifiable() {
return this.modifiable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public void onPlayerJoinEvent(@NotNull PlayerJoinEvent event) {
public void onAsyncPlayerChatEvent(@NotNull AsyncPlayerChatEvent event) {
UserChatEvent chameleonEvent = new UserChatEvent(
this.userManager.wrap(event.getPlayer()),
event.getMessage(), event.isCancelled()
event.getMessage(), event.isCancelled(),
true, true
);
this.chameleon.getEventBus().dispatch(chameleonEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public void onPostLoginEvent(@NotNull PostLoginEvent event) {
public void onChatEvent(@NotNull ChatEvent event) {
UserChatEvent chameleonEvent = new UserChatEvent(
wrap((ProxiedPlayer) event.getSender()),
event.getMessage(),
event.isCancelled()
event.getMessage(), event.isCancelled(),
true, true
);
this.chameleon.getEventBus().dispatch(chameleonEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public void onPlayerJoinEvent(@NotNull PlayerJoinEvent event) {
public void onPlayerChatEvent(@NotNull PlayerChatEvent event) {
UserChatEvent chameleonEvent = new UserChatEvent(
this.chameleon.getUserManager().wrap(event.getPlayer()),
event.getMessage(), event.isCancelled()
event.getMessage(), event.isCancelled(),
true, true
);
this.chameleon.getEventBus().dispatch(chameleonEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public void onChatEvent(@NotNull PlayerChatEvent event) {
}

UserChatEvent chameleonEvent = new UserChatEvent(
this.chameleon.getUserManager().wrap(sender), serialized, event.isCancelled());
this.chameleon.getUserManager().wrap(sender),
serialized, event.isCancelled(),
true, true
);
this.chameleon.getEventBus().dispatch(chameleonEvent);

if (!serialized.equals(chameleonEvent.getMessage())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent.ChatResult;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import dev.hypera.chameleon.event.common.UserChatEvent;
import dev.hypera.chameleon.event.common.UserConnectEvent;
Expand Down Expand Up @@ -83,19 +82,22 @@ public void onPostLoginEvent(@NotNull PostLoginEvent event) {
*/
@Subscribe
public void onChatEvent(@NotNull PlayerChatEvent event) {
boolean immutable = event.getPlayer().getProtocolVersion().getProtocol() >= 760;
UserChatEvent chameleonEvent = new UserChatEvent(
this.chameleon.getUserManager().wrap(event.getPlayer()),
event.getMessage(),
!event.getResult().isAllowed()
!event.getResult().isAllowed(),
immutable, immutable
);
this.chameleon.getEventBus().dispatch(chameleonEvent);

if (!event.getMessage().equals(chameleonEvent.getMessage()) &&
catchChatModification(event.getPlayer(), false)) {
if (immutable) {
return;
}
if (!event.getMessage().equals(chameleonEvent.getMessage())) {
event.setResult(ChatResult.message(chameleonEvent.getMessage()));
}

if (chameleonEvent.isCancelled() && catchChatModification(event.getPlayer(), true)) {
if (chameleonEvent.isCancelled()) {
event.setResult(ChatResult.denied());
}
}
Expand Down Expand Up @@ -125,26 +127,6 @@ public void onServerSwitchEvent(@NotNull ServerConnectedEvent event) {
));
}

private boolean catchChatModification(@NotNull Player player, boolean cancel) {
if (player.getProtocolVersion().getProtocol() >= 760) {
this.chameleon.getInternalLogger().error(
"Failed to %s a chat message for a player using 1.19.1 or above, doing so "
+ "may result in Velocity throwing an exception and the sender being disconnected.",
cancel ? "cancel" : "modify"
);
this.chameleon.getInternalLogger().error(
"This IS NOT a bug, but rather an intentional change in Velocity caused by"
+ "changes in Minecraft 1.19.1."
);
this.chameleon.getInternalLogger().error(
"See https://github.com/PaperMC/Velocity/issues/804 for more information."
);
return false;
} else {
return true;
}
}

private @NotNull Server wrap(@NotNull RegisteredServer server) {
return new VelocityServer(this.chameleon, server);
}
Expand Down