Calculates how long would it take to mine the specified block given the best tool in this toolset is used. A negative value is returned if the specified block is unbreakable. @param item the item to mine it with @param state the blockstate to be mined @return how long it would take in ticks
(ItemStack item, BlockState state)
| 205 | * @return how long it would take in ticks |
| 206 | */ |
| 207 | public static double calculateSpeedVsBlock(ItemStack item, BlockState state) { |
| 208 | float hardness; |
| 209 | try { |
| 210 | hardness = state.getDestroySpeed(null, null); |
| 211 | } catch (NullPointerException npe) { |
| 212 | // can't easily determine the hardness so treat it as unbreakable |
| 213 | return -1; |
| 214 | } |
| 215 | if (hardness < 0) { |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | float speed = item.getDestroySpeed(state); |
| 220 | if (speed > 1) { |
| 221 | final ItemEnchantments itemEnchantments = item.getEnchantments(); |
| 222 | OUTER: for (Holder<Enchantment> enchant : itemEnchantments.keySet()) { |
| 223 | List<EnchantmentAttributeEffect> effects = enchant.value().getEffects(EnchantmentEffectComponents.ATTRIBUTES); |
| 224 | for (EnchantmentAttributeEffect e : effects) { |
| 225 | if (e.attribute().is(Attributes.MINING_EFFICIENCY.unwrapKey().get())) { |
| 226 | speed += e.amount().calculate(itemEnchantments.getLevel(enchant)); |
| 227 | break OUTER; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | speed /= hardness; |
| 234 | if (!state.requiresCorrectToolForDrops() || (!item.isEmpty() && item.isCorrectToolForDrops(state))) { |
| 235 | return speed / 30; |
| 236 | } else { |
| 237 | return speed / 100; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Calculates any modifier to breaking time based on status effects. |