Skip to content

Commit 7edb211

Browse files
committed
item drop callbacks
1 parent 53bb4de commit 7edb211

File tree

3 files changed

+82
-48
lines changed

3 files changed

+82
-48
lines changed

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.lambda.Lambda.mc
2121
import com.lambda.config.groups.BuildConfig
2222
import com.lambda.context.SafeContext
2323
import com.lambda.event.EventFlow.post
24+
import com.lambda.event.events.EntityEvent
2425
import com.lambda.event.events.TickEvent
2526
import com.lambda.event.events.UpdateManagerEvent
2627
import com.lambda.event.events.WorldEvent
@@ -195,15 +196,19 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
195196
if (event.newState.matches(pending.context.checkedState))
196197
return@listen
197198

198-
pendingBreaks.remove(pending)
199199
// return if the block's not broken
200-
if (!matchesTargetState(event.pos, pending.context.targetState, event.newState))
200+
if (!matchesTargetState(event.pos, pending.context.targetState, event.newState)) {
201+
pendingBreaks.remove(pending)
201202
return@listen
203+
}
202204

203205
if (pending.breakConfig.breakConfirmation == BreakConfirmationMode.AwaitThenBreak) {
204206
destroyBlock(pending)
205207
}
206-
pending.onBreak()
208+
pending.internalOnBreak()
209+
if (pending.callbacksCompleted) {
210+
pendingBreaks.remove(pending)
211+
}
207212
return@listen
208213
}
209214

@@ -218,22 +223,34 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
218223
return@listen
219224
}
220225
destroyBlock(info)
221-
info.onBreak()
226+
info.internalOnBreak()
222227
info.nullify()
228+
if (!info.callbacksCompleted) {
229+
pendingBreaks.add(info)
230+
}
223231
}
224232
}
225233

226-
//ToDo: drop callback stuff
227234
// ToDo: Dependent on the tracked data order. When set stack is called after position it wont work
228-
// listen<EntityEvent.EntityUpdate> {
229-
// if (it.entity !is ItemEntity) return@listen
230-
// pendingBreaks
231-
// .firstOrNull { info -> matchesBlockItem(info, it.entity) }
232-
// ?.onItemDrop?.invoke(it.entity)
233-
// ?: breakingInfos
234-
// .filterNotNull()
235-
// .firstOrNull { info -> matchesBlockItem(info, it.entity) }?.onItemDrop?.invoke(it.entity)
236-
// }
235+
listen<EntityEvent.EntityUpdate> {
236+
if (it.entity !is ItemEntity) return@listen
237+
pendingBreaks
238+
.firstOrNull { info -> matchesBlockItem(info, it.entity) }
239+
?.let { pending ->
240+
pending.internalOnItemDrop(it.entity)
241+
if (pending.callbacksCompleted) {
242+
pendingBreaks.remove(pending)
243+
}
244+
return@listen
245+
}
246+
247+
breakingInfos
248+
.filterNotNull()
249+
.firstOrNull { info -> matchesBlockItem(info, it.entity) }
250+
?.let { info ->
251+
info.internalOnItemDrop(it.entity)
252+
}
253+
}
237254
}
238255

239256
private fun matchesBlockItem(info: BreakInfo, entity: ItemEntity): Boolean {
@@ -509,6 +526,32 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
509526
var soundsCooldown = 0.0f
510527
var startedWithSecondary = false
511528

529+
@Volatile
530+
private var broken = false
531+
private var item: ItemEntity? = null
532+
533+
val callbacksCompleted
534+
@Synchronized get() = broken && (onItemDrop == null || item != null)
535+
536+
fun internalOnBreak() {
537+
synchronized(this) {
538+
broken = true
539+
onBreak()
540+
item?.let { item ->
541+
onItemDrop?.invoke(item)
542+
}
543+
}
544+
}
545+
546+
fun internalOnItemDrop(item: ItemEntity) {
547+
synchronized(this) {
548+
this.item = item
549+
if (broken) {
550+
onItemDrop?.invoke(item)
551+
}
552+
}
553+
}
554+
512555
fun requestHotbarSwap() =
513556
hotbarConfig.request(HotbarRequest(context.hotbarIndex)).done
514557

common/src/main/kotlin/com/lambda/module/modules/player/HighwayTools.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ object HighwayTools : Module(
119119
emptyStructure()
120120
}
121121
}.build(
122+
collectDrops = build.collectDrops,
122123
build = build,
123124
rotation = rotation,
124125
interact = interact,

common/src/main/kotlin/com/lambda/task/tasks/BuildTask.kt

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import com.lambda.config.groups.InteractionConfig
2424
import com.lambda.config.groups.InventoryConfig
2525
import com.lambda.interaction.request.rotation.RotationConfig
2626
import com.lambda.context.SafeContext
27-
import com.lambda.event.events.EntityEvent
2827
import com.lambda.event.events.TickEvent
2928
import com.lambda.event.listener.SafeListener.Companion.listen
3029
import com.lambda.interaction.construction.blueprint.Blueprint
@@ -204,46 +203,37 @@ class BuildTask @Ta5kBuilder constructor(
204203
return@listen
205204
}
206205
}
207-
208-
// ToDo: Dependent on the tracked data order. When set stack is called after position it wont work
209-
listen<EntityEvent.EntityUpdate> {
210-
if (!collectDrops) return@listen
211-
if (it.entity !is ItemEntity) return@listen
212-
pendingInteractions.find { context ->
213-
val inRange = context.expectedPos.toCenterPos().isInRange(it.entity.pos, 0.5)
214-
val correctMaterial = context.checkedState.block == it.entity.stack.item.block
215-
inRange && correctMaterial
216-
}?.let { _ ->
217-
dropsToCollect.add(it.entity)
218-
}
219-
}
220206
}
221207

222208
private fun SafeContext.collectDrops() =
223-
dropsToCollect.firstOrNull()?.let { itemDrop ->
224-
if (!world.entities.contains(itemDrop)) {
225-
dropsToCollect.remove(itemDrop)
226-
BaritoneUtils.cancel()
227-
return true
228-
}
209+
dropsToCollect
210+
.firstOrNull()
211+
?.let { itemDrop ->
212+
if (positionBlockingManagers.any { it.blockedPositions.isNotEmpty() }) return true
213+
214+
if (!world.entities.contains(itemDrop)) {
215+
dropsToCollect.remove(itemDrop)
216+
BaritoneUtils.cancel()
217+
return true
218+
}
229219

230-
val noInventorySpace = player.hotbarAndStorage.none { it.isEmpty }
231-
if (noInventorySpace) {
232-
val stackToThrow = player.currentScreenHandler.inventorySlots.firstOrNull {
233-
it.stack.item.block in TaskFlowModule.inventory.disposables
234-
} ?: run {
235-
failure("No item in inventory to throw but inventory is full and cant pick up item drop")
220+
val noInventorySpace = player.hotbarAndStorage.none { it.isEmpty }
221+
if (noInventorySpace) {
222+
val stackToThrow = player.currentScreenHandler.inventorySlots.firstOrNull {
223+
it.stack.item.block in TaskFlowModule.inventory.disposables
224+
} ?: run {
225+
failure("No item in inventory to throw but inventory is full and cant pick up item drop")
226+
return true
227+
}
228+
transfer(player.currentScreenHandler) {
229+
throwStack(stackToThrow.id)
230+
}.execute(this@BuildTask)
236231
return true
237232
}
238-
transfer(player.currentScreenHandler) {
239-
throwStack(stackToThrow.id)
240-
}.execute(this@BuildTask)
241-
return true
242-
}
243233

244-
BaritoneUtils.setGoalAndPath(GoalBlock(itemDrop.blockPos))
245-
return true
246-
} ?: false
234+
BaritoneUtils.setGoalAndPath(GoalBlock(itemDrop.blockPos))
235+
return true
236+
} ?: false
247237

248238
companion object {
249239
@Ta5kBuilder

0 commit comments

Comments
 (0)