diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java index da7e8ba1..1669bdd2 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/CommandPacket.java @@ -193,7 +193,7 @@ class CommandPacket extends CommandBase { sender.sendMessage(ChatColor.YELLOW + "Warning: Missing protocol (PLAY, etc) - assuming legacy IDs."); } if (arguments.size() > 0) { - throw new IllegalArgumentException("Insufficient arguments."); + throw new IllegalArgumentException("Cannot parse " + arguments); } // The last element is optional diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java index 4b64d140..7b8ca8d0 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java @@ -2,6 +2,7 @@ package com.comphenix.protocol; import java.io.Serializable; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.UUID; import java.util.concurrent.Callable; @@ -690,6 +691,17 @@ public class PacketType implements Serializable, Comparable { throw new IllegalArgumentException("Class " + packetClass + " is not a registered packet."); } + /** + * Retrieve every packet type with the given UPPER_CAMEL_CASE name. + *

+ * Note that the collection is unmodiable. + * @param name - the name. + * @return Every packet type, or an empty collection. + */ + public static Collection fromName(String name) { + return getLookup().getFromName(name); + } + /** * Determine if a given class represents a packet class. * @param packetClass - the class to lookup. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeLookup.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeLookup.java index 38d1602f..1f3e42cd 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeLookup.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeLookup.java @@ -1,9 +1,16 @@ package com.comphenix.protocol; +import java.util.Collection; +import java.util.Collections; + import com.comphenix.protocol.PacketType.Protocol; import com.comphenix.protocol.PacketType.Sender; import com.comphenix.protocol.collections.IntegerMap; import com.google.common.base.Preconditions; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; /** * Retrieve a packet type based on its version and ID, optionally with protocol and sender too. @@ -51,6 +58,9 @@ class PacketTypeLookup { // Packets for 1.7.2 private final ProtocolSenderLookup currentLookup = new ProtocolSenderLookup(); + // Packets based on name + private final Multimap nameLookup = HashMultimap.create(); + /** * Add a collection of packet types to the lookup. * @param types - the types to add. @@ -73,6 +83,8 @@ class PacketTypeLookup { if (type.getCurrentId() != PacketType.UNKNOWN_PACKET) { currentLookup.getMap(type.getProtocol(), type.getSender()).put(type.getCurrentId(), type); } + // Save name + nameLookup.put(type.name(), type); } return this; } @@ -86,6 +98,15 @@ class PacketTypeLookup { return legacyLookup.get(packetId); } + /** + * Retrieve an unmodifiable view of all the packet types with this name. + * @param name - the name. + * @return The packet types, usually one. + */ + public Collection getFromName(String name) { + return Collections.unmodifiableCollection(nameLookup.get(name)); + } + /** * Retrieve a packet type from a legacy (1.6.4 and below) packet ID. * @param packetId - the legacy packet ID. diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeParser.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeParser.java index 7432a0fa..b6ddf70a 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeParser.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketTypeParser.java @@ -1,6 +1,8 @@ package com.comphenix.protocol; +import java.util.Collection; import java.util.Deque; +import java.util.Iterator; import java.util.List; import java.util.Set; @@ -21,7 +23,9 @@ class PacketTypeParser { public Set parseTypes(Deque arguments, Range defaultRange) { Set result = Sets.newHashSet(); - + side = null; + protocol = null; + // Find these first while (side == null) { String arg = arguments.poll(); @@ -45,9 +49,24 @@ class PacketTypeParser { // Then we move on to parsing IDs (named packet types soon to come) List> ranges = RangeParser.getRanges(arguments, DEFAULT_MAX_RANGE); + + // And finally, parse packet names if we have a protocol + if (protocol != null) { + for (Iterator it = arguments.iterator(); it.hasNext(); ) { + String name = it.next().toUpperCase(); + Collection names = PacketType.fromName(name); + + for (PacketType type : names) { + if (type.getProtocol() == protocol && type.getSender() == side) { + result.add(type); + it.remove(); + } + } + } + } // Supply a default integer range - if (ranges.size() == 0) { + if (ranges.isEmpty() && result.isEmpty()) { ranges = Lists.newArrayList(); ranges.add(defaultRange); }