/** * Add a collection of packet types to the lookup. * @param types - the types to add. */ public PacketTypeLookup addPacketTypes(Iterable<? extends PacketType> types) { Preconditions.checkNotNull(types, "types cannot be NULL"); for (PacketType type : types) { int legacy = type.getLegacyId(); // Skip unknown legacy packets if (legacy != PacketType.UNKNOWN_PACKET) { if (type.isServer()) serverLookup.put(type.getLegacyId(), type); if (type.isClient()) clientLookup.put(type.getLegacyId(), type); legacyLookup.put(type.getLegacyId(), type); } // Skip unknown current packets if (type.getCurrentId() != PacketType.UNKNOWN_PACKET) { idLookup.getMap(type.getProtocol(), type.getSender()).put(type.getCurrentId(), type); classLookup.getMap(type.getProtocol(), type.getSender()).put(type.getClassNames()[0], type); } nameLookup.put(type.name(), type); } return this; }
/** * Auxiliary method for suppressing exceptions. * * @param protocol * @param sender * @param name * PacketType if available, null otherwise. * @return */ public static PacketType findPacketTypeByName(Protocol protocol, Sender sender, String name) { try { return PacketType.findCurrent(protocol, sender, name); } catch (Throwable t) { // uh return null; } }
/** * Creates a packet container for an existing packet. * @param id - ID of the given packet. * @param handle - contained packet. * @deprecated Use {@link #PacketContainer(PacketType, Object)} instead */ @Deprecated public PacketContainer(int id, Object handle) { this(PacketType.findLegacy(id), handle); }
@Override public String toString() { Class<?> clazz = getPacketClass(); if (clazz == null) return name() + "[" + protocol + ", " + sender + ", " + currentId + ", classNames: " + Arrays.toString(classNames) + " (unregistered)]"; else return name() + "[class=" + clazz.getSimpleName() + ", id=" + currentId + "]"; }
@Override public int compareTo(PacketType other) { return ComparisonChain.start(). compare(protocol, other.getProtocol()). compare(sender, other.getSender()). compare(currentId, other.getCurrentId()). result(); }
private String getPacketId(PacketType type) { return Strings.padStart(Integer.toString(type.getCurrentId()), 2, '0') + " (Legacy: " + type.getLegacyId() + ")"; }
try { // Try IDs first int id = Integer.parseInt(args[2]); type = PacketType.findCurrent(protocol, pSender, id); } catch (NumberFormatException ex) { // Check packet names String name = args[2]; for (PacketType packet : PacketType.values()) { if (packet.getProtocol() == protocol && packet.getSender() == pSender) { if (packet.name().equalsIgnoreCase(name)) { type = packet; break; for (String className : packet.getClassNames()) { if (className.equalsIgnoreCase(name)) { type = packet; sender.sendMessage(ChatColor.GREEN + "Now logging " + type.getPacketClass().getSimpleName()); return true;
for (Iterator<String> it = arguments.iterator(); it.hasNext(); ) { String name = it.next().toUpperCase(Locale.ENGLISH); Collection<PacketType> names = PacketType.fromName(name); if (type.getProtocol() == protocol && type.getSender() == side) { result.add(type); it.remove(); if (PacketType.hasLegacy(id)) { result.add(PacketType.findLegacy(id, side)); if (PacketType.hasCurrent(protocol, side, id)) { result.add(PacketType.findCurrent(protocol, side, id));
@Override public Integer apply(PacketType type) { return type.getLegacyId(); }; });
@Override public Object getGeneric(MultiBlockChangeInfo specific) { try { if (constructor == null) { constructor = nmsClass.getConstructor( PacketType.Play.Server.MULTI_BLOCK_CHANGE.getPacketClass(), short.class, MinecraftReflection.getIBlockDataClass() ); } return constructor.newInstance( null, specific.location, BukkitConverters.getWrappedBlockDataConverter().getGeneric(specific.data) ); } catch (Throwable ex) { throw new RuntimeException("Failed to construct MultiBlockChangeInfo instance.", ex); } }
private void printStatistic(Writer destination, PacketType key, final StatisticsStream stream) throws IOException { destination.write(String.format(STATISTICS_ROW, key != null ? key.getProtocol() : "SUM", key != null ? key.name() : "-", key != null ? getPacketId(key) : "-", stream.getCount(), toMilli(stream.getMinimum()), toMilli(stream.getMaximum()), toMilli(stream.getMean()), toMilli(stream.getStandardDeviation()) )); }
private Set<PacketType> filterTypes(Set<PacketType> types, Sender sender) { Set<PacketType> result = Sets.newHashSet(); for (PacketType type : types) { if (type.getSender() == sender) { result.add(type); } } return result; }
/** * Determine if a given packet may be sent during login. * @param type - the packet type. * @return TRUE if it may, FALSE otherwise. */ public boolean isLoginPacket(PacketType type) { if (!MinecraftReflection.isUsingNetty()) return isLoginPacket(type.getLegacyId(), type.getSender().toSide()); return PacketType.Login.Client.getInstance().hasMember(type) || PacketType.Login.Server.getInstance().hasMember(type) || PacketType.Status.Client.getInstance().hasMember(type) || PacketType.Status.Server.getInstance().hasMember(type); } }
/** * Retrieve a packet type from a legacy (1.6.4 and below) packet ID. * @param packetId - the legacy packet ID. * @param preference - the preferred sender, or NULL for any arbitrary sender. * @return The corresponding packet type. * @throws IllegalArgumentException If the legacy packet could not be found. * @deprecated Legacy IDs haven't functioned properly for some time */ @Deprecated public static PacketType findLegacy(int packetId, Sender preference) { if (preference == null) return findLegacy(packetId); PacketType type = getLookup().getFromLegacy(packetId, preference); if (type != null) return type; throw new IllegalArgumentException("Cannot find legacy packet " + packetId); }
/** * Retrieve the correct object enum from a specific packet type. * @param type - the packet type. * @return The corresponding object enum. */ public static PacketTypeEnum getObjectEnum(final PacketType type) { switch (type.getProtocol()) { case HANDSHAKING: return type.isClient() ? Handshake.Client.getInstance() : Handshake.Server.getInstance(); case PLAY: return type.isClient() ? Play.Client.getInstance() : Play.Server.getInstance(); case STATUS: return type.isClient() ? Status.Client.getInstance() : Status.Server.getInstance(); case LOGIN: return type.isClient() ? Login.Client.getInstance() : Login.Server.getInstance(); case LEGACY: return type.isClient() ? Legacy.Client.getInstance() : Legacy.Server.getInstance(); default: throw new IllegalStateException("Unexpected protocol: " + type.getProtocol()); } }
private void ensureMainThread(UUID uuid, PacketContainer packet) { Player player = Bukkit.getPlayer(uuid); if (player == null) { return; } PacketType packetType = packet.getType(); if (packetType.equals(SCOREBOARD_SCORE)) { handleScorePacket(player, packet); } else if (packetType.equals(SCOREBOARD_OBJECTIVE)) { handleObjectivePacket(player, packet); } else if (packetType.equals(SCOREBOARD_DISPLAY_OBJECTIVE)) { handleDisplayPacket(player, packet); } else if (packetType.equals(SCOREBOARD_TEAM)) { handleTeamPacket(player, packet); } }
private static PacketType find(Map<String, PacketType> map, String clazz) { PacketType ret = map.get(clazz); if (ret != null) { return ret; } // Check any aliases for (PacketType check : map.values()) { String[] aliases = check.getClassNames(); if (aliases.length > 1) { for (String alias : aliases) { if (alias.equals(clazz)) { // We have a match! return check; } } } } return null; }
@Override public InputStream openStream() throws IOException { byte[] data = new byte[] { (byte) type.getLegacyId() }; return new ByteArrayInputStream(data); } };
public static Class<?> getSoundEffectClass() { try { return getMinecraftClass("SoundEffect"); } catch (RuntimeException ex) { FuzzyReflection fuzzy = FuzzyReflection.fromClass(PacketType.Play.Server.NAMED_SOUND_EFFECT.getPacketClass(), true); Field field = fuzzy.getFieldByType("(.*)(Sound)(.*)"); return setMinecraftClass("SoundEffect", field.getType()); } }