Skip to content

Commit ffd345f

Browse files
committed
use isEmpty and isNotEmpty over isAir
1 parent 6d1ef93 commit ffd345f

File tree

6 files changed

+31
-24
lines changed

6 files changed

+31
-24
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import com.lambda.util.BlockUtils
5858
import com.lambda.util.BlockUtils.blockState
5959
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
6060
import com.lambda.util.BlockUtils.instantBreakable
61+
import com.lambda.util.BlockUtils.isEmpty
62+
import com.lambda.util.BlockUtils.isNotEmpty
6163
import com.lambda.util.BlockUtils.vecOf
6264
import com.lambda.util.Communication.warn
6365
import com.lambda.util.item.ItemStackUtils.equal
@@ -195,10 +197,10 @@ object BuildSimulator {
195197
val acc = mutableSetOf<BuildResult>()
196198
val targetPosState = blockState(pos)
197199

198-
if (target.isAir() || !targetPosState.isReplaceable) return acc
200+
if (target.isEmpty() || !targetPosState.isReplaceable) return acc
199201

200202
preProcessing.sides.forEach { neighbor ->
201-
val hitPos = if (!place.airPlace.isEnabled() && (targetPosState.isAir || targetPosState.isLiquid))
203+
val hitPos = if (!place.airPlace.isEnabled() && targetPosState.isEmpty)
202204
pos.offset(neighbor)
203205
else pos
204206
val hitSide = neighbor.opposite
@@ -638,7 +640,7 @@ object BuildSimulator {
638640
val state = blockState(pos)
639641

640642
/* is a block that will be destroyed by breaking adjacent blocks */
641-
if (breaking.breakWeakBlocks && state.block.hardness == 0f && !state.isAir) {
643+
if (breaking.breakWeakBlocks && state.block.hardness == 0f && state.isNotEmpty) {
642644
acc.add(BuildResult.Ignored(pos))
643645
return acc
644646
}

common/src/main/kotlin/com/lambda/interaction/construction/verify/StateMatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ import net.minecraft.util.math.BlockPos
2626
interface StateMatcher {
2727
fun matches(state: BlockState, pos: BlockPos, world: ClientWorld, ignoredProperties: Collection<Property<*>> = emptySet()): Boolean
2828
fun getStack(world: ClientWorld, pos: BlockPos): ItemStack
29-
fun isAir(): Boolean
29+
fun isEmpty(): Boolean
3030
}

common/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.interaction.construction.verify
1919

2020
import com.lambda.interaction.material.container.ContainerManager.findDisposable
2121
import com.lambda.module.modules.client.TaskFlowModule
22+
import com.lambda.util.BlockUtils.isEmpty
2223
import com.lambda.util.BlockUtils.matches
2324
import com.lambda.util.StringUtils.capitalize
2425
import com.lambda.util.item.ItemUtils.block
@@ -45,7 +46,7 @@ sealed class TargetState(val type: Type) : StateMatcher {
4546
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
4647
ItemStack.EMPTY
4748

48-
override fun isAir() = true
49+
override fun isEmpty() = true
4950
}
5051

5152
data object Solid : TargetState(Type.SOLID) {
@@ -59,7 +60,7 @@ sealed class TargetState(val type: Type) : StateMatcher {
5960
it.item.block in TaskFlowModule.inventory.disposables
6061
} ?: ItemStack(Items.NETHERRACK)
6162

62-
override fun isAir() = false
63+
override fun isEmpty() = false
6364
}
6465

6566
data class Support(val direction: Direction) : TargetState(Type.SUPPORT) {
@@ -74,7 +75,7 @@ sealed class TargetState(val type: Type) : StateMatcher {
7475
it.item.block in TaskFlowModule.inventory.disposables
7576
} ?: ItemStack(Items.NETHERRACK)
7677

77-
override fun isAir() = false
78+
override fun isEmpty() = false
7879
}
7980

8081
data class State(val blockState: BlockState) : TargetState(Type.STATE) {
@@ -86,7 +87,7 @@ sealed class TargetState(val type: Type) : StateMatcher {
8687
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
8788
blockState.block.getPickStack(world, pos, blockState)
8889

89-
override fun isAir() = blockState.isAir
90+
override fun isEmpty() = blockState.isEmpty
9091
}
9192

9293
data class Block(val block: net.minecraft.block.Block) : TargetState(Type.BLOCK) {
@@ -98,7 +99,7 @@ sealed class TargetState(val type: Type) : StateMatcher {
9899
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
99100
block.getPickStack(world, pos, block.defaultState)
100101

101-
override fun isAir() = block.defaultState.isAir
102+
override fun isEmpty() = block.defaultState.isEmpty
102103
}
103104

104105
data class Stack(val itemStack: ItemStack) : TargetState(Type.STACK) {
@@ -113,6 +114,6 @@ sealed class TargetState(val type: Type) : StateMatcher {
113114
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
114115
itemStack
115116

116-
override fun isAir() = false
117+
override fun isEmpty() = false
117118
}
118119
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ import com.lambda.interaction.request.placing.PlaceManager
4949
import com.lambda.interaction.request.rotating.RotationRequest
5050
import com.lambda.threading.runSafe
5151
import com.lambda.util.BlockUtils.blockState
52-
import com.lambda.util.BlockUtils.brokenState
5352
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
54-
import com.lambda.util.BlockUtils.isBroken
53+
import com.lambda.util.BlockUtils.emptyState
5554
import com.lambda.util.BlockUtils.isEmpty
55+
import com.lambda.util.BlockUtils.isNotBroken
56+
import com.lambda.util.BlockUtils.isNotEmpty
5657
import com.lambda.util.Communication.warn
5758
import com.lambda.util.item.ItemUtils.block
5859
import com.lambda.util.math.lerp
@@ -150,8 +151,8 @@ object BreakManager : RequestHandler<BreakRequest>(
150151
.firstOrNull { it.context.expectedPos == event.pos }
151152
?.let { info ->
152153
// if not broken
153-
if (!isBroken(info.context.checkedState, event.newState)) {
154-
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.checkedState.brokenState}")
154+
if (isNotBroken(info.context.checkedState, event.newState)) {
155+
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.checkedState.emptyState}")
155156
// update the checked state
156157
info.context.checkedState = event.newState
157158
return@listen
@@ -364,7 +365,7 @@ object BreakManager : RequestHandler<BreakRequest>(
364365
val blockState = blockState(ctx.expectedPos)
365366
val hardness = ctx.checkedState.getHardness(world, ctx.expectedPos)
366367

367-
return !blockState.isEmpty && hardness != 600f && hardness != -1f
368+
return blockState.isNotEmpty && hardness != 600f && hardness != -1f
368369
}
369370

370371
/**
@@ -700,7 +701,7 @@ object BreakManager : RequestHandler<BreakRequest>(
700701
lastPosStarted = ctx.expectedPos
701702

702703
val blockState = blockState(ctx.expectedPos)
703-
val notEmpty = !blockState.isEmpty
704+
val notEmpty = blockState.isNotEmpty
704705
if (notEmpty && info.breakingTicks == 0) {
705706
blockState.onBlockBreakStart(world, ctx.expectedPos, player)
706707
}

common/src/main/kotlin/com/lambda/interaction/request/breaking/BrokenBlockHandler.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import com.lambda.interaction.request.breaking.BreakManager.lastPosStarted
2929
import com.lambda.interaction.request.breaking.BreakManager.matchesBlockItem
3030
import com.lambda.interaction.request.breaking.ReBreakManager.reBreak
3131
import com.lambda.module.modules.client.TaskFlowModule
32-
import com.lambda.util.BlockUtils.brokenState
32+
import com.lambda.util.BlockUtils.emptyState
3333
import com.lambda.util.BlockUtils.fluidState
34-
import com.lambda.util.BlockUtils.isBroken
34+
import com.lambda.util.BlockUtils.isEmpty
35+
import com.lambda.util.BlockUtils.isNotBroken
3536
import com.lambda.util.BlockUtils.matches
3637
import com.lambda.util.Communication.info
3738
import com.lambda.util.Communication.warn
@@ -79,9 +80,9 @@ object BrokenBlockHandler {
7980
return@listen
8081

8182
// return if the block's not broken
82-
if (!isBroken(pending.context.checkedState, event.newState)) {
83+
if (isNotBroken(pending.context.checkedState, event.newState)) {
8384
if (!pending.isReBreaking) {
84-
this@BrokenBlockHandler.warn("Broken block at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${pending.context.checkedState.brokenState}")
85+
this@BrokenBlockHandler.warn("Broken block at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${pending.context.checkedState.emptyState}")
8586
pending.stopPending()
8687
} else {
8788
pending.context.checkedState = event.newState
@@ -174,7 +175,7 @@ object BrokenBlockHandler {
174175
return false
175176
val block = ctx.checkedState.block
176177
if (block is OperatorBlock && !player.isCreativeLevelTwoOp) return false
177-
if (ctx.checkedState.isAir) return false
178+
if (ctx.checkedState.isEmpty) return false
178179

179180
block.onBreak(world, ctx.expectedPos, ctx.checkedState, player)
180181
val fluidState = fluidState(ctx.expectedPos)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ object BlockUtils {
311311
return speedMultiplier
312312
}
313313

314-
val BlockState.isEmpty get() = matches(fluidState.blockState)
315-
val BlockState.brokenState: BlockState get() = fluidState.blockState
316-
fun isBroken(oldState: BlockState, newState: BlockState) = !oldState.isEmpty && oldState.brokenState.matches(newState)
314+
val BlockState.isEmpty get() = matches(emptyState)
315+
val BlockState.isNotEmpty get() = !isEmpty
316+
val BlockState.emptyState: BlockState get() = fluidState.blockState
317+
fun isBroken(oldState: BlockState, newState: BlockState) = oldState.isNotEmpty && oldState.emptyState.matches(newState)
318+
fun isNotBroken(oldState: BlockState, newState: BlockState) = !isBroken(oldState, newState)
317319
}

0 commit comments

Comments
 (0)