Skip to content

Commit

Permalink
Bring back toast removal (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiler Sesame authored Jul 8, 2024
1 parent 378a7fb commit a7c2360
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public void onInitialize() {
try {
config = loader.load();
final boolean rewriteChat = config.node("rewrite-chat").getBoolean(true);
final boolean claimSecureChatEnforced = config.node("claim-secure-chat-enforced").getBoolean(true);
final boolean noChatReports = config.node("send-prevents-chat-reports-to-client").getBoolean(false);
loader.save(config);

handler = new FreedomHandler(
this,
rewriteChat,
claimSecureChatEnforced,
noChatReports
);
} catch (final ConfigurateException e) {
Expand Down
27 changes: 25 additions & 2 deletions fabric/src/main/java/ru/bk/oharass/freedomchat/FreedomHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.network.message.MessageType;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.ChatMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.network.packet.s2c.query.QueryResponseS2CPacket;
import net.minecraft.network.state.PlayStateFactories;
Expand All @@ -27,20 +28,23 @@ public class FreedomHandler extends MessageToByteEncoder<Packet<?>> {
private static final int STATUS_RESPONSE_PACKET_ID = 0x00;
private final PacketCodec<ByteBuf, Packet<? super ClientPlayPacketListener>> s2cPlayPacketCodec;
private final boolean rewriteChat;
private final boolean claimSecureChatEnforced;
private final boolean noChatReports;

public FreedomHandler(final FreedomChat freedom, final boolean rewriteChat, final boolean noChatReports) {
public FreedomHandler(final FreedomChat freedom, final boolean rewriteChat, final boolean claimSecureChatEnforced, final boolean noChatReports) {
final DynamicRegistryManager registryAccess = freedom.getServer().getRegistryManager();
final Function<ByteBuf, RegistryByteBuf> bufRegistryAccess = RegistryByteBuf.makeFactory(registryAccess);
this.s2cPlayPacketCodec = PlayStateFactories.S2C.bind(bufRegistryAccess).codec();
this.rewriteChat = rewriteChat;
this.claimSecureChatEnforced = claimSecureChatEnforced;
this.noChatReports = noChatReports;
}

@Override
public boolean acceptOutboundMessage(final Object msg) {
return rewriteChat && msg instanceof ChatMessageS2CPacket
|| noChatReports && msg instanceof QueryResponseS2CPacket;
|| noChatReports && msg instanceof QueryResponseS2CPacket
|| claimSecureChatEnforced && msg instanceof GameJoinS2CPacket;
}

@Override
Expand All @@ -51,6 +55,8 @@ protected void encode(final ChannelHandlerContext ctx, final Packet msg, final B
encode(ctx, packet, fbb);
} else if (msg instanceof final QueryResponseS2CPacket packet) {
encode(ctx, packet, fbb);
} else if (msg instanceof final GameJoinS2CPacket packet) {
encode(ctx, packet, fbb);
}
}

Expand All @@ -65,6 +71,23 @@ private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx,
s2cPlayPacketCodec.encode(buf, system);
}

private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx, final GameJoinS2CPacket msg, final PacketByteBuf buf) {
final GameJoinS2CPacket rewritten = new GameJoinS2CPacket(
msg.playerEntityId(),
msg.hardcore(),
msg.dimensionIds(),
msg.maxPlayers(),
msg.viewDistance(),
msg.simulationDistance(),
msg.reducedDebugInfo(),
msg.showDeathScreen(),
msg.doLimitedCrafting(),
msg.commonPlayerSpawnInfo(),
true // Enforced secure chat
);
s2cPlayPacketCodec.encode(buf, rewritten);
}

private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx, final QueryResponseS2CPacket msg, final PacketByteBuf buf) {
final ServerMetadata status = msg.metadata();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Mixin(PlayerManager.class)
public class PlayerManagerMixin {
@Inject(method = "onPlayerConnect", at = @At(value = "TAIL"))
@Inject(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;transitionInbound(Lnet/minecraft/network/NetworkState;Lnet/minecraft/network/listener/PacketListener;)V", shift = At.Shift.AFTER))
public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, CallbackInfo ci) {
final ChannelPipeline pipeline = ((ServerCommonNetworkHandlerAccess) player.networkHandler).getConnectionAccess().getChannel().pipeline();
pipeline.addAfter("packet_handler", "freedom_handler", FreedomChat.getHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void onEnable() {

final FreedomHandler handler = new FreedomHandler(
config.getBoolean("rewrite-chat", true),
config.getBoolean("claim-secure-chat-enforced", true),
config.getBoolean("send-prevents-chat-reports-to-client", false)
);

Expand Down
27 changes: 25 additions & 2 deletions paper/src/main/java/ru/bk/oharass/freedomchat/FreedomHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket;
import net.minecraft.network.protocol.game.ClientboundSystemChatPacket;
import net.minecraft.network.protocol.game.GameProtocols;
Expand All @@ -28,20 +29,23 @@ public class FreedomHandler extends MessageToByteEncoder<Packet<?>> {
private static final int STATUS_RESPONSE_PACKET_ID = 0x00;
private final StreamCodec<ByteBuf, Packet<? super ClientGamePacketListener>> s2cPlayPacketCodec;
private final boolean rewriteChat;
private final boolean claimSecureChatEnforced;
private final boolean noChatReports;

public FreedomHandler(final boolean rewriteChat, final boolean noChatReports) {
public FreedomHandler(final boolean rewriteChat, final boolean claimSecureChatEnforced, final boolean noChatReports) {
final RegistryAccess registryAccess = MinecraftServer.getServer().registryAccess();
final Function<ByteBuf, RegistryFriendlyByteBuf> bufRegistryAccess = RegistryFriendlyByteBuf.decorator(registryAccess);
this.s2cPlayPacketCodec = GameProtocols.CLIENTBOUND_TEMPLATE.bind(bufRegistryAccess).codec();
this.rewriteChat = rewriteChat;
this.claimSecureChatEnforced = claimSecureChatEnforced;
this.noChatReports = noChatReports;
}

@Override
public boolean acceptOutboundMessage(final Object msg) {
return rewriteChat && msg instanceof ClientboundPlayerChatPacket
|| noChatReports && msg instanceof ClientboundStatusResponsePacket;
|| noChatReports && msg instanceof ClientboundStatusResponsePacket
|| claimSecureChatEnforced && msg instanceof ClientboundLoginPacket;
}

@Override
Expand All @@ -52,6 +56,8 @@ protected void encode(final ChannelHandlerContext ctx, final Packet msg, final B
encode(ctx, packet, fbb);
} else if (msg instanceof final ClientboundStatusResponsePacket packet) {
encode(ctx, packet, fbb);
} else if (msg instanceof final ClientboundLoginPacket packet) {
encode(ctx, packet, fbb);
}
}

Expand All @@ -66,6 +72,23 @@ private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx,
s2cPlayPacketCodec.encode(buf, system);
}

private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx, final ClientboundLoginPacket msg, final FriendlyByteBuf buf) {
final ClientboundLoginPacket rewritten = new ClientboundLoginPacket(
msg.playerId(),
msg.hardcore(),
msg.levels(),
msg.maxPlayers(),
msg.chunkRadius(),
msg.simulationDistance(),
msg.reducedDebugInfo(),
msg.showDeathScreen(),
msg.doLimitedCrafting(),
msg.commonPlayerSpawnInfo(),
true // Enforced secure chat
);
s2cPlayPacketCodec.encode(buf, rewritten);
}

private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx, final ClientboundStatusResponsePacket msg, final FriendlyByteBuf buf) {
final ServerStatus status = msg.status();

Expand Down
8 changes: 8 additions & 0 deletions paper/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
# system messages. This is what makes chat not reportable.
rewrite-chat: true

# Whether FreedomChat should claim to clients that secure chat is enforced.
# With this set to true, the "Chat messages can't be verified" toast will not
# be shown. This is, in default configurations, unrelated to allowing clients
# not signing their messages join. In modern versions, clients also
# require a valid access token to be present for the toast to be hidden.
# That being said, you may still see the toast even if this option is enabled.
claim-secure-chat-enforced: true

# Whether to report the server as secure (disabling chat reporting) to the
# NoChatReports client mod. This displays a green icon on the server list
# screen and if enforce-secure-profiles is disabled the open chat screen
Expand Down
2 changes: 1 addition & 1 deletion paper/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ authors:
- "ocelotpotpie"
- "pop4959"
website: "https://github.com/ocelotpotpie/FreedomChat"
api-version: "1.20"
api-version: "1.21"
folia-supported: true

3 comments on commit a7c2360

@Kichura
Copy link

@Kichura Kichura commented on a7c2360 Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be backported to 1.20.5/6 just in case but as 1.6.1 in all honesty. (1.21 version can be 1.7.0)

@pop4959
Copy link
Collaborator

@pop4959 pop4959 commented on a7c2360 Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a terrible idea to backport, but I have limited time right now. Should be easy if anyone's willing to pick that up.

@pop4959
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone is still interested in a release for 1.20.6 you can find it on the actions build for the backport/1.20.6 branch.

Please sign in to comment.