Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-17 05:20:14 +01:00
Add chat event
Dieser Commit ist enthalten in:
Ursprung
db5645f80e
Commit
7598918d93
@ -111,4 +111,45 @@ public interface ResultedEvent<R extends ResultedEvent.Result> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
package com.velocitypowered.api.event.player;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.velocitypowered.api.event.ResultedEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
private final Player player;
|
||||
private final String message;
|
||||
private ChatResult result;
|
||||
|
||||
public PlayerChatEvent(Player player, String message) {
|
||||
this.player = Preconditions.checkNotNull(player, "player");
|
||||
this.message = Preconditions.checkNotNull(message, "message");
|
||||
this.result = (ChatResult) ChatResult.allowed();
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatResult getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResult(@NonNull ChatResult result) {
|
||||
this.result = Preconditions.checkNotNull(result, "result");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PlayerChatEvent{" +
|
||||
"player=" + player +
|
||||
", message=" + message +
|
||||
", result=" + result +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
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.PluginMessageEvent;
|
||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
|
||||
import com.velocitypowered.proxy.VelocityServer;
|
||||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
|
||||
@ -13,8 +15,10 @@ import com.velocitypowered.proxy.protocol.packet.*;
|
||||
import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
|
||||
import com.velocitypowered.proxy.util.ThrowableUtils;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.kyori.text.Component;
|
||||
import net.kyori.text.TextComponent;
|
||||
import net.kyori.text.format.TextColor;
|
||||
import net.kyori.text.serializer.ComponentSerializers;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@ -93,7 +97,18 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
PlayerChatEvent event = new PlayerChatEvent(player, msg);
|
||||
server.getEventManager().fire(event)
|
||||
.thenAcceptAsync(pme -> {
|
||||
if (pme.getResult().equals(ResultedEvent.ChatResult.allowed())){
|
||||
player.getConnectedServer().getMinecraftConnection().write(chat);
|
||||
} else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){
|
||||
Chat modifiedChat = new Chat();
|
||||
modifiedChat.setType(Chat.CHAT);
|
||||
modifiedChat.setMessage(pme.getResult().getMessage().get());
|
||||
player.getConnectedServer().getMinecraftConnection().write(modifiedChat);
|
||||
}
|
||||
}, player.getConnectedServer().getMinecraftConnection().getChannel().eventLoop());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren