@@ -21,6 +21,7 @@ import com.lambda.Lambda.mc
2121import com.lambda.config.groups.BuildConfig
2222import com.lambda.context.SafeContext
2323import com.lambda.event.EventFlow.post
24+ import com.lambda.event.events.EntityEvent
2425import com.lambda.event.events.TickEvent
2526import com.lambda.event.events.UpdateManagerEvent
2627import 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
0 commit comments