diff --git a/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java b/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java index fd889a19b..84fbc1aa0 100644 --- a/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/ResultedEvent.java @@ -8,6 +8,7 @@ package com.velocitypowered.api.event; import com.google.common.base.Preconditions; +import java.util.Objects; import java.util.Optional; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; @@ -122,5 +123,22 @@ public interface ResultedEvent { Preconditions.checkNotNull(reason, "reason"); return new ComponentResult(false, reason); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ComponentResult that = (ComponentResult) o; + return status == that.status && Objects.equals(reason, that.reason); + } + + @Override + public int hashCode() { + return Objects.hash(status, reason); + } } } diff --git a/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java b/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java index a799986fe..9381721c6 100644 --- a/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/command/CommandExecuteEvent.java @@ -11,6 +11,7 @@ import com.google.common.base.Preconditions; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; +import java.util.Objects; import java.util.Optional; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -110,6 +111,24 @@ public interface CommandExecuteEvent extends ResultedEvent { Preconditions.checkNotNull(newCommand, "newCommand"); return new CommandResult(true, false, newCommand); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CommandResult that = (CommandResult) o; + return status == that.status && forward == that.forward + && Objects.equals(command, that.command); + } + + @Override + public int hashCode() { + return Objects.hash(command, status, forward); + } } } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/KickedFromServerEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/KickedFromServerEvent.java index 0d098a08e..83ac0637b 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/KickedFromServerEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/KickedFromServerEvent.java @@ -11,6 +11,7 @@ import com.google.common.base.Preconditions; import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.server.RegisteredServer; +import java.util.Objects; import java.util.Optional; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; @@ -105,6 +106,23 @@ public interface KickedFromServerEvent extends public static DisconnectPlayer create(Component reason) { return new DisconnectPlayer(reason); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DisconnectPlayer that = (DisconnectPlayer) o; + return Objects.equals(message, that.message); + } + + @Override + public int hashCode() { + return Objects.hash(message); + } } /** @@ -149,6 +167,24 @@ public interface KickedFromServerEvent extends public static ServerKickResult create(RegisteredServer server) { return new RedirectPlayer(server, null); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + RedirectPlayer that = (RedirectPlayer) o; + return Objects.equals(message, that.message) && Objects + .equals(server, that.server); + } + + @Override + public int hashCode() { + return Objects.hash(message, server); + } } /** @@ -182,5 +218,22 @@ public interface KickedFromServerEvent extends public static Notify create(Component message) { return new Notify(message); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Notify notify = (Notify) o; + return Objects.equals(message, notify.message); + } + + @Override + public int hashCode() { + return Objects.hash(message); + } } } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java index a8b390bd9..2d26b9a9c 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEvent.java @@ -7,76 +7,37 @@ package com.velocitypowered.api.event.player; -import com.google.common.base.Preconditions; import com.velocitypowered.api.event.ResultedEvent; +import com.velocitypowered.api.event.ResultedEvent.GenericResult; import com.velocitypowered.api.proxy.connection.Player; -import java.util.Optional; -import org.checkerframework.checker.nullness.qual.NonNull; -import org.checkerframework.checker.nullness.qual.Nullable; -public interface PlayerChatEvent extends ResultedEvent { - - Player player(); - - String sentMessage(); +public interface PlayerChatEvent extends ResultedEvent { /** - * Represents the result of the {@link PlayerChatEvent}. + * Returns the player sending the message. + * + * @return the player sending the message */ - final class ChatResult implements Result { + Player player(); - private static final ChatResult ALLOWED = new ChatResult(true, null); - private static final ChatResult DENIED = new ChatResult(false, null); + /** + * Returns the message the player originally sent. + * + * @return the message the player originally sent + */ + String originalMessage(); - private @Nullable String message; - private final boolean status; + /** + * Returns the message currently being sent, which may be modified by plugins. + * + * @return the message currently being sent + */ + String currentMessage(); - private ChatResult(boolean status, @Nullable String message) { - this.status = status; - this.message = message; - } - - public Optional modifiedMessage() { - return Optional.ofNullable(message); - } - - @Override - public boolean isAllowed() { - return status; - } - - @Override - public String toString() { - return status ? "allowed" : "denied"; - } - - /** - * Allows the message to be sent, without modification. - * - * @return the allowed result - */ - public static ChatResult allowed() { - return ALLOWED; - } - - /** - * Prevents the message from being sent. - * - * @return the denied result - */ - public static ChatResult denied() { - return DENIED; - } - - /** - * Allows the message to be sent, but silently replaced with another. - * - * @param message the message to use instead - * @return a result with a new message - */ - public static ChatResult replaceMessage(@NonNull String message) { - Preconditions.checkNotNull(message, "message"); - return new ChatResult(true, message); - } - } + /** + * Sets a new message to send, if the message is allowed to be sent. + * + * @param currentMessage the message to send instead of the current (or original) message + */ + void setCurrentMessage(String currentMessage); } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEventImpl.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEventImpl.java index 60eb99447..eacd14740 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEventImpl.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChatEventImpl.java @@ -16,8 +16,9 @@ import com.velocitypowered.api.proxy.connection.Player; public final class PlayerChatEventImpl implements PlayerChatEvent { private final Player player; - private final String message; - private ChatResult result; + private final String originalMessage; + private String currentMessage; + private GenericResult result; /** * Constructs a PlayerChatEvent. @@ -26,8 +27,9 @@ public final class PlayerChatEventImpl implements PlayerChatEvent { */ public PlayerChatEventImpl(Player player, String message) { this.player = Preconditions.checkNotNull(player, "player"); - this.message = Preconditions.checkNotNull(message, "message"); - this.result = ChatResult.allowed(); + this.originalMessage = Preconditions.checkNotNull(message, "message"); + this.result = GenericResult.allowed(); + this.currentMessage = message; } @Override @@ -36,17 +38,27 @@ public final class PlayerChatEventImpl implements PlayerChatEvent { } @Override - public String sentMessage() { - return message; + public String originalMessage() { + return originalMessage; } @Override - public ChatResult result() { + public String currentMessage() { + return currentMessage; + } + + @Override + public void setCurrentMessage(String currentMessage) { + this.currentMessage = Preconditions.checkNotNull(currentMessage, "currentMessage"); + } + + @Override + public GenericResult result() { return result; } @Override - public void setResult(ChatResult result) { + public void setResult(GenericResult result) { this.result = Preconditions.checkNotNull(result, "result"); } @@ -54,7 +66,7 @@ public final class PlayerChatEventImpl implements PlayerChatEvent { public String toString() { return "PlayerChatEvent{" + "player=" + player - + ", message=" + message + + ", message=" + originalMessage + ", result=" + result + '}'; } diff --git a/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java index bd5cc7daf..343693f1b 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/ServerPreConnectEvent.java @@ -13,6 +13,7 @@ import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder.Status; import com.velocitypowered.api.proxy.server.RegisteredServer; +import java.util.Objects; import java.util.Optional; import org.checkerframework.checker.nullness.qual.Nullable; @@ -92,5 +93,22 @@ public interface ServerPreConnectEvent extends ResultedEvent { - PlayerChatEventImpl.ChatResult chatResult = pme.result(); - if (chatResult.isAllowed()) { - Optional eventMsg = pme.result().modifiedMessage(); - if (eventMsg.isPresent()) { - smc.write(new ServerboundChatPacket(eventMsg.get())); - } else { - smc.write(packet); - } + if (pme.result().isAllowed()) { + smc.write(new ServerboundChatPacket(pme.currentMessage())); } }, smc.eventLoop()) .exceptionally((ex) -> {