Skip to content

Commit

Permalink
feat: Add the option to clear the message counters when joining a world
Browse files Browse the repository at this point in the history
  • Loading branch information
caoimhebyrne committed Jun 5, 2022
1 parent 17586d5 commit d529a5a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/main/java/dev/cbyrne/compactchat/CompactChat.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package dev.cbyrne.compactchat;

import com.mojang.logging.LogUtils;
import dev.cbyrne.compactchat.config.Configuration;
import dev.cbyrne.compactchat.event.ClientJoinWorldCallback;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.api.ModInitializer;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class CompactChat implements ModInitializer {
public static final Map<String, Integer> messageCounters = new HashMap<>();
public static final Logger LOGGER = LogUtils.getLogger();
public static final Map<String, Integer> MESSAGE_COUNTERS = new HashMap<>();

@Override
public void onInitialize() {
AutoConfig.register(Configuration.class, GsonConfigSerializer::new);
ClientJoinWorldCallback.EVENT.register(() -> {
if (!Configuration.getInstance().resetCounterOnWorldJoin) return;

LOGGER.info("Clearing message counters of length " + MESSAGE_COUNTERS.size() + " because the client is joining a new world!");
MESSAGE_COUNTERS.clear();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Config(name = "compactchat")
public class Configuration implements ConfigData {
public boolean infiniteChatHistory = true;
public boolean resetCounterOnWorldJoin = false;

public static Configuration getInstance() {
return AutoConfig.getConfigHolder(Configuration.class).getConfig();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/cbyrne/compactchat/mixin/ChatHudMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ private void modifyMessage(Text message, CallbackInfo ci) {
var trimmedString = messageString.trim();
if (trimmedString.isEmpty() || trimmedString.isBlank() || trimmedString.contains("--------")) return;

var occurrences = CompactChat.messageCounters.get(messageString);
var occurrences = CompactChat.MESSAGE_COUNTERS.get(messageString);
if (occurrences == null) {
CompactChat.messageCounters.put(messageString, 1);
CompactChat.MESSAGE_COUNTERS.put(messageString, 1);
return;
}

Expand All @@ -62,7 +62,7 @@ private void modifyMessage(Text message, CallbackInfo ci) {
}

occurrences++;
CompactChat.messageCounters.put(messageString, occurrences);
CompactChat.MESSAGE_COUNTERS.put(messageString, occurrences);

addMessage(compactChat$addOccurrencesToText(message, occurrences));
ci.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
public class KeyboardMixin {
@Inject(method = "processF3", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/client/gui/hud/ChatHud;clear(Z)V"))
private void compactChat$resetMessageCounters(int key, CallbackInfoReturnable<Boolean> cir) {
CompactChat.messageCounters.clear();
CompactChat.MESSAGE_COUNTERS.clear();
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/compactchat/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"text.autoconfig.compactchat.title": "CompactChat Settings",
"text.autoconfig.compactchat.option.infiniteChatHistory": "Infinite Chat History"
"text.autoconfig.compactchat.option.infiniteChatHistory": "Infinite chat history",
"text.autoconfig.compactchat.option.resetCounterOnWorldJoin": "Reset message counter on world join"
}

0 comments on commit d529a5a

Please sign in to comment.