Search in sources :

Example 1 with NbtWrapper

use of com.comphenix.protocol.wrappers.nbt.NbtWrapper in project WheelCore by Plugindustry.

the class NBTBasedProvider method getOreDictionary.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public Set<String> getOreDictionary(@Nullable ItemStack itemStack) {
    if (itemStack == null)
        return Collections.emptySet();
    Set<String> defaultOreDict = getInstance(itemStack) == null ? ItemMapping.getVanillaOreDict(itemStack.getType()) : Collections.emptySet();
    Optional<NbtWrapper<?>> nbtWrapperOptional = NbtFactory.fromItemOptional((ItemStack) ShadowManager.shadowUnpack(CraftItemStack.asCraftCopy(itemStack)));
    if (nbtWrapperOptional.isEmpty())
        return defaultOreDict;
    NbtWrapper<?> nbtWrapper = nbtWrapperOptional.get();
    if (nbtWrapper.getType() != NbtType.TAG_COMPOUND)
        return defaultOreDict;
    NbtCompound compound = NbtFactory.asCompound(nbtWrapper);
    if (!compound.containsKey("wheel_core_item_ore_dictionary"))
        return defaultOreDict;
    Object data = compound.getObject("wheel_core_item_ore_dictionary");
    if ((!(data instanceof NbtList)) || ((NbtList<?>) data).getElementType() != NbtType.TAG_STRING)
        return defaultOreDict;
    return ((NbtList<String>) data).asCollection().stream().map(NbtBase::getValue).collect(Collectors.toSet());
}
Also used : NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) NbtList(com.comphenix.protocol.wrappers.nbt.NbtList) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) Nonnull(javax.annotation.Nonnull)

Example 2 with NbtWrapper

use of com.comphenix.protocol.wrappers.nbt.NbtWrapper in project LibsDisguises by libraryaddict.

the class ReflectionManager method convertInvalidMeta.

public static Object convertInvalidMeta(Object value) {
    if (value instanceof Optional) {
        Optional opt = (Optional) value;
        if (!opt.isPresent()) {
            return NmsVersion.v1_13.isSupported() ? value : com.google.common.base.Optional.absent();
        }
        if (nmsReflection != null) {
            return nmsReflection.convertOptional(opt.get());
        }
        Object val = opt.get();
        if (val instanceof BlockPosition) {
            BlockPosition pos = (BlockPosition) val;
            try {
                Object obj = blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
                return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (val instanceof WrappedBlockData) {
            try {
                Object obj = ((WrappedBlockData) val).getHandle();
                return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (val instanceof ItemStack) {
            val = getNmsItem((ItemStack) val);
            if (val == null) {
                return NmsVersion.v1_13.isSupported() ? Optional.empty() : com.google.common.base.Optional.absent();
            } else {
                return Optional.of(val);
            }
        } else if (val instanceof WrappedChatComponent) {
            Object obj = ((WrappedChatComponent) val).getHandle();
            return NmsVersion.v1_13.isSupported() ? Optional.of(obj) : com.google.common.base.Optional.of(obj);
        } else if (!NmsVersion.v1_13.isSupported()) {
            return com.google.common.base.Optional.of(val);
        } else {
            return Optional.of(val);
        }
    } else if (value instanceof Vector3F) {
        Vector3F angle = (Vector3F) value;
        if (nmsReflection != null) {
            return nmsReflection.convertVec3(angle);
        }
        try {
            return vector3FConstructor.newInstance(angle.getX(), angle.getY(), angle.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof EulerAngle) {
        EulerAngle angle = (EulerAngle) value;
        if (nmsReflection != null) {
            return nmsReflection.convertVec3(angle);
        }
        try {
            return vector3FConstructor.newInstance((float) angle.getX(), (float) angle.getY(), (float) angle.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof Direction) {
        if (nmsReflection != null) {
            return nmsReflection.convertDirection((Direction) value);
        }
        try {
            return enumDirectionFrom.invoke(null, ((Direction) value).ordinal());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof BlockPosition) {
        BlockPosition pos = (BlockPosition) value;
        if (nmsReflection != null) {
            return nmsReflection.getBlockPosition(pos.getX(), pos.getY(), pos.getZ());
        }
        try {
            return blockPositionConstructor.newInstance(pos.getX(), pos.getY(), pos.getZ());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (value instanceof ItemStack) {
        return getNmsItem((ItemStack) value);
    } else if (value instanceof Double) {
        return ((Double) value).floatValue();
    } else if (value instanceof NbtWrapper) {
        return ((NbtWrapper) value).getHandle();
    } else if (value instanceof WrappedParticle) {
        return ((WrappedParticle) value).getHandle();
    } else if (value instanceof EntityPose) {
        return getNmsEntityPose((EntityPose) value);
    } else if (value instanceof VillagerData) {
        return getNmsVillagerData((VillagerData) value);
    } else if (value instanceof WrappedChatComponent) {
        return ((WrappedChatComponent) value).getHandle();
    }
    return value;
}
Also used : WrappedBlockData(com.comphenix.protocol.wrappers.WrappedBlockData) WrappedChatComponent(com.comphenix.protocol.wrappers.WrappedChatComponent) Optional(java.util.Optional) BlockPosition(com.comphenix.protocol.wrappers.BlockPosition) WrappedParticle(com.comphenix.protocol.wrappers.WrappedParticle) Direction(com.comphenix.protocol.wrappers.EnumWrappers.Direction) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FieldAccessException(com.comphenix.protocol.reflect.FieldAccessException) Vector3F(com.comphenix.protocol.wrappers.Vector3F) VillagerData(me.libraryaddict.disguise.disguisetypes.VillagerData) WrappedDataWatcherObject(com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject) AccessibleObject(java.lang.reflect.AccessibleObject) WrappedWatchableObject(com.comphenix.protocol.wrappers.WrappedWatchableObject) ItemStack(org.bukkit.inventory.ItemStack) EulerAngle(org.bukkit.util.EulerAngle) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) EntityPose(me.libraryaddict.disguise.disguisetypes.EntityPose)

Example 3 with NbtWrapper

use of com.comphenix.protocol.wrappers.nbt.NbtWrapper in project WheelCore by Plugindustry.

the class NBTBasedProvider method getInstance.

@Nullable
@Override
public ItemBase getInstance(@Nullable ItemStack itemStack) {
    if (itemStack == null)
        return null;
    Optional<NbtWrapper<?>> nbtWrapperOptional = NbtFactory.fromItemOptional((ItemStack) ShadowManager.shadowUnpack(CraftItemStack.asCraftCopy(itemStack)));
    if (nbtWrapperOptional.isEmpty())
        return null;
    NbtWrapper<?> nbtWrapper = nbtWrapperOptional.get();
    if (nbtWrapper.getType() != NbtType.TAG_COMPOUND)
        return null;
    NbtCompound compound = NbtFactory.asCompound(nbtWrapper);
    if (!compound.containsKey("wheel_core_item_type"))
        return null;
    Object type = compound.getObject("wheel_core_item_type");
    return type instanceof String ? MainManager.getItemInstanceFromId((String) type) : null;
}
Also used : NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) Nullable(javax.annotation.Nullable)

Example 4 with NbtWrapper

use of com.comphenix.protocol.wrappers.nbt.NbtWrapper in project WheelCore by Plugindustry.

the class NBTBasedProvider method getData.

@Nullable
@Override
public ItemData getData(@Nullable ItemStack itemStack) {
    if (itemStack == null)
        return null;
    Optional<NbtWrapper<?>> nbtWrapperOptional = NbtFactory.fromItemOptional((ItemStack) ShadowManager.shadowUnpack(CraftItemStack.asCraftCopy(itemStack)));
    if (nbtWrapperOptional.isEmpty())
        return null;
    NbtWrapper<?> nbtWrapper = nbtWrapperOptional.get();
    if (nbtWrapper.getType() != NbtType.TAG_COMPOUND)
        return null;
    NbtCompound compound = NbtFactory.asCompound(nbtWrapper);
    if (!compound.containsKey("wheel_core_item_data"))
        return null;
    Object data = compound.getObject("wheel_core_item_data");
    return data instanceof String ? gson.fromJson((String) data, ItemData.class) : null;
}
Also used : NbtCompound(com.comphenix.protocol.wrappers.nbt.NbtCompound) NbtWrapper(com.comphenix.protocol.wrappers.nbt.NbtWrapper) ItemData(io.github.plugindustry.wheelcore.interfaces.item.ItemData) Nullable(javax.annotation.Nullable)

Aggregations

NbtWrapper (com.comphenix.protocol.wrappers.nbt.NbtWrapper)4 NbtCompound (com.comphenix.protocol.wrappers.nbt.NbtCompound)3 Nullable (javax.annotation.Nullable)2 FieldAccessException (com.comphenix.protocol.reflect.FieldAccessException)1 BlockPosition (com.comphenix.protocol.wrappers.BlockPosition)1 Direction (com.comphenix.protocol.wrappers.EnumWrappers.Direction)1 Vector3F (com.comphenix.protocol.wrappers.Vector3F)1 WrappedBlockData (com.comphenix.protocol.wrappers.WrappedBlockData)1 WrappedChatComponent (com.comphenix.protocol.wrappers.WrappedChatComponent)1 WrappedDataWatcherObject (com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject)1 WrappedParticle (com.comphenix.protocol.wrappers.WrappedParticle)1 WrappedWatchableObject (com.comphenix.protocol.wrappers.WrappedWatchableObject)1 NbtList (com.comphenix.protocol.wrappers.nbt.NbtList)1 ItemData (io.github.plugindustry.wheelcore.interfaces.item.ItemData)1 IOException (java.io.IOException)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Optional (java.util.Optional)1 Nonnull (javax.annotation.Nonnull)1 EntityPose (me.libraryaddict.disguise.disguisetypes.EntityPose)1