Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yarn_mappings=1.21.3+build.2
loader_version=0.15.11
fabric_api_version=0.112.1+1.21.3

mod_version=1.2.5
mod_version=1.2.7
maven_group=com.arematics
archives_base_name=AdvancedChatLog

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 DarkKronicle
* Copyright (C) 2021-2025 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -11,6 +11,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.logging.LogUtils;
import fi.dy.masa.malilib.config.IConfigBase;
import fi.dy.masa.malilib.config.IConfigHandler;
import fi.dy.masa.malilib.config.options.*;
Expand All @@ -28,6 +29,7 @@
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.Util;

@Environment(EnvType.CLIENT)
public class ChatLogConfigStorage implements IConfigHandler {
Expand Down Expand Up @@ -205,11 +207,17 @@ public static void saveFromFile() {

@Override
public void load() {
LogUtils.getLogger().info("[AdvancedChatLog] Loading...");
long time = Util.getMeasuringTimeMs();
loadFromFile();
LogUtils.getLogger().info("[AdvancedChatLog] Load completed in {}ms", Util.getMeasuringTimeMs() - time);
}

@Override
public void save() {
LogUtils.getLogger().info("[AdvancedChatLog] Saving...");
long time = Util.getMeasuringTimeMs();
saveFromFile();
LogUtils.getLogger().info("[AdvancedChatLog] Save completed in {}ms", Util.getMeasuringTimeMs() - time);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021-2024 DarkKronicle
* Copyright (C) 2021-2025 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -32,7 +32,6 @@
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
/*
* Copyright (C) 2021-2024 DarkKronicle
* Copyright (C) 2021-2025 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package io.github.darkkronicle.advancedchatlog.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import io.github.darkkronicle.advancedchatcore.chat.ChatMessage;
import io.github.darkkronicle.advancedchatcore.interfaces.IJsonSave;
import io.github.darkkronicle.advancedchatlog.AdvancedChatLog;
import io.github.darkkronicle.advancedchatlog.config.ChatLogConfigStorage;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.registry.BuiltinRegistries;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.text.Style;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
public class LogChatMessageSerializer implements IJsonSave<LogChatMessage> {
private static final Gson GSON = (new GsonBuilder()).disableHtmlEscaping().create();
private final Text.Serializer serializer = new Text.Serializer(DynamicRegistryManager.EMPTY);
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

public LogChatMessageSerializer() {}
Expand All @@ -51,15 +50,30 @@ private Text transfer(Text text) {
return base;
}

private Style forceCleanStyle(Style style) {
style = style.withClickEvent(null);
style = style.withHoverEvent(null);
style = style.withInsertion(null);
return style;
}

private Text forceTransfer(Text text) {
// Using the built in serializer LiteralText is required
Text base = Text.empty();
for (Text t : text.getSiblings()) {
Text newT = Text.literal(t.getString()).fillStyle(forceCleanStyle(t.getStyle()));
base.getSiblings().add(newT);
}
return base;
}

@Override
public LogChatMessage load(JsonObject obj) {
RegistryWrapper.WrapperLookup wrapperLookup = BuiltinRegistries.createWrapperLookup();
LocalDateTime dateTime = LocalDateTime.from(formatter.parse(obj.get("time").getAsString()));
LocalDate date = dateTime.toLocalDate();
LocalTime time = dateTime.toLocalTime();

Text display = Text.Serialization.fromJson(obj.get("display").getAsString(), wrapperLookup);
Text original = Text.Serialization.fromJson(obj.get("original").getAsString(), wrapperLookup);
Text display = serializer.deserialize(obj.get("display"), Text.class, null);
Text original = serializer.deserialize(obj.get("original"), Text.class, null);
int stacks = obj.get("stacks").getAsByte();
ChatMessage message =
ChatMessage.builder()
Expand All @@ -75,10 +89,27 @@ public JsonObject save(LogChatMessage message) {
JsonObject json = new JsonObject();
ChatMessage chat = message.getMessage();
LocalDateTime dateTime = LocalDateTime.of(message.getDate(), chat.getTime());
json.addProperty("time", formatter.format(dateTime));
json.addProperty("stacks", chat.getStacks());
json.add("display", GSON.toJsonTree(chat.getDisplayText()));
json.add("original", GSON.toJsonTree(chat.getOriginalText()));
try {
json.addProperty("time", formatter.format(dateTime));
json.addProperty("stacks", chat.getStacks());
json.add("display", serializer.serialize(transfer(chat.getDisplayText()), Text.class, null));
json.add("original", serializer.serialize(transfer(chat.getOriginalText()), Text.class, null));
} catch (JsonParseException e) {
try {
AdvancedChatLog.LOGGER.warn("[AdvancedChatLog] Save Error 1: {}", e.getMessage());
AdvancedChatLog.LOGGER.warn("Original Message:");
AdvancedChatLog.LOGGER.warn(chat.getOriginalText().getString());
AdvancedChatLog.LOGGER.warn(chat.getOriginalText().toString());
json = new JsonObject();
json.addProperty("time", formatter.format(dateTime));
json.addProperty("stacks", chat.getStacks());
json.add("display", serializer.serialize(forceTransfer(chat.getDisplayText()), Text.class, null));
json.add("original", serializer.serialize(forceTransfer(chat.getOriginalText()), Text.class, null));
} catch (Exception e2) {
AdvancedChatLog.LOGGER.warn("[AdvancedChatLog] Save Error 2", e2);
return new JsonObject();
}
}
return json;
}
}