Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-16 21:10:30 +01:00
Add equals/hashCode to all results and remove another result I don't like
Fixes #483
Dieser Commit ist enthalten in:
Ursprung
f4fc07768b
Commit
47a1332514
@ -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<R extends ResultedEvent.Result> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<CommandResult> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<PlayerChatEvent.ChatResult> {
|
||||
|
||||
Player player();
|
||||
|
||||
String sentMessage();
|
||||
public interface PlayerChatEvent extends ResultedEvent<GenericResult> {
|
||||
|
||||
/**
|
||||
* 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<String> 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);
|
||||
}
|
||||
|
@ -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
|
||||
+ '}';
|
||||
}
|
||||
|
@ -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<ServerPreConnectEve
|
||||
Preconditions.checkNotNull(target, "server");
|
||||
return new ServerResult(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
ServerResult that = (ServerResult) o;
|
||||
return Objects.equals(target, that.target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -175,14 +175,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
|
||||
PlayerChatEvent event = new PlayerChatEventImpl(player, msg);
|
||||
server.eventManager().fire(event)
|
||||
.thenAcceptAsync(pme -> {
|
||||
PlayerChatEventImpl.ChatResult chatResult = pme.result();
|
||||
if (chatResult.isAllowed()) {
|
||||
Optional<String> 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) -> {
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren