From b51a39ca6e14177f016533c20326329adc0fec15 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 4 Jun 2021 21:22:02 +0800 Subject: pretty print with new PrintUtil --- .../com/keuin/blame/command/BlameBlockCommand.java | 31 +++++++++---- src/main/java/com/keuin/blame/data/WorldPos.java | 2 +- .../java/com/keuin/blame/data/entry/LogEntry.java | 2 +- src/main/java/com/keuin/blame/util/PrintUtil.java | 53 +++++++++++++++++++++- 4 files changed, 76 insertions(+), 12 deletions(-) (limited to 'src/main/java/com') diff --git a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java index d82e4c4..8c80fe4 100644 --- a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java +++ b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java @@ -5,6 +5,7 @@ import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.lookup.BlockPosLookupFilter; import com.keuin.blame.lookup.LookupCallback; import com.keuin.blame.lookup.LookupManager; +import com.keuin.blame.util.PrettyUtil; import com.keuin.blame.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; @@ -12,6 +13,7 @@ import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.entity.Entity; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -61,18 +63,31 @@ public class BlameBlockCommand { @Override public void onLookupFinishes(Iterable logEntries) { - StringBuilder printBuilder = new StringBuilder(); int printCount = 0; + PrintUtil.Printer printer = PrintUtil.newPrinter(); + boolean isFirst = true; for (LogEntry logEntry : logEntries) { - printBuilder.append(logEntry.toString()); - printBuilder.append("\n") - .append("================") - .append("\n"); + if (!isFirst) + printer.append("\n"); + printer.append(Formatting.YELLOW, "Time: ", PrettyUtil.timestampToString(logEntry.timeMillis), "\n") + .append(Formatting.YELLOW, "Subject: ", Formatting.AQUA, logEntry.subjectId, "{", logEntry.subjectUUID, "} @ ", logEntry.subjectPos, "\n") + .append(Formatting.YELLOW, "Action: ", Formatting.AQUA, logEntry.actionType, "\n") + .append(Formatting.YELLOW, "Object: ", Formatting.AQUA, logEntry.objectType, "[", logEntry.objectId, "] @ ", logEntry.objectPos, "\n") + .append(Formatting.YELLOW, "Log version: ", logEntry.version, "\n") + .append(Formatting.YELLOW, "Game version: ", logEntry.gameVersion, "\n") + .append("================"); ++printCount; + isFirst = false; + } + if (printCount > 0) { + printer.sendTo(context); + PrintUtil.message(context, + "Showed " + printCount + " event items. ", + Formatting.ITALIC, + "Use `/blame limit` to change print count limit."); + } else { + PrintUtil.message(context, "No logs found."); } - printBuilder.append(String.format("Displayed the most recent %d items. " + - "Use `/blame limit` to change your display limit.", printCount)); - PrintUtil.msgInfo(context, printBuilder.toString()); } } diff --git a/src/main/java/com/keuin/blame/data/WorldPos.java b/src/main/java/com/keuin/blame/data/WorldPos.java index 6783fea..050aada 100644 --- a/src/main/java/com/keuin/blame/data/WorldPos.java +++ b/src/main/java/com/keuin/blame/data/WorldPos.java @@ -57,7 +57,7 @@ public class WorldPos { @Override public String toString() { - return String.format("(%s, %s, %s -> %s)", prettyDouble(x), prettyDouble(y), prettyDouble(z), world); + return String.format("(%s, %s, %s, %s)", prettyDouble(x), prettyDouble(y), prettyDouble(z), world); } private String prettyDouble(double d) { diff --git a/src/main/java/com/keuin/blame/data/entry/LogEntry.java b/src/main/java/com/keuin/blame/data/entry/LogEntry.java index d12718d..49ad720 100644 --- a/src/main/java/com/keuin/blame/data/entry/LogEntry.java +++ b/src/main/java/com/keuin/blame/data/entry/LogEntry.java @@ -77,7 +77,7 @@ public class LogEntry { public LogEntry() { } - // 拷贝构造器 + // copy constructor public LogEntry(LogEntry entry) { this.version = entry.version; this.gameVersion = entry.gameVersion; diff --git a/src/main/java/com/keuin/blame/util/PrintUtil.java b/src/main/java/com/keuin/blame/util/PrintUtil.java index 75b73d4..f1d8695 100644 --- a/src/main/java/com/keuin/blame/util/PrintUtil.java +++ b/src/main/java/com/keuin/blame/util/PrintUtil.java @@ -1,17 +1,20 @@ package com.keuin.blame.util; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.minecraft.network.MessageType; import net.minecraft.server.MinecraftServer; import net.minecraft.server.PlayerManager; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.LiteralText; -import net.minecraft.text.Style; +import net.minecraft.text.*; import net.minecraft.util.Formatting; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.Optional; +import java.util.function.Consumer; + import static com.keuin.blame.util.UuidUtils.UUID_NULL; @@ -135,4 +138,50 @@ public final class PrintUtil implements ServerLifecycleEvents.ServerStarted { LOGGER.error(LOG_HEADING + " " + string); } + public static void message(CommandContext context, Object... objects) { + new Printer().append(objects).sendTo(context); + } + + public static Printer newPrinter() { + return new Printer(); + } + + public static class Printer { + private final BaseText message = new LiteralText(""); + public Printer() { + } + + public Printer append(Object... objects) { + Style currentStyle = null; // accumulated style appended with a text + for (Object obj : objects) { + if (obj instanceof Formatting) { + currentStyle = Optional.ofNullable(currentStyle).orElse(Style.EMPTY) + .withFormatting((Formatting) obj); + } else if (obj != null) { + if (!(obj instanceof String)) + obj = obj.toString(); + message.append(new LiteralText((String) obj) + .setStyle(Optional.ofNullable(currentStyle).orElse(Style.EMPTY))); + currentStyle = null; + } + } + if (currentStyle != null) + throw new IllegalStateException("parameter with type Formatting must be appended " + + "with a BaseText-like object"); + return this; + } + + public void sendTo(Consumer receiver) { + receiver.accept(message); + } + + public void sendTo(CommandContext context) { + try { + context.getSource().getPlayer().sendSystemMessage(message, UUID_NULL); + } catch (CommandSyntaxException e) { + throw new IllegalArgumentException("CommandContext must be of a player"); + } + } + } + } -- cgit v1.2.3