From 188f03b39e6bff8fbd6f8ef52bbaf7666582642e Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sat, 3 Nov 2012 07:32:14 +0100 Subject: [PATCH] Fixed the columns slightly. Still needs tweaking. --- .../com/comphenix/protocol/CommandPacket.java | 37 ++++++++++++++++++- .../protocol/utility/ChatExtensions.java | 2 +- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java index a833f57f..bee9c430 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java @@ -28,6 +28,7 @@ import com.comphenix.protocol.injector.GamePhase; import com.comphenix.protocol.reflect.FieldAccessException; import com.comphenix.protocol.reflect.PrettyPrinter; import com.comphenix.protocol.utility.ChatExtensions; +import com.google.common.base.Strings; import com.google.common.collect.DiscreteDomains; import com.google.common.collect.Range; import com.google.common.collect.Ranges; @@ -54,6 +55,11 @@ class CommandPacket implements CommandExecutor { * Name of this command. */ public static final String NAME = "packet"; + + /** + * Default width of the chat window. + */ + private static final int CHAT_WIDTH = 90; private Plugin plugin; private Logger logger; @@ -127,7 +133,7 @@ class CommandPacket implements CommandExecutor { /** * Send a message without invoking the packet listeners. - * @param player - the player to send it to. + * @param receiver - the player to send it to. * @param message - the message to send. * @return TRUE if the message was sent successfully, FALSE otherwise. */ @@ -205,15 +211,24 @@ class CommandPacket implements CommandExecutor { } else if (subCommand == SubCommand.NAMES) { Set named = getNamedPackets(side); + List messages = new ArrayList(); // Print the equivalent name of every given ID for (Range range : ranges) { for (int id : range.asSet(DiscreteDomains.integers())) { if (named.contains(id)) { - sendMessageSilently(sender, ChatColor.BLUE + "" + id + ": " + Packets.getDeclaredName(id)); + messages.add(ChatColor.BLUE + "" + id + ": " + Packets.getDeclaredName(id)); } } } + + // Convert to two rows + messages = getMessagesInRows(messages, 2, CHAT_WIDTH); + + // Print that + for (String message : messages) { + sendMessageSilently(sender, message); + } } } catch (NumberFormatException e) { @@ -228,6 +243,24 @@ class CommandPacket implements CommandExecutor { return false; } + private List getMessagesInRows(List messages, int rows, int totalWidth) { + List output = new ArrayList(); + int columnWidth = totalWidth / rows; + + for (int i = 0; i < messages.size(); i++) { + int mapped = i / rows; + + // Either create a new row, or add to an existing row + if (mapped < output.size()) { + output.set(mapped, Strings.padEnd(output.get(mapped), columnWidth, ' ') + messages.get(i)); + } else { + output.add(messages.get(i)); + } + } + + return output; + } + /** * Parse ranges from an array of tokens. * @param args - array of tokens. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java index fdf6bc21..ff7621a5 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/ChatExtensions.java @@ -28,7 +28,7 @@ public class ChatExtensions { /** * Send a message without invoking the packet listeners. - * @param player - the player to send it to. + * @param receiver - the receiver. * @param message - the message to send. * @return TRUE if the message was sent successfully, FALSE otherwise. * @throws InvocationTargetException If we were unable to send the message.