Skip to content

Commit 8151f90

Browse files
committed
item specific break speed functions
1 parent fc6f6bc commit 8151f90

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

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

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ import com.lambda.context.SafeContext
2121
import com.lambda.util.item.ItemUtils.block
2222
import com.lambda.util.item.ItemUtils.shulkerBoxes
2323
import com.lambda.util.math.MathUtils.floorToInt
24-
import net.minecraft.block.Block
25-
import net.minecraft.block.BlockState
26-
import net.minecraft.block.Blocks
24+
import net.minecraft.block.*
25+
import net.minecraft.enchantment.EnchantmentHelper
26+
import net.minecraft.enchantment.Enchantments
27+
import net.minecraft.entity.effect.StatusEffectUtil
28+
import net.minecraft.entity.effect.StatusEffects
29+
import net.minecraft.entity.player.PlayerEntity
2730
import net.minecraft.fluid.FluidState
2831
import net.minecraft.fluid.Fluids
2932
import net.minecraft.item.Item
33+
import net.minecraft.item.ItemStack
34+
import net.minecraft.item.SwordItem
35+
import net.minecraft.registry.tag.FluidTags
3036
import net.minecraft.util.math.*
3137
import net.minecraft.world.BlockView
3238

@@ -155,6 +161,69 @@ object BlockUtils {
155161
val ticksNeeded = 1 / blockState.calcBlockBreakingDelta(player, world, blockPos)
156162
return (ticksNeeded <= 1 && ticksNeeded != 0f) || player.isCreative
157163
}
164+
fun SafeContext.instantBreakable(blockState: BlockState, blockPos: BlockPos, item: ItemStack): Boolean {
165+
val ticksNeeded = 1 / blockState.calcItemBlockBreakingDelta(player, world, blockPos, item)
166+
return (ticksNeeded <= 1 && ticksNeeded != 0f) || player.isCreative
167+
}
168+
fun BlockState.calcItemBlockBreakingDelta(
169+
player: PlayerEntity,
170+
world: BlockView,
171+
blockPos: BlockPos,
172+
item: ItemStack
173+
): Float =
174+
if ((block is BambooShootBlock || block is BambooBlock)
175+
&& item.item is SwordItem) {
176+
1.0f
177+
} else {
178+
val f: Float = this.getHardness(world, blockPos)
179+
if (f == -1.0f) {
180+
0.0f
181+
} else {
182+
val i = if (item.canHarvest(this)) 30 else 100
183+
player.getItemBlockBreakingSpeed(this, item) / f / i.toFloat()
184+
}
185+
}
186+
187+
fun ItemStack.canHarvest(blockState: BlockState) =
188+
!blockState.isToolRequired || this.isSuitableFor(blockState)
189+
190+
fun PlayerEntity.getItemBlockBreakingSpeed(blockState: BlockState, item: ItemStack): Float {
191+
var f = item.getMiningSpeedMultiplier(blockState)
192+
if (f > 1.0f) {
193+
val i = EnchantmentHelper.getLevel(Enchantments.EFFICIENCY, item)
194+
if (i > 0 && !item.isEmpty) {
195+
f += (i * i + 1).toFloat()
196+
}
197+
}
198+
199+
if (StatusEffectUtil.hasHaste(this)) {
200+
f *= 1.0f + (StatusEffectUtil.getHasteAmplifier(this) + 1).toFloat() * 0.2f
201+
}
202+
203+
if (this.hasStatusEffect(StatusEffects.MINING_FATIGUE)) {
204+
val g = when (getStatusEffect(StatusEffects.MINING_FATIGUE)!!.amplifier) {
205+
0 -> 0.3f
206+
1 -> 0.09f
207+
2 -> 0.0027f
208+
3 -> 8.1E-4f
209+
else -> 8.1E-4f
210+
}
211+
212+
f *= g
213+
}
214+
215+
if (this.isSubmergedIn(FluidTags.WATER)
216+
&& !EnchantmentHelper.hasAquaAffinity(this)
217+
) {
218+
f /= 5.0f
219+
}
220+
221+
if (!this.isOnGround) {
222+
f /= 5.0f
223+
}
224+
225+
return f
226+
}
158227

159228
val Vec3i.blockPos: BlockPos get() = BlockPos(this)
160229
val Block.item: Item get() = asItem()

0 commit comments

Comments
 (0)