Skip to content

Commit

Permalink
update: consume sponge stick
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Sep 28, 2023
1 parent 5399d6b commit 0b80c78
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "net.azisaba"
version = "6.7.3"
version = "6.7.4"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.mori01231.lifecore.listener.item;

import com.github.mori01231.lifecore.util.ItemStackExKt;
import com.github.mori01231.lifecore.util.ItemUtil;
import org.bukkit.*;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void onPlayerInteract(PlayerInteractEvent e) {
}
}
if (affected > 0 && e.getPlayer().getGameMode() != GameMode.CREATIVE) {
// TODO: reduce durability
ItemStackExKt.damage(e.getItem(), e.getPlayer(), 1, false, true);
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/github/mori01231/lifecore/util/ItemStackEx.kt
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)
}
}
}
}

0 comments on commit 0b80c78

Please sign in to comment.