Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public void onInitialize() {
final boolean rewriteChat = config.node("rewrite-chat").getBoolean(true);
final boolean claimSecureChatEnforced = config.node("claim-secure-chat-enforced").getBoolean(false);
final boolean noChatReports = config.node("send-prevents-chat-reports-to-client").getBoolean(false);
final boolean bedrockOnly = config.node("rewrite-bedrock-only").getBoolean(false);
loader.save(config);

handler = new FreedomHandler(
this,
rewriteChat,
claimSecureChatEnforced,
noChatReports
noChatReports,
bedrockOnly
);
} catch (final ConfigurateException e) {
logger.error("An error occurred while loading this configuration: " + e.getMessage());
Expand Down
12 changes: 10 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 @@ -21,6 +21,7 @@
import ru.bk.oharass.freedomchat.rewrite.CustomServerMetadata;

import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;

@ChannelHandler.Sharable
Expand All @@ -30,19 +31,21 @@ public class FreedomHandler extends MessageToByteEncoder<Packet<?>> {
private final boolean rewriteChat;
private final boolean claimSecureChatEnforced;
private final boolean noChatReports;
private final boolean bedrockOnly;

public FreedomHandler(final FreedomChat freedom, final boolean rewriteChat, final boolean claimSecureChatEnforced, final boolean noChatReports) {
public FreedomHandler(final FreedomChat freedom, final boolean rewriteChat, final boolean claimSecureChatEnforced, final boolean noChatReports, final boolean bedrockOnly) {
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;
this.bedrockOnly = bedrockOnly;
}

@Override
public boolean acceptOutboundMessage(final Object msg) {
return rewriteChat && msg instanceof ChatMessageS2CPacket
return (rewriteChat && msg instanceof ChatMessageS2CPacket packet && (!bedrockOnly || isBedrockPlayer(packet.sender())))
|| noChatReports && msg instanceof QueryResponseS2CPacket
|| claimSecureChatEnforced && msg instanceof GameJoinS2CPacket;
}
Expand Down Expand Up @@ -103,4 +106,9 @@ private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx,
buf.writeVarInt(STATUS_RESPONSE_PACKET_ID);
buf.encodeAsJson(CustomServerMetadata.CODEC, customStatus);
}

private boolean isBedrockPlayer(final UUID uuid) {
// Bedrock players use a nil uuid bit masked with their xbox user id (xuid). The version is always zero.
return uuid.version() == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void onEnable() {
final FreedomHandler handler = new FreedomHandler(
config.getBoolean("rewrite-chat", true),
config.getBoolean("claim-secure-chat-enforced", false),
config.getBoolean("send-prevents-chat-reports-to-client", false)
config.getBoolean("send-prevents-chat-reports-to-client", false),
config.getBoolean("rewrite-bedrock-only", false)
);

addListener(listenerKey, channel -> channel.pipeline().addAfter("packet_handler", "freedom_handler", handler));
Expand Down
12 changes: 10 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 @@ -22,6 +22,7 @@
import ru.bk.oharass.freedomchat.rewrite.CustomServerMetadata;

import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;

@ChannelHandler.Sharable
Expand All @@ -31,19 +32,21 @@ public class FreedomHandler extends MessageToByteEncoder<Packet<?>> {
private final boolean rewriteChat;
private final boolean claimSecureChatEnforced;
private final boolean noChatReports;
private final boolean bedrockOnly;

public FreedomHandler(final boolean rewriteChat, final boolean claimSecureChatEnforced, final boolean noChatReports) {
public FreedomHandler(final boolean rewriteChat, final boolean claimSecureChatEnforced, final boolean noChatReports, final boolean bedrockOnly) {
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;
this.bedrockOnly = bedrockOnly;
}

@Override
public boolean acceptOutboundMessage(final Object msg) {
return rewriteChat && msg instanceof ClientboundPlayerChatPacket
return (rewriteChat && msg instanceof ClientboundPlayerChatPacket packet && (!bedrockOnly || isBedrockPlayer(packet.sender())))
|| noChatReports && msg instanceof ClientboundStatusResponsePacket
|| claimSecureChatEnforced && msg instanceof ClientboundLoginPacket;
}
Expand Down Expand Up @@ -104,4 +107,9 @@ private void encode(@SuppressWarnings("unused") final ChannelHandlerContext ctx,
buf.writeVarInt(STATUS_RESPONSE_PACKET_ID);
buf.writeJsonWithCodec(CustomServerMetadata.CODEC, customStatus);
}

private boolean isBedrockPlayer(final UUID uuid) {
// Bedrock players use a nil uuid bit masked with their xbox user id (xuid). The version is always zero.
return uuid.version() == 0;
}
}
6 changes: 6 additions & 0 deletions paper/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ claim-secure-chat-enforced: false
# screen and if enforce-secure-profiles is disabled the open chat screen
# for users of the client-side mod.
send-prevents-chat-reports-to-client: false

# Only enable chat rewriting for Bedrock. This can be desirable if secure chat
# is supported by the server, and you want Java players to be able to use
# features provided through the Social Interactions menu, but still want to
# support chat from Bedrock players connected through Geyser.
rewrite-bedrock-only: false