Skip to content

Commit

Permalink
Catch IllegalArgumentException from ProtocolLib
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 21, 2022
1 parent 1a102e7 commit 45d7a07
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
41 changes: 30 additions & 11 deletions main/src/main/java/net/citizensnpcs/ProtocolLibListener.java
Expand Up @@ -18,6 +18,7 @@
import com.comphenix.protocol.wrappers.EnumWrappers;

import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.RotationTrait.PacketRotationSession;
Expand All @@ -33,23 +34,15 @@ public ProtocolLibListener(Citizens plugin) {
flagsClass = MinecraftReflection.getMinecraftClass("EnumPlayerTeleportFlags",
"PacketPlayOutPosition$EnumPlayerTeleportFlags",
"network.protocol.game.PacketPlayOutPosition$EnumPlayerTeleportFlags");

manager.addPacketListener(
new PacketAdapter(plugin, ListenerPriority.MONITOR, Server.ENTITY_HEAD_ROTATION, Server.ENTITY_LOOK) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
Entity entity = null;
try {
entity = manager.getEntityFromID(event.getPlayer().getWorld(),
packet.getIntegers().getValues().get(0));
} catch (FieldAccessException ex) {
return;
}

if (!(entity instanceof NPCHolder))
NPC npc = getNPCFromPacket(event);
if (npc == null)
return;

NPC npc = ((NPCHolder) entity).getNPC();
RotationTrait trait = npc.getTraitNullable(RotationTrait.class);
if (trait == null)
return;
Expand All @@ -58,6 +51,7 @@ public void onPacketSending(PacketEvent event) {
if (session == null || !session.isActive())
return;

PacketContainer packet = event.getPacket();
PacketType type = event.getPacketType();
if (type == Server.ENTITY_HEAD_ROTATION) {
packet.getBytes().write(0, degToByte(session.getHeadYaw()));
Expand All @@ -83,6 +77,29 @@ private StructureModifier<Set<PlayerTeleportFlag>> getFlagsModifier(PacketContai
return handle.getSets(EnumWrappers.getGenericConverter(flagsClass, PlayerTeleportFlag.class));
}

private NPC getNPCFromPacket(PacketEvent event) {
PacketContainer packet = event.getPacket();
Entity entity = null;
try {
Integer id = packet.getIntegers().readSafely(0);
if (id == null)
return null;
entity = manager.getEntityFromID(event.getPlayer().getWorld(), id);
} catch (FieldAccessException | IllegalArgumentException ex) {
if (!LOGGED_ERROR) {
Messaging.severe(
"Error retrieving entity from ID: ProtocolLib error? Suppressing further exceptions unless debugging.");
ex.printStackTrace();
LOGGED_ERROR = true;
} else if (Messaging.isDebugging()) {
ex.printStackTrace();
}
return null;
}

return entity instanceof NPCHolder ? ((NPCHolder) entity).getNPC() : null;
}

public enum PlayerTeleportFlag {
X,
Y,
Expand All @@ -94,4 +111,6 @@ public enum PlayerTeleportFlag {
private static byte degToByte(float in) {
return (byte) (in * 256.0F / 360.0F);
}

private static boolean LOGGED_ERROR = false;
}
Expand Up @@ -73,7 +73,7 @@ private Team getTeam() {

@Override
public void onDespawn() {
previosuGlowingColor = null;
previousGlowingColor = null;
if (npc.getEntity() == null)
return;
String name = npc.getEntity() instanceof Player ? npc.getEntity().getName() : npc.getUniqueId().toString();
Expand Down

0 comments on commit 45d7a07

Please sign in to comment.