Skip to content

Commit 9df5ac4

Browse files
committed
almost working block break speed. Might need to rework the hotbar manager to account for the new selected slot logic bypassing serverSlot
1 parent 7a102e9 commit 9df5ac4

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import com.lambda.interaction.request.breaking.BrokenBlockHandler.destroyBlock
6262
import com.lambda.interaction.request.breaking.BrokenBlockHandler.pendingActions
6363
import com.lambda.interaction.request.breaking.BrokenBlockHandler.setPendingConfigs
6464
import com.lambda.interaction.request.breaking.BrokenBlockHandler.startPending
65+
import com.lambda.interaction.request.hotbar.HotbarManager
6566
import com.lambda.interaction.request.interacting.InteractionManager
6667
import com.lambda.interaction.request.placing.PlaceManager
6768
import com.lambda.interaction.request.rotating.RotationRequest
@@ -809,14 +810,14 @@ object BreakManager : RequestHandler<BreakRequest>(
809810
return true
810811
}
811812

812-
private fun BlockState.calcBreakDelta(
813+
fun BlockState.calcBreakDelta(
813814
player: ClientPlayerEntity,
814815
world: BlockView,
815816
pos: BlockPos,
816817
config: BreakConfig,
817818
item: ItemStack? = null
818819
) = runSafe {
819-
val delta = calcItemBlockBreakingDelta(player, world, pos, item ?: player.mainHandStack)
820+
val delta = calcItemBlockBreakingDelta(player, world, pos, item ?: player.inventory.getStack(HotbarManager.serverSlot))
820821
//ToDo: This setting requires some fixes / improvements in the player movement prediction to work properly. Currently, it's broken
821822
// if (config.desyncFix) {
822823
// val nextTickPrediction = buildPlayerPrediction().next()

src/main/kotlin/com/lambda/interaction/request/breaking/ReBreakManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.event.events.TickEvent
2222
import com.lambda.event.listener.SafeListener.Companion.listen
2323
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
2424
import com.lambda.interaction.construction.context.BreakContext
25+
import com.lambda.interaction.request.breaking.BreakManager.calcBreakDelta
2526
import com.lambda.interaction.request.breaking.BrokenBlockHandler.destroyBlock
2627
import com.lambda.threading.runSafe
2728
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
@@ -86,7 +87,7 @@ object ReBreakManager {
8687
reBreak.updateInfo(ctx, breakRequest)
8788

8889
val context = reBreak.context
89-
val breakDelta = context.cachedState.calcBlockBreakingDelta(player, world, context.blockPos)
90+
val breakDelta = context.cachedState.calcBreakDelta(player, world, context.blockPos, reBreak.breakConfig)
9091
return@runSafe if ((reBreak.breakingTicks - reBreak.breakConfig.fudgeFactor) * breakDelta >= reBreak.breakConfig.breakThreshold) {
9192
if (reBreak.breakConfig.breakConfirmation != BreakConfig.BreakConfirmationMode.AwaitThenBreak) {
9293
destroyBlock(reBreak)

src/main/kotlin/com/lambda/util/BlockUtils.kt

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.util
1919

2020
import com.lambda.context.SafeContext
21+
import com.lambda.util.EnchantmentUtils.getEnchantment
2122
import com.lambda.util.player.gamemode
2223
import net.minecraft.block.AbstractCauldronBlock
2324
import net.minecraft.block.AbstractFurnaceBlock
@@ -80,11 +81,16 @@ import net.minecraft.block.StructureBlock
8081
import net.minecraft.block.SweetBerryBushBlock
8182
import net.minecraft.block.TntBlock
8283
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
8388
import net.minecraft.entity.player.PlayerEntity
8489
import net.minecraft.fluid.FluidState
8590
import net.minecraft.fluid.Fluids
8691
import net.minecraft.item.Item
8792
import net.minecraft.item.ItemStack
93+
import net.minecraft.registry.tag.FluidTags
8894
import net.minecraft.state.property.Property
8995
import net.minecraft.util.math.*
9096
import net.minecraft.world.BlockView
@@ -242,7 +248,54 @@ object BlockUtils {
242248
world: BlockView,
243249
blockPos: BlockPos,
244250
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+
}
246299

247300
val BlockState.isEmpty get() = matches(emptyState)
248301
val BlockState.isNotEmpty get() = !isEmpty

0 commit comments

Comments
 (0)