This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add packet-level chat handling support
- Loading branch information
1 parent
bf12d44
commit 20b5917
Showing
11 changed files
with
213 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
velocity/src/main/java/net/william278/huskchat/listener/VelocityChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.william278.huskchat.listener; | ||
|
||
import com.velocitypowered.api.event.player.PlayerChatEvent; | ||
import net.william278.huskchat.HuskChat; | ||
import net.william278.huskchat.channel.Channel; | ||
import net.william278.huskchat.message.ChatMessage; | ||
import net.william278.huskchat.user.VelocityUser; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Optional; | ||
|
||
public interface VelocityChatListener { | ||
|
||
default boolean handlePlayerChat(PlayerChatEvent e) { | ||
final VelocityUser player = VelocityUser.adapt(e.getPlayer(), plugin()); | ||
final Optional<Channel> channel = plugin().getChannels().getChannel( | ||
plugin().getUserCache().getPlayerChannel(player.getUuid()) | ||
); | ||
if (channel.isEmpty()) { | ||
plugin().getLocales().sendMessage(player, "error_no_channel"); | ||
return false; | ||
} | ||
|
||
// Send the chat message, determine if the event should be canceled | ||
return !new ChatMessage(channel.get(), player, e.getMessage(), plugin()).dispatch(); | ||
} | ||
|
||
@NotNull | ||
HuskChat plugin(); | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
velocity/src/main/java/net/william278/huskchat/listener/VelocityEventChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package net.william278.huskchat.listener; | ||
|
||
import com.velocitypowered.api.event.PostOrder; | ||
import com.velocitypowered.api.event.Subscribe; | ||
import com.velocitypowered.api.event.player.PlayerChatEvent; | ||
import net.william278.huskchat.HuskChat; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public record VelocityEventChatListener(@NotNull HuskChat plugin) implements VelocityChatListener { | ||
|
||
@Subscribe(order = PostOrder.LATE) | ||
public void onPlayerChat(PlayerChatEvent e) { | ||
if (!e.getResult().isAllowed()) { | ||
return; | ||
} | ||
if (!this.handlePlayerChat(e)) { | ||
e.setResult(PlayerChatEvent.ChatResult.denied()); | ||
} | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public HuskChat plugin() { | ||
return plugin; | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
velocity/src/main/java/net/william278/huskchat/listener/VelocityPacketChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package net.william278.huskchat.listener; | ||
|
||
import com.google.common.collect.Sets; | ||
import com.velocitypowered.api.event.AwaitingEventExecutor; | ||
import com.velocitypowered.api.event.EventTask; | ||
import com.velocitypowered.api.event.connection.DisconnectEvent; | ||
import com.velocitypowered.api.event.connection.PostLoginEvent; | ||
import com.velocitypowered.api.event.player.PlayerChatEvent; | ||
import com.velocitypowered.api.proxy.Player; | ||
import com.velocitypowered.proxy.connection.client.ConnectedPlayer; | ||
import com.velocitypowered.proxy.network.Connections; | ||
import com.velocitypowered.proxy.protocol.packet.chat.keyed.KeyedPlayerChatPacket; | ||
import com.velocitypowered.proxy.protocol.packet.chat.legacy.LegacyChatPacket; | ||
import com.velocitypowered.proxy.protocol.packet.chat.session.SessionPlayerChatPacket; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelDuplexHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPromise; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import net.william278.desertwell.util.ThrowingConsumer; | ||
import net.william278.huskchat.HuskChat; | ||
import net.william278.huskchat.VelocityHuskChat; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class VelocityPacketChatListener { | ||
private static final String KEY = "huskchat"; | ||
|
||
private final VelocityHuskChat plugin; | ||
@Getter | ||
private final Set<UUID> huskChatEntries; | ||
|
||
public VelocityPacketChatListener(@NotNull VelocityHuskChat plugin) { | ||
this.plugin = plugin; | ||
this.huskChatEntries = Sets.newConcurrentHashSet(); | ||
} | ||
|
||
public void register() { | ||
this.loadPlayers(); | ||
this.loadListeners(); | ||
} | ||
|
||
private void loadPlayers() { | ||
plugin.getServer().getAllPlayers().forEach(this::injectPlayer); | ||
} | ||
|
||
private void loadListeners() { | ||
plugin.getServer().getEventManager().register(plugin, PostLoginEvent.class, | ||
(AwaitingEventExecutor<PostLoginEvent>) postLoginEvent -> EventTask.withContinuation(continuation -> { | ||
injectPlayer(postLoginEvent.getPlayer()); | ||
continuation.resume(); | ||
})); | ||
|
||
plugin.getServer().getEventManager().register(plugin, DisconnectEvent.class, | ||
(AwaitingEventExecutor<DisconnectEvent>) disconnectEvent -> | ||
disconnectEvent.getLoginStatus() == DisconnectEvent.LoginStatus.CONFLICTING_LOGIN | ||
? null | ||
: EventTask.async(() -> removePlayer(disconnectEvent.getPlayer()))); | ||
} | ||
|
||
public void injectPlayer(@NotNull Player player) { | ||
final PlayerChannelHandler handler = new PlayerChannelHandler(plugin, player); | ||
final ConnectedPlayer connectedPlayer = (ConnectedPlayer) player; | ||
removePlayer(player); | ||
connectedPlayer.getConnection() | ||
.getChannel() | ||
.pipeline() | ||
.addBefore(Connections.HANDLER, KEY, handler); | ||
} | ||
|
||
public void removePlayer(@NotNull Player player) { | ||
final ConnectedPlayer connectedPlayer = (ConnectedPlayer) player; | ||
final Channel channel = connectedPlayer.getConnection().getChannel(); | ||
if (channel.pipeline().get(KEY) != null) { | ||
channel.pipeline().remove(KEY); | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
public static class PlayerChannelHandler extends ChannelDuplexHandler implements VelocityChatListener { | ||
|
||
private final VelocityHuskChat plugin; | ||
private final Player player; | ||
|
||
@Override | ||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { | ||
final Optional<String> message = this.extractChatMessage(msg); | ||
if (message.isEmpty()) { | ||
super.write(ctx, msg, promise); | ||
return; | ||
} | ||
this.handleChat(message.get(), (passthrough) -> super.write(ctx, msg, promise)); | ||
} | ||
|
||
@NotNull | ||
private Optional<String> extractChatMessage(Object msg) { | ||
if (msg instanceof final SessionPlayerChatPacket session) { | ||
// Handle session chat (1.19.4+) | ||
return Optional.of(session.getMessage()); | ||
} else if (msg instanceof final KeyedPlayerChatPacket keyed) { | ||
// Handle keyed chat (1.19.2-4) | ||
return Optional.of(keyed.getMessage()); | ||
} else if (msg instanceof final LegacyChatPacket legacy) { | ||
// Handle legacy chat (pre-1.19.1) | ||
return Optional.of(legacy.getMessage()); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private void handleChat(@NotNull String message, @NotNull ThrowingConsumer<Void> ifAllowed) { | ||
this.dispatchEvent(message) | ||
.thenApply(event -> event.getResult().isAllowed() && handlePlayerChat(event)) | ||
.thenAccept(allowed -> { | ||
if (allowed) { | ||
ifAllowed.accept(null); | ||
} | ||
}); | ||
} | ||
|
||
@NotNull | ||
private CompletableFuture<PlayerChatEvent> dispatchEvent(@NotNull String message) { | ||
return plugin.getServer().getEventManager().fire(new PlayerChatEvent(player, message)); | ||
} | ||
|
||
@Override | ||
@NotNull | ||
public HuskChat plugin() { | ||
return plugin; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
velocity/src/main/java/net/william278/huskchat/packet/PacketManager.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.