Java Code Examples for org.bukkit.inventory.ItemStack#getDurability()

The following examples show how to use org.bukkit.inventory.ItemStack#getDurability() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LanguageHelper.java    From LanguageUtils with MIT License 6 votes vote down vote up
/**
 * Return the localized name of the item.
 *
 * @param item   The item
 * @param locale The language of the item
 * @return The localized name. if the item doesn't have a localized name, this method will return the unlocalized name of it.
 */
public static String getItemName(ItemStack item, String locale) {
    switch (item.getType()) {
        case POTION:
        case SPLASH_POTION:
        case LINGERING_POTION:
        case TIPPED_ARROW:
            return EnumPotionEffect.getLocalizedName(item, locale);
        case MONSTER_EGG:
            return EnumEntity.getSpawnEggName(item, locale);
        case SKULL_ITEM:
            if (item.getDurability() == 3) {
                return EnumItem.getPlayerSkullName(item, locale);
            }
        default: return translateToLocal(getItemUnlocalizedName(item), locale);
    }
}
 
Example 2
Source File: ItemFactory.java    From TradePlus with GNU General Public License v3.0 6 votes vote down vote up
public ItemFactory(ItemStack stack) {
  damage = stack.getDurability();
  material = stack.getType();
  amount = stack.getAmount();
  data = stack.getData().getData();
  if (stack.hasItemMeta()) {
    ItemMeta meta = stack.getItemMeta();
    if (meta.hasDisplayName()) {
      display = meta.getDisplayName();
    }
    if (meta.hasLore()) {
      lore = meta.getLore();
    }
    if (Sounds.version != 17) flags = new ArrayList<>(meta.getItemFlags());
  }
  if (Sounds.version > 113) {
    customModelData = ItemUtils1_14.getCustomModelData(stack);
  }
}
 
Example 3
Source File: Utils.java    From Shopkeepers with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isCustomHeadItem(ItemStack item) {
	if (item == null) return false;
	if (item.getType() != Material.SKULL_ITEM) {
		return false;
	}
	if (item.getDurability() != SkullType.PLAYER.ordinal()) {
		return false;
	}

	ItemMeta meta = item.getItemMeta();
	if (meta instanceof SkullMeta) {
		SkullMeta skullMeta = (SkullMeta) meta;
		if (skullMeta.hasOwner() && skullMeta.getOwner() == null) {
			// custom head items usually don't have a valid owner
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: OLD_ItemFact.java    From QualityArmory with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public static List<String> getCraftingLore(CustomBaseObject a) {
	List<String> lore = new ArrayList<String>();
	lore.add(ChatColor.RED + QAMain.S_ITEM_ING + ": ");
	for (Object raw : (a).getIngredientsRaw()) {
		if (raw instanceof ItemStack) {
			ItemStack is = (ItemStack) raw;
			StringBuilder sb = new StringBuilder();
			// Chris: itemName from message.yml
			String itemName = is.getType().name();
			sb.append(ChatColor.RED + "- " + QAMain.findCraftEntityName(itemName, itemName) + " x " + is.getAmount());
			if (is.getDurability() != 0)
				sb.append(":" + is.getDurability());
			lore.add(sb.toString());

		} else if (raw instanceof String) {
			lore.add(ChatColor.RED + "- " + QualityArmory.getCustomItemByName((String) raw).getDisplayName());
		}
		if ((a).getCraftingReturn() > 1) {
			lore.add(ChatColor.DARK_RED + QAMain.S_ITEM_CRAFTS + " " + (a).getCraftingReturn());
		}
	}
	return lore;
}
 
Example 5
Source File: QualityArmory.java    From QualityArmory with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public static boolean isCustomItemNextId(ItemStack is) {
	if (is == null)
		return false;
	try{
		if(CustomItemManager.isUsingCustomData())
			return false;
	}catch (Error|Exception e4){}
	List<MaterialStorage> ms = new ArrayList<MaterialStorage>();
	ms.addAll(QAMain.expansionPacks);
	ms.addAll(QAMain.gunRegister.keySet());
	ms.addAll(QAMain.armorRegister.keySet());
	ms.addAll(QAMain.ammoRegister.keySet());
	ms.addAll(QAMain.miscRegister.keySet());
	for (MaterialStorage mat : ms) {
		if (mat.getMat() == is.getType())
			if (mat.getData() == (is.getDurability() + 1))
				if (!mat.hasVariant())
					return true;
	}
	return false;
}
 
Example 6
Source File: QualityArmory.java    From QualityArmory with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public static boolean isIronSights(ItemStack is) {
	if (is == null)
		return false;
	if (is != null && is.getType() == IronsightsHandler.ironsightsMaterial	)
		try{
			if(!is.hasItemMeta() || !is.getItemMeta().hasCustomModelData())
				return false;
			if(is.getItemMeta().getCustomModelData() == IronsightsHandler
			.ironsightsData)
				return true;
		}catch (Error|Exception e4){
			if(is.getDurability() == IronsightsHandler.ironsightsData)
				return true;
		}
	return false;
}
 
Example 7
Source File: ConfigPre113Importer.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
private String convertItemAmount(String reward) {
    Matcher m = ITEM_AMOUNT_PATTERN.matcher(reward);
    if (!m.matches()) {
        throw new IllegalArgumentException("Unknown item: '" + reward + "'");
    }
    String itemstack = m.group("itemstack");
    ItemStack itemStack = ItemStackUtil.createItemStack(itemstack);
    String newItemStack = itemStack.getType().name() + (itemStack.getDurability() > 0 ? ":" + itemStack.getDurability() : "");
    return replaceItemStack(m, reward, newItemStack);
}
 
Example 8
Source File: ItemUtil.java    From EchoPet with GNU General Public License v3.0 5 votes vote down vote up
public static boolean matches(ItemStack itemStack1, ItemStack itemStack2) {
    if (itemStack1 == null || itemStack2 == null) {
        return false;
    }

    if (!itemStack1.getType().equals(itemStack2.getType())) {
        return false;
    }

    if (itemStack1.getAmount() != itemStack2.getAmount()) {
        return false;
    }

    if (itemStack1.getDurability() != itemStack2.getDurability()) {
        return false;
    }

    if (itemStack1.getMaxStackSize() != itemStack2.getMaxStackSize()) {
        return false;
    }

    if (itemStack1.hasItemMeta() != itemStack2.hasItemMeta()) {
        return false;
    }

    if (itemStack1.hasItemMeta() && itemStack2.hasItemMeta() && !itemStack1.getItemMeta().equals(itemStack2.getItemMeta())) {
        return false;
    }

    return true;
}
 
Example 9
Source File: ConfigPre113Importer.java    From uSkyBlock with GNU General Public License v3.0 5 votes vote down vote up
private String convertRequiredItems(String value) {
    Matcher m = REQ_PATTERN.matcher(value);
    if (m.matches()) {
        String itemstack = m.group("itemstack");
        ItemStack itemStack = ItemStackUtil.createItemStack(itemstack);
        String newItemStack = itemStack.getType().name() + (itemStack.getDurability() > 0 ? ":" + itemStack.getDurability() : "");
        return replaceItemStack(m, value, newItemStack);
    }
    return null;
}
 
Example 10
Source File: NMSHandler.java    From askyblock with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isPotion(ItemStack item) {
    if (item.getType().equals(Material.POTION) && item.getDurability() != 0) {
        return true;
    }
    return false;
}
 
Example 11
Source File: NMSHandler.java    From askyblock with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isPotion(ItemStack item) {
    if (item.getType().equals(Material.POTION) && item.getDurability() != 0) {
        return true;
    }
    return false;
}
 
Example 12
Source File: ItemFrameStorage.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public void setItem(ItemStack stack) {

		ItemFrame frame = getItemFrame();
		if (frame != null) {
			ItemStack newStack = new ItemStack(stack.getType(), 1, stack.getDurability());
			newStack.setData(stack.getData());
			newStack.setItemMeta(stack.getItemMeta());
			frame.setItem(newStack);
		} else {
			CivLog.warning("Frame:"+this.frameID+" was null when trying to set to "+stack.getType().name());
		}
		
	}
 
Example 13
Source File: ResourceSpawner.java    From BedwarsRel with GNU General Public License v3.0 5 votes vote down vote up
public boolean canContainItem(Inventory inv, ItemStack item) {
  int space = 0;
  for (ItemStack stack : inv.getContents()) {
    if (stack == null) {
      space += item.getMaxStackSize();
    } else if (stack.getType() == item.getType()
        && stack.getDurability() == item.getDurability()) {
      space += item.getMaxStackSize() - stack.getAmount();
    }
  }
  return space >= item.getAmount();
}
 
Example 14
Source File: BrushBoundBaseBlock.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public BrushBoundBaseBlock(Player player, LocalSession session, ItemStack item) {
    super(item.getTypeId(), item.getType().getMaxDurability() != 0 || item.getDurability() > 15 ? 0 : Math.max(0, item.getDurability()), getNBT(item));
    this.item = item;
    this.tool = brushCache.get(getKey(item));
    this.player = player;
    this.session = session;
}
 
Example 15
Source File: NMSHandler.java    From askyblock with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isPotion(ItemStack item) {
    if (item.getType().equals(Material.POTION) && item.getDurability() != 0) {
        return true;
    }
    return false;
}
 
Example 16
Source File: Craft.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
public Creator setRecipeItem(int i, ItemStack recipeItem){
	recipe[i] = new ItemStack(recipeItem.getType(), 1, recipeItem.getDurability());
	return this;
}
 
Example 17
Source File: DurabilityBar.java    From AdditionsAPI with MIT License 4 votes vote down vote up
public static void sendDurabilityBossBar(Player player, ItemStack item, EquipmentSlot slot) {
	BossBarConfig config = ConfigFile.getInstance().getBossBarConfig();
	if (!config.show())
		return;
	UUID uuid = player.getUniqueId();
	BossBar bar;
	HashMap<UUID, BossBar> playersBars;
	String title;
	if (slot.equals(EquipmentSlot.HAND)) {
		title = LangFileUtils.get("item_durability_main_hand");
		playersBars = playersBarsMain;
	} else if (slot.equals(EquipmentSlot.OFF_HAND)) {
		title = LangFileUtils.get("item_durability_off_hand");
		playersBars = playersBarsOff;
	} else {
		return;
	}
	if (!playersBars.containsKey(uuid)) {
		bar = Bukkit.createBossBar(title, BarColor.GREEN, BarStyle.SOLID);
		bar.addPlayer(player);
		playersBars.put(uuid, bar);
	} else {
		bar = playersBars.get(uuid);
	}
	if (item == null || item.getType() == Material.AIR) {
		bar.setVisible(false);
		bar.setProgress(1.0D);
		return;
	}
	int durability = 0;
	int durabilityMax = 0;
	if (AdditionsAPI.isCustomItem(item)) {
		if (!config.showCustomItems() || item.getType().getMaxDurability() == 0) {
			bar.setVisible(false);
			bar.setProgress(1.0D);
			return;
		}
		CustomItemStack cStack = new CustomItemStack(item);
		CustomItem cItem = cStack.getCustomItem();
		if (cItem.hasFakeDurability()) {
			durability = cStack.getFakeDurability();
			durabilityMax = cItem.getFakeDurability();
		} else if (cStack.getCustomItem().isUnbreakable()) {
			bar.setVisible(false);
			bar.setProgress(1.0D);
			return;
		} else {
			durabilityMax = item.getType().getMaxDurability();
			durability = durabilityMax - item.getDurability();
		}
	} else if (item.getType().getMaxDurability() != 0) {
		if (!config.showVanillaItems()) {
			bar.setVisible(false);
			bar.setProgress(1.0D);
			return;
		}
		durabilityMax = item.getType().getMaxDurability();
		durability = durabilityMax - item.getDurability();
	} else {
		bar.setVisible(false);
		bar.setProgress(1.0D);
		return;
	}
	double progress = (double) durability / (double) durabilityMax;
	if (progress > 1) {
		progress = 1;
	} else if (progress < 0) {
		progress = 0;
	}
	bar.setVisible(true);
	if (progress < 0)
		progress = 0;
	else if (progress > 1)
		progress = 1;
	try {
		bar.setProgress(progress);
	} catch (IllegalArgumentException event) {}
	if (progress >= 0.5) {
		bar.setColor(BarColor.GREEN);
		bar.setTitle(title + ChatColor.GREEN + durability + " / " + durabilityMax);
	} else if (progress >= 0.25) {
		bar.setColor(BarColor.YELLOW);
		bar.setTitle(title + ChatColor.YELLOW + durability + " / " + durabilityMax);
	} else {
		bar.setColor(BarColor.RED);
		bar.setTitle(title + ChatColor.RED + durability + " / " + durabilityMax);
	}
}
 
Example 18
Source File: VeinMinerListener.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onBlockBreak(BlockBreakEvent e){
    Player player = e.getPlayer();

    if (!player.isSneaking()){
        return;
    }

    Block block = e.getBlock();
    ItemStack tool = player.getItemInHand();

    if (block.getType() == UniversalMaterial.GLOWING_REDSTONE_ORE.getType()){
        block.setType(Material.REDSTONE_ORE);
    }

    if (!UniversalMaterial.isCorrectTool(block.getType(), player.getItemInHand().getType())){
        return;
    }

    // find all surrounding blocks
    Vein vein = new Vein(block);
    vein.process();

    player.getWorld().dropItem(player.getLocation().getBlock().getLocation().add(.5,.5,.5), vein.getDrops(getVeinMultiplier(vein.getDropType())));

    if (vein.getTotalXp() != 0){
        UhcItems.spawnExtraXp(player.getLocation(), vein.getTotalXp());
    }

    // Process blood diamonds.
    if (isActivated(Scenario.BLOODDIAMONDS) && vein.getDropType() == Material.DIAMOND){
        player.getWorld().playSound(player.getLocation(), UniversalSound.PLAYER_HURT.getSound(), 1, 1);

        if (player.getHealth() < vein.getOres()){
            player.setHealth(0);
        }else {
            player.setHealth(player.getHealth() - vein.getOres());
        }
    }

    int newDurability = tool.getDurability()-vein.getOres();
    if (newDurability<1) newDurability = 1;

    tool.setDurability((short) newDurability);
    player.setItemInHand(tool);
}
 
Example 19
Source File: ItemCost.java    From GiantTrees with GNU General Public License v3.0 4 votes vote down vote up
public ItemCost(ItemStack stack) {
    super(stack.getAmount());
    this.matchData = stack.getDurability() != 32767;
    this.matchMeta = true;
    this.toMatch = stack.clone();
}
 
Example 20
Source File: CraftItemStack.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
private CraftItemStack(ItemStack item) {
    this(item.getTypeId(), item.getAmount(), item.getDurability(), item.hasItemMeta() ? item.getItemMeta() : null);
}