Skip to content

Commit

Permalink
add support for 1.19.4-1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
celestialfault committed Oct 14, 2024
1 parent 62cb0ca commit 1ccfc13
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 54 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Initial release
## Added

- Support for versions 1.19.4-1.20.6
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Compacting

[![Available on Modrinth](https://cdn.jsdelivr.net/npm/@intergrav/devins-badges@3/assets/compact/available/modrinth_vector.svg)](https://modrinth.com/mod/compacting)

An intelligent compact chat mod for Fabric, largely inspired by the logic of same feature from [Patcher].

Messages are compacted based on how long ago they were last seen, instead of only by comparing the
last few chat messages.
Messages are compacted based on how long ago they were last seen, instead of only by comparing the last few chat messages.

It also intelligently handles dividers:

![](.github/dividers.png)

## Known Issues

- Any mod that adds timestamps in any capacity will very likely break chat compacting.
If that's something you find important, you're unfortunately going to have to settle for other mods that do this
differently, like [Compact Chat].
- Any mod that adds timestamps in any capacity will *probably* break chat compacting.
If that's something you find important, you're likely going to have to settle for other mods that do this
differently, like [Compact Chat] or [Chat Patches].

[Patcher]: https://sk1er.club/mods/patcher
[Compact Chat]: https://modrinth.com/mod/compact-chat
[Chat Patches]: https://modrinth.com/mod/chatpatches
11 changes: 8 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ loom {
}
}

val targetJava = if(stonecutter.eval(mcVersion, ">=1.20.5")) 21 else 17

java {
val java = if (stonecutter.eval(mcVersion, ">=1.20.6")) JavaVersion.VERSION_21 else JavaVersion.VERSION_17
targetCompatibility = java
sourceCompatibility = java
targetCompatibility = JavaVersion.toVersion(targetJava)
sourceCompatibility = JavaVersion.toVersion(targetJava)
}

kotlin {
jvmToolchain(targetJava)
}

tasks.processResources {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ stonecutter {
centralScript = "build.gradle.kts"

shared {
versions("1.21.1")
versions("1.20.1", "1.21.1")
}
create(rootProject)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,92 @@

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import dev.celestialfault.compacting.Compacting;
import dev.celestialfault.compacting.Message;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.gui.hud.ChatHudLine;
import net.minecraft.client.gui.hud.MessageIndicator;
import net.minecraft.network.message.MessageSignatureData;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = ChatHud.class, priority = 9999)
abstract class ChatHudMixin {
private final @Unique ThreadLocal<@Nullable Message> CURRENT = new ThreadLocal<>();

@ModifyVariable(
//? if >=1.20.5 {
method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V",
//?} else if >=1.19.4 {
/*method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V",
*///?}
at = @At("HEAD"),
argsOnly = true
)
public Text compacting$compactText(Text text) {
var message = Compacting.compact(text);
if(message == null) {
CURRENT.remove();
return text;
}
CURRENT.set(message);
if(message.shouldCompact()) {
return message.getTextWithCounter();
}
return text;
}

@WrapOperation(
//? if >=1.20.5 {
method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V",
//?} else if >=1.19.4 {
/*method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V",
*///?}
at = @At(
value = "NEW",
target = "(ILnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)Lnet/minecraft/client/gui/hud/ChatHudLine;"
)
)
public ChatHudLine celestetweaks$compactChat(int ticks, Text message, MessageSignatureData sig, MessageIndicator indicator, Operation<ChatHudLine> original) {
return Compacting.compact(original.call(ticks, message, sig, indicator));
public ChatHudLine compacting$associateHudLine(int creationTick, Text text, MessageSignatureData messageSignatureData, MessageIndicator messageIndicator, Operation<ChatHudLine> original) {
var line = original.call(creationTick, text, messageSignatureData, messageIndicator);
var message = CURRENT.get();
if(message != null) {
message.setLastLine(line);
}
return line;
}

@WrapOperation(
//? if >=1.20.5 {
method = "addVisibleMessage",
//?} else if >=1.19.4 {
/*method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V",
*///?}
at = @At(
value = "NEW",
target = "(ILnet/minecraft/text/OrderedText;Lnet/minecraft/client/gui/hud/MessageIndicator;Z)Lnet/minecraft/client/gui/hud/ChatHudLine$Visible;"
)
)
public ChatHudLine.Visible celestetweaks$associateVisibleLine(int tick, OrderedText text, MessageIndicator indicator,
boolean endOfEntry, Operation<ChatHudLine.Visible> original,
@Local(argsOnly = true) ChatHudLine line) {
public ChatHudLine.Visible compacting$associateVisibleLine(int tick, OrderedText text, MessageIndicator indicator,
boolean endOfEntry, Operation<ChatHudLine.Visible> original) {
var visible = original.call(tick, text, indicator, endOfEntry);
Compacting.associate(line, visible);
var message = CURRENT.get();
if(message != null) {
message.getLastVisible().add(visible);
}
return visible;
}

@Inject(method = "clear", at = @At("TAIL"))
public void celestetweaks$clearCompactHistory(boolean clearHistory, CallbackInfo ci) {
public void compacting$clearMessages(boolean clearHistory, CallbackInfo ci) {
Compacting.clear();
CURRENT.remove();
}
}
41 changes: 11 additions & 30 deletions src/main/kotlin/dev/celestialfault/compacting/Compacting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.hud.ChatHudLine
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import org.slf4j.Logger
import kotlin.time.Duration.Companion.seconds

object Compacting : ClientModInitializer {
val LOG: Logger = LogUtils.getLogger()

private val messages: MutableMap<Text, Message> = mutableMapOf()
private val dividers: MutableList<Message> = mutableListOf()
private var currentDividerSet: MutableList<Message>? = null

override fun onInitializeClient() {
Expand All @@ -35,22 +34,14 @@ object Compacting : ClientModInitializer {

private fun prune() {
messages.values.removeIf(Message::isOld)
dividers.removeIf(Message::isOld)
}

private fun remove(message: Message) {
var hud = MinecraftClient.getInstance().inGameHud.chatHud as ChatHudAccessor
hud.messages.remove(message.lastLine)
message.lastLine?.let(hud.messages::remove)
message.lastVisible.forEach(hud.visibleMessages::remove)
message.dividers.forEach(this::remove)
message.dividers.clear()
dividers.remove(message)
}

private fun find(line: ChatHudLine): Message? {
// note that we can't actually use a hashmap lookup here as we modify the text to add the compact count,
// changing the hash and therefore breaking such a lookup.
return messages.values.firstOrNull { it.lastLine === line } ?: dividers.firstOrNull { it.lastLine === line }
}

private fun associateDividers(lastDivider: Message) {
Expand All @@ -69,7 +60,6 @@ object Compacting : ClientModInitializer {
}
}
if(message.isDivider) {
dividers.add(message)
if(currentDividerSet == null) {
currentDividerSet = mutableListOf()
} else {
Expand All @@ -81,36 +71,27 @@ object Compacting : ClientModInitializer {
}

@JvmStatic
fun compact(line: ChatHudLine): ChatHudLine {
if(!Config.enabled) return line
fun compact(text: Text): Message? {
if(!Config.enabled) return null
prune()

val message = messages[line.content] ?: Message(line).also { if(!it.isDivider) messages.put(line.content, it) }
var message: Message? = messages[text]
if(message == null) {
message = Message(text.copy())
if(!message.isDivider) messages.put(text, message)
}
processDivider(message)

message.timesSeen++
message.lastSeen = Timestamp.now()
if(!message.shouldCompact) {
message.lastLine = line
return line
}

remove(message)
val newLine = ChatHudLine(line.creationTick, message.textWithCounter, line.signature, line.indicator)
message.lastLine = newLine
message.lastVisible.clear()
return newLine
}

@JvmStatic
fun associate(line: ChatHudLine, visible: ChatHudLine.Visible) {
find(line)?.lastVisible?.add(visible)
if(message.shouldCompact) remove(message)
return message
}

@JvmStatic
fun clear() {
messages.clear()
dividers.clear()
currentDividerSet = null
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/dev/celestialfault/compacting/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import net.minecraft.text.Text
import net.minecraft.util.Formatting
import kotlin.time.Duration.Companion.seconds

internal class Message(val text: MutableText, var lastLine: ChatHudLine) {
constructor(line: ChatHudLine) : this(line.content.copy(), line)

class Message(val text: MutableText) {
var lastLine: ChatHudLine? = null
val lastVisible: MutableList<ChatHudLine.Visible> = mutableListOf()
val dividers: MutableList<Message> = mutableListOf()

Expand All @@ -27,6 +26,7 @@ internal class Message(val text: MutableText, var lastLine: ChatHudLine) {
.copy()
.append(Text.literal(" ($timesSeen)").setStyle(Style.EMPTY.withExclusiveFormatting(Formatting.GRAY)))

@get:JvmName("shouldCompact")
val shouldCompact: Boolean
get() = timesSeen > 1 && !isDivider

Expand Down
8 changes: 8 additions & 0 deletions versions/1.20.1/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
deps.yarn_build=10
deps.fabric_api=0.92.2+1.20.1
deps.modmenu=7.2.2
deps.yacl=3.5.0+1.20.1

mod.mc_dep=>=1.19.4 <1.20.5
mod.mc_title=1.19.4-1.20.4
mod.mc_targets=1.19.4 1.20 1.20.1 1.20.2 1.20.3 1.20.4
6 changes: 3 additions & 3 deletions versions/1.21.1/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ deps.fabric_api=0.105.0+1.21.1
deps.modmenu=11.0.2
deps.yacl=3.5.0+1.21

mod.mc_dep=>=1.21
mod.mc_title=1.21
mod.mc_targets=1.21 1.21.1
mod.mc_dep=>=1.20.5
mod.mc_title=1.20.5+
mod.mc_targets=1.20.5 1.20.6 1.21 1.21.1

0 comments on commit 1ccfc13

Please sign in to comment.