3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-17 05:20:14 +01:00

Add equals/hashCode to all results and remove another result I don't like

Fixes #483
Dieser Commit ist enthalten in:
Andrew Steinborn 2021-05-11 06:52:46 -04:00
Ursprung f4fc07768b
Commit 47a1332514
7 geänderte Dateien mit 155 neuen und 80 gelöschten Zeilen

Datei anzeigen

@ -8,6 +8,7 @@
package com.velocitypowered.api.event; package com.velocitypowered.api.event;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
@ -122,5 +123,22 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
Preconditions.checkNotNull(reason, "reason"); Preconditions.checkNotNull(reason, "reason");
return new ComponentResult(false, 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);
}
} }
} }

Datei anzeigen

@ -11,6 +11,7 @@ import com.google.common.base.Preconditions;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -110,6 +111,24 @@ public interface CommandExecuteEvent extends ResultedEvent<CommandResult> {
Preconditions.checkNotNull(newCommand, "newCommand"); Preconditions.checkNotNull(newCommand, "newCommand");
return new CommandResult(true, false, 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);
}
} }
} }

Datei anzeigen

@ -11,6 +11,7 @@ import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.connection.Player; import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -105,6 +106,23 @@ public interface KickedFromServerEvent extends
public static DisconnectPlayer create(Component reason) { public static DisconnectPlayer create(Component reason) {
return new DisconnectPlayer(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) { public static ServerKickResult create(RegisteredServer server) {
return new RedirectPlayer(server, null); 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) { public static Notify create(Component message) {
return new Notify(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);
}
} }
} }

Datei anzeigen

@ -7,76 +7,37 @@
package com.velocitypowered.api.event.player; package com.velocitypowered.api.event.player;
import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.ResultedEvent.GenericResult;
import com.velocitypowered.api.proxy.connection.Player; 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> { public interface PlayerChatEvent extends ResultedEvent<GenericResult> {
Player player();
String sentMessage();
/** /**
* 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; * Sets a new message to send, if the message is allowed to be sent.
this.message = message; *
} * @param currentMessage the message to send instead of the current (or original) message
*/
public Optional<String> modifiedMessage() { void setCurrentMessage(String currentMessage);
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);
}
}
} }

Datei anzeigen

@ -16,8 +16,9 @@ import com.velocitypowered.api.proxy.connection.Player;
public final class PlayerChatEventImpl implements PlayerChatEvent { public final class PlayerChatEventImpl implements PlayerChatEvent {
private final Player player; private final Player player;
private final String message; private final String originalMessage;
private ChatResult result; private String currentMessage;
private GenericResult result;
/** /**
* Constructs a PlayerChatEvent. * Constructs a PlayerChatEvent.
@ -26,8 +27,9 @@ public final class PlayerChatEventImpl implements PlayerChatEvent {
*/ */
public PlayerChatEventImpl(Player player, String message) { public PlayerChatEventImpl(Player player, String message) {
this.player = Preconditions.checkNotNull(player, "player"); this.player = Preconditions.checkNotNull(player, "player");
this.message = Preconditions.checkNotNull(message, "message"); this.originalMessage = Preconditions.checkNotNull(message, "message");
this.result = ChatResult.allowed(); this.result = GenericResult.allowed();
this.currentMessage = message;
} }
@Override @Override
@ -36,17 +38,27 @@ public final class PlayerChatEventImpl implements PlayerChatEvent {
} }
@Override @Override
public String sentMessage() { public String originalMessage() {
return message; return originalMessage;
} }
@Override @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; return result;
} }
@Override @Override
public void setResult(ChatResult result) { public void setResult(GenericResult result) {
this.result = Preconditions.checkNotNull(result, "result"); this.result = Preconditions.checkNotNull(result, "result");
} }
@ -54,7 +66,7 @@ public final class PlayerChatEventImpl implements PlayerChatEvent {
public String toString() { public String toString() {
return "PlayerChatEvent{" return "PlayerChatEvent{"
+ "player=" + player + "player=" + player
+ ", message=" + message + ", message=" + originalMessage
+ ", result=" + result + ", result=" + result
+ '}'; + '}';
} }

Datei anzeigen

@ -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;
import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder.Status; import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder.Status;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -92,5 +93,22 @@ public interface ServerPreConnectEvent extends ResultedEvent<ServerPreConnectEve
Preconditions.checkNotNull(target, "server"); Preconditions.checkNotNull(target, "server");
return new ServerResult(target); 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);
}
} }
} }

Datei anzeigen

@ -175,14 +175,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
PlayerChatEvent event = new PlayerChatEventImpl(player, msg); PlayerChatEvent event = new PlayerChatEventImpl(player, msg);
server.eventManager().fire(event) server.eventManager().fire(event)
.thenAcceptAsync(pme -> { .thenAcceptAsync(pme -> {
PlayerChatEventImpl.ChatResult chatResult = pme.result(); if (pme.result().isAllowed()) {
if (chatResult.isAllowed()) { smc.write(new ServerboundChatPacket(pme.currentMessage()));
Optional<String> eventMsg = pme.result().modifiedMessage();
if (eventMsg.isPresent()) {
smc.write(new ServerboundChatPacket(eventMsg.get()));
} else {
smc.write(packet);
}
} }
}, smc.eventLoop()) }, smc.eventLoop())
.exceptionally((ex) -> { .exceptionally((ex) -> {