Search in sources :

Example 1 with MyPetBukkitPart

use of de.Keyle.MyPet.api.entity.MyPetBukkitPart in project MyPet by xXKeyleXx.

the class ProtocolLibHook method registerSyncEnderDragonInteractionFix.

private void registerSyncEnderDragonInteractionFix() {
    ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), PacketType.Play.Client.USE_ENTITY) {

        @Override
        public void onPacketReceiving(PacketEvent event) {
            if ((checkTemporaryPlayers && event.isPlayerTemporary()) || event.isCancelled()) {
                return;
            }
            PacketContainer packet = event.getPacket();
            if (packet.getType() == PacketType.Play.Client.USE_ENTITY) {
                try {
                    Entity ent = Bukkit.getServer().getScheduler().callSyncMethod(MyPetApi.getPlugin(), () -> {
                        int id = packet.getIntegers().read(0);
                        Entity entity = null;
                        try {
                            entity = packet.getEntityModifier(event).readSafely(0);
                        } catch (RuntimeException ignored) {
                        }
                        if (entity == null && event.getPlayer() != null) {
                            entity = MyPetApi.getPlatformHelper().getEntity(id, event.getPlayer().getWorld());
                        }
                        if (entity instanceof MyPetBukkitPart) {
                            entity = ((MyPetBukkitPart) entity).getPetOwner();
                        }
                        return entity;
                    }).get();
                    if (ent != null) {
                        packet.getIntegers().write(0, ent.getEntityId());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
Also used : Entity(org.bukkit.entity.Entity) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) PacketContainer(com.comphenix.protocol.events.PacketContainer) MyPetBukkitPart(de.Keyle.MyPet.api.entity.MyPetBukkitPart) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) PacketEvent(com.comphenix.protocol.events.PacketEvent)

Aggregations

PacketAdapter (com.comphenix.protocol.events.PacketAdapter)1 PacketContainer (com.comphenix.protocol.events.PacketContainer)1 PacketEvent (com.comphenix.protocol.events.PacketEvent)1 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)1 MyPetBukkitPart (de.Keyle.MyPet.api.entity.MyPetBukkitPart)1 Entity (org.bukkit.entity.Entity)1