From 37395a1a6da01e407a8f8894492c10e851c125d7 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 7 Jul 2016 22:10:58 +1200 Subject: [PATCH] Catch errors and cancel, fix some disguises --- .../disguise/utilities/ReflectionManager.java | 63 +++++ .../packetlisteners/PacketListenerMain.java | 13 +- .../PacketListenerViewDisguises.java | 252 +++++++++--------- 3 files changed, 205 insertions(+), 123 deletions(-) diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index 9c590349..1736eb06 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -15,6 +15,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.World; +import org.bukkit.block.BlockFace; import org.bukkit.entity.Ambient; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; @@ -24,7 +25,9 @@ import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; +import org.bukkit.util.EulerAngle; +import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.MinecraftKey; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; @@ -908,6 +911,58 @@ public class ReflectionManager return null; } + private static Object convertInvalidItem(Object value) + { + if (value instanceof Optional) + { + Optional opt = (Optional) value; + + if (!opt.isPresent()) + return value; + + Object val = opt.get(); + + if (val instanceof ItemStack) + { + return Optional.of(getNmsItem((ItemStack) val)); + } + else if (val instanceof BlockPosition) + { + BlockPosition pos = (BlockPosition) val; + + try + { + return Optional.of(getNmsConstructor("BlockPosition", int.class, int.class, int.class).newInstance(pos.getX(), + pos.getY(), pos.getZ())); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + } + else if (value instanceof EulerAngle) + { + EulerAngle angle = (EulerAngle) value; + + try + { + return getNmsConstructor("Vector3f", float.class, float.class, float.class).newInstance((float) angle.getX(), + (float) angle.getY(), (float) angle.getZ()); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + else if (value instanceof BlockFace) + { + return getEnumDirection(((BlockFace) value).ordinal()); + } + + return value; + } + /** * This creates a DataWatcherItem usable with WrappedWatchableObject * @@ -920,6 +975,8 @@ public class ReflectionManager if (value == null) return null; + value = convertInvalidItem(value); + Serializer serializer; if (value instanceof Optional) @@ -933,6 +990,12 @@ public class ReflectionManager serializer = Registry.get(value.getClass()); } + if (serializer == null) + { + throw new IllegalArgumentException( + "Unable to find Serializer for " + value + "! Are you running the latest version of ProtocolLib?"); + } + WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer); Constructor construct = getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class); diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java index 6d93d939..9cdd289a 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerMain.java @@ -51,7 +51,18 @@ public class PacketListenerMain extends PacketAdapter if (entity == observer) return; - PacketContainer[][] packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity); + PacketContainer[][] packets; + + try + { + packets = PacketsManager.transformPacket(event.getPacket(), event.getPlayer(), entity); + } + catch (Exception ex) + { + ex.printStackTrace(); + event.setCancelled(true); + return; + } if (packets == null) { diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java index 3ea108e9..1c5859f0 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerViewDisguises.java @@ -42,151 +42,159 @@ public class PacketListenerViewDisguises extends PacketAdapter if (event.isCancelled()) return; - final Player observer = event.getPlayer(); - - if (observer.getName().contains("UNKNOWN[")) // If the player is temporary - return; - - if (event.getPacket().getIntegers().read(0) != observer.getEntityId()) + try { - return; - } + final Player observer = event.getPlayer(); - if (!DisguiseAPI.isSelfDisguised(observer)) - { - return; - } + if (observer.getName().contains("UNKNOWN[")) // If the player is temporary + return; - // Here I grab the packets to convert them to, So I can display them as if the disguise sent them. - PacketContainer[][] transformed = PacketsManager.transformPacket(event.getPacket(), observer, observer); - - PacketContainer[] packets = transformed == null ? null : transformed[0]; - - final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1]; - - if (packets == null) - { - packets = new PacketContainer[] - { - event.getPacket() - }; - } - - for (PacketContainer packet : packets) - { - if (packet.getType() != Server.PLAYER_INFO) + if (event.getPacket().getIntegers().read(0) != observer.getEntityId()) { - if (packet.equals(event.getPacket())) + return; + } + + if (!DisguiseAPI.isSelfDisguised(observer)) + { + return; + } + + // Here I grab the packets to convert them to, So I can display them as if the disguise sent them. + PacketContainer[][] transformed = PacketsManager.transformPacket(event.getPacket(), observer, observer); + + PacketContainer[] packets = transformed == null ? null : transformed[0]; + + final PacketContainer[] delayedPackets = transformed == null ? null : transformed[1]; + + if (packets == null) + { + packets = new PacketContainer[] + { + event.getPacket() + }; + } + + for (PacketContainer packet : packets) + { + if (packet.getType() != Server.PLAYER_INFO) { - packet = packet.shallowClone(); + if (packet.equals(event.getPacket())) + { + packet = packet.shallowClone(); + } + + packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); } - packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); - } - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - - if (delayedPackets != null && delayedPackets.length > 0) - { - Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() - { - public void run() + try { - try + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + + if (delayedPackets != null && delayedPackets.length > 0) + { + Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() + { + public void run() { - for (PacketContainer packet : delayedPackets) + try { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + for (PacketContainer packet : delayedPackets) + { + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); + } + } + catch (InvocationTargetException e) + { + e.printStackTrace(); } } - catch (InvocationTargetException e) + }, 2); + } + + if (event.getPacketType() == Server.ENTITY_METADATA) + { + event.setPacket(event.getPacket().deepClone()); + + for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) + { + if (watch.getIndex() == 0) { - e.printStackTrace(); + byte b = (byte) watch.getValue(); + + byte a = (byte) (b | 1 << 5); + + if ((b & 1 << 3) != 0) + a = (byte) (a | 1 << 3); + + watch.setValue(a); } } - }, 2); - } - - if (event.getPacketType() == Server.ENTITY_METADATA) - { - event.setPacket(event.getPacket().deepClone()); - - for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) + } + else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) { - if (watch.getIndex() == 0) + event.setCancelled(true); + + PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA); + + StructureModifier mods = packet.getModifier(); + + mods.write(0, observer.getEntityId()); + + List watchableList = new ArrayList<>(); + Byte b = 1 << 5; + + if (observer.isSprinting()) + b = (byte) (b | 1 << 3); + + WrappedWatchableObject watch = ReflectionManager.createWatchable(0, b); + + watchableList.add(watch); + packet.getWatchableCollectionModifier().write(0, watchableList); + + try { - byte b = (byte) watch.getValue(); + ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); + } + catch (InvocationTargetException e) + { + e.printStackTrace(); + } + } + else if (event.getPacketType() == Server.ANIMATION) + { + if (event.getPacket().getIntegers().read(1) != 2) + { + event.setCancelled(true); + } + } + else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE + || event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK + || event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_HEAD_ROTATION + || event.getPacketType() == Server.ENTITY_EFFECT || event.getPacketType() == Server.ENTITY_EQUIPMENT) + { + event.setCancelled(true); + } + else if (event.getPacketType() == Server.ENTITY_STATUS) + { + Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); - byte a = (byte) (b | 1 << 5); - - if ((b & 1 << 3) != 0) - a = (byte) (a | 1 << 3); - - watch.setValue(a); + if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() + && event.getPacket().getBytes().read(0) == 2) + { + event.setCancelled(true); } } } - else if (event.getPacketType() == Server.NAMED_ENTITY_SPAWN) + catch (Exception ex) { event.setCancelled(true); - - PacketContainer packet = new PacketContainer(Server.ENTITY_METADATA); - - StructureModifier mods = packet.getModifier(); - - mods.write(0, observer.getEntityId()); - - List watchableList = new ArrayList<>(); - Byte b = 1 << 5; - - if (observer.isSprinting()) - b = (byte) (b | 1 << 3); - - WrappedWatchableObject watch = ReflectionManager.createWatchable(0, b); - - watchableList.add(watch); - packet.getWatchableCollectionModifier().write(0, watchableList); - - try - { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet); - } - catch (InvocationTargetException e) - { - e.printStackTrace(); - } - } - else if (event.getPacketType() == Server.ANIMATION) - { - if (event.getPacket().getIntegers().read(1) != 2) - { - event.setCancelled(true); - } - } - else if (event.getPacketType() == Server.ATTACH_ENTITY || event.getPacketType() == Server.REL_ENTITY_MOVE - || event.getPacketType() == Server.REL_ENTITY_MOVE_LOOK || event.getPacketType() == Server.ENTITY_LOOK - || event.getPacketType() == Server.ENTITY_TELEPORT || event.getPacketType() == Server.ENTITY_HEAD_ROTATION - || event.getPacketType() == Server.ENTITY_EFFECT || event.getPacketType() == Server.ENTITY_EQUIPMENT) - { - event.setCancelled(true); - } - else if (event.getPacketType() == Server.ENTITY_STATUS) - { - Disguise disguise = DisguiseAPI.getDisguise(event.getPlayer(), event.getPlayer()); - - if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() - && event.getPacket().getBytes().read(0) == 2) - { - event.setCancelled(true); - } + ex.printStackTrace(); } } }