From 4952cbc2edb2f077e36f672dd577f3bacea4bd4d Mon Sep 17 00:00:00 2001 From: Wesley1808 <74668381+Wesley1808@users.noreply.github.com> Date: Sun, 30 Jun 2024 15:51:54 +0200 Subject: [PATCH] Improve styledchat support for socialspy text --- .../wesley1808/advancedchat/impl/AdvancedChat.java | 7 ++++--- .../impl/predicates/ChannelPredicate.java | 2 +- .../impl/predicates/CustomDistancePredicate.java | 2 +- ...cateCompare.java => PlayerComparePredicate.java} | 10 +++++----- .../advancedchat/impl/predicates/Predicates.java | 2 +- .../advancedchat/impl/utils/PlaceHolders.java | 2 +- .../advancedchat/impl/utils/Socialspy.java | 13 +++++++++++-- .../advancedchat/mixins/MsgCommandMixin.java | 10 +++++----- 8 files changed, 29 insertions(+), 19 deletions(-) rename src/main/java/me/wesley1808/advancedchat/impl/predicates/{PlayerPredicateCompare.java => PlayerComparePredicate.java} (83%) diff --git a/src/main/java/me/wesley1808/advancedchat/impl/AdvancedChat.java b/src/main/java/me/wesley1808/advancedchat/impl/AdvancedChat.java index 27e0af3..3685cd3 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/AdvancedChat.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/AdvancedChat.java @@ -53,9 +53,10 @@ public void onInitialize() { }); if (ModCompat.STYLEDCHAT) { - StyledChatEvents.PRE_MESSAGE_CONTENT.register((message, context) -> { - return context.hasPlayer() && Filter.isEnabled() ? Filter.process(message).filteredOrEmpty() : message; - }); + StyledChatEvents.PRE_MESSAGE_CONTENT.register((message, context) -> context.hasPlayer() && Filter.isEnabled() + ? Filter.process(message).filteredOrEmpty() + : message + ); } } } \ No newline at end of file diff --git a/src/main/java/me/wesley1808/advancedchat/impl/predicates/ChannelPredicate.java b/src/main/java/me/wesley1808/advancedchat/impl/predicates/ChannelPredicate.java index 3025031..0e98247 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/predicates/ChannelPredicate.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/predicates/ChannelPredicate.java @@ -16,7 +16,7 @@ public class ChannelPredicate extends AbstractPredicate { private static final String GLOBAL_CHANNEL = "global"; - public static final ResourceLocation ID = ResourceLocation.tryBuild("advancedchat", "channel"); + public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("advancedchat", "channel"); public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance .group(Codec.STRING.fieldOf("channel").forGetter(ChannelPredicate::channel)) .apply(instance, ChannelPredicate::new) diff --git a/src/main/java/me/wesley1808/advancedchat/impl/predicates/CustomDistancePredicate.java b/src/main/java/me/wesley1808/advancedchat/impl/predicates/CustomDistancePredicate.java index 25b17f5..50b414a 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/predicates/CustomDistancePredicate.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/predicates/CustomDistancePredicate.java @@ -10,7 +10,7 @@ import net.minecraft.world.phys.Vec3; public class CustomDistancePredicate extends AbstractChatPredicate { - public static final ResourceLocation ID = ResourceLocation.tryBuild("advancedchat", "distance"); + public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("advancedchat", "distance"); public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance .group(DistancePredicate.CODEC.fieldOf("value").forGetter((inst) -> inst.predicate)) .apply(instance, CustomDistancePredicate::new) diff --git a/src/main/java/me/wesley1808/advancedchat/impl/predicates/PlayerPredicateCompare.java b/src/main/java/me/wesley1808/advancedchat/impl/predicates/PlayerComparePredicate.java similarity index 83% rename from src/main/java/me/wesley1808/advancedchat/impl/predicates/PlayerPredicateCompare.java rename to src/main/java/me/wesley1808/advancedchat/impl/predicates/PlayerComparePredicate.java index c1144e8..55ae9ac 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/predicates/PlayerPredicateCompare.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/predicates/PlayerComparePredicate.java @@ -13,16 +13,16 @@ import java.util.Objects; -public class PlayerPredicateCompare extends AbstractChatPredicate { - public static final ResourceLocation ID = ResourceLocation.tryBuild("advancedchat", "compare"); - public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( +public class PlayerComparePredicate extends AbstractChatPredicate { + public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("advancedchat", "compare"); + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( GenericObject.CODEC.fieldOf("compare_predicate").forGetter((x) -> x.predicateObj) - ).apply(instance, PlayerPredicateCompare::new)); + ).apply(instance, PlayerComparePredicate::new)); private final Object predicateObj; private final MinecraftPredicate predicate; - public PlayerPredicateCompare(Object predicateObj) { + public PlayerComparePredicate(Object predicateObj) { super(ID, CODEC); this.predicateObj = predicateObj; diff --git a/src/main/java/me/wesley1808/advancedchat/impl/predicates/Predicates.java b/src/main/java/me/wesley1808/advancedchat/impl/predicates/Predicates.java index 541b2bb..53012a4 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/predicates/Predicates.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/predicates/Predicates.java @@ -7,6 +7,6 @@ public class Predicates { public static void register() { PredicateRegistry.register(CustomDistancePredicate.ID, CustomDistancePredicate.CODEC); PredicateRegistry.register(ChannelPredicate.ID, ChannelPredicate.CODEC); - PredicateRegistry.register(PlayerPredicateCompare.ID, PlayerPredicateCompare.CODEC); + PredicateRegistry.register(PlayerComparePredicate.ID, PlayerComparePredicate.CODEC); } } diff --git a/src/main/java/me/wesley1808/advancedchat/impl/utils/PlaceHolders.java b/src/main/java/me/wesley1808/advancedchat/impl/utils/PlaceHolders.java index 72781f7..4d31163 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/utils/PlaceHolders.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/utils/PlaceHolders.java @@ -23,7 +23,7 @@ public static void register() { } private static void register(String name, PlaceholderHandler handler) { - Placeholders.register(ResourceLocation.tryBuild("advancedchat", name), handler); + Placeholders.register(ResourceLocation.fromNamespaceAndPath("advancedchat", name), handler); } } diff --git a/src/main/java/me/wesley1808/advancedchat/impl/utils/Socialspy.java b/src/main/java/me/wesley1808/advancedchat/impl/utils/Socialspy.java index 06f42f5..63bb480 100644 --- a/src/main/java/me/wesley1808/advancedchat/impl/utils/Socialspy.java +++ b/src/main/java/me/wesley1808/advancedchat/impl/utils/Socialspy.java @@ -1,6 +1,7 @@ package me.wesley1808.advancedchat.impl.utils; import eu.pb4.placeholders.api.Placeholders; +import eu.pb4.styledchat.ducks.ExtSignedMessage; import me.wesley1808.advancedchat.api.AdvancedChatAPI; import me.wesley1808.advancedchat.impl.AdvancedChat; import me.wesley1808.advancedchat.impl.channels.ChatChannel; @@ -28,7 +29,7 @@ public static void send(CommandSourceStack source, ServerPlayer target, PlayerCh Map.of( "source", source.getDisplayName(), "target", target.getDisplayName(), - "message", Component.literal(message.signedContent()) + "message", getPrivateMessageContent(message) ) )); @@ -41,6 +42,14 @@ public static void send(CommandSourceStack source, ServerPlayer target, PlayerCh } } + private static Component getPrivateMessageContent(PlayerChatMessage message) { + if (ModCompat.STYLEDCHAT) { + return ExtSignedMessage.getArg(message, "base_input"); + } else { + return message.decoratedContent(); + } + } + // Channel Messages public static void send(ServerPlayer sender, List receivers, PlayerChatMessage message) { ChatChannel channel = DataManager.get(sender).channel; @@ -55,7 +64,7 @@ public static void send(ServerPlayer sender, List receivers, Playe Map.of( "channel", AdvancedChatAPI.getChannelPrefix(sender), "sender", sender.getDisplayName(), - "message", Component.literal(message.signedContent()) + "message", message.decoratedContent() ) )); diff --git a/src/main/java/me/wesley1808/advancedchat/mixins/MsgCommandMixin.java b/src/main/java/me/wesley1808/advancedchat/mixins/MsgCommandMixin.java index 7d0728d..03a3b6f 100644 --- a/src/main/java/me/wesley1808/advancedchat/mixins/MsgCommandMixin.java +++ b/src/main/java/me/wesley1808/advancedchat/mixins/MsgCommandMixin.java @@ -1,5 +1,6 @@ package me.wesley1808.advancedchat.mixins; +import com.llamalad7.mixinextras.sugar.Local; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import me.wesley1808.advancedchat.impl.config.Config; @@ -7,8 +8,6 @@ import me.wesley1808.advancedchat.impl.utils.Socialspy; import me.wesley1808.advancedchat.impl.utils.Util; import net.minecraft.commands.CommandSourceStack; -import net.minecraft.network.chat.ChatType; -import net.minecraft.network.chat.OutgoingChatMessage; import net.minecraft.network.chat.PlayerChatMessage; import net.minecraft.server.commands.MsgCommand; import net.minecraft.server.level.ServerPlayer; @@ -20,7 +19,6 @@ import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import java.util.Collection; -import java.util.Iterator; @Mixin(MsgCommand.class) public class MsgCommandMixin { @@ -47,13 +45,15 @@ public class MsgCommandMixin { @Inject( method = "sendMessage", - locals = LocalCapture.CAPTURE_FAILHARD, at = @At( value = "INVOKE", target = "Lnet/minecraft/commands/CommandSourceStack;shouldFilterMessageTo(Lnet/minecraft/server/level/ServerPlayer;)Z" ) ) - private static void advancedchat$onSendMessage(CommandSourceStack source, Collection collection, PlayerChatMessage message, CallbackInfo ci, ChatType.Bound bound, OutgoingChatMessage outgoingChatMessage, boolean bl, Iterator var6, ServerPlayer target, ChatType.Bound bound2) { + private static void advancedchat$onSendMessage( + CommandSourceStack source, Collection collection, PlayerChatMessage message, CallbackInfo ci, + @Local(ordinal = 0) ServerPlayer target + ) { ServerPlayer sender = source.getPlayer(); if (sender != null) { Util.playSound(target, Config.instance().privateMessageSound);