Java Code Examples for org.bukkit.inventory.meta.Damageable#getDamage()

The following examples show how to use org.bukkit.inventory.meta.Damageable#getDamage() . 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: DamageableItem.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method will damage the given {@link ItemStack} once.
 * It also takes into account the {@link Enchantment} {@code Unbreaking}.
 * 
 * It will only apply the damage if {@link #isDamageable()} returned true.
 * 
 * @param p
 *            The {@link Player} to which the item belongs
 * @param item
 *            The {@link ItemStack} to damage
 */
default void damageItem(Player p, ItemStack item) {
    if (isDamageable() && item != null && item.getType() != Material.AIR && item.getAmount() > 0) {
        if (item.getEnchantments().containsKey(Enchantment.DURABILITY) && Math.random() * 100 <= (60 + Math.floorDiv(40, (item.getEnchantmentLevel(Enchantment.DURABILITY) + 1)))) {
            return;
        }

        ItemMeta meta = item.getItemMeta();
        Damageable damageable = (Damageable) meta;

        if (damageable.getDamage() >= item.getType().getMaxDurability()) {
            item.setAmount(0);
        }
        else {
            damageable.setDamage(damageable.getDamage() + 1);
            item.setItemMeta(meta);
        }
    }
}
 
Example 2
Source File: LootProtectListener.java    From NyaaUtils with MIT License 5 votes vote down vote up
private static void giveExp(Player p, int amount) {
    if (amount <= 0) return;
    List<ItemStack> candidate = new ArrayList<>(13);

    for (ItemStack item : new ItemStack[]{
            p.getInventory().getHelmet(),
            p.getInventory().getChestplate(),
            p.getInventory().getLeggings(),
            p.getInventory().getBoots(),
            p.getInventory().getItemInMainHand(),
            p.getInventory().getItemInOffHand()}) {
        if (item != null && item.hasItemMeta() && item.getItemMeta().hasEnchant(Enchantment.MENDING)) {
            if (item.getType().getMaxDurability() > 0 && ((Damageable) item.getItemMeta()).getDamage() > 0) {
                candidate.add(item);
            }
        }
    }

    ItemStack repair = null;
    if (candidate.size() > 0) {
        candidate.sort(LootProtectListener::compareByDamagePercentage);
        repair = candidate.get(0);
    }

    if (repair != null) {
        Damageable itemMeta = (Damageable) repair.getItemMeta();
        int durability = itemMeta.getDamage();
        int repairPoint = durability;
        if (amount * 2 < repairPoint) repairPoint = amount * 2;
        int expConsumption = ((repairPoint % 2) == 1) ? (repairPoint + 1) / 2 : repairPoint / 2;
        itemMeta.setDamage(durability - repairPoint);
        repair.setItemMeta((ItemMeta) itemMeta);
        amount -= expConsumption;
    }

    if (amount > 0) p.giveExp(amount);
    PlayerExpChangeEvent event = new PlayerExpChangeEvent(p, amount);
    Bukkit.getServer().getPluginManager().callEvent(event);
}
 
Example 3
Source File: SelfRepair.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void effect(PlayerItemDamageEvent event) {
	if (useMending) {
		return;
	}

	ItemStack tool = event.getItem();
	if (!modManager.isToolViable(tool) && !modManager.isArmorViable(tool)) {
		return;
	}

	if (!event.getPlayer().hasPermission("minetinker.modifiers.selfrepair.use")) {
		return;
	}

	if (!modManager.hasMod(tool, this)) {
		return;
	}

	int level = modManager.getModLevel(tool, this);
	Random rand = new Random();
	int n = rand.nextInt(100);
	int c = this.percentagePerLevel * level;

	if (n <= c) {
		if (tool.getItemMeta() instanceof Damageable) {
			Damageable damageable = (Damageable) tool.getItemMeta();
			int dura = damageable.getDamage();
			int newDura = dura - this.healthRepair;

			if (newDura < 0) {
				newDura = 0;
			}

			damageable.setDamage(newDura);

			tool.setItemMeta((ItemMeta) damageable);
			ChatWriter.logModifier(event.getPlayer(), event, this, tool, String.format("Chance(%d/%d)", n, c),
					String.format("Repair(%d -> %d)", dura, newDura));
			return;
		}
	}
	ChatWriter.logModifier(event.getPlayer(), event, this, tool, String.format("Chance(%d/%d)", n, c));
}
 
Example 4
Source File: ItemTools.java    From StackMob-3 with GNU General Public License v3.0 4 votes vote down vote up
public static boolean hasEnoughDurability(ItemStack item, int stackSize) {
    Damageable meta = (Damageable) item.getItemMeta();
    int newDamage = meta.getDamage() + stackSize;
    return newDamage <= item.getType().getMaxDurability();
}
 
Example 5
Source File: RepairInstance.java    From NyaaUtils with MIT License 4 votes vote down vote up
public RepairInstance(ItemStack item, RepairConfig config, NyaaUtils plugin) {
    if (item == null || item.getType() == Material.AIR) return;
    RepairConfig.RepairConfigItem cfg = config.getRepairConfig(item.getType());
    if (cfg == null) return;
    if (!(item.getItemMeta() instanceof Repairable)) return;
    if (item.hasItemMeta() && item.getItemMeta().hasLore()) {
        if (!plugin.cfg.globalLoreBlacklist.canRepair(item.getItemMeta().getLore())) {
            stat = RepairStat.UNREPAIRABLE;
            return;
        }
    }
    stat = RepairStat.REPAIRABLE;
    if (item.getItemMeta().isUnbreakable()) {
        stat = RepairStat.UNREPAIRABLE_UNBREAKABLE;
    }
    Repairable repairableMeta = (Repairable) item.getItemMeta();
    Damageable damageableMeta = (Damageable) item.getItemMeta();
    repairLimit = cfg.repairLimit;
    if (repairLimit > 0 && repairableMeta.getRepairCost() >= repairLimit) {
        stat = RepairStat.UNREPAIRABLE_RLE;
    }

    Material toolMaterial = item.getType();
    repairMaterial = cfg.material;
    int currentDurability = damageableMeta.getDamage();
    if (currentDurability <= 0) {
        stat = RepairStat.UNREPAIRABLE_REPAIRED;
    }

    int enchLevel = 0;
    for (Enchantment ench : Enchantment.values()) {
        enchLevel += Math.max(item.getEnchantmentLevel(ench), 0);
    }

    int fullDurability = toolMaterial.getMaxDurability();
    durRecovered = (int) Math.floor((double) fullDurability / ((double) cfg.fullRepairCost + (double) enchLevel * cfg.enchantCostPerLv));
    expConsumption = (int) Math.floor(cfg.expCost + cfg.enchantCostPerLv * enchLevel);
    if (durRecovered <= 0) {
        stat = RepairStat.UNREPAIRABLE_LOWRECOVER;
    }
}