Search in sources :

Example 6 with PacketAdapter

use of com.comphenix.protocol.events.PacketAdapter 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)

Example 7 with PacketAdapter

use of com.comphenix.protocol.events.PacketAdapter in project MyPet by xXKeyleXx.

the class ProtocolLibHook method registerCompatFix_1_8.

private void registerCompatFix_1_8() {
    ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), ListenerPriority.HIGHEST, PacketType.Play.Server.SPAWN_ENTITY_LIVING, PacketType.Play.Server.ENTITY_METADATA) {

        Class entityClass = ReflectionUtil.getClass("org.bukkit.craftbukkit." + MyPetApi.getCompatUtil().getInternalVersion() + ".entity.CraftEntity");

        Method getHandleMethod = ReflectionUtil.getMethod(entityClass, "getHandle");

        @Override
        public void onPacketSending(PacketEvent event) {
            if ((checkTemporaryPlayers && event.isPlayerTemporary()) || event.isCancelled()) {
                return;
            }
            Player player = event.getPlayer();
            if (!isPlayerRunningv1_8(player)) {
                return;
            }
            PacketContainer newPacketContainer = event.getPacket().deepClone();
            event.setPacket(newPacketContainer);
            if (event.getPacketType() == PacketType.Play.Server.SPAWN_ENTITY_LIVING) {
                Entity entity = newPacketContainer.getEntityModifier(event).readSafely(0);
                if (entity instanceof MyPetBukkitEntity) {
                    MyPetBukkitEntity petEntity = (MyPetBukkitEntity) entity;
                    List<WrappedWatchableObject> wrappedWatchableObjectList = newPacketContainer.getDataWatcherModifier().read(0).getWatchableObjects();
                    newPacketContainer.getDataWatcherModifier().write(0, new WrappedDataWatcher(fixMetadata(petEntity, wrappedWatchableObjectList)));
                }
            } else if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) {
                Entity entity = newPacketContainer.getEntityModifier(event).read(0);
                if (entity instanceof MyPetBukkitEntity) {
                    MyPetBukkitEntity petEntity = (MyPetBukkitEntity) entity;
                    List<WrappedWatchableObject> wrappedWatchableObjectList = newPacketContainer.getWatchableCollectionModifier().read(0);
                    newPacketContainer.getWatchableCollectionModifier().write(0, fixMetadata(petEntity, wrappedWatchableObjectList));
                }
            }
        }

        private List<WrappedWatchableObject> fixMetadata(MyPetBukkitEntity petEntity, List<WrappedWatchableObject> wrappedWatchableObjectList) {
            if (petEntity == null || wrappedWatchableObjectList == null) {
                return wrappedWatchableObjectList;
            }
            if (petEntity.getMyPet() instanceof MyPetBaby && hasKey(12, wrappedWatchableObjectList)) {
                Object object = getKeyValue(12, wrappedWatchableObjectList);
                if (object instanceof Integer) {
                    int value = ((Number) object).intValue();
                    removeKey(12, wrappedWatchableObjectList);
                    if (petEntity.getPetType() == MyPetType.Horse) {
                        if (value == -24000) {
                            value = -1;
                        }
                    }
                    wrappedWatchableObjectList.add(new WrappedWatchableObject(12, (byte) value));
                }
            }
            if (petEntity.getPetType() == MyPetType.Wolf && hasKey(20, wrappedWatchableObjectList)) {
                Object object = getKeyValue(20, wrappedWatchableObjectList);
                if (object instanceof Byte) {
                    DyeColor color = DyeColor.getByWoolData((byte) ((Byte) object & 0xF));
                    removeKey(20, wrappedWatchableObjectList);
                    wrappedWatchableObjectList.add(new WrappedWatchableObject(20, (byte) ((15 - color.ordinal()) & 0xF)));
                }
            }
            if (petEntity.getPetType() == MyPetType.Enderman && hasKey(16, wrappedWatchableObjectList)) {
                Object object = getKeyValue(16, wrappedWatchableObjectList);
                if (object instanceof Byte) {
                    removeKey(16, wrappedWatchableObjectList);
                    wrappedWatchableObjectList.add(new WrappedWatchableObject(16, Short.valueOf((Byte) object)));
                }
            }
            return wrappedWatchableObjectList;
        }

        private boolean hasKey(int key, List<WrappedWatchableObject> wrappedWatchableObjectList) {
            for (WrappedWatchableObject next : wrappedWatchableObjectList) {
                if (next.getIndex() == key) {
                    return true;
                }
            }
            return false;
        }

        private Object getKeyValue(int key, List<WrappedWatchableObject> wrappedWatchableObjectList) {
            for (WrappedWatchableObject next : wrappedWatchableObjectList) {
                if (next.getIndex() == key) {
                    return next.getValue();
                }
            }
            return null;
        }

        private void removeKey(int key, List<WrappedWatchableObject> wrappedWatchableObjectList) {
            for (Iterator<WrappedWatchableObject> wrappedWatchableObjectIterator = wrappedWatchableObjectList.iterator(); wrappedWatchableObjectIterator.hasNext(); ) {
                WrappedWatchableObject next = wrappedWatchableObjectIterator.next();
                if (next.getIndex() == key) {
                    wrappedWatchableObjectIterator.remove();
                    break;
                }
            }
        }

        private boolean isPlayerRunningv1_8(Player player) {
            try {
                Object nmsPlayer = getHandleMethod.invoke(player);
                Object playerConnection = ReflectionUtil.getFieldValue(nmsPlayer.getClass(), nmsPlayer, "playerConnection");
                Object networkManager = ReflectionUtil.getFieldValue(playerConnection.getClass(), playerConnection, "networkManager");
                Method getVersionMethod = ReflectionUtil.getMethod(networkManager.getClass(), "getVersion");
                return (Integer) getVersionMethod.invoke(networkManager) > 5;
            } catch (Exception exception) {
                return false;
            }
        }
    });
}
Also used : Entity(org.bukkit.entity.Entity) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) Player(org.bukkit.entity.Player) PacketContainer(com.comphenix.protocol.events.PacketContainer) Method(java.lang.reflect.Method) DyeColor(org.bukkit.DyeColor) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) MyPetBaby(de.Keyle.MyPet.api.entity.MyPetBaby) Iterator(java.util.Iterator) PacketEvent(com.comphenix.protocol.events.PacketEvent) ArrayList(java.util.ArrayList) List(java.util.List) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity)

Example 8 with PacketAdapter

use of com.comphenix.protocol.events.PacketAdapter in project NoCheatPlus by NoCheatPlus.

the class ProtocolLibComponent method unregister.

private void unregister() {
    final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
    final NoCheatPlusAPI api = NCPAPIProvider.getNoCheatPlusAPI();
    for (PacketAdapter adapter : registeredPacketAdapters) {
        try {
            protocolManager.removePacketListener(adapter);
            // Bit heavy, but consistent.
            api.removeComponent(adapter);
        } catch (Throwable t) {
            StaticLog.logWarning("Failed to unregister packet level hook: " + adapter.getClass().getName());
        }
    // TODO Auto-generated method stub
    }
    registeredPacketAdapters.clear();
}
Also used : NoCheatPlusAPI(fr.neatmonster.nocheatplus.components.NoCheatPlusAPI) ProtocolManager(com.comphenix.protocol.ProtocolManager) PacketAdapter(com.comphenix.protocol.events.PacketAdapter)

Example 9 with PacketAdapter

use of com.comphenix.protocol.events.PacketAdapter in project NoCheatPlus by NoCheatPlus.

the class ProtocolLibComponent method register.

private void register(Plugin plugin) {
    StaticLog.logInfo("Adding packet level hooks for ProtocolLib (MC " + ProtocolLibrary.getProtocolManager().getMinecraftVersion().getVersion() + ")...");
    final IWorldDataManager worldMan = NCPAPIProvider.getNoCheatPlusAPI().getWorldDataManager();
    // Special purpose.
    if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET + ConfPaths.SUB_DEBUG) || ConfigManager.isTrueForAnyConfig(ConfPaths.CHECKS_DEBUG)) {
        // (Debug logging. Only activates if debug is set for checks or checks.net, not on the fly.)
        register("fr.neatmonster.nocheatplus.checks.net.protocollib.DebugAdapter", plugin);
    }
    // Actual checks.
    if (ServerVersion.compareMinecraftVersion("1.6.4") <= 0) {
        // Don't use this listener.
        NCPAPIProvider.getNoCheatPlusAPI().getLogManager().info(Streams.STATUS, "Disable EntityUseAdapter due to incompatibilities. Use fight.speed instead of net.attackfrequency.");
    } else if (worldMan.isActiveAnywhere(CheckType.NET_ATTACKFREQUENCY)) {
        // (Also sets lastKeepAliveTime, if enabled.)
        register("fr.neatmonster.nocheatplus.checks.net.protocollib.UseEntityAdapter", plugin);
    }
    if (worldMan.isActiveAnywhere(CheckType.NET_FLYINGFREQUENCY)) {
        // (Also sets lastKeepAliveTime, if enabled.)
        register("fr.neatmonster.nocheatplus.checks.net.protocollib.MovingFlying", plugin);
        register("fr.neatmonster.nocheatplus.checks.net.protocollib.OutgoingPosition", plugin);
    }
    if (worldMan.isActiveAnywhere(CheckType.NET_KEEPALIVEFREQUENCY) || worldMan.isActiveAnywhere(CheckType.FIGHT_GODMODE)) {
        // (Set lastKeepAlive if this or fight.godmode is enabled.)
        register("fr.neatmonster.nocheatplus.checks.net.protocollib.KeepAliveAdapter", plugin);
    }
    if (worldMan.isActiveAnywhere(CheckType.NET_SOUNDDISTANCE)) {
        register("fr.neatmonster.nocheatplus.checks.net.protocollib.SoundDistance", plugin);
    }
    if (ServerVersion.compareMinecraftVersion("1.9") < 0) {
        if (worldMan.isActiveAnywhere(CheckType.NET_PACKETFREQUENCY)) {
            register("fr.neatmonster.nocheatplus.checks.net.protocollib.CatchAllAdapter", plugin);
        }
    }
    if (!registeredPacketAdapters.isEmpty()) {
        List<String> names = new ArrayList<String>(registeredPacketAdapters.size());
        for (PacketAdapter adapter : registeredPacketAdapters) {
            names.add(adapter.getClass().getSimpleName());
        }
        StaticLog.logInfo("Available (and activated) packet level hooks: " + StringUtil.join(names, " | "));
        NCPAPIProvider.getNoCheatPlusAPI().addFeatureTags("packet-listeners", names);
    } else {
        StaticLog.logInfo("No packet level hooks activated.");
    }
}
Also used : IWorldDataManager(fr.neatmonster.nocheatplus.worlds.IWorldDataManager) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) ArrayList(java.util.ArrayList)

Example 10 with PacketAdapter

use of com.comphenix.protocol.events.PacketAdapter in project NoCheatPlus by NoCheatPlus.

the class ProtocolLibComponent method register.

private void register(Class<? extends PacketAdapter> clazz, Plugin plugin) {
    try {
        // Construct a new instance using reflection.
        PacketAdapter adapter = clazz.getDeclaredConstructor(Plugin.class).newInstance(plugin);
        ProtocolLibrary.getProtocolManager().addPacketListener(adapter);
        registeredPacketAdapters.add(adapter);
    } catch (Throwable t) {
        StaticLog.logWarning("Could not register packet level hook: " + clazz.getSimpleName());
        StaticLog.logWarning(t);
        if (t.getCause() != null) {
            StaticLog.logWarning(t.getCause());
        }
    }
}
Also used : PacketAdapter(com.comphenix.protocol.events.PacketAdapter) Plugin(org.bukkit.plugin.Plugin)

Aggregations

PacketAdapter (com.comphenix.protocol.events.PacketAdapter)10 PacketEvent (com.comphenix.protocol.events.PacketEvent)7 Entity (org.bukkit.entity.Entity)6 PacketContainer (com.comphenix.protocol.events.PacketContainer)5 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)5 ProtocolManager (com.comphenix.protocol.ProtocolManager)3 ArrayList (java.util.ArrayList)2 Location (org.bukkit.Location)2 Player (org.bukkit.entity.Player)2 PacketType (com.comphenix.protocol.PacketType)1 ProtocolLibrary (com.comphenix.protocol.ProtocolLibrary)1 WrappedDataWatcher (com.comphenix.protocol.wrappers.WrappedDataWatcher)1 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 NbtCompound (com.comphenix.protocol.wrappers.nbt.NbtCompound)1 VoxelGamesLib (com.voxelgameslib.voxelgameslib.VoxelGamesLib)1 User (com.voxelgameslib.voxelgameslib.user.User)1 UserHandler (com.voxelgameslib.voxelgameslib.user.UserHandler)1 ChatUtil (com.voxelgameslib.voxelgameslib.utils.ChatUtil)1 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)1 MyPetBukkitPart (de.Keyle.MyPet.api.entity.MyPetBukkitPart)1