13
0
geforkt von Mirrors/Velocity

Move ChatResult to PlayerChatEvent and don't call event on spoofChatInput()

Dieser Commit ist enthalten in:
Thomas Vanmellaerts 2018-09-26 19:47:38 +02:00
Ursprung d71f863045
Commit 3acc00de5e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: E1E6E9CCAD13E556
5 geänderte Dateien mit 51 neuen und 53 gelöschten Zeilen

Datei anzeigen

@ -111,45 +111,4 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
return new ComponentResult(false, reason); return new ComponentResult(false, reason);
} }
} }
class ChatResult implements Result {
private static final ChatResult ALLOWED = new ChatResult(true, null);
private static final ChatResult DENIED = new ChatResult(false, null);
// The server can not accept formatted text from clients!
private @Nullable String message;
private final boolean allowed;
protected ChatResult(boolean allowed, @Nullable String message) {
this.allowed = allowed;
this.message = message;
}
@Override
public boolean isAllowed() {
return allowed;
}
@Override
public String toString() {
return allowed ? "allowed" : "denied";
}
public static ChatResult allowed() {
return ALLOWED;
}
public static ChatResult denied() {
return DENIED;
}
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}
public static ChatResult message(@NonNull String message) {
Preconditions.checkNotNull(message, "message");
return new ChatResult(true, message);
}
}
} }

Datei anzeigen

@ -4,11 +4,14 @@ import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Optional;
/** /**
* This event is fired once the player has been authenticated but before they connect to a server on the proxy. * This event is fired once the player has been authenticated but before they connect to a server on the proxy.
*/ */
public class PlayerChatEvent implements ResultedEvent<ResultedEvent.ChatResult> { public class PlayerChatEvent implements ResultedEvent<PlayerChatEvent.ChatResult> {
private final Player player; private final Player player;
private final String message; private final String message;
private ChatResult result; private ChatResult result;
@ -16,7 +19,7 @@ public class PlayerChatEvent implements ResultedEvent<ResultedEvent.ChatResult>
public PlayerChatEvent(Player player, String message) { public PlayerChatEvent(Player player, String message) {
this.player = Preconditions.checkNotNull(player, "player"); this.player = Preconditions.checkNotNull(player, "player");
this.message = Preconditions.checkNotNull(message, "message"); this.message = Preconditions.checkNotNull(message, "message");
this.result = (ChatResult) ChatResult.allowed(); this.result = ChatResult.allowed();
} }
public Player getPlayer() { public Player getPlayer() {
@ -46,5 +49,49 @@ public class PlayerChatEvent implements ResultedEvent<ResultedEvent.ChatResult>
'}'; '}';
} }
/**
* Represents the result of the {@link PlayerChatEvent}.
*/
public static class ChatResult implements ResultedEvent.Result {
private static final ChatResult ALLOWED = new ChatResult(true, null);
private static final ChatResult DENIED = new ChatResult(false, null);
// The server can not accept formatted text from clients!
private @Nullable String message;
private final boolean allowed;
protected ChatResult(boolean allowed, @Nullable String message) {
this.allowed = allowed;
this.message = message;
}
@Override
public boolean isAllowed() {
return allowed;
}
@Override
public String toString() {
return allowed ? "allowed" : "denied";
}
public static ChatResult allowed() {
return ALLOWED;
}
public static ChatResult denied() {
return DENIED;
}
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}
public static ChatResult message(@NonNull String message) {
Preconditions.checkNotNull(message, "message");
return new ChatResult(true, message);
}
}
} }

Datei anzeigen

@ -1,9 +1,7 @@
package com.velocitypowered.proxy.connection.client; package com.velocitypowered.proxy.connection.client;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
@ -16,10 +14,8 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import com.velocitypowered.proxy.util.EventUtil; import com.velocitypowered.proxy.util.EventUtil;
import com.velocitypowered.proxy.util.ThrowableUtils; import com.velocitypowered.proxy.util.ThrowableUtils;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.kyori.text.Component;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
import net.kyori.text.format.TextColor; import net.kyori.text.format.TextColor;
import net.kyori.text.serializer.ComponentSerializers;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;

Datei anzeigen

@ -29,7 +29,6 @@ import com.velocitypowered.proxy.connection.util.ConnectionRequestResults;
import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolConstants;
import com.velocitypowered.proxy.protocol.packet.*; import com.velocitypowered.proxy.protocol.packet.*;
import com.velocitypowered.proxy.server.VelocityRegisteredServer; import com.velocitypowered.proxy.server.VelocityRegisteredServer;
import com.velocitypowered.proxy.util.EventUtil;
import com.velocitypowered.proxy.util.ThrowableUtils; import com.velocitypowered.proxy.util.ThrowableUtils;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
@ -421,7 +420,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
@Override @Override
public void spoofChatInput(String input) { public void spoofChatInput(String input) {
Preconditions.checkArgument(input.length() <= Chat.MAX_SERVERBOUND_MESSAGE_LENGTH, "input cannot be greater than " + Chat.MAX_SERVERBOUND_MESSAGE_LENGTH + " characters in length"); Preconditions.checkArgument(input.length() <= Chat.MAX_SERVERBOUND_MESSAGE_LENGTH, "input cannot be greater than " + Chat.MAX_SERVERBOUND_MESSAGE_LENGTH + " characters in length");
EventUtil.callPlayerChatEvent(server, this, input, Chat.createServerbound(input)); connectedServer.getMinecraftConnection().write(Chat.createServerbound(input));
} }
private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder { private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder {

Datei anzeigen

@ -1,20 +1,17 @@
package com.velocitypowered.proxy.util; package com.velocitypowered.proxy.util;
import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import com.velocitypowered.proxy.protocol.packet.Chat; import com.velocitypowered.proxy.protocol.packet.Chat;
import javax.annotation.Nullable;
public class EventUtil { public class EventUtil {
public static void callPlayerChatEvent(VelocityServer server, ConnectedPlayer player, String message, Chat original){ public static void callPlayerChatEvent(VelocityServer server, ConnectedPlayer player, String message, Chat original){
PlayerChatEvent event = new PlayerChatEvent(player, message); PlayerChatEvent event = new PlayerChatEvent(player, message);
server.getEventManager().fire(event) server.getEventManager().fire(event)
.thenAcceptAsync(pme -> { .thenAcceptAsync(pme -> {
if (pme.getResult().equals(ResultedEvent.ChatResult.allowed())){ if (pme.getResult().equals(PlayerChatEvent.ChatResult.allowed())){
player.getConnectedServer().getMinecraftConnection().write(original); player.getConnectedServer().getMinecraftConnection().write(original);
} else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){ } else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){
player.getConnectedServer().getMinecraftConnection().write(Chat.createServerbound(pme.getResult().getMessage().get())); player.getConnectedServer().getMinecraftConnection().write(Chat.createServerbound(pme.getResult().getMessage().get()));