int usesA = type.getMaxDurability() - itemA.getDurability(); int usesB = type.getMaxDurability() - itemB.getDurability(); int totalUses = (int) (usesA + usesB + type.getMaxDurability() * 0.05); int damage = type.getMaxDurability() - totalUses;
/** * Inflicts damage to an item. Unbreaking enchantment is applied if present. * * @param player the player holding the item, or null if held by a dispenser * @param holding the item * * @return the updated item stack */ public static ItemStack damageItem(GlowPlayer player, ItemStack holding) { if (player != null && player.getGameMode() == GameMode.CREATIVE) { return holding; } // Apply unbreaking enchantment. // TODO: Armor has a different formula for chance to avoid damage int durability = holding.getEnchantmentLevel(Enchantment.DURABILITY); if (durability > 0 && ThreadLocalRandom.current().nextDouble() < 1 / (durability + 1)) { return holding; } holding.setDurability((short) (holding.getDurability() + 1)); if (holding.getDurability() == holding.getType().getMaxDurability() + 1) { if (player != null) { EventFactory.getInstance() .callEvent(new PlayerItemBreakEvent(player, holding)); } return createEmptyStack(); } return holding; }
private static void generateMaterialStatistics() { for (Material material : Material.values()) { String name = ItemIds.getKeyName(material); if (material.isBlock()) { set(MINE_BLOCK, material, "mineBlock.minecraft." + name); } if (material.isItem()) { set(USE_ITEM, material, "useItem.minecraft." + name); set(CRAFT_ITEM, material, "craftItem.minecraft." + name); if (material.getMaxDurability() != 0) { set(BREAK_ITEM, material, "breakItem.minecraft." + name); } } set(DROP, material, "drop.minecraft." + name); set(PICKUP, material, "pickup.minecraft." + name); } }
short maxDurability = tool.getType().getMaxDurability(); if (!InventoryUtil.isEmpty(tool) && maxDurability != 0 && durability != maxDurability) { int baseDamage; // Before applying unbreaking enchantment
if (!InventoryUtil.isEmpty(holding) && holding.getType().getMaxDurability() > 0 && holding.getDurability() > holding.getType().getMaxDurability()) { holding.setAmount(holding.getAmount() - 1); holding.setDurability((short) 0);
ItemStack chestplate = player.getInventory().getChestplate(); boolean hasElytra = chestplate != null && chestplate.getType() == Material.ELYTRA && chestplate.getDurability() < chestplate.getType().getMaxDurability(); if (!player.isOnGround() && !player.isGliding() && !player.isInWater() && hasElytra) {
&& chestplate.getDurability() < chestplate.getType().getMaxDurability(); if (player.isGliding() && (player.isOnGround() || !hasElytra)) { player.setGliding(false);
public static final int getArmorPoints(Player player) { int currentDurability = 0; int baseDurability = 0; int baseArmorPoints = 0; ItemStack[] itm = new ItemStack[4]; PlayerInventory inv = player.getInventory(); itm[0] = inv.getBoots(); itm[1]= inv.getLeggings(); itm[2] = inv.getChestplate(); itm[3] = inv.getHelmet(); for(int i = 0; i < 4; i++) { if(itm[i] == null) continue; int dur = itm[i].getDurability(); int max = itm[i].getType().getMaxDurability(); if(max <= 0) continue; if(i == 2) max = max + 1; /* Always 1 too low for chestplate */ else max = max - 3; /* Always 3 too high, versus how client calculates it */ baseDurability += max; currentDurability += max - dur; baseArmorPoints += armorPoints[i]; } int ap = 0; if(baseDurability > 0) ap = ((baseArmorPoints - 1) * currentDurability) / baseDurability + 1; return ap; } }
@Override public short getMaxDurability() { return this.m.getMaxDurability(); }
private void addRepairableLines(String materialName, ArrayList<String> skillContents) { skillContents.add(" " + " " + "Repairable: true"); skillContents.add(" " + " " + "Repair_Material: REPAIR_MATERIAL_NAME"); skillContents.add(" " + " " + "Repair_Material_Data_Value: 0"); skillContents.add(" " + " " + "Repair_Material_Quantity: 9"); skillContents.add(" " + " " + "Repair_Material_Pretty_Name: Repair Item Name"); skillContents.add(" " + " " + "Repair_MinimumLevel: 0"); skillContents.add(" " + " " + "Repair_XpMultiplier: 1.0"); Material material = Material.matchMaterial(materialName); short durability = (material == null) ? (short) 9999 : material.getMaxDurability(); skillContents.add(" " + " " + "Durability: " + ((durability > 0) ? durability : (short) 9999)); }
@Override public HashMap<Integer, ? extends ItemStack> all(Material material) { HashMap<Integer, ItemStack> items = new HashMap<>(); if (material.getMaxDurability() != 0) { for (short currentDurability = 0; currentDurability < material.getMaxDurability(); currentDurability++) { items.put((int) currentDurability, new ItemStack(material, Integer.MAX_VALUE, currentDurability)); } return items; } items.put(1, new ItemStack(material, Integer.MAX_VALUE)); return items; }
public static short getDurability(ItemStack item, int timeleft, int max) { return (short) ((item.getType().getMaxDurability() / max) * timeleft); }
private int calculateDurability(ItemStack item) { //Check if the user have an item on the slot and if the item isn't a stone block or something if (item == null || item.getType().getMaxDurability() == 0) { return 0; } else { //calculate in percent return item.getDurability() * 100 / item.getType().getMaxDurability(); } } }
protected void updateDurability() { int maxDurability = item.getType().getMaxDurability(); if (maxDurability > 0 && effectiveManaMax > 0) { int durability = (short)(getMana() * maxDurability / effectiveManaMax); durability = maxDurability - durability; if (durability >= maxDurability) { durability = maxDurability - 1; } else if (durability < 0) { durability = 0; } item.setDurability((short)durability); } }
public static ItemStack addDurability(ItemStack i, int d) { i.setDurability((short) (i.getDurability() + d)); int min = i.getDurability(); int max = i.getType().getMaxDurability(); if (min >= max) { i.setType(Material.AIR); } return i; }
public static String getCustomName(ItemStack item) { if (item == null || item.getType().equals(Material.AIR)) { return TLocale.asString("ITEM-UTILS.EMPTY-ITEM"); } int data = item.getType().getMaxDurability() == 0 ? item.getDurability() : 0; return item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : itemLib.get(item.getType() + ":" + data) == null ? item.getType().toString() : itemLib.get(item.getType() + ":" + data); }
/** * Modify the durability of an ItemStack. * * @param itemStack The ItemStack which durability should be modified * @param durabilityModifier the amount to modify the durability by * @param maxDamageModifier the amount to adjust the max damage by */ public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, double maxDamageModifier) { if (itemStack.hasItemMeta() && itemStack.getItemMeta().isUnbreakable()) { return; } Material type = itemStack.getType(); short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability(); durabilityModifier = (int) Math.min(durabilityModifier / (itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1), maxDurability * maxDamageModifier); itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability)); }
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL) public void onItemDamage(PlayerItemDamageEvent e) { ItemMeta itemMeta = e.getItem().getItemMeta(); if (e.getItem().getType().getMaxDurability() - (((Damageable) itemMeta).getDamage() + e.getDamage()) <= 0) { if (ItemManager.toRPGItem(e.getItem()).isPresent()) { e.setCancelled(true); } } } }
protected boolean repair(CastContext context, ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR || context.getController().getAPI().isWand(itemStack)) { return false; } short maxDurability = itemStack.getType().getMaxDurability(); if (maxDurability <= 0 || itemStack.getDurability() <= 0) { return false; } itemStack.setDurability((short)0); return true; }
public static Material getMaterial(Material legacymat, int data) { if(legacymat == null) { return null; } if(legacymat.getMaxDurability() == 0) { if(legacymat.equals(Material.LEGACY_ANVIL)) { // special cases where we prioritize item conversion return Bukkit.getUnsafe().fromLegacy(new MaterialData(legacymat, (byte) data), true); } return Bukkit.getUnsafe().fromLegacy(new MaterialData(legacymat, (byte) data)); } else { // ignore data when it's actually durability return Bukkit.getUnsafe().fromLegacy(legacymat); } }