From 5121ebb65d6128b4c775c09943122d5b2f344e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Sun, 20 Feb 2011 13:38:27 +0100 Subject: [PATCH] Fixed issue with color markers in messages sent to the client. This fix eliminates crashes related to color markers being at end-of-line, and also makes colors persist across line breaks. - The broadcast message is split up in multiple packets, one per line - Color markers are reorganized, so that color 'sticks across lines' - The wrapping method calculates the *rendered* line length. Thus, if the user has a weird font, it might look weird. --- .../minecraft/server/NetServerHandler.java | 11 ++-- .../org/bukkit/craftbukkit/TextWrapper.java | 60 +++++++++++++++++++ .../craftbukkit/entity/CraftPlayer.java | 5 +- 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/bukkit/craftbukkit/TextWrapper.java diff --git a/src/main/java/net/minecraft/server/NetServerHandler.java b/src/main/java/net/minecraft/server/NetServerHandler.java index ebd28b2bf4..ec91edfa79 100644 --- a/src/main/java/net/minecraft/server/NetServerHandler.java +++ b/src/main/java/net/minecraft/server/NetServerHandler.java @@ -17,6 +17,7 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; +import org.bukkit.craftbukkit.TextWrapper; import org.bukkit.entity.Player; import org.bukkit.event.Event.Type; import org.bukkit.event.block.BlockDamageEvent; @@ -593,7 +594,7 @@ public class NetServerHandler extends NetHandler implements ICommandListener { // CraftBukkit end } } - + // CraftBukkit start public boolean chat(String msg) { if (msg.startsWith("/")) { @@ -608,12 +609,14 @@ public class NetServerHandler extends NetHandler implements ICommandListener { if (event.isCancelled()) { return true; } - // CraftBukkit end a.info(msg); - this.d.f.a((Packet) (new Packet3Chat(msg))); + for (final String line: TextWrapper.wrapText(msg)) { + this.d.f.a((Packet) (new Packet3Chat(line))); + } + // CraftBukkit end } - + return false; } // CraftBukkit end diff --git a/src/main/java/org/bukkit/craftbukkit/TextWrapper.java b/src/main/java/org/bukkit/craftbukkit/TextWrapper.java new file mode 100644 index 0000000000..46c1c2cd3b --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/TextWrapper.java @@ -0,0 +1,60 @@ +package org.bukkit.craftbukkit; + +public class TextWrapper { + private static final int[] characterWidths = new int[] { + 1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9, + 8, 8, 8, 8, 9, 9, 8, 9, 8, 8, 8, 8, 8, 9, 9, 9, + 4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6, + 7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6, + 3, 6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6, + 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 3, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, + 6, 3, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 2, 6, 6, + 8, 9, 9, 6, 6, 6, 8, 8, 6, 8, 8, 8, 8, 8, 6, 6, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 9, 5, 9, 9, + 8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7, + 7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1 + }; + private static final int CHAT_WINDOW_WIDTH = 318; + + public static String[] wrapText(final String text) { + final StringBuilder out = new StringBuilder(); + char colorChar = 'f'; + int lineWidth = 0; + boolean hasColored = true; + for (int i = 0; i < text.length(); i++) { + char ch = text.charAt(i); + if (ch == '\u00A7' && i < text.length() - 1) { + i++; + colorChar = text.charAt(i); + hasColored = false; + continue; + } else if (ch >= characterWidths.length) { + ch = (char) (characterWidths.length - 1); + } + final int width = characterWidths[(int) ch]; + if (lineWidth + width >= CHAT_WINDOW_WIDTH) { + out.append('\n'); + if (colorChar != 'f') { + out.append('\u00A7'); + out.append(colorChar); + } + out.append(ch); + lineWidth = width; + } else { + if (!hasColored) { + out.append('\u00A7'); + out.append(colorChar); + hasColored = true; + } + out.append(ch); + lineWidth += width; + } + } + return out.toString().split("\n"); + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 69a95e1e83..d017bbd079 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -14,6 +14,7 @@ import net.minecraft.server.WorldServer; import org.bukkit.Location; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; +import org.bukkit.craftbukkit.TextWrapper; import org.bukkit.entity.Player; public class CraftPlayer extends CraftHumanEntity implements Player { @@ -80,7 +81,9 @@ public class CraftPlayer extends CraftHumanEntity implements Player { } public void sendMessage(String message) { - entity.a.b(new Packet3Chat(message)); + for (final String line: TextWrapper.wrapText(message)) { + entity.a.b(new Packet3Chat(line)); + } } public String getDisplayName() {