mcMMO/src/main/java/com/gmail/nossr50/util/ItemUtils.java
2018-07-24 20:50:17 -04:00

759 lines
20 KiB
Java

package com.gmail.nossr50.util;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.party.ItemWeightConfig;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.meta.ItemMeta;
public final class ItemUtils {
private ItemUtils() {}
/**
* Checks if the item is a bow.
*
* @param item Item to check
* @return true if the item is a bow, false otherwise
*/
public static boolean isBow(ItemStack item) {
Material type = item.getType();
switch (type) {
case BOW:
return true;
default:
return mcMMO.getModManager().isCustomBow(type);
}
}
/**
* Checks if the item is a sword.
*
* @param item Item to check
* @return true if the item is a sword, false otherwise
*/
public static boolean isSword(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_SWORD:
case GOLDEN_SWORD:
case IRON_SWORD:
case STONE_SWORD:
case WOODEN_SWORD:
return true;
default:
return mcMMO.getModManager().isCustomSword(type);
}
}
/**
* Checks if the item is a hoe.
*
* @param item Item to check
* @return true if the item is a hoe, false otherwise
*/
public static boolean isHoe(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_HOE:
case GOLDEN_HOE:
case IRON_HOE:
case STONE_HOE:
case WOODEN_HOE:
return true;
default:
return mcMMO.getModManager().isCustomHoe(type);
}
}
/**
* Checks if the item is a shovel.
*
* @param item Item to check
* @return true if the item is a shovel, false otherwise
*/
public static boolean isShovel(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_SHOVEL:
case GOLDEN_SHOVEL:
case IRON_SHOVEL:
case STONE_SHOVEL:
case WOODEN_SHOVEL:
return true;
default:
return mcMMO.getModManager().isCustomShovel(type);
}
}
/**
* Checks if the item is an axe.
*
* @param item Item to check
* @return true if the item is an axe, false otherwise
*/
public static boolean isAxe(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_AXE:
case GOLDEN_AXE:
case IRON_AXE:
case STONE_AXE:
case WOODEN_AXE:
return true;
default:
return mcMMO.getModManager().isCustomAxe(type);
}
}
/**
* Checks if the item is a pickaxe.
*
* @param item Item to check
* @return true if the item is a pickaxe, false otherwise
*/
public static boolean isPickaxe(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_PICKAXE:
case GOLDEN_PICKAXE:
case IRON_PICKAXE:
case STONE_PICKAXE:
case WOODEN_PICKAXE:
return true;
default:
return mcMMO.getModManager().isCustomPickaxe(type);
}
}
/**
* Checks if the item counts as unarmed.
*
* @param item Item to check
* @return true if the item counts as unarmed, false otherwise
*/
public static boolean isUnarmed(ItemStack item) {
if (Config.getInstance().getUnarmedItemsAsUnarmed()) {
return !isMinecraftTool(item);
}
return item.getType() == Material.AIR;
}
/**
* Checks if the item is a helmet.
*
* @param item Item to check
* @return true if the item is a helmet, false otherwise
*/
public static boolean isHelmet(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_HELMET:
case GOLDEN_HELMET:
case IRON_HELMET:
case CHAINMAIL_HELMET:
case LEATHER_HELMET:
return true;
default:
return mcMMO.getModManager().isCustomHelmet(type);
}
}
/**
* Checks if the item is a chestplate.
*
* @param item Item to check
* @return true if the item is a chestplate, false otherwise
*/
public static boolean isChestplate(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_CHESTPLATE:
case GOLDEN_CHESTPLATE:
case IRON_CHESTPLATE:
case CHAINMAIL_CHESTPLATE:
case LEATHER_CHESTPLATE:
return true;
default:
return mcMMO.getModManager().isCustomChestplate(type);
}
}
/**
* Checks if the item is a pair of pants.
*
* @param item Item to check
* @return true if the item is a pair of pants, false otherwise
*/
public static boolean isLeggings(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_LEGGINGS:
case GOLDEN_LEGGINGS:
case IRON_LEGGINGS:
case CHAINMAIL_LEGGINGS:
case LEATHER_LEGGINGS:
return true;
default:
return mcMMO.getModManager().isCustomLeggings(type);
}
}
/**
* Checks if the item is a pair of boots.
*
* @param item Item to check
* @return true if the item is a pair of boots, false otherwise
*/
public static boolean isBoots(ItemStack item) {
Material type = item.getType();
switch (type) {
case DIAMOND_BOOTS:
case GOLDEN_BOOTS:
case IRON_BOOTS:
case CHAINMAIL_BOOTS:
case LEATHER_BOOTS:
return true;
default:
return mcMMO.getModManager().isCustomBoots(type);
}
}
/**
* Checks to see if an item is a wearable armor piece.
*
* @param item Item to check
* @return true if the item is armor, false otherwise
*/
public static boolean isArmor(ItemStack item) {
return isHelmet(item) || isChestplate(item) || isLeggings(item) || isBoots(item);
}
/**
* Checks to see if an item is a wearable *vanilla* armor piece.
*
* @param item Item to check
* @return true if the item is armor, false otherwise
*/
public static boolean isMinecraftArmor(ItemStack item) {
return isLeatherArmor(item) || isGoldArmor(item) || isIronArmor(item) || isDiamondArmor(item) || isChainmailArmor(item);
}
/**
* Checks to see if an item is a leather armor piece.
*
* @param item Item to check
* @return true if the item is leather armor, false otherwise
*/
public static boolean isLeatherArmor(ItemStack item) {
switch (item.getType()) {
case LEATHER_BOOTS:
case LEATHER_CHESTPLATE:
case LEATHER_HELMET:
case LEATHER_LEGGINGS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a gold armor piece.
*
* @param item Item to check
* @return true if the item is gold armor, false otherwise
*/
public static boolean isGoldArmor(ItemStack item) {
switch (item.getType()) {
case GOLDEN_BOOTS:
case GOLDEN_CHESTPLATE:
case GOLDEN_HELMET:
case GOLDEN_LEGGINGS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is an iron armor piece.
*
* @param item Item to check
* @return true if the item is iron armor, false otherwise
*/
public static boolean isIronArmor(ItemStack item) {
switch (item.getType()) {
case IRON_BOOTS:
case IRON_CHESTPLATE:
case IRON_HELMET:
case IRON_LEGGINGS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a diamond armor piece.
*
* @param item Item to check
* @return true if the item is diamond armor, false otherwise
*/
public static boolean isDiamondArmor(ItemStack item) {
switch (item.getType()) {
case DIAMOND_BOOTS:
case DIAMOND_CHESTPLATE:
case DIAMOND_HELMET:
case DIAMOND_LEGGINGS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a chainmail armor piece.
*
* @param item Item to check
* @return true if the item is chainmail armor, false otherwise
*/
public static boolean isChainmailArmor(ItemStack item) {
switch (item.getType()) {
case CHAINMAIL_BOOTS:
case CHAINMAIL_CHESTPLATE:
case CHAINMAIL_HELMET:
case CHAINMAIL_LEGGINGS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a *vanilla* tool.
*
* @param item Item to check
* @return true if the item is a tool, false otherwise
*/
public static boolean isMinecraftTool(ItemStack item) {
return isStoneTool(item) || isWoodTool(item) || isGoldTool(item) || isIronTool(item) || isDiamondTool(item) || isStringTool(item);
}
/**
* Checks to see if an item is a stone tool.
*
* @param item Item to check
* @return true if the item is a stone tool, false otherwise
*/
public static boolean isStoneTool(ItemStack item) {
switch (item.getType()) {
case STONE_AXE:
case STONE_HOE:
case STONE_PICKAXE:
case STONE_SHOVEL:
case STONE_SWORD:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a wooden tool.
*
* @param item Item to check
* @return true if the item is a wooden tool, false otherwise
*/
public static boolean isWoodTool(ItemStack item) {
switch (item.getType()) {
case WOODEN_AXE:
case WOODEN_HOE:
case WOODEN_PICKAXE:
case WOODEN_SHOVEL:
case WOODEN_SWORD:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a string tool.
*
* @param item Item to check
* @return true if the item is a string tool, false otherwise
*/
public static boolean isStringTool(ItemStack item) {
switch (item.getType()) {
case BOW:
case CARROT_ON_A_STICK:
case FISHING_ROD:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a gold tool.
*
* @param item Item to check
* @return true if the item is a stone tool, false otherwise
*/
public static boolean isGoldTool(ItemStack item) {
switch (item.getType()) {
case GOLDEN_AXE:
case GOLDEN_HOE:
case GOLDEN_PICKAXE:
case GOLDEN_SHOVEL:
case GOLDEN_SWORD:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is an iron tool.
*
* @param item Item to check
* @return true if the item is an iron tool, false otherwise
*/
public static boolean isIronTool(ItemStack item) {
switch (item.getType()) {
case BUCKET:
case FLINT_AND_STEEL:
case IRON_AXE:
case IRON_HOE:
case IRON_PICKAXE:
case IRON_SHOVEL:
case IRON_SWORD:
case SHEARS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a diamond tool.
*
* @param item Item to check
* @return true if the item is a diamond tool, false otherwise
*/
public static boolean isDiamondTool(ItemStack item) {
switch (item.getType()) {
case DIAMOND_AXE:
case DIAMOND_HOE:
case DIAMOND_PICKAXE:
case DIAMOND_SHOVEL:
case DIAMOND_SWORD:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is enchantable.
*
* @param item Item to check
* @return true if the item is enchantable, false otherwise
*/
public static boolean isEnchantable(ItemStack item) {
switch (item.getType()) {
case ENCHANTED_BOOK:
case SHEARS:
case FISHING_ROD:
case CARROT_ON_A_STICK:
case FLINT_AND_STEEL:
return true;
default:
return isArmor(item) || isSword(item) || isAxe(item) || isShovel(item) || isPickaxe(item) || isBow(item);
}
}
public static boolean isSmeltable(ItemStack item) {
return item != null && MaterialUtils.isOre(item.getData());
}
public static boolean isSmelted(ItemStack item) {
if (item == null) {
return false;
}
for (Recipe recipe : mcMMO.p.getServer().getRecipesFor(item)) {
if (recipe instanceof FurnaceRecipe && MaterialUtils.isOre(((FurnaceRecipe) recipe).getInput().getData())) {
return true;
}
}
return false;
}
/**
* Check if an item is sharable.
*
* @param item Item that will get shared
* @return True if the item can be shared.
*/
public static boolean isSharable(ItemStack item) {
if (item == null || item.getType() == Material.AIR) {
return false;
}
return isMiningDrop(item) || isWoodcuttingDrop(item) || isMobDrop(item) || isHerbalismDrop(item) || isMiscDrop(item);
}
/**
* Checks to see if an item is a mining drop.
*
* @param item Item to check
* @return true if the item is a mining drop, false otherwise
*/
public static boolean isMiningDrop(ItemStack item) {
switch (item.getType()) {
case COAL:
case COAL_ORE:
case DIAMOND:
case DIAMOND_ORE:
case EMERALD:
case EMERALD_ORE:
case GOLD_ORE:
case IRON_ORE:
case LAPIS_ORE:
case REDSTONE_ORE: // Should we also have Glowing Redstone Ore here?
case REDSTONE:
case GLOWSTONE_DUST: // Should we also have Glowstone here?
case QUARTZ:
case NETHER_QUARTZ_ORE:
case LAPIS_LAZULI:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a herbalism drop.
*
* @param item Item to check
* @return true if the item is a herbalism drop, false otherwise
*/
public static boolean isHerbalismDrop(ItemStack item) {
switch (item.getType()) {
case WHEAT:
case WHEAT_SEEDS:
case CARROT:
case CHORUS_FRUIT:
case CHORUS_FLOWER:
case POTATO:
case BEETROOT:
case BEETROOT_SEEDS:
case NETHER_WART:
case BROWN_MUSHROOM:
case RED_MUSHROOM:
case ROSE_RED:
case DANDELION_YELLOW:
case CACTUS:
case SUGAR_CANE:
case MELON:
case MELON_SEEDS:
case PUMPKIN:
case PUMPKIN_SEEDS:
case LILY_PAD:
case VINE:
case TALL_GRASS:
case COCOA_BEANS:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a mob drop.
*
* @param item Item to check
* @return true if the item is a mob drop, false otherwise
*/
public static boolean isMobDrop(ItemStack item) {
switch (item.getType()) {
case STRING:
case FEATHER:
case CHICKEN:
case COOKED_CHICKEN:
case LEATHER:
case BEEF:
case COOKED_BEEF:
case PORKCHOP:
case COOKED_PORKCHOP:
case WHITE_WOOL:
case BLACK_WOOL:
case BLUE_WOOL:
case BROWN_WOOL:
case CYAN_WOOL:
case GRAY_WOOL:
case GREEN_WOOL:
case LIGHT_BLUE_WOOL:
case LIGHT_GRAY_WOOL:
case LIME_WOOL:
case MAGENTA_WOOL:
case ORANGE_WOOL:
case PINK_WOOL:
case PURPLE_WOOL:
case RED_WOOL:
case YELLOW_WOOL:
case IRON_INGOT:
case SNOWBALL:
case BLAZE_ROD:
case SPIDER_EYE:
case GUNPOWDER:
case ENDER_PEARL:
case GHAST_TEAR:
case MAGMA_CREAM:
case BONE:
case ARROW:
case SLIME_BALL:
case NETHER_STAR:
case ROTTEN_FLESH:
case GOLD_NUGGET:
case EGG:
case ROSE_RED:
case COAL:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a woodcutting drop.
*
* @param item Item to check
* @return true if the item is a woodcutting drop, false otherwise
*/
public static boolean isWoodcuttingDrop(ItemStack item) {
switch (item.getType()) {
case ACACIA_LOG:
case BIRCH_LOG:
case DARK_OAK_LOG:
case JUNGLE_LOG:
case OAK_LOG:
case SPRUCE_LOG:
case OAK_WOOD:
case ACACIA_WOOD:
case BIRCH_WOOD:
case DARK_OAK_WOOD:
case STRIPPED_ACACIA_WOOD:
case JUNGLE_WOOD:
case SPRUCE_WOOD:
case STRIPPED_BIRCH_WOOD:
case STRIPPED_DARK_OAK_WOOD:
case STRIPPED_JUNGLE_WOOD:
case STRIPPED_OAK_WOOD:
case STRIPPED_SPRUCE_WOOD:
case STRIPPED_ACACIA_LOG:
case STRIPPED_BIRCH_LOG:
case STRIPPED_DARK_OAK_LOG:
case STRIPPED_JUNGLE_LOG:
case STRIPPED_OAK_LOG:
case STRIPPED_SPRUCE_LOG:
case ACACIA_SAPLING:
case SPRUCE_SAPLING:
case BIRCH_SAPLING:
case DARK_OAK_SAPLING:
case JUNGLE_SAPLING:
case OAK_SAPLING:
case ACACIA_LEAVES:
case BIRCH_LEAVES:
case DARK_OAK_LEAVES:
case JUNGLE_LEAVES:
case OAK_LEAVES:
case SPRUCE_LEAVES:
case APPLE:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a miscellaneous drop. These items are read from the config file
*
* @param item Item to check
* @return true if the item is a miscellaneous drop, false otherwise
*/
public static boolean isMiscDrop(ItemStack item) {
return ItemWeightConfig.getInstance().getMiscItems().contains(item.getType());
}
public static boolean isMcMMOItem(ItemStack item) {
if (!item.hasItemMeta()) {
return false;
}
ItemMeta itemMeta = item.getItemMeta();
return itemMeta.hasLore() && itemMeta.getLore().contains("mcMMO Item");
}
public static boolean isChimaeraWing(ItemStack item) {
if (!isMcMMOItem(item)) {
return false;
}
ItemMeta itemMeta = item.getItemMeta();
return itemMeta.hasDisplayName() && itemMeta.getDisplayName().equals(ChatColor.GOLD + LocaleLoader.getString("Item.ChimaeraWing.Name"));
}
public static boolean isFluxPickaxe(ItemStack item) {
if (!isMcMMOItem(item)) {
return false;
}
ItemMeta itemMeta = item.getItemMeta();
return itemMeta.hasDisplayName() && itemMeta.getDisplayName().equals(ChatColor.GOLD + LocaleLoader.getString("Item.FluxPickaxe.Name"));
}
}