From 57194df2d98afa07a3ed47a39014d50ce76f277f Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 5 May 2016 19:45:22 +0200 Subject: [PATCH] RecipeUtil, isPotion and Qualsish. --- .../massivecore/util/InventoryUtil.java | 13 ++ .../massivecraft/massivecore/util/MUtil.java | 15 +++ .../massivecore/util/RecipeUtil.java | 119 ++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/com/massivecraft/massivecore/util/RecipeUtil.java diff --git a/src/com/massivecraft/massivecore/util/InventoryUtil.java b/src/com/massivecraft/massivecore/util/InventoryUtil.java index 670a2eed..7313dc14 100644 --- a/src/com/massivecraft/massivecore/util/InventoryUtil.java +++ b/src/com/massivecraft/massivecore/util/InventoryUtil.java @@ -880,6 +880,19 @@ public class InventoryUtil 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 // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/util/MUtil.java b/src/com/massivecraft/massivecore/util/MUtil.java index babda48f..3f10adbc 100644 --- a/src/com/massivecraft/massivecore/util/MUtil.java +++ b/src/com/massivecraft/massivecore/util/MUtil.java @@ -1669,6 +1669,7 @@ public class MUtil { if (object1 == null) return object2 == null; if (object2 == null) return false; + return object1.equals(object2); } @@ -1691,6 +1692,20 @@ public class MUtil 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 // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/util/RecipeUtil.java b/src/com/massivecraft/massivecore/util/RecipeUtil.java new file mode 100644 index 00000000..99e5c510 --- /dev/null +++ b/src/com/massivecraft/massivecore/util/RecipeUtil.java @@ -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; + } + +}