Fix TextWrapping issues; Now limits the packets send to the client to either: 119 chars or 320 width. This will strip disallowed characters, propagate colors properly to the next line and not 'eat' multiple color-codes.

This commit is contained in:
Erik Broes 2011-04-25 18:14:06 +02:00
parent 6940f56d4d
commit 88ebcc8db5
5 changed files with 103 additions and 40 deletions

View File

@ -14,6 +14,7 @@ import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.event.CraftEventFactory; import org.bukkit.craftbukkit.event.CraftEventFactory;
import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.TextWrapper;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.block.*; import org.bukkit.event.block.*;
@ -579,9 +580,15 @@ public class NetServerHandler extends NetHandler implements ICommandListener {
if (packet instanceof Packet6SpawnPosition) { if (packet instanceof Packet6SpawnPosition) {
Packet6SpawnPosition packet6 = (Packet6SpawnPosition) packet; Packet6SpawnPosition packet6 = (Packet6SpawnPosition) packet;
this.player.compassTarget = new Location(getPlayer().getWorld(), packet6.x, packet6.y, packet6.z); this.player.compassTarget = new Location(getPlayer().getWorld(), packet6.x, packet6.y, packet6.z);
} else if (packet instanceof Packet3Chat) {
String message = ((Packet3Chat) packet).a;
for (final String line: TextWrapper.wrapText(message)) {
this.networkManager.a(new Packet3Chat(line));
}
packet = null;
} }
if (packet != null) this.networkManager.a(packet);
// CraftBukkit // CraftBukkit
this.networkManager.a(packet);
this.g = this.f; this.g = this.f;
} }

View File

@ -150,9 +150,9 @@ public abstract class Packet {
} }
} }
public abstract void a(DataInputStream datainputstream); public abstract void a(DataInputStream datainputstream) throws IOException; // CraftBukkit
public abstract void a(DataOutputStream dataoutputstream); public abstract void a(DataOutputStream dataoutputstream) throws IOException; // CraftBukkit
public abstract void a(NetHandler nethandler); public abstract void a(NetHandler nethandler);

View File

@ -0,0 +1,38 @@
package net.minecraft.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Packet3Chat extends Packet {
public String a;
public Packet3Chat() {}
public Packet3Chat(String s) {
// CraftBukkit start - handle this later
//if (s.length() > 119) {
// s = s.substring(0, 119);
//}
// CraftBukkit end
this.a = s;
}
public void a(DataInputStream datainputstream) throws IOException { // CraftBukkit
this.a = a(datainputstream, 119);
}
public void a(DataOutputStream dataoutputstream) throws IOException { // CraftBukkit
a(this.a, dataoutputstream);
}
public void a(NetHandler nethandler) {
nethandler.a(this);
}
public int a() {
return this.a.length();
}
}

View File

@ -2,6 +2,8 @@ package org.bukkit.craftbukkit;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.sound.sampled.LineListener;
public class TextWrapper { public class TextWrapper {
private static final int[] characterWidths = new int[] { private static final int[] characterWidths = new int[] {
1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9, 1, 9, 9, 8, 8, 8, 8, 7, 9, 8, 9, 9, 8, 9, 9, 9,
@ -21,50 +23,70 @@ public class TextWrapper {
8, 7, 7, 8, 7, 8, 8, 8, 7, 8, 8, 7, 9, 9, 6, 7, 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 7, 7, 7, 7, 9, 6, 7, 8, 7, 6, 6, 9, 7, 6, 7, 1
}; };
private static final char COLOR_CHAR = '\u00A7';
private static final int CHAT_WINDOW_WIDTH = 320; private static final int CHAT_WINDOW_WIDTH = 320;
private static final Pattern pattern = Pattern.compile("\n.*\u00A7", Pattern.DOTALL); private static final int CHAT_STRING_LENGTH = 119;
private static final String allowedChars = net.minecraft.server.FontAllowedCharacters.a;
public static String[] wrapText(final String text) { public static String[] wrapText(final String text) {
final StringBuilder out = new StringBuilder(); final StringBuilder out = new StringBuilder();
char colorChar = 'f'; char colorChar = 'f';
int lineWidth = 0; int lineWidth = 0;
int lineCount = 0; int lineLenght = 0;
boolean hasColored = true;
// Go over the message char by char.
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i); char ch = text.charAt(i);
if (ch == '\u00A7' && i < text.length() - 1) {
colorChar = text.charAt(++i); // Get the color
hasColored = false; if (ch == COLOR_CHAR && i < text.length() - 1) {
continue; // We might need a linebreak ... so ugly ;(
} else if (ch >= characterWidths.length) { if (lineLenght + 2 > CHAT_STRING_LENGTH) {
ch = (char) (characterWidths.length - 1); out.append('\n');
} lineLenght = 0;
final int width = characterWidths[(int) ch]; if (colorChar != 'f') {
if (lineWidth + width >= CHAT_WINDOW_WIDTH) { out.append(COLOR_CHAR).append(colorChar);
out.append('\n'); lineLenght += 2;
lineCount++; }
if (colorChar != 'f') { }
out.append('\u00A7'); colorChar = text.charAt(++i);
out.append(colorChar); out.append(COLOR_CHAR).append(colorChar);
lineLenght += 2;
continue;
}
// Figure out if it's allowed
int index = allowedChars.indexOf(ch);
if (index == -1) {
// Invalid character .. skip it.
continue;
} else {
// Sadly needed as the allowedChars string misses the first
index += 32;
}
// Find the width
final int width = characterWidths[ index ];
// See if we need a linebreak
if (lineLenght + 1 > CHAT_STRING_LENGTH || lineWidth + width >= CHAT_WINDOW_WIDTH) {
out.append('\n');
lineLenght = 0;
// Re-apply the last color if it isn't the default
if (colorChar != 'f') {
out.append(COLOR_CHAR).append(colorChar);
lineLenght += 2;
} }
out.append(ch);
lineWidth = width; lineWidth = width;
} else { } else {
if (!hasColored) {
out.append('\u00A7');
out.append(colorChar);
hasColored = true;
}
out.append(ch);
lineWidth += width; lineWidth += width;
} }
out.append(ch);
lineLenght++;
} }
// See if we need to split the string // Return it split
String result = out.toString(); return out.toString().split("\n");
if (pattern.matcher(result).find()) return result.split("\n");
if (lineCount > 0) result.replace("\n", "");
return new String[] {result};
} }
} }

View File

@ -5,7 +5,6 @@ import java.net.SocketAddress;
import net.minecraft.server.EntityHuman; import net.minecraft.server.EntityHuman;
import net.minecraft.server.EntityPlayer; import net.minecraft.server.EntityPlayer;
import net.minecraft.server.ItemInWorldManager; import net.minecraft.server.ItemInWorldManager;
import net.minecraft.server.Packet;
import net.minecraft.server.Packet200Statistic; import net.minecraft.server.Packet200Statistic;
import net.minecraft.server.Packet3Chat; import net.minecraft.server.Packet3Chat;
import net.minecraft.server.Packet6SpawnPosition; import net.minecraft.server.Packet6SpawnPosition;
@ -17,7 +16,6 @@ import org.bukkit.Material;
import org.bukkit.Statistic; import org.bukkit.Statistic;
import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.TextWrapper;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class CraftPlayer extends CraftHumanEntity implements Player { public class CraftPlayer extends CraftHumanEntity implements Player {
@ -84,9 +82,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
} }
public void sendMessage(String message) { public void sendMessage(String message) {
for (final String line: TextWrapper.wrapText(message)) { this.sendRawMessage(message);
getHandle().netServerHandler.sendPacket(new Packet3Chat(line));
}
} }
public String getDisplayName() { public String getDisplayName() {
@ -256,7 +252,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
if (!material.isBlock()) { if (!material.isBlock()) {
mat -= 255; mat -= 255;
} }
sendStatistic(statistic.getId() + mat, amount); sendStatistic(statistic.getId() + mat, amount);
} }
@ -265,7 +261,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
sendStatistic(id, Byte.MAX_VALUE); sendStatistic(id, Byte.MAX_VALUE);
amount -= Byte.MAX_VALUE; amount -= Byte.MAX_VALUE;
} }
getHandle().netServerHandler.sendPacket(new Packet200Statistic(id, amount)); getHandle().netServerHandler.sendPacket(new Packet200Statistic(id, amount));
} }
} }