Archiviert
13
0

Lookup packet types by name as well.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2014-01-20 06:21:20 +01:00
Ursprung 5cffccd8ca
Commit 7ae0399721
4 geänderte Dateien mit 55 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -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

Datei anzeigen

@ -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<PacketType> {
throw new IllegalArgumentException("Class " + packetClass + " is not a registered packet.");
}
/**
* Retrieve every packet type with the given UPPER_CAMEL_CASE name.
* <p>
* Note that the collection is unmodiable.
* @param name - the name.
* @return Every packet type, or an empty collection.
*/
public static Collection<PacketType> fromName(String name) {
return getLookup().getFromName(name);
}
/**
* Determine if a given class represents a packet class.
* @param packetClass - the class to lookup.

Datei anzeigen

@ -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<String, PacketType> 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<PacketType> 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.

Datei anzeigen

@ -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,6 +23,8 @@ class PacketTypeParser {
public Set<PacketType> parseTypes(Deque<String> arguments, Range<Integer> defaultRange) {
Set<PacketType> result = Sets.newHashSet();
side = null;
protocol = null;
// Find these first
while (side == null) {
@ -46,8 +50,23 @@ class PacketTypeParser {
// Then we move on to parsing IDs (named packet types soon to come)
List<Range<Integer>> ranges = RangeParser.getRanges(arguments, DEFAULT_MAX_RANGE);
// And finally, parse packet names if we have a protocol
if (protocol != null) {
for (Iterator<String> it = arguments.iterator(); it.hasNext(); ) {
String name = it.next().toUpperCase();
Collection<PacketType> 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);
}