2022-10-03 13:03:21 +02:00
|
|
|
package net.knarcraft.blacksmith.util;
|
|
|
|
|
2023-01-09 05:03:28 +01:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.enchantments.EnchantmentTarget;
|
2022-10-03 13:03:21 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.inventory.meta.Damageable;
|
|
|
|
|
2023-01-09 05:03:28 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2023-01-13 20:34:34 +01:00
|
|
|
/**
|
|
|
|
* A helper class for getting information about items
|
|
|
|
*/
|
2022-10-03 13:03:21 +02:00
|
|
|
public final class ItemHelper {
|
|
|
|
|
|
|
|
private ItemHelper() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-14 15:10:40 +01:00
|
|
|
/**
|
|
|
|
* Gets whether the given item is repairable
|
|
|
|
*
|
|
|
|
* @param item <p>The item to check</p>
|
|
|
|
* @return <p>True if the item is repairable</p>
|
|
|
|
*/
|
|
|
|
public static boolean isRepairable(ItemStack item) {
|
|
|
|
return item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item);
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:34:34 +01:00
|
|
|
/**
|
|
|
|
* Gets the max durability of an item
|
|
|
|
*
|
|
|
|
* @param itemStack <p>The item to get the durability of</p>
|
|
|
|
* @return <p>The max durability of the item</p>
|
|
|
|
*/
|
|
|
|
public static short getMaxDurability(ItemStack itemStack) {
|
|
|
|
return itemStack.getType().getMaxDurability();
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:03:21 +02:00
|
|
|
/**
|
|
|
|
* Gets the current durability of the given item
|
|
|
|
*
|
|
|
|
* @param itemStack <p>The item to get the durability of</p>
|
|
|
|
* @return <p>The durability of the item</p>
|
|
|
|
*/
|
|
|
|
public static short getDurability(ItemStack itemStack) {
|
|
|
|
Damageable damageable = (Damageable) itemStack.getItemMeta();
|
2023-01-13 20:34:34 +01:00
|
|
|
int maxDurability = getMaxDurability(itemStack);
|
2022-10-03 13:03:21 +02:00
|
|
|
if (damageable != null) {
|
|
|
|
return (short) (maxDurability - damageable.getDamage());
|
|
|
|
} else {
|
|
|
|
return (short) maxDurability;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the damage done to the given item
|
|
|
|
*
|
|
|
|
* @param itemStack <p>The damage done to the item</p>
|
|
|
|
* @return <p>The damage done to the item</p>
|
|
|
|
*/
|
|
|
|
public static short getDamage(ItemStack itemStack) {
|
|
|
|
Damageable damageable = (Damageable) itemStack.getItemMeta();
|
|
|
|
if (damageable != null) {
|
|
|
|
return (short) damageable.getDamage();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 05:03:28 +01:00
|
|
|
/**
|
|
|
|
* Gets a complete list of all reforge-able materials
|
|
|
|
*
|
|
|
|
* @return <p>A complete list of reforge-able materials</p>
|
|
|
|
*/
|
|
|
|
public static List<Material> getAllReforgeAbleMaterials() {
|
|
|
|
List<Material> reforgeAbleMaterials = new ArrayList<>();
|
|
|
|
for (Material material : Material.values()) {
|
|
|
|
ItemStack item = new ItemStack(material);
|
|
|
|
if (item.getItemMeta() instanceof Damageable && EnchantmentTarget.BREAKABLE.includes(item)) {
|
|
|
|
reforgeAbleMaterials.add(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reforgeAbleMaterials;
|
|
|
|
}
|
|
|
|
|
2023-01-09 23:47:17 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the given material is an anvil
|
|
|
|
*
|
|
|
|
* @param material <p>The material to check</p>
|
|
|
|
* @param requireDamaged <p>Whether only a damaged anvil should count</p>
|
|
|
|
* @return <p>True if the given material is an anvil</p>
|
|
|
|
*/
|
|
|
|
public static boolean isAnvil(Material material, boolean requireDamaged) {
|
|
|
|
boolean isDamagedAnvil = material == Material.CHIPPED_ANVIL || material == Material.DAMAGED_ANVIL;
|
|
|
|
boolean isAnvil = isDamagedAnvil || material == Material.ANVIL;
|
|
|
|
return (requireDamaged && isDamagedAnvil) || (!requireDamaged && isAnvil);
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:03:21 +02:00
|
|
|
}
|