Skip to content

Commit

Permalink
Improve styledchat support for socialspy text
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesley1808 committed Jun 30, 2024
1 parent bb61581 commit 4952cbc
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChannelPredicate> CODEC = RecordCodecBuilder.mapCodec(instance -> instance
.group(Codec.STRING.fieldOf("channel").forGetter(ChannelPredicate::channel))
.apply(instance, ChannelPredicate::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomDistancePredicate> CODEC = RecordCodecBuilder.mapCodec(instance -> instance
.group(DistancePredicate.CODEC.fieldOf("value").forGetter((inst) -> inst.predicate))
.apply(instance, CustomDistancePredicate::new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerPredicateCompare> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
public class PlayerComparePredicate extends AbstractChatPredicate {
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("advancedchat", "compare");
public static final MapCodec<PlayerComparePredicate> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

13 changes: 11 additions & 2 deletions src/main/java/me/wesley1808/advancedchat/impl/utils/Socialspy.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
)
));

Expand All @@ -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<ServerPlayer> receivers, PlayerChatMessage message) {
ChatChannel channel = DataManager.get(sender).channel;
Expand All @@ -55,7 +64,7 @@ public static void send(ServerPlayer sender, List<ServerPlayer> receivers, Playe
Map.of(
"channel", AdvancedChatAPI.getChannelPrefix(sender),
"sender", sender.getDisplayName(),
"message", Component.literal(message.signedContent())
"message", message.decoratedContent()
)
));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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;
import me.wesley1808.advancedchat.impl.interfaces.IServerPlayer;
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;
Expand All @@ -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 {
Expand All @@ -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<ServerPlayer> 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<ServerPlayer> collection, PlayerChatMessage message, CallbackInfo ci,
@Local(ordinal = 0) ServerPlayer target
) {
ServerPlayer sender = source.getPlayer();
if (sender != null) {
Util.playSound(target, Config.instance().privateMessageSound);
Expand Down

0 comments on commit 4952cbc

Please sign in to comment.