Search in sources :

Example 1 with FieldAccessException

use of com.comphenix.protocol.reflect.FieldAccessException in project InteractiveChat by LOOHP.

the class MapViewer method showMap.

@SuppressWarnings("deprecation")
public static void showMap(Player player, ItemStack item) {
    if (!FilledMapUtils.isFilledMap(item)) {
        throw new IllegalArgumentException("ItemStack is not a filled map");
    }
    try {
        int mapId = FilledMapUtils.getMapId(item);
        MapView mapView = FilledMapUtils.getMapView(item);
        PacketContainer packet1;
        if (InteractiveChat.version.isOld()) {
            packet1 = InteractiveChat.protocolManager.createPacket(PacketType.Play.Server.SET_SLOT);
            packet1.getIntegers().write(0, 0);
            packet1.getIntegers().write(1, player.getInventory().getHeldItemSlot() + 36);
            packet1.getItemModifier().write(0, item);
        } else {
            packet1 = InteractiveChat.protocolManager.createPacket(PacketType.Play.Server.ENTITY_EQUIPMENT);
            packet1.getIntegers().write(0, player.getEntityId());
            if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_16)) {
                List<Pair<ItemSlot, ItemStack>> list = new ArrayList<>();
                list.add(new Pair<ItemSlot, ItemStack>(ItemSlot.MAINHAND, item));
                packet1.getSlotStackPairLists().write(0, list);
            } else {
                packet1.getItemSlots().write(0, ItemSlot.MAINHAND);
                packet1.getItemModifier().write(0, item);
            }
        }
        PacketContainer packet2 = InteractiveChat.protocolManager.createPacket(PacketType.Play.Server.MAP);
        int mapIconFieldPos = 2;
        if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_17)) {
            packet2.getIntegers().write(0, mapId);
            packet2.getBytes().write(0, (byte) 0);
            packet2.getBooleans().write(0, false);
        } else {
            packet2.getIntegers().write(0, mapId);
            packet2.getBytes().write(0, (byte) 0);
            if (!InteractiveChat.version.isOld()) {
                packet2.getBooleans().write(0, false);
                mapIconFieldPos++;
            }
            if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_14)) {
                packet2.getBooleans().write(1, false);
                mapIconFieldPos++;
            }
            packet2.getIntegers().write(1, 0);
            packet2.getIntegers().write(2, 0);
            packet2.getIntegers().write(3, 128);
            packet2.getIntegers().write(4, 128);
        }
        MAP_VIEWERS.put(player, item);
        InteractiveChat.protocolManager.sendServerPacket(player, packet1);
        int mapIconFieldPos0 = mapIconFieldPos;
        new BukkitRunnable() {

            @Override
            public void run() {
                ItemStack itemStack = MAP_VIEWERS.get(player);
                if (itemStack != null && itemStack.equals(item)) {
                    if (!player.getInventory().containsAtLeast(itemStack, 1)) {
                        try {
                            byte[] colors = FilledMapUtils.getColors(mapView, player);
                            List<MapCursor> mapCursors = FilledMapUtils.getCursors(mapView, player);
                            Iterator<MapCursor> itr = mapCursors.iterator();
                            while (itr.hasNext()) {
                                MapCursor mapCursor = itr.next();
                                int type = mapCursor.getRawType();
                                if (type == 0 || type == 6 || type == 7) {
                                    itr.remove();
                                }
                            }
                            if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_17)) {
                                packet2.getModifier().write(3, FilledMapUtils.toNMSMapIconList(mapCursors));
                                packet2.getModifier().write(4, FilledMapUtils.getNMSWorldMapBClassConstructor().newInstance(0, 0, 128, 128, colors));
                            } else {
                                List<?> nmsMapIconsList = FilledMapUtils.toNMSMapIconList(mapCursors);
                                Object nmsMapIconsArray = Array.newInstance(FilledMapUtils.getNMSMapIconClass(), nmsMapIconsList.size());
                                for (int i = 0; i < nmsMapIconsList.size(); i++) {
                                    Object nmsMapIconObject = nmsMapIconsList.get(i);
                                    Array.set(nmsMapIconsArray, i, nmsMapIconObject);
                                }
                                packet2.getByteArrays().write(0, colors);
                                packet2.getModifier().write(mapIconFieldPos0, nmsMapIconsArray);
                            }
                            InteractiveChat.protocolManager.sendServerPacket(player, packet2);
                        } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | FieldAccessException | InstantiationException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    this.cancel();
                }
            }
        }.runTaskTimer(InteractiveChat.plugin, 0, 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PacketContainer(com.comphenix.protocol.events.PacketContainer) ArrayList(java.util.ArrayList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MapCursor(org.bukkit.map.MapCursor) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ItemSlot(com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot) Iterator(java.util.Iterator) MapView(org.bukkit.map.MapView) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) Pair(com.comphenix.protocol.wrappers.Pair)

Example 2 with FieldAccessException

use of com.comphenix.protocol.reflect.FieldAccessException in project LibsDisguises by libraryaddict.

the class ReflectionManager method createNMSValues.

private static void createNMSValues(DisguiseType disguiseType) {
    String nmsEntityName = toReadable(disguiseType.name());
    Class nmsClass = ReflectionManager.getNmsClassIgnoreErrors("Entity" + nmsEntityName);
    if (nmsClass == null || Modifier.isAbstract(nmsClass.getModifiers())) {
        String[] split = splitReadable(disguiseType.name());
        ArrayUtils.reverse(split);
        nmsEntityName = StringUtils.join(split);
        nmsClass = ReflectionManager.getNmsClassIgnoreErrors("Entity" + nmsEntityName);
        if (nmsClass == null || Modifier.isAbstract(nmsClass.getModifiers())) {
            nmsEntityName = null;
        }
    }
    if (nmsEntityName == null) {
        switch(disguiseType) {
            case AXOLOTL:
                nmsEntityName = "Axolotl";
                break;
            case GOAT:
                nmsEntityName = "Goat";
                break;
            case GLOW_ITEM_FRAME:
                nmsEntityName = "GlowItemFrame";
                break;
            case GLOW_SQUID:
                nmsEntityName = "GlowSquid";
                break;
            case MARKER:
                nmsEntityName = "Marker";
                break;
            case DONKEY:
                nmsEntityName = "HorseDonkey";
                break;
            case ARROW:
                nmsEntityName = "TippedArrow";
                break;
            case DROPPED_ITEM:
                nmsEntityName = "Item";
                break;
            case FIREBALL:
                nmsEntityName = "LargeFireball";
                break;
            case FIREWORK:
                nmsEntityName = "Fireworks";
                break;
            case GIANT:
                nmsEntityName = "GiantZombie";
                break;
            case HUSK:
                nmsEntityName = "ZombieHusk";
                break;
            case ILLUSIONER:
                nmsEntityName = "IllagerIllusioner";
                break;
            case LEASH_HITCH:
                nmsEntityName = "Leash";
                break;
            case MINECART:
                nmsEntityName = "MinecartRideable";
                break;
            case MINECART_COMMAND:
                nmsEntityName = "MinecartCommandBlock";
                break;
            case MINECART_TNT:
                nmsEntityName = "MinecartTNT";
                break;
            case MULE:
                nmsEntityName = "HorseMule";
                break;
            case PRIMED_TNT:
                nmsEntityName = "TNTPrimed";
                break;
            case PUFFERFISH:
                nmsEntityName = "PufferFish";
                break;
            case SPLASH_POTION:
                nmsEntityName = "Potion";
                break;
            case STRAY:
                nmsEntityName = "SkeletonStray";
                break;
            case TRIDENT:
                nmsEntityName = "ThrownTrident";
                break;
            case WANDERING_TRADER:
                nmsEntityName = "VillagerTrader";
                break;
            case TRADER_LLAMA:
                // Interesting capitalization
                nmsEntityName = "LLamaTrader";
                break;
            case ZOMBIFIED_PIGLIN:
                nmsEntityName = "PigZombie";
                break;
            default:
                break;
        }
    }
    try {
        if (disguiseType == DisguiseType.UNKNOWN || disguiseType.isCustom()) {
            DisguiseValues disguiseValues = new DisguiseValues(disguiseType, 0);
            disguiseValues.setAdultBox(new FakeBoundingBox(0, 0, 0));
            SoundGroup sound = SoundGroup.getGroup(disguiseType.name());
            if (sound != null) {
                sound.setDamageAndIdleSoundVolume(1f);
            }
            return;
        }
        if (nmsEntityName == null) {
            DisguiseUtilities.getLogger().warning("Entity name not found! (" + disguiseType.name() + ")");
            return;
        }
        Object nmsEntity = ReflectionManager.createEntityInstance(disguiseType, nmsReflection != null ? disguiseType.getEntityType().getKey().getKey() : nmsEntityName);
        if (nmsEntity == null) {
            DisguiseUtilities.getLogger().warning("Entity not found! (" + nmsEntityName + ")");
            return;
        }
        disguiseType.setTypeId(ReflectionManager.getEntityTypeId(disguiseType.getEntityType()));
        Entity bukkitEntity = ReflectionManager.getBukkitEntity(nmsEntity);
        DisguiseValues disguiseValues = new DisguiseValues(disguiseType, bukkitEntity instanceof Damageable ? ((Damageable) bukkitEntity).getMaxHealth() : 0);
        WrappedDataWatcher watcher = WrappedDataWatcher.getEntityWatcher(bukkitEntity);
        ArrayList<MetaIndex> indexes = MetaIndex.getMetaIndexes(disguiseType.getWatcherClass());
        boolean loggedName = false;
        for (WrappedWatchableObject watch : watcher.getWatchableObjects()) {
            MetaIndex flagType = MetaIndex.getMetaIndex(disguiseType.getWatcherClass(), watch.getIndex());
            if (flagType == null) {
                DisguiseUtilities.getLogger().severe("MetaIndex not found for " + disguiseType + "! Index: " + watch.getIndex());
                DisguiseUtilities.getLogger().severe("Value: " + watch.getRawValue() + " (" + watch.getRawValue().getClass() + ") (" + nmsEntity.getClass() + ") & " + disguiseType.getWatcherClass().getSimpleName());
                continue;
            }
            indexes.remove(flagType);
            Object ourValue = ReflectionManager.convertInvalidMeta(flagType.getDefault());
            Object nmsValue = ReflectionManager.convertInvalidMeta(watch.getRawValue());
            if (ourValue.getClass() != nmsValue.getClass()) {
                if (!loggedName) {
                    DisguiseUtilities.getLogger().severe(StringUtils.repeat("=", 20));
                    DisguiseUtilities.getLogger().severe("MetaIndex mismatch! Disguise " + disguiseType + ", Entity " + nmsEntityName);
                    loggedName = true;
                }
                DisguiseUtilities.getLogger().severe(StringUtils.repeat("-", 20));
                DisguiseUtilities.getLogger().severe("Index: " + watch.getIndex() + " | " + flagType.getFlagWatcher().getSimpleName() + " | " + MetaIndex.getName(flagType));
                Object flagDefault = flagType.getDefault();
                DisguiseUtilities.getLogger().severe("LibsDisguises: " + flagDefault + " (" + flagDefault.getClass() + ")");
                DisguiseUtilities.getLogger().severe("LibsDisguises Converted: " + ourValue + " (" + ourValue.getClass() + ")");
                DisguiseUtilities.getLogger().severe("Minecraft: " + watch.getRawValue() + " (" + watch.getRawValue().getClass() + ")");
                DisguiseUtilities.getLogger().severe("Minecraft Converted: " + nmsValue + " (" + nmsValue.getClass() + ")");
                DisguiseUtilities.getLogger().severe(StringUtils.repeat("-", 20));
            }
        }
        for (MetaIndex index : indexes) {
            DisguiseUtilities.getLogger().severe(disguiseType + " has MetaIndex remaining! " + index.getFlagWatcher().getSimpleName() + " at index " + index.getIndex());
        }
        SoundGroup sound = SoundGroup.getGroup(disguiseType.name());
        if (sound != null) {
            Float soundStrength = ReflectionManager.getSoundModifier(nmsEntity);
            if (soundStrength != null) {
                sound.setDamageAndIdleSoundVolume(soundStrength);
            }
        }
        // Get the bounding box
        disguiseValues.setAdultBox(ReflectionManager.getBoundingBox(bukkitEntity));
        if (bukkitEntity instanceof Ageable) {
            ((Ageable) bukkitEntity).setBaby();
            disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity));
        } else if (bukkitEntity instanceof Zombie) {
            ((Zombie) bukkitEntity).setBaby(true);
            disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity));
        } else if (bukkitEntity instanceof ArmorStand) {
            ((ArmorStand) bukkitEntity).setSmall(true);
            disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity));
        }
    } catch (SecurityException | IllegalArgumentException | FieldAccessException ex) {
        DisguiseUtilities.getLogger().severe("Uh oh! Trouble while making values for the disguise " + disguiseType.name() + "!");
        DisguiseUtilities.getLogger().severe("Before reporting this error, " + "please make sure you are using the latest version of LibsDisguises and ProtocolLib.");
        DisguiseUtilities.getLogger().severe("Development builds are available at (ProtocolLib) " + "http://ci.dmulloy2.net/job/ProtocolLib/ and (LibsDisguises) https://ci.md-5" + ".net/job/LibsDisguises/");
        ex.printStackTrace();
    }
}
Also used : DisguiseValues(me.libraryaddict.disguise.utilities.DisguiseValues) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) SoundGroup(me.libraryaddict.disguise.utilities.sounds.SoundGroup) Damageable(org.bukkit.entity.Damageable) Zombie(org.bukkit.entity.Zombie) Ageable(org.bukkit.entity.Ageable) MetaIndex(me.libraryaddict.disguise.disguisetypes.MetaIndex) ArmorStand(org.bukkit.entity.ArmorStand) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) AccessibleObject(java.lang.reflect.AccessibleObject) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) WrappedDataWatcher(com.comphenix.protocol.wrappers.WrappedDataWatcher) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException)

Example 3 with FieldAccessException

use of com.comphenix.protocol.reflect.FieldAccessException in project CombatLogX by SirBlobman.

the class ForceFieldAdapter method getLocation0.

@Nullable
private Location getLocation0(World world, PacketContainer packetContainer) {
    try {
        StructureModifier<BlockPosition> blockPositionModifier = packetContainer.getBlockPositionModifier();
        BlockPosition blockPosition = blockPositionModifier.readSafely(0);
        if (blockPosition == null) {
            return getLocation1(world, packetContainer);
        }
        return blockPosition.toLocation(world);
    } catch (FieldAccessException ex) {
        return getLocation1(world, packetContainer);
    }
}
Also used : BlockPosition(com.comphenix.protocol.wrappers.BlockPosition) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with FieldAccessException

use of com.comphenix.protocol.reflect.FieldAccessException in project CombatLogX by SirBlobman.

the class ForceFieldAdapter method getLocation1.

@Nullable
private Location getLocation1(World world, PacketContainer packetContainer) {
    try {
        StructureModifier<MovingObjectPositionBlock> movingBlockPositionModifier = packetContainer.getMovingBlockPositions();
        MovingObjectPositionBlock movingObjectPositionBlock = movingBlockPositionModifier.readSafely(0);
        if (movingObjectPositionBlock == null) {
            return null;
        }
        BlockPosition blockPosition = movingObjectPositionBlock.getBlockPosition();
        return (blockPosition == null ? null : blockPosition.toLocation(world));
    } catch (FieldAccessException ex) {
        return null;
    }
}
Also used : BlockPosition(com.comphenix.protocol.wrappers.BlockPosition) MovingObjectPositionBlock(com.comphenix.protocol.wrappers.MovingObjectPositionBlock) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FieldAccessException (com.comphenix.protocol.reflect.FieldAccessException)4 BlockPosition (com.comphenix.protocol.wrappers.BlockPosition)2 Nullable (org.jetbrains.annotations.Nullable)2 PacketContainer (com.comphenix.protocol.events.PacketContainer)1 ItemSlot (com.comphenix.protocol.wrappers.EnumWrappers.ItemSlot)1 MovingObjectPositionBlock (com.comphenix.protocol.wrappers.MovingObjectPositionBlock)1 Pair (com.comphenix.protocol.wrappers.Pair)1 WrappedDataWatcher (com.comphenix.protocol.wrappers.WrappedDataWatcher)1 WrappedDataWatcherObject (com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject)1 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MetaIndex (me.libraryaddict.disguise.disguisetypes.MetaIndex)1 DisguiseValues (me.libraryaddict.disguise.utilities.DisguiseValues)1 SoundGroup (me.libraryaddict.disguise.utilities.sounds.SoundGroup)1 Ageable (org.bukkit.entity.Ageable)1 ArmorStand (org.bukkit.entity.ArmorStand)1