Search in sources :

Example 1 with PacketContainer

use of com.comphenix.protocol.events.PacketContainer in project AuthMeReloaded by AuthMe.

the class InventoryPacketAdapter method onPacketSending.

@Override
public void onPacketSending(PacketEvent packetEvent) {
    Player player = packetEvent.getPlayer();
    PacketContainer packet = packetEvent.getPacket();
    byte windowId = packet.getIntegers().read(0).byteValue();
    if (windowId == PLAYER_INVENTORY && !playerCache.isAuthenticated(player.getName())) {
        packetEvent.setCancelled(true);
    }
}
Also used : Player(org.bukkit.entity.Player) PacketContainer(com.comphenix.protocol.events.PacketContainer)

Example 2 with PacketContainer

use of com.comphenix.protocol.events.PacketContainer in project AuthMeReloaded by AuthMe.

the class InventoryPacketAdapter method sendBlankInventoryPacket.

public void sendBlankInventoryPacket(Player player) {
    ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
    PacketContainer inventoryPacket = protocolManager.createPacket(PacketType.Play.Server.WINDOW_ITEMS);
    inventoryPacket.getIntegers().write(0, PLAYER_INVENTORY);
    int inventorySize = CRAFTING_SIZE + ARMOR_SIZE + MAIN_SIZE + HOTBAR_SIZE;
    ItemStack[] blankInventory = new ItemStack[inventorySize];
    Arrays.fill(blankInventory, new ItemStack(Material.AIR));
    //old minecraft versions
    StructureModifier<ItemStack[]> itemArrayModifier = inventoryPacket.getItemArrayModifier();
    if (itemArrayModifier.size() > 0) {
        itemArrayModifier.write(0, blankInventory);
    } else {
        //minecraft versions above 1.11
        StructureModifier<List<ItemStack>> itemListModifier = inventoryPacket.getItemListModifier();
        itemListModifier.write(0, Arrays.asList(blankInventory));
    }
    try {
        protocolManager.sendServerPacket(player, inventoryPacket, false);
    } catch (InvocationTargetException invocationExc) {
        ConsoleLogger.logException("Error during sending blank inventory", invocationExc);
    }
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) ProtocolManager(com.comphenix.protocol.ProtocolManager) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with PacketContainer

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

the class ProtocolLibHook method registerCompatFix_1_8.

private static 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");

        private final EnumMap<DyeColor, Integer> convertedDyeColors = new EnumMap<DyeColor, Integer>(DyeColor.class) {

            {
                put(DyeColor.WHITE, 15);
                put(DyeColor.ORANGE, 14);
                put(DyeColor.MAGENTA, 13);
                put(DyeColor.LIGHT_BLUE, 12);
                put(DyeColor.YELLOW, 11);
                put(DyeColor.LIME, 10);
                put(DyeColor.PINK, 9);
                put(DyeColor.GRAY, 8);
                put(DyeColor.SILVER, 7);
                put(DyeColor.CYAN, 6);
                put(DyeColor.PURPLE, 5);
                put(DyeColor.BLUE, 4);
                put(DyeColor.BROWN, 3);
                put(DyeColor.GREEN, 2);
                put(DyeColor.RED, 1);
                put(DyeColor.BLACK, 0);
            }
        };

        @Override
        public void onPacketSending(PacketEvent event) {
            if (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 != null && 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 != null && 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) ((this.convertedDyeColors.get(color)) & 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;
                }
            }
        }

        @SuppressWarnings("unchecked")
        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) List(java.util.List) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) EnumMap(java.util.EnumMap) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity)

Example 4 with PacketContainer

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

the class ProtocolLibHook method registerEnderDragonFix.

private static void registerEnderDragonFix() {
    ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), PacketType.Play.Server.ENTITY_LOOK, PacketType.Play.Server.ENTITY_MOVE_LOOK, PacketType.Play.Server.ENTITY_TELEPORT) {

        @Override
        public void onPacketSending(PacketEvent event) {
            PacketContainer packet = event.getPacket();
            final Entity entity = packet.getEntityModifier(event).readSafely(0);
            // Now - are we dealing with an invisible slime?
            if (entity != null && entity instanceof MyPetBukkitEntity && ((MyPetBukkitEntity) entity).getPetType() == MyPetType.EnderDragon) {
                if (packet.getType() == PacketType.Play.Server.ENTITY_LOOK) {
                    // MyPetLogger.write("ENTITY_LOOK: " + packet.getBytes().getValues());
                    byte angle = packet.getBytes().read(3);
                    angle += Byte.MAX_VALUE;
                    packet.getBytes().write(3, angle);
                } else if (packet.getType() == PacketType.Play.Server.ENTITY_MOVE_LOOK) {
                    // MyPetLogger.write("ENTITY_MOVE_LOOK: " + packet.getBytes().getValues());
                    byte angle = packet.getBytes().read(3);
                    angle += Byte.MAX_VALUE;
                    packet.getBytes().write(3, angle);
                } else if (packet.getType() == PacketType.Play.Server.ENTITY_TELEPORT) {
                    // MyPetLogger.write("ENTITY_TELEPORT: " + packet.getBytes().getValues());
                    byte angle = packet.getBytes().read(1);
                    angle += Byte.MAX_VALUE;
                    packet.getBytes().write(1, angle);
                }
            }
        }
    });
}
Also used : Entity(org.bukkit.entity.Entity) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) PacketContainer(com.comphenix.protocol.events.PacketContainer) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) PacketEvent(com.comphenix.protocol.events.PacketEvent) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity)

Example 5 with PacketContainer

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

the class ProtocolLibHook method registerEnderDragonFix_post_1_9.

private static void registerEnderDragonFix_post_1_9() {
    ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(MyPetApi.getPlugin(), PacketType.Play.Server.ENTITY_LOOK, PacketType.Play.Server.ENTITY_MOVE_LOOK, PacketType.Play.Server.ENTITY_TELEPORT) {

        @Override
        public void onPacketSending(PacketEvent event) {
            PacketContainer packet = event.getPacket();
            final Entity entity = packet.getEntityModifier(event).readSafely(0);
            if (entity != null && entity instanceof MyPetBukkitEntity && ((MyPetBukkitEntity) entity).getPetType() == MyPetType.EnderDragon) {
                if (packet.getType() == PacketType.Play.Server.ENTITY_LOOK) {
                    // MyPetLogger.write("ENTITY_LOOK: " + packet.getBytes().getValues());
                    byte angle = packet.getBytes().read(0);
                    angle += Byte.MAX_VALUE;
                    packet.getBytes().write(0, angle);
                } else if (packet.getType() == PacketType.Play.Server.ENTITY_MOVE_LOOK) {
                    // MyPetLogger.write("ENTITY_MOVE_LOOK: " + packet.getBytes().getValues());
                    byte angle = packet.getBytes().read(0);
                    angle += Byte.MAX_VALUE;
                    packet.getBytes().write(0, angle);
                } else if (packet.getType() == PacketType.Play.Server.ENTITY_TELEPORT) {
                    // MyPetLogger.write("ENTITY_TELEPORT: " + packet.getBytes().getValues());
                    byte angle = packet.getBytes().read(1);
                    angle += Byte.MAX_VALUE;
                    packet.getBytes().write(1, angle);
                }
            }
        }
    });
}
Also used : Entity(org.bukkit.entity.Entity) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) PacketContainer(com.comphenix.protocol.events.PacketContainer) PacketAdapter(com.comphenix.protocol.events.PacketAdapter) PacketEvent(com.comphenix.protocol.events.PacketEvent) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity)

Aggregations

PacketContainer (com.comphenix.protocol.events.PacketContainer)5 PacketAdapter (com.comphenix.protocol.events.PacketAdapter)3 PacketEvent (com.comphenix.protocol.events.PacketEvent)3 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)3 Entity (org.bukkit.entity.Entity)3 List (java.util.List)2 Player (org.bukkit.entity.Player)2 ProtocolManager (com.comphenix.protocol.ProtocolManager)1 WrappedDataWatcher (com.comphenix.protocol.wrappers.WrappedDataWatcher)1 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 EnumMap (java.util.EnumMap)1 Iterator (java.util.Iterator)1 DyeColor (org.bukkit.DyeColor)1 ItemStack (org.bukkit.inventory.ItemStack)1