RecipeUtil, isPotion and Qualsish.

This commit is contained in:
Olof Larsson 2016-05-05 19:45:22 +02:00
parent d9a3a26f68
commit 57194df2d9
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
3 changed files with 147 additions and 0 deletions

View File

@ -880,6 +880,19 @@ public class InventoryUtil
return true; return true;
} }
public static boolean isPotion(ItemStack itemStack)
{
if (isNothing(itemStack)) return false;
Material material = itemStack.getType();
return isPotion(material);
}
public static boolean isPotion(Material material)
{
if (material == null) return false;
return material.name().contains("POTION");
}
// -------------------------------------------- // // -------------------------------------------- //
// CLONE ITEMSTACKS/INVENTORY // CLONE ITEMSTACKS/INVENTORY
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1669,6 +1669,7 @@ public class MUtil
{ {
if (object1 == null) return object2 == null; if (object1 == null) return object2 == null;
if (object2 == null) return false; if (object2 == null) return false;
return object1.equals(object2); return object1.equals(object2);
} }
@ -1691,6 +1692,20 @@ public class MUtil
return true; return true;
} }
// -------------------------------------------- //
// EQUALSISH
// -------------------------------------------- //
public static final double EQUALSISH_EPSILON = 0.0001;
public static boolean equalsish(Number number1, Number number2)
{
if (number1 == null) return number2 == null;
if (number2 == null) return false;
return Math.abs(number1.doubleValue() - number2.doubleValue()) < EQUALSISH_EPSILON;
}
// -------------------------------------------- // // -------------------------------------------- //
// SORTING // SORTING
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -0,0 +1,119 @@
package com.massivecraft.massivecore.util;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
// NOTE: This utility targets 1.9 and will crash on older servers.
public class RecipeUtil
{
// ------------------------------------------- //
// POTION
// -------------------------------------------- //
public static ItemStack createPotionItemStack(PotionType type, Material material, boolean upgraded, boolean extended, int amount)
{
ItemStack ret = new ItemStack(material, amount);
PotionMeta meta = (PotionMeta)ret.getItemMeta();
PotionData data = new PotionData(type, extended, upgraded);
meta.setBasePotionData(data);
ret.setItemMeta(meta);
return ret;
}
public static ShapelessRecipe createPotion(PotionType type, Material material, boolean upgraded, boolean extended, Object... objects)
{
// When brewing you actually get 3 potions.
final int amount = 3;
ItemStack item = createPotionItemStack(type, material, upgraded, extended, amount);
return createShapeless(item, objects);
}
public static ShapelessRecipe addPotion(PotionType type, Material material, boolean upgraded, boolean extended, Object... objects)
{
ShapelessRecipe recipe = createPotion(type, material, upgraded, extended, objects);
Bukkit.getServer().addRecipe(recipe);
return recipe;
}
// ------------------------------------------- //
// CIRCULAR
// -------------------------------------------- //
public static void addCircular(Material material, int maxData)
{
ItemStack[] items = new ItemStack[maxData];
for (int i = 0; i < maxData; i++)
{
items[i] = new ItemStack(material, 1, (short) i);
}
addCircular(items);
}
public static void addCircular(ItemStack... items)
{
for (int i = 0; i < items.length; i++)
{
int next = (i+1) % items.length;
ItemStack item = items[i];
addShapeless(items[next], item.getDurability(), item.getAmount(), item.getType());
}
}
// ------------------------------------------- //
// SHAPELESS
// -------------------------------------------- //
@SuppressWarnings("deprecation")
public static ShapelessRecipe createShapeless(ItemStack result, Object... objects)
{
ShapelessRecipe recipe = new ShapelessRecipe(result);
int quantity = 1;
int data = 0;
Material material = null;
for (Object object : objects)
{
if (object instanceof Number)
{
if (object instanceof Integer)
{
quantity = ((Integer)object).intValue();
}
else
{
data = ((Number)object).intValue();
}
}
else if (object instanceof Material)
{
material = (Material)object;
recipe.addIngredient(quantity, material, data);
quantity = 1;
data = 0;
material = null;
}
else
{
throw new IllegalArgumentException(String.valueOf(object));
}
}
return recipe;
}
public static ShapelessRecipe addShapeless(ItemStack result, Object... objects)
{
ShapelessRecipe recipe = createShapeless(result, objects);
Bukkit.getServer().addRecipe(recipe);
return recipe;
}
}