diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/IgnitionChamber.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/IgnitionChamber.java index 50c55891aa..681dc8c9d5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/IgnitionChamber.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/blocks/IgnitionChamber.java @@ -4,6 +4,9 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.thebusybiscuit.slimefun4.utils.UnbreakingAlgorithm; +import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedDurability; +import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedEnchantment; import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.block.Block; @@ -11,6 +14,7 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Dropper; import org.bukkit.block.data.type.Dispenser; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -82,31 +86,44 @@ public static boolean useFlintAndSteel(Player p, Block smelteryBlock) { } // Check if the chamber contains a Flint and Steel - if (inv.contains(Material.FLINT_AND_STEEL)) { - ItemStack item = inv.getItem(inv.first(Material.FLINT_AND_STEEL)); - ItemMeta meta = item.getItemMeta(); - - // Only damage the Flint and Steel if it isn't unbreakable. - if (!meta.isUnbreakable()) { - // Update the damage value - ((Damageable) meta).setDamage(((Damageable) meta).getDamage() + 1); - - if (((Damageable) meta).getDamage() >= item.getType().getMaxDurability()) { - // The Flint and Steel broke - item.setAmount(0); - SoundEffect.IGNITION_CHAMBER_USE_FLINT_AND_STEEL_SOUND.playAt(smelteryBlock); - } else { - item.setItemMeta(meta); - } - } - - SoundEffect.IGNITION_CHAMBER_USE_FLINT_AND_STEEL_SOUND.playAt(smelteryBlock); - return true; - } else { + if (!inv.contains(Material.FLINT_AND_STEEL)) { // Notify the Player there is a chamber but without any Flint and Steel Slimefun.getLocalization().sendMessage(p, "machines.ignition-chamber-no-flint", true); return false; } + + ItemStack item = inv.getItem(inv.first(Material.FLINT_AND_STEEL)); + + damageFlintAndSteel(item, smelteryBlock); + + SoundEffect.IGNITION_CHAMBER_USE_FLINT_AND_STEEL_SOUND.playAt(smelteryBlock); + return true; + } + + private static void damageFlintAndSteel(ItemStack flintAndSteel, Block smelteryBlock) { + ItemMeta meta = flintAndSteel.getItemMeta(); + Damageable damageable = (Damageable) meta; + + // Only damage the Flint and Steel if it isn't unbreakable. + if (meta.isUnbreakable()) { + return; + } + + Enchantment unbreaking = VersionedEnchantment.UNBREAKING; + int lvl = flintAndSteel.getEnchantmentLevel(unbreaking); + + if (!UnbreakingAlgorithm.TOOLS.evaluate(lvl)) { + damageable.setDamage(damageable.getDamage() + 1); + } + + if (damageable.getDamage() > VersionedDurability.getDurability(flintAndSteel)) { + // The Flint and Steel broke + flintAndSteel.setAmount(0); + SoundEffect.LIMITED_USE_ITEM_BREAK_SOUND.playAt(smelteryBlock); + } else { + SoundEffect.IGNITION_CHAMBER_USE_FLINT_AND_STEEL_SOUND.playAt(smelteryBlock); + flintAndSteel.setItemMeta(meta); + } } private static @Nullable Inventory findIgnitionChamber(@Nonnull Block b) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/compatibility/VersionedDurability.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/compatibility/VersionedDurability.java new file mode 100644 index 0000000000..ce04c1dd16 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/compatibility/VersionedDurability.java @@ -0,0 +1,24 @@ +package io.github.thebusybiscuit.slimefun4.utils.compatibility; + +import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; + +public class VersionedDurability { + private static final MinecraftVersion version = Slimefun.getMinecraftVersion(); + + public static int getDurability(ItemStack itemStack) { + + if (version.isAtLeast(MinecraftVersion.MINECRAFT_1_20_5)) { + Damageable damageable = (Damageable) itemStack.getItemMeta(); + if (damageable == null) { + return 0; + } + + return damageable.getMaxDamage(); + } else { + return itemStack.getType().getMaxDurability(); + } + } +}