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

The following examples show how to use org.bukkit.inventory.meta.Damageable#setDamage() . 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: Power.java    From MineTinker with GNU General Public License v3.0 6 votes vote down vote up
private void powerCreateFarmland(Player player, ItemStack tool, Block block) {
	if (block.getType().equals(Material.GRASS_BLOCK) || block.getType().equals(Material.DIRT)) {
		if (block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0)).getType().equals(Material.AIR)) {
			if (tool.getItemMeta() instanceof Damageable) {
				Damageable damageable = (Damageable) tool.getItemMeta();
				damageable.setDamage(damageable.getDamage() + 1);
				tool.setItemMeta((ItemMeta) damageable);
			}

			PlayerInteractEvent event = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, tool, block, BlockFace.UP);
			Bukkit.getPluginManager().callEvent(event);

			block.setType(Material.FARMLAND); // Event only does Plugin event (no vanilla conversion to Farmland and
			// Tool-Damage)
		}
	}
}
 
Example 2
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 3
Source File: RepairEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
private void repairItem(PlayerInteractEvent event, Player player, ItemStack item) {
    ItemMeta itemMeta = item.getItemMeta();
    Damageable damageable = (Damageable) itemMeta;
    if (damageable == null) {
        player.sendMessage(Civs.getPrefix() +
                LocaleManager.getInstance().getTranslationWithPlaceholders(player, "cant-repair-item"));
        event.setCancelled(true);
        return;
    }
    int repairCost = getRepairCost(item.getType(), damageable.getDamage());
    if (repairCost == 0) {
        player.sendMessage(Civs.getPrefix() +
                LocaleManager.getInstance().getTranslationWithPlaceholders(player, "cant-repair-item"));
        event.setCancelled(true);
        return;
    }
    HashSet<Material> reagents = getRequiredReagent(item.getType());
    if (!reagents.isEmpty()) {
        boolean hasReagent = false;
        Material firstMat = reagents.iterator().next();
        for (Material mat : reagents) {
            ItemStack cost = new ItemStack(mat, repairCost);
            if (!hasReagentCost(player, cost)) {
                continue;
            }
            hasReagent = true;
            player.getInventory().removeItem(cost);
        }
        if (!hasReagent) {
            String message = LocaleManager.getInstance().getTranslationWithPlaceholders(player, "more-repair-items");
            message = message.replace("$1", firstMat.name().toLowerCase().replace("_", " "));
            player.sendMessage(Civs.getPrefix() + message);
            return;
        }
    }
    damageable.setDamage(0);
    item.setItemMeta(itemMeta);
}
 
Example 4
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 5
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 6
Source File: ItemTools.java    From StackMob-3 with GNU General Public License v3.0 4 votes vote down vote up
public static ItemStack damageItem(ItemStack item, int stackSize){
    Damageable meta = (Damageable) item.getItemMeta();
    meta.setDamage(meta.getDamage() + stackSize);
    item.setItemMeta((ItemMeta) meta);
    return item;
}