Skip to content

Commit 12d274f

Browse files
committed
Refactor component serialization logic for 1.7.10
1 parent 6bc71d6 commit 12d274f

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

spark-forge1710/src/main/java/me/lucko/spark/forge/Forge1710CommandSender.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
import me.lucko.spark.forge.plugin.Forge1710SparkPlugin;
2525
import net.kyori.adventure.text.Component;
2626
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
27-
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
2827
import net.minecraft.command.ICommandSender;
2928
import net.minecraft.entity.player.EntityPlayer;
3029
import net.minecraft.network.rcon.RConConsoleSource;
3130
import net.minecraft.server.MinecraftServer;
32-
import net.minecraft.util.ChatComponentText;
3331
import net.minecraft.util.IChatComponent;
34-
import net.minecraftforge.common.ForgeHooks;
3532

33+
import java.util.ArrayList;
34+
import java.util.List;
3635
import java.util.UUID;
3736

3837
public class Forge1710CommandSender extends AbstractCommandSender<ICommandSender> {
@@ -64,23 +63,40 @@ public UUID getUniqueId() {
6463
return null;
6564
}
6665

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+
6796
@Override
6897
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));
84100
super.delegate.addChatMessage(mcComponent);
85101
}
86102
}

0 commit comments

Comments
 (0)