|
24 | 24 | import me.lucko.spark.forge.plugin.Forge1710SparkPlugin; |
25 | 25 | import net.kyori.adventure.text.Component; |
26 | 26 | import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; |
27 | | -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; |
28 | 27 | import net.minecraft.command.ICommandSender; |
29 | 28 | import net.minecraft.entity.player.EntityPlayer; |
30 | 29 | import net.minecraft.network.rcon.RConConsoleSource; |
31 | 30 | import net.minecraft.server.MinecraftServer; |
32 | | -import net.minecraft.util.ChatComponentText; |
33 | 31 | import net.minecraft.util.IChatComponent; |
34 | | -import net.minecraftforge.common.ForgeHooks; |
35 | 32 |
|
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
36 | 35 | import java.util.UUID; |
37 | 36 |
|
38 | 37 | public class Forge1710CommandSender extends AbstractCommandSender<ICommandSender> { |
@@ -64,23 +63,40 @@ public UUID getUniqueId() { |
64 | 63 | return null; |
65 | 64 | } |
66 | 65 |
|
| 66 | + private static List<Component> splitOnNewline(Component root) { |
| 67 | + List<Component> lines = new ArrayList<>(); |
| 68 | + List<Component> current = new ArrayList<>(); |
| 69 | + |
| 70 | + splitRecursive(root, current, lines); |
| 71 | + |
| 72 | + if (!current.isEmpty()) { |
| 73 | + lines.add(Component.empty().children(current)); |
| 74 | + } |
| 75 | + |
| 76 | + return lines; |
| 77 | + } |
| 78 | + |
| 79 | + private static void splitRecursive(Component comp, List<Component> current, List<Component> lines) { |
| 80 | + if (comp.equals(Component.newline())) { |
| 81 | + // flush current line |
| 82 | + lines.add(Component.empty().children(current)); |
| 83 | + current.clear(); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // copy the component but recurse into its children |
| 88 | + List<Component> newChildren = new ArrayList<>(); |
| 89 | + for (Component child : comp.children()) { |
| 90 | + splitRecursive(child, newChildren, lines); |
| 91 | + } |
| 92 | + |
| 93 | + current.add(comp.children(newChildren)); |
| 94 | + } |
| 95 | + |
67 | 96 | @Override |
68 | 97 | public void sendMessage(Component message) { |
69 | | - /* |
70 | | - * Due to limitations in 1.7.10, messages with \n render incorrectly on the client. |
71 | | - * To work around this, we convert the message to a string first, split it by newline, |
72 | | - * and send each line individually. |
73 | | - * |
74 | | - * This adds a performance penalty, but avoids any weirdness with this old client. |
75 | | - */ |
76 | | - LegacyComponentSerializer serializer = LegacyComponentSerializer.builder() |
77 | | - .character(LegacyComponentSerializer.SECTION_CHAR) |
78 | | - .extractUrls() |
79 | | - .build(); |
80 | | - String output = serializer.serialize(message); |
81 | | - for(String line : output.split("\n")) { |
82 | | - Component deserialized = serializer.deserialize(line); |
83 | | - IChatComponent mcComponent = IChatComponent.Serializer.func_150699_a(GsonComponentSerializer.gson().serialize(deserialized)); |
| 98 | + for (Component line : splitOnNewline(message)) { |
| 99 | + IChatComponent mcComponent = IChatComponent.Serializer.func_150699_a(GsonComponentSerializer.colorDownsamplingGson().serialize(line)); |
84 | 100 | super.delegate.addChatMessage(mcComponent); |
85 | 101 | } |
86 | 102 | } |
|
0 commit comments