diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketEvent.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketEvent.java index 97bc9fb1..6a0e8530 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketEvent.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketEvent.java @@ -28,11 +28,28 @@ import org.bukkit.event.Cancellable; import com.comphenix.protocol.Application; import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.async.AsyncMarker; +import com.comphenix.protocol.error.Report; +import com.comphenix.protocol.error.ReportType; import com.google.common.base.Objects; import com.google.common.base.Preconditions; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.SetMultimap; +/** + * Represents a packet sending or receiving event. Changes to the packet will be + * reflected in the final version to be sent or received. It is also possible to cancel an event. + * @author Kristian + */ public class PacketEvent extends EventObject implements Cancellable { + public static final ReportType REPORT_CHANGING_PACKET_TYPE_IS_CONFUSING = new ReportType( + "Plugin %s changed packet type from %s to %s in packet listener. This is confusing for other plugins! (Not an error, though!)"); + + private static final SetMultimap CHANGE_WARNINGS = + Multimaps.synchronizedSetMultimap(HashMultimap.create()); + /** * Automatically generated by Eclipse. */ @@ -170,8 +187,19 @@ public class PacketEvent extends EventObject implements Cancellable { throw new IllegalStateException("The packet event is read-only."); if (packet == null) throw new IllegalArgumentException("Cannot set packet to NULL. Use setCancelled() instead."); - if (this.packet != null && !Objects.equal(this.packet.getType(), packet.getType())) - throw new IllegalArgumentException("Cannot change packet type from " + this.packet.getType() + " to " + packet.getType()); + + // Change warnings + final PacketType oldType = this.packet.getType(); + final PacketType newType = packet.getType(); + if (this.packet != null && !Objects.equal(oldType, newType)) { + // Only report this once + if (CHANGE_WARNINGS.put(oldType, newType)) { + ProtocolLibrary.getErrorReporter().reportWarning(this, + Report.newBuilder(REPORT_CHANGING_PACKET_TYPE_IS_CONFUSING). + messageParam(oldType, newType). + build()); + } + } this.packet = packet; }