Archiviert
13
0

Look for the correct client or server packet when we have the chance.

This should improve backwards compatiblity.
Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-12-05 20:40:45 +01:00
Ursprung 8f408c2109
Commit 348019c1e1
12 geänderte Dateien mit 106 neuen und 13 gelöschten Zeilen

Datei anzeigen

@ -443,6 +443,23 @@ public class PacketType implements Serializable {
throw new IllegalArgumentException("Cannot find legacy packet " + packetId); throw new IllegalArgumentException("Cannot find legacy packet " + packetId);
} }
/**
* 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.
*/
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 a packet type from a protocol, sender and packet ID. * Retrieve a packet type from a protocol, sender and packet ID.
* @param protocol - the current protocol. * @param protocol - the current protocol.

Datei anzeigen

@ -45,6 +45,8 @@ class PacketTypeLookup {
// Packet IDs from 1.6.4 and below // Packet IDs from 1.6.4 and below
private final IntegerMap<PacketType> legacyLookup = new IntegerMap<PacketType>(); private final IntegerMap<PacketType> legacyLookup = new IntegerMap<PacketType>();
private final IntegerMap<PacketType> serverLookup = new IntegerMap<PacketType>();
private final IntegerMap<PacketType> clientLookup = new IntegerMap<PacketType>();
// Packets for 1.7.2 // Packets for 1.7.2
private final ProtocolSenderLookup currentLookup = new ProtocolSenderLookup(); private final ProtocolSenderLookup currentLookup = new ProtocolSenderLookup();
@ -61,6 +63,10 @@ class PacketTypeLookup {
// Skip unknown legacy packets // Skip unknown legacy packets
if (legacy != PacketType.UNKNOWN_PACKET) { 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); legacyLookup.put(type.getLegacyId(), type);
} }
currentLookup.getMap(type.getProtocol(), type.getSender()).put(type.getCurrentId(), type); currentLookup.getMap(type.getProtocol(), type.getSender()).put(type.getCurrentId(), type);
@ -77,6 +83,27 @@ class PacketTypeLookup {
return legacyLookup.get(packetId); return legacyLookup.get(packetId);
} }
/**
* Retrieve a packet type from a legacy (1.6.4 and below) packet ID.
* @param packetId - the legacy packet ID.
* @param preference - which packet type to look for first.
* @return The corresponding packet type, or NULL if not found.
*/
public PacketType getFromLegacy(int packetId, Sender preference) {
if (preference == Sender.CLIENT)
return getFirst(packetId, clientLookup, serverLookup);
else
return getFirst(packetId, serverLookup, clientLookup);
}
// Helper method for looking up in two sets
private <T> T getFirst(int packetId, IntegerMap<T> first, IntegerMap<T> second) {
if (first.containsKey(packetId))
return first.get(packetId);
else
return second.get(packetId);
}
/** /**
* Retrieve a packet type from a protocol, sender and packet ID. * Retrieve a packet type from a protocol, sender and packet ID.
* @param protocol - the current protocol. * @param protocol - the current protocol.

Datei anzeigen

@ -17,6 +17,8 @@
package com.comphenix.protocol.events; package com.comphenix.protocol.events;
import com.comphenix.protocol.PacketType.Sender;
/** /**
* Used to set a packet filter. * Used to set a packet filter.
* *
@ -46,6 +48,20 @@ public enum ConnectionSide {
return this == SERVER_SIDE || this == BOTH; return this == SERVER_SIDE || this == BOTH;
} }
/**
* Retrieve the sender of this connection side.
* <p>
* This is NULL for {@link #BOTH}.
* @return The sender.
*/
public Sender getSender() {
if (this == SERVER_SIDE)
return Sender.SERVER;
else if (this == CLIENT_SIDE)
return Sender.CLIENT;
return null;
}
/** /**
* If both connection sides are present, return {@link #BOTH} - otherwise, return the one valud connection side. * If both connection sides are present, return {@link #BOTH} - otherwise, return the one valud connection side.
* <p> * <p>

Datei anzeigen

@ -195,7 +195,7 @@ public abstract class PacketAdapter implements PacketListener {
GamePhase gamePhase, ListenerOptions[] options, Integer... packets) { GamePhase gamePhase, ListenerOptions[] options, Integer... packets) {
this(plugin, connectionSide, listenerPriority, gamePhase, options, this(plugin, connectionSide, listenerPriority, gamePhase, options,
PacketRegistry.toPacketTypes(Sets.newHashSet(packets)).toArray(new PacketType[0]) PacketRegistry.toPacketTypes(Sets.newHashSet(packets), connectionSide.getSender()).toArray(new PacketType[0])
); );
} }

Datei anzeigen

@ -122,27 +122,36 @@ public class PacketContainer implements Serializable {
/** /**
* Creates a packet container for a new packet. * Creates a packet container for a new packet.
* <p>
* Deprecated: Use {@link #PacketContainer(PacketType)} instead.
* @param id - ID of the packet to create. * @param id - ID of the packet to create.
*/ */
@Deprecated
public PacketContainer(int id) { public PacketContainer(int id) {
this(PacketType.findLegacy(id), StructureCache.newPacket(PacketType.findLegacy(id))); this(PacketType.findLegacy(id), StructureCache.newPacket(PacketType.findLegacy(id)));
} }
/** /**
* Creates a packet container for an existing packet. * Creates a packet container for an existing packet.
* <p>
* Deprecated: Use {@link #PacketContainer(PacketType, Object))} instead.
* @param id - ID of the given packet. * @param id - ID of the given packet.
* @param handle - contained packet. * @param handle - contained packet.
*/ */
@Deprecated
public PacketContainer(int id, Object handle) { public PacketContainer(int id, Object handle) {
this(PacketType.findLegacy(id), handle); this(PacketType.findLegacy(id), handle);
} }
/** /**
* Creates a packet container for an existing packet. * Creates a packet container for an existing packet.
* <p>
* Deprecated: Use {@link #PacketContainer(PacketType, Object, StructureModifier))} instead.
* @param id - ID of the given packet. * @param id - ID of the given packet.
* @param handle - contained packet. * @param handle - contained packet.
* @param structure - structure modifier. * @param structure - structure modifier.
*/ */
@Deprecated
public PacketContainer(int id, Object handle, StructureModifier<Object> structure) { public PacketContainer(int id, Object handle, StructureModifier<Object> structure) {
this(PacketType.findLegacy(id), handle, structure); this(PacketType.findLegacy(id), handle, structure);
} }
@ -495,7 +504,7 @@ public class PacketContainer implements Serializable {
*/ */
public PacketContainer shallowClone() { public PacketContainer shallowClone() {
Object clonedPacket = SHALLOW_CLONER.clone(getHandle()); Object clonedPacket = SHALLOW_CLONER.clone(getHandle());
return new PacketContainer(getID(), clonedPacket); return new PacketContainer(getType(), clonedPacket);
} }
/** /**

Datei anzeigen

@ -17,6 +17,7 @@ import org.bukkit.plugin.PluginManager;
import com.comphenix.protocol.AsynchronousManager; import com.comphenix.protocol.AsynchronousManager;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
@ -382,12 +383,12 @@ public class DelayedPacketManager implements ProtocolManager, InternalManager {
@Override @Override
public Set<PacketType> getSendingFilterTypes() { public Set<PacketType> getSendingFilterTypes() {
return PacketRegistry.toPacketTypes(getSendingFilters()); return PacketRegistry.toPacketTypes(getSendingFilters(), Sender.SERVER);
} }
@Override @Override
public Set<PacketType> getReceivingFilterTypes() { public Set<PacketType> getReceivingFilterTypes() {
return PacketRegistry.toPacketTypes(getReceivingFilters()); return PacketRegistry.toPacketTypes(getReceivingFilters(), Sender.CLIENT);
} }
@Override @Override

Datei anzeigen

@ -23,6 +23,7 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
@ -236,7 +237,7 @@ public class PacketRegistry {
// Handle legacy // Handle legacy
if (NETTY_SERVER_PACKETS == null) { if (NETTY_SERVER_PACKETS == null) {
NETTY_SERVER_PACKETS = toPacketTypes(LEGACY.getServerPackets()); NETTY_SERVER_PACKETS = toPacketTypes(LEGACY.getServerPackets(), Sender.SERVER);
} }
return NETTY_SERVER_PACKETS; return NETTY_SERVER_PACKETS;
} }
@ -273,7 +274,7 @@ public class PacketRegistry {
// Handle legacy // Handle legacy
if (NETTY_CLIENT_PACKETS == null) { if (NETTY_CLIENT_PACKETS == null) {
NETTY_CLIENT_PACKETS = toPacketTypes(LEGACY.getClientPackets()); NETTY_CLIENT_PACKETS = toPacketTypes(LEGACY.getClientPackets(), Sender.CLIENT);
} }
return NETTY_CLIENT_PACKETS; return NETTY_CLIENT_PACKETS;
} }
@ -297,10 +298,20 @@ public class PacketRegistry {
* @return Set of packet types. * @return Set of packet types.
*/ */
public static Set<PacketType> toPacketTypes(Set<Integer> ids) { public static Set<PacketType> toPacketTypes(Set<Integer> ids) {
return toPacketTypes(ids);
}
/**
* Convert a set of legacy packet IDs to packet types.
* @param types - legacy packet IDs.
* @param preference - the sender preference, if any.
* @return Set of packet types.
*/
public static Set<PacketType> toPacketTypes(Set<Integer> ids, Sender preference) {
Set<PacketType> result = Sets.newHashSet(); Set<PacketType> result = Sets.newHashSet();
for (int id : ids) for (int id : ids)
result.add(PacketType.findLegacy(id)); result.add(PacketType.findLegacy(id, preference));
return Collections.unmodifiableSet(result); return Collections.unmodifiableSet(result);
} }

Datei anzeigen

@ -33,6 +33,7 @@ import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.NoOp; import net.sf.cglib.proxy.NoOp;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
@ -318,7 +319,7 @@ class ProxyPacketInjector implements PacketInjector {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public Set<PacketType> getPacketHandlers() { public Set<PacketType> getPacketHandlers() {
return PacketRegistry.toPacketTypes(PacketRegistry.getPreviousPackets().keySet()); return PacketRegistry.toPacketTypes(PacketRegistry.getPreviousPackets().keySet(), Sender.CLIENT);
} }
// Called from the ReadPacketModified monitor // Called from the ReadPacketModified monitor

Datei anzeigen

@ -23,6 +23,8 @@ import java.io.InputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
@ -140,7 +142,8 @@ class ReadPacketModifier implements MethodInterceptor {
byte[] buffer = bufferStream != null ? bufferStream.toByteArray() : null; byte[] buffer = bufferStream != null ? bufferStream.toByteArray() : null;
// Let the people know // Let the people know
PacketContainer container = new PacketContainer(packetID, thisObj); PacketType type = PacketType.findLegacy(packetID, Sender.CLIENT);
PacketContainer container = new PacketContainer(type, thisObj);
PacketEvent event = packetInjector.packetRecieved(container, input, buffer); PacketEvent event = packetInjector.packetRecieved(container, input, buffer);
// Handle override // Handle override

Datei anzeigen

@ -29,7 +29,9 @@ import net.sf.cglib.proxy.Factory;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.Packets; import com.comphenix.protocol.Packets;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.error.ReportType;
@ -608,7 +610,8 @@ public abstract class PlayerInjector implements SocketInjector {
NetworkMarker marker = queuedMarkers.remove(packet); NetworkMarker marker = queuedMarkers.remove(packet);
// A packet has been sent guys! // A packet has been sent guys!
PacketContainer container = new PacketContainer(id, packet); PacketType type = PacketType.findLegacy(id, Sender.SERVER);
PacketContainer container = new PacketContainer(type, packet);
PacketEvent event = PacketEvent.fromServer(invoker, container, marker, currentPlayer); PacketEvent event = PacketEvent.fromServer(invoker, container, marker, currentPlayer);
invoker.invokePacketSending(event); invoker.invokePacketSending(event);

Datei anzeigen

@ -35,6 +35,7 @@ import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.Packets; import com.comphenix.protocol.Packets;
import com.comphenix.protocol.concurrency.BlockingHashMap; import com.comphenix.protocol.concurrency.BlockingHashMap;
import com.comphenix.protocol.concurrency.IntegerSet; import com.comphenix.protocol.concurrency.IntegerSet;
@ -711,7 +712,7 @@ class ProxyPlayerInjectionHandler implements PlayerInjectionHandler {
*/ */
@Override @Override
public Set<PacketType> getSendingFilters() { public Set<PacketType> getSendingFilters() {
return PacketRegistry.toPacketTypes(sendingFilters.toSet()); return PacketRegistry.toPacketTypes(sendingFilters.toSet(), Sender.SERVER);
} }
@Override @Override

Datei anzeigen

@ -22,6 +22,8 @@ import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.NoOp; import net.sf.cglib.proxy.NoOp;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.concurrency.PacketTypeSet; import com.comphenix.protocol.concurrency.PacketTypeSet;
import com.comphenix.protocol.error.DelegatedErrorReporter; import com.comphenix.protocol.error.DelegatedErrorReporter;
import com.comphenix.protocol.error.ErrorReporter; import com.comphenix.protocol.error.ErrorReporter;
@ -426,7 +428,8 @@ public class SpigotPacketInjector implements SpigotPacketListener {
} }
Player sender = getInjector(networkManager, connection).getUpdatedPlayer(); Player sender = getInjector(networkManager, connection).getUpdatedPlayer();
PacketContainer container = new PacketContainer(id, packet); PacketType type = PacketType.findLegacy(id, Sender.CLIENT);
PacketContainer container = new PacketContainer(type, packet);
PacketEvent event = packetReceived(container, sender, readBufferedPackets.get(packet)); PacketEvent event = packetReceived(container, sender, readBufferedPackets.get(packet));
if (!event.isCancelled()) if (!event.isCancelled())
@ -450,7 +453,8 @@ public class SpigotPacketInjector implements SpigotPacketListener {
} }
Player reciever = getInjector(networkManager, connection).getUpdatedPlayer(); Player reciever = getInjector(networkManager, connection).getUpdatedPlayer();
PacketContainer container = new PacketContainer(id, packet); PacketType type = PacketType.findLegacy(id, Sender.SERVER);
PacketContainer container = new PacketContainer(type, packet);
PacketEvent event = packetQueued(container, reciever); PacketEvent event = packetQueued(container, reciever);
if (!event.isCancelled()) if (!event.isCancelled())