Skip to content

Commit

Permalink
Silently swallow non-packet packets
Browse files Browse the repository at this point in the history
If the packet listener receives a non-NMS packet, it now just
silently moves on with life and ignores it. Previously it
threw a fit, crashing and disconnecting the player.

The problem is still logged in debug mode, to ease debugging.

Closes: #355
  • Loading branch information
I-Al-Istannen committed Dec 28, 2019
1 parent e1e586a commit 4df12fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Packet createFromNMSPacket(Object nmsPacket){
throw new IllegalStateException("Could not find packet class! Therefore this class is broken.");
}

if(!Reflector.inheritsFrom(nmsPacket.getClass(), NMS_PACKET_CLASS)){
if(!isNmsPacket(nmsPacket)){
throw new IllegalArgumentException("You must pass a 'Packet' object!");
}

Expand All @@ -115,6 +115,16 @@ public static Packet createFromNMSPacket(Object nmsPacket){
}
}

/**
* Returns true if the given object is a NMS packet.
*
* @param nmsPacket the object to check
* @return true if the given object is a NMS packet
*/
public static boolean isNmsPacket(Object nmsPacket){
return Reflector.inheritsFrom(nmsPacket.getClass(), NMS_PACKET_CLASS);
}

/**
* @return the NMS packet
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package kernitus.plugin.OldCombatMechanics.utilities.packet;

import kernitus.plugin.OldCombatMechanics.utilities.Messenger;
import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import kernitus.plugin.OldCombatMechanics.utilities.Messenger;
import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector;
import org.bukkit.entity.Player;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -150,6 +150,11 @@ public void write(ChannelHandlerContext channelHandlerContext, Object packet, Ch
return;
}

if(!Packet.isNmsPacket(packet)){
debug("Received a packet THAT IS NO PACKET: " + packet.getClass() + " " + packet);
return;
}

PacketEvent event = new PacketEvent(
packet,
PacketEvent.ConnectionDirection.TO_CLIENT,
Expand Down Expand Up @@ -186,6 +191,11 @@ public void channelRead(ChannelHandlerContext channelHandlerContext, Object pack
return;
}

if(!Packet.isNmsPacket(packet)){
debug("Received a packet THAT IS NO PACKET: " + packet.getClass() + " " + packet);
return;
}

PacketEvent event = new PacketEvent(
packet,
PacketEvent.ConnectionDirection.TO_SERVER,
Expand Down

0 comments on commit 4df12fd

Please sign in to comment.