From 55859b6b15074f0d822870f72b98c23bda60c077 Mon Sep 17 00:00:00 2001 From: Allink Date: Sat, 20 May 2023 04:07:55 +0100 Subject: [PATCH] Add adventure utility class This class is meant to track a global PlainTextComponentSerializer instance, so we don't have to call PlainTextComponentSerializer#plainText in every class that needs to serialize a Component into a String. I implemented this so I can add component logging to FreedomLogger. --- .../totalfreedom/utils/FreedomAdventure.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Patchwork/src/main/java/me/totalfreedom/utils/FreedomAdventure.java diff --git a/Patchwork/src/main/java/me/totalfreedom/utils/FreedomAdventure.java b/Patchwork/src/main/java/me/totalfreedom/utils/FreedomAdventure.java new file mode 100644 index 0000000..cd0fa3d --- /dev/null +++ b/Patchwork/src/main/java/me/totalfreedom/utils/FreedomAdventure.java @@ -0,0 +1,48 @@ +package me.totalfreedom.utils; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; + +import java.util.function.Supplier; + +/** + * This class contains the only reference to plain text component serializer, and allows access to it via wrapper functions. + */ +public class FreedomAdventure +{ + private FreedomAdventure() + { + throw new UnsupportedOperationException("Instantiation of a static utility class is not supported."); + } + + private static final PlainTextComponentSerializer PLAIN_TEXT_COMPONENT_SERIALIZER = PlainTextComponentSerializer.plainText(); + + public static String toPlainText(Component component) + { + return PLAIN_TEXT_COMPONENT_SERIALIZER.serialize(component); + } + + public static String toPlainText(Supplier supplier) + { + return toPlainText(supplier.get()); + } + + public static Supplier supplyPlainText(Supplier supplier) + { + return new StringRepresentationSupplier(supplier.get()); + } + + public static Supplier supplyPlainText(Component component) + { + return new StringRepresentationSupplier(component); + } + + private record StringRepresentationSupplier(Component component) implements Supplier + { + @Override + public String get() + { + return PLAIN_TEXT_COMPONENT_SERIALIZER.serialize(component); + } + } +}