Skip to content

Commit 30fe81d

Browse files
committed
Move getPacketDescription from the command to HexDumper as a public API
Related to filoghost/HolographicDisplays#385
1 parent b54dd49 commit 30fe81d

File tree

2 files changed

+72
-69
lines changed

2 files changed

+72
-69
lines changed

src/main/java/com/comphenix/protocol/CommandPacket.java

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ private enum SubCommand {
7676
*/
7777
public static final int PAGE_LINE_COUNT = 9;
7878

79-
/**
80-
* Number of bytes before we do a hex dump.
81-
*/
82-
private static final int HEX_DUMP_THRESHOLD = 256;
83-
8479
private Plugin plugin;
8580
private Logger logger;
8681
private ProtocolManager manager;
@@ -370,7 +365,7 @@ private void printInformation(PacketEvent event) {
370365
logger.info("Initial packet:\n" + original + " -> ");
371366
}
372367

373-
logger.info(shortDescription + ":\n" + getPacketDescription(
368+
logger.info(shortDescription + ":\n" + HexDumper.getPacketDescription(
374369
event.getPacket())
375370
);
376371
} catch (IllegalAccessException e) {
@@ -427,7 +422,7 @@ public void onPacketReceiving(PacketEvent event) {
427422
*/
428423
private void savePacketState(PacketEvent event) {
429424
try {
430-
originalPackets.put(event, getPacketDescription(event.getPacket()));
425+
originalPackets.put(event, HexDumper.getPacketDescription(event.getPacket()));
431426
} catch (IllegalAccessException e) {
432427
throw new RuntimeException("Cannot read packet.", e);
433428
}
@@ -449,68 +444,6 @@ public Plugin getPlugin() {
449444
}
450445
};
451446
}
452-
453-
/**
454-
* Retrieve a detailed string representation of the given packet.
455-
* @param packetContainer - the packet to describe.
456-
* @return The detailed description.
457-
* @throws IllegalAccessException An error occured.
458-
*/
459-
public String getPacketDescription(PacketContainer packetContainer) throws IllegalAccessException {
460-
Object packet = packetContainer.getHandle();
461-
Class<?> clazz = packet.getClass();
462-
463-
// Get the first Minecraft super class
464-
while (clazz != null && clazz != Object.class &&
465-
(!MinecraftReflection.isMinecraftClass(clazz) ||
466-
ByteBuddyGenerated.class.isAssignableFrom(clazz))) {
467-
clazz = clazz.getSuperclass();
468-
}
469-
470-
return PrettyPrinter.printObject(packet, clazz, MinecraftReflection.getPacketClass(), PrettyPrinter.RECURSE_DEPTH, new ObjectPrinter() {
471-
@Override
472-
public boolean print(StringBuilder output, Object value) {
473-
// Special case
474-
if (value instanceof byte[]) {
475-
byte[] data = (byte[]) value;
476-
477-
if (data.length > HEX_DUMP_THRESHOLD) {
478-
output.append("[");
479-
HexDumper.defaultDumper().appendTo(output, data);
480-
output.append("]");
481-
return true;
482-
}
483-
} else if (value != null) {
484-
EquivalentConverter<Object> converter = findConverter(value.getClass());
485-
486-
if (converter != null) {
487-
output.append(converter.getSpecific(value));
488-
return true;
489-
}
490-
}
491-
return false;
492-
}
493-
});
494-
}
495-
496-
/**
497-
* Retrieve the closest equivalent converter to a specific class.
498-
* @param clazz - the class.
499-
* @return The closest converter, or NULL if not found,
500-
*/
501-
private EquivalentConverter<Object> findConverter(Class<?> clazz) {
502-
Map<Class<?>, EquivalentConverter<Object>> converters = BukkitConverters.getConvertersForGeneric();
503-
504-
while (clazz != null) {
505-
EquivalentConverter<Object> result = converters.get(clazz);
506-
507-
if (result != null)
508-
return result;
509-
else
510-
clazz = clazz.getSuperclass();
511-
}
512-
return null;
513-
}
514447

515448
public PacketListener updatePacketListener() {
516449
if (listener != null) {

src/main/java/com/comphenix/protocol/utility/HexDumper.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.comphenix.protocol.utility;
22

33
import java.io.IOException;
4+
import java.util.Map;
45

6+
import com.comphenix.protocol.events.PacketContainer;
7+
import com.comphenix.protocol.reflect.EquivalentConverter;
8+
import com.comphenix.protocol.reflect.PrettyPrinter;
9+
import com.comphenix.protocol.wrappers.BukkitConverters;
510
import com.google.common.base.Preconditions;
611

712
/**
@@ -12,6 +17,11 @@
1217
public class HexDumper {
1318
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
1419

20+
/**
21+
* Number of bytes before we do a hex dump.
22+
*/
23+
private static final int HEX_DUMP_THRESHOLD = 256;
24+
1525
// Default values
1626
private int positionLength = 6;
1727
private char[] positionSuffix = ": ".toCharArray();
@@ -228,4 +238,64 @@ public int getLineLength(int byteCount) {
228238
// Total expected length of each line
229239
return constant + delimiter.length * (groups - 1) + groupLength * groups;
230240
}
241+
242+
/**
243+
* Retrieve the closest equivalent converter to a specific class.
244+
* @param clazz - the class.
245+
* @return The closest converter, or NULL if not found,
246+
*/
247+
public static EquivalentConverter<Object> findConverter(Class<?> clazz) {
248+
Map<Class<?>, EquivalentConverter<Object>> converters = BukkitConverters.getConvertersForGeneric();
249+
250+
while (clazz != null) {
251+
EquivalentConverter<Object> result = converters.get(clazz);
252+
253+
if (result != null)
254+
return result;
255+
else
256+
clazz = clazz.getSuperclass();
257+
}
258+
return null;
259+
}
260+
261+
/**
262+
* Retrieve a detailed string representation of the given packet.
263+
* @param packetContainer - the packet to describe.
264+
* @return The detailed description.
265+
* @throws IllegalAccessException An error occured.
266+
*/
267+
public static String getPacketDescription(PacketContainer packetContainer) throws IllegalAccessException {
268+
Object packet = packetContainer.getHandle();
269+
Class<?> clazz = packet.getClass();
270+
271+
// Get the first Minecraft super class
272+
while (clazz != null && clazz != Object.class &&
273+
(!MinecraftReflection.isMinecraftClass(clazz) ||
274+
ByteBuddyGenerated.class.isAssignableFrom(clazz))) {
275+
clazz = clazz.getSuperclass();
276+
}
277+
278+
return PrettyPrinter.printObject(packet, clazz, MinecraftReflection.getPacketClass(),
279+
PrettyPrinter.RECURSE_DEPTH, (output, value) -> {
280+
// Special case
281+
if (value instanceof byte[]) {
282+
byte[] data = (byte[]) value;
283+
284+
if (data.length > HEX_DUMP_THRESHOLD) {
285+
output.append("[");
286+
HexDumper.defaultDumper().appendTo(output, data);
287+
output.append("]");
288+
return true;
289+
}
290+
} else if (value != null) {
291+
EquivalentConverter<Object> converter = findConverter(value.getClass());
292+
293+
if (converter != null) {
294+
output.append(converter.getSpecific(value));
295+
return true;
296+
}
297+
}
298+
return false;
299+
});
300+
}
231301
}

0 commit comments

Comments
 (0)