Skip to content

Commit

Permalink
Fix team message and private messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 17, 2023
1 parent b1a0459 commit 9bd2791
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ Additionally, you can enable markdown and legacy (&X) formatting in the config
"dark_aqua": true
},
"emoticons": {
"$emojibase:builtin:joypixels": "${emoji}",
"potion": "🧪",
"trident": "🔱",
"rod": "🎣",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.83.0+1.20.1

# Mod Properties

mod_version = 2.2.1+1.20.1
mod_version = 2.2.2+1.20.1
maven_group = eu.pb4
archives_base_name = styled-chat

Expand Down
1 change: 1 addition & 0 deletions src/main/java/eu/pb4/styledchat/StyledChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public static Text formatFor(ServerCommandSource source, String original) {
}
}

@Deprecated
public static SignedMessage toEventMessage(SignedMessage message, PlaceholderContext context) {
var ext = (ExtSignedMessage) (Object) message;

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/eu/pb4/styledchat/mixin/SentMessageMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import eu.pb4.styledchat.StyledChatMod;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.ducks.ExtSignedMessage;
import eu.pb4.styledchat.other.ExtendedSentMessage;
import eu.pb4.styledchat.other.StyledChatSentMessage;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SentMessage;
import net.minecraft.network.message.SignedMessage;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
Expand All @@ -27,4 +31,38 @@ public interface SentMessageMixin {
}
}
}

@Inject(method = "of", at = @At("RETURN"), cancellable = true)
private static void styledChat$forceAttach(SignedMessage message, CallbackInfoReturnable<SentMessage> cir) {
if (cir.getReturnValue() instanceof SentMessage.Profileless) {
((ExtendedSentMessage) cir.getReturnValue()).styledChat$setMessage(message);
}
}

@Mixin(SentMessage.Chat.class)
class ChatMixin implements ExtendedSentMessage {

@Shadow @Final private SignedMessage message;

@Override
public SignedMessage styledChat$message() {
return this.message;
}
}

@Mixin(SentMessage.Profileless.class)
class ProfilelessMixin implements ExtendedSentMessage {
@Unique
private SignedMessage styledChat$message;

@Override
public SignedMessage styledChat$message() {
return this.styledChat$message;
}

@Override
public void styledChat$setMessage(SignedMessage value) {
styledChat$message = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.pb4.styledchat.ducks.ExtSignedMessage;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.config.ConfigManager;
import eu.pb4.styledchat.other.ExtendedSentMessage;
import eu.pb4.styledchat.other.StyledChatSentMessage;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SentMessage;
Expand Down Expand Up @@ -33,26 +34,26 @@ private static void styledChat_noopFeedback(ServerCommandSource instance, SentMe
// noop
}

@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;sendChatMessage(Lnet/minecraft/network/message/SentMessage;ZLnet/minecraft/network/message/MessageType$Parameters;)V"))
@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;sendChatMessage(Lnet/minecraft/network/message/SentMessage;ZLnet/minecraft/network/message/MessageType$Parameters;)V"))
private static void styledChat_formatText(ServerPlayerEntity instance, SentMessage message, boolean bl, MessageType.Parameters parameters, ServerCommandSource source) {
if (message instanceof StyledChatSentMessage styledChatSentMessage) {
if (message instanceof ExtendedSentMessage extSentMessage) {
try {
var sent = StyledChatMod.getMessageType().params(StyledChatStyles.getPrivateMessageSent(
source.getDisplayName(),
instance.getDisplayName(),
ExtSignedMessage.getArg(styledChatSentMessage.message(), "base_input"), instance.getCommandSource()
ExtSignedMessage.getArg(extSentMessage.styledChat$message(), "base_input"), instance.getCommandSource()
));


source.sendChatMessage(styledChatSentMessage.reformat(sent, MessageType.MSG_COMMAND_OUTGOING), bl, sent);
source.sendChatMessage(message, bl, sent);

var rex = StyledChatMod.getMessageType().params(StyledChatStyles.getPrivateMessageReceived(
source.getDisplayName(),
instance.getDisplayName(),
ExtSignedMessage.getArg(styledChatSentMessage.message(), "base_input"), source
ExtSignedMessage.getArg(extSentMessage.styledChat$message(), "base_input"), source
));

instance.sendChatMessage(styledChatSentMessage.reformat(rex, MessageType.TEAM_MSG_COMMAND_INCOMING), bl, rex);
instance.sendChatMessage(message, bl, rex);
return;
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import eu.pb4.styledchat.StyledChatMod;
import eu.pb4.styledchat.StyledChatStyles;
import eu.pb4.styledchat.StyledChatUtils;
import eu.pb4.styledchat.config.ConfigManager;
import eu.pb4.styledchat.ducks.ExtSignedMessage;
import eu.pb4.styledchat.other.StyledChatSentMessage;
import eu.pb4.styledchat.other.ExtendedSentMessage;
import net.minecraft.entity.Entity;
import net.minecraft.network.message.MessageType;
import net.minecraft.network.message.SentMessage;
Expand Down Expand Up @@ -36,24 +35,24 @@ private static void styledChat_formatOgText(ServerCommandSource serverCommandSou

@Redirect(method = "execute", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;sendChatMessage(Lnet/minecraft/network/message/SentMessage;ZLnet/minecraft/network/message/MessageType$Parameters;)V"))
private static void styledChat_replaceForSelf(ServerPlayerEntity instance, SentMessage message, boolean bl, MessageType.Parameters parameters, ServerCommandSource source) {
if (message instanceof StyledChatSentMessage styledChatSentMessage) {
if (message instanceof ExtendedSentMessage extSentMessage) {
try {
if (source.getPlayer() == instance) {
var sent = StyledChatMod.getMessageType().params(StyledChatStyles.getTeamChatSent(
((Team) source.getEntity().getScoreboardTeam()).getFormattedName(),
source.getDisplayName(),
ExtSignedMessage.getArg(styledChatSentMessage.message(), "base_input"), instance.getCommandSource()
ExtSignedMessage.getArg(extSentMessage.styledChat$message(), "base_input"), instance.getCommandSource()
));

source.sendChatMessage(styledChatSentMessage.reformat(sent, MessageType.TEAM_MSG_COMMAND_OUTGOING), bl, sent);
source.sendChatMessage(message, bl, sent);
} else {
var rex = StyledChatMod.getMessageType().params(StyledChatStyles.getTeamChatReceived(
((Team) source.getEntity().getScoreboardTeam()).getFormattedName(),
source.getDisplayName(),
ExtSignedMessage.getArg(styledChatSentMessage.message(), "base_input"), source
ExtSignedMessage.getArg(extSentMessage.styledChat$message(), "base_input"), source
));

instance.sendChatMessage(styledChatSentMessage.reformat(rex, MessageType.TEAM_MSG_COMMAND_INCOMING), bl, rex);
instance.sendChatMessage(message, bl, rex);
}
return;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package eu.pb4.styledchat.other;

import net.minecraft.network.message.SignedMessage;

public interface ExtendedSentMessage {
SignedMessage styledChat$message();
default void styledChat$setMessage(SignedMessage value) {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.util.Objects;

public interface StyledChatSentMessage extends SentMessage {
public interface StyledChatSentMessage extends SentMessage, ExtendedSentMessage {
Text override();

SignedMessage message();
Expand All @@ -24,8 +24,12 @@ public interface StyledChatSentMessage extends SentMessage {

RegistryKey<MessageType> sourceType();

record Chat(SignedMessage message, Text override, MessageType.Parameters parameters, RegistryKey<MessageType> sourceType) implements StyledChatSentMessage {
@Override
default SignedMessage styledChat$message() {
return this.message();
}

record Chat(SignedMessage message, Text override, MessageType.Parameters parameters, RegistryKey<MessageType> sourceType) implements StyledChatSentMessage {
public Text getContent() {
return message.unsignedContent();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/styledchat.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"package": "eu.pb4.styledchat.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"RegistryLoaderMixin",
"CommandManagerMixin",
"MessageArgumentTypeMixin",
"MessageFormatMixin",
"MinecraftServerMixin",
"PlayerAdvancementTrackerMixin",
"PlayerEntityMixin",
"PlayerManagerMixin",
"RegistryLoaderMixin",
"SentMessageMixin",
"SentMessageMixin$ChatMixin",
"SentMessageMixin$ProfilelessMixin",
"ServerPlayerEntityMixin",
"ServerPlayNetworkManagerMixin",
"SignedMessageMixin",
Expand Down

0 comments on commit 9bd2791

Please sign in to comment.