Search in sources :

Example 1 with DisguiseValues

use of me.libraryaddict.disguise.utilities.DisguiseValues 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)

Aggregations

FieldAccessException (com.comphenix.protocol.reflect.FieldAccessException)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 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 Damageable (org.bukkit.entity.Damageable)1 Entity (org.bukkit.entity.Entity)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Zombie (org.bukkit.entity.Zombie)1