/*
 * This file is part of NucleusFramework for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jcwhatever.nucleus.utils.potions;

import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import javax.annotation.Nullable;

/**
 * Potion utilities.
 */
public final class PotionUtils {

    private PotionUtils() {}

    /**
     * Get a {@link org.bukkit.potion.PotionEffectType} from an object.
     *
     * <p>The object must be an instance of {@link org.bukkit.potion.PotionEffectType},
     * {@link org.bukkit.potion.PotionEffect} or the name of the type.</p>
     *
     * @param object  The potion effect type or name.
     *
     * @return  The potion effect type or null if it could not be converted..
     */
    @Nullable
    public static PotionEffectType getPotionEffectType(Object object) {

        if (object instanceof String) {
            String name = ((String)object).toUpperCase();

            PotionEffectType type = PotionEffectType.getByName(name);
            if (type == null)
                return null;

            return type;
        }
        else if (object instanceof PotionEffectType) {
            return (PotionEffectType)object;
        }
        else if (object instanceof PotionEffect) {
            PotionEffect effect = (PotionEffect)object;
            return effect.getType();
        }
        else {
            return null;
        }
    }
}

@@ -1266,16 +1266,326 @@
 tions;%0A%0A
+import com.jcwhatever.nucleus.utils.PreCon;%0Aimport com.jcwhatever.nucleus.utils.materials.Materials;%0Aimport com.jcwhatever.nucleus.utils.nms.INmsPotionHandler;%0Aimport com.jcwhatever.nucleus.utils.nms.NmsUtils;%0Aimport org.bukkit.Material;%0Aimport org.bukkit.inventory.ItemStack;%0Aimport org.bukkit.potion.Potion;%0A
 import o
@@ -1615,16 +1615,16 @@
 Effect;%0A
-
 import o
@@ -1787,16 +1787,100 @@
 s() %7B%7D%0A%0A
+    private static final INmsPotionHandler _handler = NmsUtils.getPotionHandler();%0A%0A
     /**%0A
@@ -2930,16 +2930,16 @@
 n null;%0A
-
         
@@ -2945,11 +2945,1529 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Determine if an item stack can potentially be used as a potion ingredient.%0A     *%0A     * @param itemStack  The item stack to check.%0A     */%0A    public static boolean isPotionIngredient(ItemStack itemStack) %7B%0A        PreCon.notNull(itemStack);%0A%0A        Material material = itemStack.getType();%0A%0A        return Materials.isPotionIngredient(material)%0A                && (material != Material.RAW_FISH %7C%7C itemStack.getData().getData() == 3);%0A    %7D%0A%0A    /**%0A     * Get %7B@link Potion%7D for the result of the specified potion recipe.%0A     *%0A     * @param ingredient  The potion ingredient.%0A     * @param bottle      The potion target.%0A     *%0A     * @return  The potion or null if not a valid recipe.%0A     */%0A    @Nullable%0A    public static Potion getPotionFromRecipe(ItemStack ingredient, ItemStack bottle) %7B%0A        int potionId = getPotionIdFromRecipe(ingredient, bottle);%0A        if (potionId == -1)%0A            return null;%0A%0A        return new Potion(potionId);%0A    %7D%0A%0A    /**%0A     * Get the potion ID for the result of the specified potion recipe.%0A     *%0A     * @param ingredient  The potion ingredient.%0A     * @param bottle      The potion target.%0A     *%0A     * @return  The potion Id or -1 if not a valid recipe.%0A     */%0A    public static int getPotionIdFromRecipe(ItemStack ingredient, ItemStack bottle) %7B%0A        PreCon.notNull(ingredient);%0A        PreCon.notNull(bottle);%0A%0A        if (_handler != null)%0A            return _handler.getPotionIdFromRecipe(ingredient, bottle);%0A%0A        return -1;%0A    %7D
 %0A%7D%0A

add PotionUtils#getPotionFromRecipe, #getPotionIdFromRecipe