mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 16:37:43 +01:00
45 lines
2.0 KiB
Diff
45 lines
2.0 KiB
Diff
From 503d1eafc0e7e449e37110bb4731e7b9d75b8d73 Mon Sep 17 00:00:00 2001
|
|
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
|
|
Date: Sat, 15 Aug 2020 03:49:33 -0500
|
|
Subject: [PATCH] Add component util
|
|
|
|
|
|
diff --git a/src/main/java/net/pl3x/purpur/ComponentUtil.java b/src/main/java/net/pl3x/purpur/ComponentUtil.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..3f7bc68d1a6fb00758b178bb46113e38b8bc24bc
|
|
--- /dev/null
|
|
+++ b/src/main/java/net/pl3x/purpur/ComponentUtil.java
|
|
@@ -0,0 +1,32 @@
|
|
+package net.pl3x.purpur;
|
|
+
|
|
+import net.md_5.bungee.api.chat.BaseComponent;
|
|
+import net.md_5.bungee.api.chat.TextComponent;
|
|
+import net.md_5.bungee.chat.ComponentSerializer;
|
|
+import net.minecraft.server.IChatBaseComponent;
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+
|
|
+import java.util.List;
|
|
+
|
|
+public class ComponentUtil {
|
|
+ public static String fromComponent(IChatBaseComponent component) {
|
|
+ String json = "";
|
|
+ try {
|
|
+ int chop;
|
|
+ List<IChatBaseComponent> siblings = component.getSiblings();
|
|
+ if (siblings.size() > 0) chop = siblings.get(0).getChatModifier().getColor() == null ? 4 : 2;
|
|
+ else chop = component.getChatModifier().getColor() == null ? 2 : 0;
|
|
+ json = IChatBaseComponent.ChatSerializer.componentToJson(component);
|
|
+ BaseComponent[] parsed = ComponentSerializer.parse(json);
|
|
+ return TextComponent.toLegacyText(parsed).substring(chop);
|
|
+ } catch (Exception e) {
|
|
+ MinecraftServer.LOGGER.warn("There was a problem processing a chat component!");
|
|
+ MinecraftServer.LOGGER.warn("We have fallen back to legacy colorless string to prevent real errors");
|
|
+ MinecraftServer.LOGGER.warn("Please report this to Purpur!");
|
|
+ MinecraftServer.LOGGER.warn("JSON: " + json);
|
|
+ MinecraftServer.LOGGER.warn("The following error describes what went wrong:");
|
|
+ e.printStackTrace();
|
|
+ return component.getString();
|
|
+ }
|
|
+ }
|
|
+}
|