forked from 4drian3d/UnSignedVelocity
-
Notifications
You must be signed in to change notification settings - Fork 0
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 listeners to convert Player Chat to System Chat and …
…block Chat Header/Chat Session packets
- Loading branch information
1 parent
90cff1a
commit 928a186
Showing
7 changed files
with
196 additions
and
8 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
33 changes: 33 additions & 0 deletions
33
...in/java/io/github/_4drian3d/unsignedvelocity/listener/packet/chat/ChatHeaderListener.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,33 @@ | ||
package io.github._4drian3d.unsignedvelocity.listener.packet.chat; | ||
|
||
import com.github.retrooper.packetevents.event.PacketListenerAbstract; | ||
import com.github.retrooper.packetevents.event.PacketListenerPriority; | ||
import com.github.retrooper.packetevents.event.PacketReceiveEvent; | ||
import com.github.retrooper.packetevents.protocol.packettype.PacketType; | ||
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; | ||
import com.google.inject.Inject; | ||
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity; | ||
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener; | ||
|
||
public final class ChatHeaderListener extends PacketListenerAbstract implements LoadablePacketListener { | ||
private final UnSignedVelocity plugin; | ||
|
||
@Inject | ||
public ChatHeaderListener(UnSignedVelocity plugin) { | ||
super(PacketListenerPriority.LOWEST); | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean canBeLoaded() { | ||
return plugin.getConfiguration().blockChatHeaderPackets(); | ||
} | ||
|
||
@Override | ||
public void onPacketReceive(final PacketReceiveEvent event) { | ||
final PacketTypeCommon packetType = event.getPacketType(); | ||
if (packetType == PacketType.Play.Client.CHAT_SESSION_UPDATE) { | ||
event.setCancelled(true); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...n/java/io/github/_4drian3d/unsignedvelocity/listener/packet/chat/ChatSessionListener.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,33 @@ | ||
package io.github._4drian3d.unsignedvelocity.listener.packet.chat; | ||
|
||
import com.github.retrooper.packetevents.event.PacketListenerAbstract; | ||
import com.github.retrooper.packetevents.event.PacketListenerPriority; | ||
import com.github.retrooper.packetevents.event.PacketReceiveEvent; | ||
import com.github.retrooper.packetevents.protocol.packettype.PacketType; | ||
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; | ||
import com.google.inject.Inject; | ||
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity; | ||
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener; | ||
|
||
public final class ChatSessionListener extends PacketListenerAbstract implements LoadablePacketListener { | ||
private final UnSignedVelocity plugin; | ||
|
||
@Inject | ||
public ChatSessionListener(UnSignedVelocity plugin) { | ||
super(PacketListenerPriority.LOWEST); | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean canBeLoaded() { | ||
return plugin.getConfiguration().blockChatSessionPackets(); | ||
} | ||
|
||
@Override | ||
public void onPacketReceive(final PacketReceiveEvent event) { | ||
final PacketTypeCommon packetType = event.getPacketType(); | ||
if (packetType == PacketType.Play.Client.CHAT_SESSION_UPDATE) { | ||
event.setCancelled(true); | ||
} | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
...in/java/io/github/_4drian3d/unsignedvelocity/listener/packet/chat/ServerChatListener.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,67 @@ | ||
package io.github._4drian3d.unsignedvelocity.listener.packet.chat; | ||
|
||
import com.github.retrooper.packetevents.event.PacketListenerAbstract; | ||
import com.github.retrooper.packetevents.event.PacketListenerPriority; | ||
import com.github.retrooper.packetevents.event.PacketSendEvent; | ||
import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage; | ||
import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage_v1_19; | ||
import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage_v1_19_1; | ||
import com.github.retrooper.packetevents.protocol.chat.message.ChatMessage_v1_19_3; | ||
import com.github.retrooper.packetevents.protocol.packettype.PacketType; | ||
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; | ||
import com.github.retrooper.packetevents.protocol.player.ClientVersion; | ||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerChatMessage; | ||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSystemChatMessage; | ||
import com.google.inject.Inject; | ||
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity; | ||
import io.github._4drian3d.unsignedvelocity.listener.LoadablePacketListener; | ||
import net.kyori.adventure.text.Component; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Optional; | ||
|
||
public final class ServerChatListener extends PacketListenerAbstract implements LoadablePacketListener { | ||
private final UnSignedVelocity plugin; | ||
|
||
@Inject | ||
public ServerChatListener(UnSignedVelocity plugin) { | ||
super(PacketListenerPriority.LOWEST); | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean canBeLoaded() { | ||
return plugin.getConfiguration().convertPlayerChatToSystemChat(); | ||
} | ||
|
||
@Override | ||
public void onPacketSend(final PacketSendEvent event) { | ||
final PacketTypeCommon packetType = event.getPacketType(); | ||
if (packetType == PacketType.Play.Server.CHAT_MESSAGE) { | ||
if (event.getUser().getClientVersion().isOlderThan(ClientVersion.V_1_19)) { | ||
return; | ||
} | ||
Component messageContent = getComponentFromChatPacket(event); | ||
final WrapperPlayServerSystemChatMessage newPacket = new WrapperPlayServerSystemChatMessage(false, messageContent); | ||
event.getUser().sendPacketSilently(newPacket); | ||
event.setCancelled(true); | ||
} | ||
} | ||
|
||
private static @Nullable Component getComponentFromChatPacket(PacketSendEvent event) { | ||
final WrapperPlayServerChatMessage packet = new WrapperPlayServerChatMessage(event); | ||
ChatMessage chatMessage = packet.getMessage(); | ||
Component messageContent = chatMessage.getChatContent(); | ||
if (chatMessage instanceof ChatMessage_v1_19) { | ||
messageContent = ((ChatMessage_v1_19) chatMessage).getUnsignedChatContent(); | ||
} else if (chatMessage instanceof ChatMessage_v1_19_1) { | ||
messageContent = ((ChatMessage_v1_19_1) chatMessage).getUnsignedChatContent(); | ||
} else if (chatMessage instanceof ChatMessage_v1_19_3) { | ||
Optional<Component> unsignedChatContent = ((ChatMessage_v1_19_3) chatMessage).getUnsignedChatContent(); | ||
if (unsignedChatContent.isPresent()) { | ||
messageContent = unsignedChatContent.get(); | ||
} | ||
} | ||
return messageContent; | ||
} | ||
} |
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