-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5399d6b
commit 0b80c78
Showing
3 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/github/mori01231/lifecore/util/ItemStackEx.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.mori01231.lifecore.util | ||
|
||
import org.bukkit.Material | ||
import org.bukkit.Particle | ||
import org.bukkit.Sound | ||
import org.bukkit.enchantments.Enchantment | ||
import org.bukkit.entity.Player | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.meta.Damageable | ||
|
||
fun ItemStack.damage(player: Player? = null, amount: Int = 1, checkDurabilityEnchantment: Boolean = false, consumeIfNotDamageable: Boolean = true) { | ||
if (itemMeta !is Damageable || type.maxDurability.toInt() == 0) { | ||
if (!consumeIfNotDamageable) error("item is not damageable and consumeIfNotDamageable is false") | ||
this.amount-- | ||
return | ||
} | ||
itemMeta = itemMeta.apply { | ||
if (this !is Damageable) error("not damageable") | ||
|
||
val level = getEnchantLevel(Enchantment.DURABILITY) | ||
if (checkDurabilityEnchantment && Math.random() >= 1.0 / (level + 1)) { | ||
return | ||
} | ||
this.damage += amount | ||
if (type.maxDurability < this.damage) { | ||
setAmount(0) | ||
type = Material.AIR | ||
if (player != null) { | ||
player.world.playSound(player.location, Sound.ITEM_SHIELD_BREAK, 1F, 1F) | ||
player.world.spawnParticle(Particle.ITEM_CRACK, player.location, 1, this@damage) | ||
} | ||
} | ||
} | ||
} |