Skip to content

Commit ac690c2

Browse files
committed
place manager documentation
1 parent 3b9c161 commit ac690c2

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ object BrokenBlockHandler {
107107
}
108108

109109
/**
110-
* Adds the [info] to the break manager, and requesters, pending interaction collections.
110+
* Adds the [info] to the [BrokenBlockHandler], and requesters, pending interaction collections.
111111
*/
112112
fun BreakInfo.startPending() {
113113
pendingBreaks.add(this)
114114
pendingInteractions.add(context)
115115
}
116116

117117
/**
118-
* Removes the [info] from the break manager, and requesters, pending interaction collections.
118+
* Removes the [info] from the [BrokenBlockHandler], and requesters, pending interaction collections.
119119
*/
120120
private fun BreakInfo.stopPending() {
121121
pendingBreaks.remove(this)

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceManager.kt

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.lambda.interaction.request.placing.PlaceManager.activeRequest
3535
import com.lambda.interaction.request.placing.PlaceManager.processRequest
3636
import com.lambda.interaction.request.placing.PlacedBlockHandler.addPendingPlace
3737
import com.lambda.interaction.request.placing.PlacedBlockHandler.pendingPlacements
38+
import com.lambda.interaction.request.placing.PlacedBlockHandler.setPendingConfigs
3839
import com.lambda.util.BlockUtils.blockState
3940
import com.lambda.util.Communication.warn
4041
import com.lambda.util.player.gamemode
@@ -97,6 +98,12 @@ object PlaceManager : RequestHandler<PlaceRequest>(
9798
}
9899
}
99100

101+
/**
102+
* accepts, and processes the request, as long as the current [activeRequest] is null, and the [BreakManager] has not
103+
* been active this tick.
104+
*
105+
* @see processRequest
106+
*/
100107
override fun SafeContext.handleRequest(request: PlaceRequest) {
101108
if (activeRequest != null || BreakManager.activeThisTick) return
102109

@@ -105,6 +112,16 @@ object PlaceManager : RequestHandler<PlaceRequest>(
105112
if (placementsThisTick > 0) activeThisTick = true
106113
}
107114

115+
/**
116+
* If the request is fresh, local variables are populated through the [processRequest] method.
117+
* It then attempts to perform as many placements within this tick as possible from the [potentialPlacements] collection.
118+
*
119+
* If all the [maxPlacementsThisTick] limit is reached and the user has rotations enabled, it will start rotating to
120+
* the next predicted placement in the list for optimal speed.
121+
*
122+
* @see populateFrom
123+
* @see placeBlock
124+
*/
108125
fun SafeContext.processRequest(request: PlaceRequest) {
109126
pendingPlacements.cleanUp()
110127

@@ -136,12 +153,16 @@ object PlaceManager : RequestHandler<PlaceRequest>(
136153
}
137154
}
138155

156+
/**
157+
* Filters and sorts the [request]'s [PlaceContext]s, placing them into the [potentialPlacements] collection, and
158+
* setting the maxPlacementsThisTick value.
159+
*
160+
* @see canPlace
161+
*/
139162
private fun SafeContext.populateFrom(request: PlaceRequest) {
140163
val place = request.build.placing
141164

142-
pendingPlacements.setSizeLimit(place.maxPendingPlacements)
143-
pendingPlacements.setDecayTime(request.build.interactionTimeout * 50L)
144-
165+
setPendingConfigs(request)
145166
potentialPlacements = request.contexts
146167
.filter { canPlace(it) }
147168
.sortedWith(
@@ -153,11 +174,19 @@ object PlaceManager : RequestHandler<PlaceRequest>(
153174
maxPlacementsThisTick = (place.placementsPerTick.coerceAtMost(pendingLimit))
154175
}
155176

177+
/**
178+
* @return if none of the [pendingPlacements] match positions with the [placeContext]
179+
*/
156180
private fun canPlace(placeContext: PlaceContext) =
157181
pendingPlacements.none { pending ->
158182
pending.context.expectedPos == placeContext.expectedPos
159183
}
160184

185+
/**
186+
* A modified version of the minecraft interactBlock method, renamed to better suit its usage.
187+
*
188+
* @see net.minecraft.client.network.ClientPlayerInteractionManager.interactBlock
189+
*/
161190
private fun SafeContext.placeBlock(placeContext: PlaceContext, request: PlaceRequest, hand: Hand): ActionResult {
162191
interaction.syncSelectedSlot()
163192
val hitResult = placeContext.result
@@ -166,6 +195,11 @@ object PlaceManager : RequestHandler<PlaceRequest>(
166195
return interactBlockInternal(placeContext, request, request.build.placing, hand, hitResult)
167196
}
168197

198+
/**
199+
* A modified version of the minecraft interactBlockInternal method.
200+
*
201+
* @see net.minecraft.client.network.ClientPlayerInteractionManager.interactBlockInternal
202+
*/
169203
private fun SafeContext.interactBlockInternal(
170204
placeContext: PlaceContext,
171205
request: PlaceRequest,
@@ -203,6 +237,11 @@ object PlaceManager : RequestHandler<PlaceRequest>(
203237
return ActionResult.PASS
204238
}
205239

240+
/**
241+
* A modified version of the minecraft useOnBlock method.
242+
*
243+
* @see net.minecraft.item.Item.useOnBlock
244+
*/
206245
private fun SafeContext.useOnBlock(
207246
placeContext: PlaceContext,
208247
request: PlaceRequest,
@@ -223,6 +262,11 @@ object PlaceManager : RequestHandler<PlaceRequest>(
223262
return place(placeContext, request, hand, hitResult, placeConfig, item, ItemPlacementContext(context))
224263
}
225264

265+
/**
266+
* A modified version of the minecraft place method.
267+
*
268+
* @see net.minecraft.item.BlockItem.place
269+
*/
226270
private fun SafeContext.place(
227271
placeContext: PlaceContext,
228272
request: PlaceRequest,
@@ -290,11 +334,17 @@ object PlaceManager : RequestHandler<PlaceRequest>(
290334
return ActionResult.success(world.isClient)
291335
}
292336

337+
/**
338+
* sends the block placement packet using the given [hand] and [hitResult].
339+
*/
293340
private fun SafeContext.sendPlacePacket(hand: Hand, hitResult: BlockHitResult) =
294341
interaction.sendSequencedPacket(world) { sequence: Int ->
295342
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
296343
}
297344

345+
/**
346+
* Plays the block placement sound at a given position.
347+
*/
298348
fun SafeContext.placeSound(item: BlockItem, state: BlockState, pos: BlockPos) {
299349
val blockSoundGroup = state.soundGroup
300350
world.playSound(
@@ -307,6 +357,9 @@ object PlaceManager : RequestHandler<PlaceRequest>(
307357
)
308358
}
309359

360+
/**
361+
* Must be called before and after placing a block to bypass grim's air place checks.
362+
*/
310363
private fun SafeContext.airPlaceOffhandSwap() {
311364
connection.sendPacket(
312365
PlayerActionC2SPacket(

common/src/main/kotlin/com/lambda/interaction/request/placing/PlacedBlockHandler.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,35 @@ object PlacedBlockHandler {
6868
}
6969
}
7070

71+
/**
72+
* Adds the info to the [PlacedBlockHandler], and requesters, pending interaction collections.
73+
*/
7174
fun addPendingPlace(info: PlaceInfo) {
7275
pendingPlacements.add(info)
7376
info.pendingInteractionsList.add(info.context)
7477
}
7578

79+
/**
80+
* Removes the info from the [PlacedBlockHandler], and requesters, pending interaction collections.
81+
*/
7682
private fun removePendingPlace(info: PlaceInfo) {
7783
pendingPlacements.remove(info)
7884
info.pendingInteractionsList.remove(info.context)
7985
}
8086

87+
/**
88+
* Sets the size limit and decay time for the [pendingPlacements] using the [request]'s configs
89+
*/
90+
fun setPendingConfigs(request: PlaceRequest) {
91+
pendingPlacements.setSizeLimit(request.build.placing.maxPendingPlacements)
92+
pendingPlacements.setDecayTime(request.build.interactionTimeout * 50L)
93+
}
94+
95+
/**
96+
* @return if the [targetState] matches the [newState]
97+
*
98+
* @see TargetState
99+
*/
81100
private fun SafeContext.matchesTargetState(pos: BlockPos, targetState: TargetState, newState: BlockState) =
82101
if (targetState.matches(newState, pos, world)) true
83102
else {

0 commit comments

Comments
 (0)