Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/flint and steal unbreaking #4247

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.utils.UnbreakingAlgorithm;
import io.github.thebusybiscuit.slimefun4.utils.compatibility.VersionedEnchantment;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
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;
Expand Down Expand Up @@ -82,31 +85,43 @@ 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));

// Only damage the Flint and Steel if it isn't unbreakable.
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;

if (meta.isUnbreakable()) {
return;
}

Enchantment unbreaking = VersionedEnchantment.UNBREAKING;
int lvl = flintAndSteel.getEnchantmentLevel(unbreaking);

if (UnbreakingAlgorithm.TOOLS.evaluate(lvl)) {
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
damageable.setDamage(damageable.getDamage() + 1);
}

if (damageable.getDamage() >= flintAndSteel.getType().getMaxDurability()) {
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
// The Flint and Steel broke
flintAndSteel.setAmount(0);
SoundEffect.IGNITION_CHAMBER_USE_FLINT_AND_STEEL_SOUND.playAt(smelteryBlock);
} else {
flintAndSteel.setItemMeta(meta);
}
}

private static @Nullable Inventory findIgnitionChamber(@Nonnull Block b) {
Expand Down