2021-12-30 22:16:37 +01:00
|
|
|
package io.gitlab.jfronny.googlechat.mixin;
|
|
|
|
|
2022-01-29 12:22:56 +01:00
|
|
|
import io.gitlab.jfronny.googlechat.GoogleChat;
|
2022-03-25 19:52:25 +01:00
|
|
|
import io.gitlab.jfronny.libjf.translate.api.Language;
|
2022-02-28 19:39:13 +01:00
|
|
|
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
2022-01-29 12:22:56 +01:00
|
|
|
import net.minecraft.network.PacketByteBuf;
|
2021-12-30 22:16:37 +01:00
|
|
|
import net.minecraft.network.packet.c2s.play.ChatMessageC2SPacket;
|
|
|
|
import org.spongepowered.asm.mixin.Final;
|
|
|
|
import org.spongepowered.asm.mixin.Mixin;
|
|
|
|
import org.spongepowered.asm.mixin.Mutable;
|
|
|
|
import org.spongepowered.asm.mixin.Shadow;
|
|
|
|
import org.spongepowered.asm.mixin.injection.At;
|
|
|
|
import org.spongepowered.asm.mixin.injection.Inject;
|
|
|
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
|
|
|
|
|
|
|
@Mixin(ChatMessageC2SPacket.class)
|
|
|
|
public class ChatMessageC2SPacketMixin {
|
|
|
|
@Mutable
|
|
|
|
@Final
|
|
|
|
@Shadow
|
|
|
|
private String chatMessage;
|
|
|
|
|
|
|
|
@Inject(at = @At("RETURN"), method = "<init>(Ljava/lang/String;)V")
|
|
|
|
public void init(String chatMessage, CallbackInfo info) {
|
2022-02-28 19:39:13 +01:00
|
|
|
googlechat$translate(GoogleChat.TRANSLATE_SERVICE);
|
2022-01-29 12:22:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Inject(at = @At("RETURN"), method = "<init>(Lnet/minecraft/network/PacketByteBuf;)V")
|
|
|
|
public void init(PacketByteBuf buf, CallbackInfo ci) {
|
2022-02-28 19:39:13 +01:00
|
|
|
googlechat$translate(GoogleChat.TRANSLATE_SERVICE);
|
2022-01-29 12:22:56 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 19:52:25 +01:00
|
|
|
private <T extends Language> void googlechat$translate(TranslateService<T> ts) {
|
2022-04-02 17:34:17 +02:00
|
|
|
if (chatMessage.startsWith("/")) return;
|
|
|
|
chatMessage = GoogleChat.translateIfNeeded(chatMessage, GoogleChat.Direction.C2S, true);
|
2021-12-30 22:16:37 +01:00
|
|
|
if (chatMessage.length() > 256) chatMessage = chatMessage.substring(0, 256);
|
|
|
|
}
|
|
|
|
}
|