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,17 @@
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;
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 +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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}