|
18 | 18 | package com.lambda.util |
19 | 19 |
|
20 | 20 | import com.lambda.context.SafeContext |
| 21 | +import com.lambda.util.EnchantmentUtils.getEnchantment |
21 | 22 | import com.lambda.util.player.gamemode |
22 | 23 | import net.minecraft.block.AbstractCauldronBlock |
23 | 24 | import net.minecraft.block.AbstractFurnaceBlock |
@@ -80,11 +81,16 @@ import net.minecraft.block.StructureBlock |
80 | 81 | import net.minecraft.block.SweetBerryBushBlock |
81 | 82 | import net.minecraft.block.TntBlock |
82 | 83 | import net.minecraft.block.TrapdoorBlock |
| 84 | +import net.minecraft.enchantment.Enchantments |
| 85 | +import net.minecraft.entity.attribute.EntityAttributes |
| 86 | +import net.minecraft.entity.effect.StatusEffectUtil |
| 87 | +import net.minecraft.entity.effect.StatusEffects |
83 | 88 | import net.minecraft.entity.player.PlayerEntity |
84 | 89 | import net.minecraft.fluid.FluidState |
85 | 90 | import net.minecraft.fluid.Fluids |
86 | 91 | import net.minecraft.item.Item |
87 | 92 | import net.minecraft.item.ItemStack |
| 93 | +import net.minecraft.registry.tag.FluidTags |
88 | 94 | import net.minecraft.state.property.Property |
89 | 95 | import net.minecraft.util.math.* |
90 | 96 | import net.minecraft.world.BlockView |
@@ -242,7 +248,54 @@ object BlockUtils { |
242 | 248 | world: BlockView, |
243 | 249 | blockPos: BlockPos, |
244 | 250 | item: ItemStack |
245 | | - ): Float = 0f |
| 251 | + ): Float { |
| 252 | + val hardness = getHardness(world, blockPos) |
| 253 | + return if (hardness == -1.0f) 0.0f else { |
| 254 | + val harvestMultiplier = if (item.canHarvest(this)) 30 else 100 |
| 255 | + player.getItemBlockBreakingSpeed(this, item) / hardness / harvestMultiplier |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + fun ItemStack.canHarvest(blockState: BlockState) = |
| 260 | + !blockState.isToolRequired || isSuitableFor(blockState) |
| 261 | + |
| 262 | + fun PlayerEntity.getItemBlockBreakingSpeed(blockState: BlockState, item: ItemStack): Float { |
| 263 | + var speedMultiplier = item.getMiningSpeedMultiplier(blockState) |
| 264 | + if (speedMultiplier > 1.0f) { |
| 265 | + val level = item.getEnchantment(Enchantments.EFFICIENCY) |
| 266 | + if (level > 0 && !item.isEmpty) { |
| 267 | + speedMultiplier += (level * level + 1) |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + if (StatusEffectUtil.hasHaste(this)) { |
| 272 | + speedMultiplier *= 1.0f + (StatusEffectUtil.getHasteAmplifier(this) + 1) * 0.2f |
| 273 | + } |
| 274 | + |
| 275 | + getStatusEffect(StatusEffects.MINING_FATIGUE)?.amplifier?.let { fatigue -> |
| 276 | + val fatigueMultiplier = when (fatigue) { |
| 277 | + 0 -> 0.3f |
| 278 | + 1 -> 0.09f |
| 279 | + 2 -> 0.0027f |
| 280 | + 3 -> 8.1E-4f |
| 281 | + else -> 8.1E-4f |
| 282 | + } |
| 283 | + |
| 284 | + speedMultiplier *= fatigueMultiplier |
| 285 | + } |
| 286 | + |
| 287 | + if (isSubmergedIn(FluidTags.WATER)) { |
| 288 | + getAttributeInstance(EntityAttributes.SUBMERGED_MINING_SPEED)?.let { speed -> |
| 289 | + speedMultiplier *= speed.getValue().toFloat() |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + if (!isOnGround) { |
| 294 | + speedMultiplier /= 5.0f |
| 295 | + } |
| 296 | + |
| 297 | + return speedMultiplier |
| 298 | + } |
246 | 299 |
|
247 | 300 | val BlockState.isEmpty get() = matches(emptyState) |
248 | 301 | val BlockState.isNotEmpty get() = !isEmpty |
|
0 commit comments