13
0
geforkt von Mirrors/Velocity

Merge pull request #102 from thomas15v/feature/chat-event

Add chat event
Dieser Commit ist enthalten in:
Andrew Steinborn 2018-09-26 14:05:01 -04:00 committet von GitHub
Commit 2f8c2af4ec
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 107 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,97 @@
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;
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.
*/
public class PlayerChatEvent implements ResultedEvent<PlayerChatEvent.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.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 +
'}';
}
/**
* 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

@ -2,6 +2,7 @@ package com.velocitypowered.proxy.connection.client;
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;
@ -93,7 +94,15 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
return;
}
} else {
PlayerChatEvent event = new PlayerChatEvent(player, msg);
server.getEventManager().fire(event)
.thenAcceptAsync(pme -> {
if (pme.getResult().equals(PlayerChatEvent.ChatResult.allowed())){
player.getConnectedServer().getMinecraftConnection().write(chat);
} else if (pme.getResult().isAllowed() && pme.getResult().getMessage().isPresent()){
player.getConnectedServer().getMinecraftConnection().write(Chat.createServerbound(pme.getResult().getMessage().get()));
}
}, player.getConnectedServer().getMinecraftConnection().getChannel().eventLoop());
}
return;
}