-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathBreakInfo.kt
More file actions
169 lines (148 loc) · 7.01 KB
/
BreakInfo.kt
File metadata and controls
169 lines (148 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2025 Lambda
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lambda.interaction.request.breaking
import com.lambda.interaction.construction.context.BreakContext
import com.lambda.interaction.request.ActionInfo
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
import com.lambda.util.Describable
import com.lambda.util.NamedEnum
import com.lambda.util.OneSetPerTick
import net.minecraft.client.network.ClientPlayerEntity
import net.minecraft.client.network.ClientPlayerInteractionManager
import net.minecraft.client.world.ClientWorld
import net.minecraft.entity.ItemEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action
import net.minecraft.world.BlockView
data class BreakInfo(
override var context: BreakContext,
var type: BreakType,
var request: BreakRequest
) : ActionInfo {
// Delegates
val breakConfig get() = request.build.breaking
override val pendingInteractionsList get() = request.pendingInteractions
// Pre Processing
var shouldProgress = false
var couldReBreak by OneSetPerTick(value = RebreakManager.RebreakPotential.None, throwOnLimitBreach = true)
var shouldSwap by OneSetPerTick(value = false, throwOnLimitBreach = true)
var swapStack: ItemStack by OneSetPerTick(ItemStack.EMPTY, true)
var minSwapTicks by OneSetPerTick(0, true)
var serverBreakTicks = 0
// BreakInfo Specific
var updatedThisTick by OneSetPerTick(false, resetAfterTick = true).apply { set(true) }
var updatedPreProcessingThisTick by OneSetPerTick(value = false, throwOnLimitBreach = true, resetAfterTick = true)
var progressedThisTick by OneSetPerTick(value = false, throwOnLimitBreach = true, resetAfterTick = true)
// Processing
var breaking = false
var abandoned = false
var breakingTicks by OneSetPerTick(0, true)
var soundsCooldown by OneSetPerTick(0f, true)
var vanillaInstantBreakable = false
val rebreakable get() = !vanillaInstantBreakable && type == BreakType.Primary
enum class BreakType(
override val displayName: String,
override val description: String
) : NamedEnum, Describable {
Primary("Primary", "The main block you’re breaking right now."),
Secondary("Secondary", "A second block broken at the same time (when double‑break is enabled)."),
RedundantSecondary("Redundant Secondary", "A previously started secondary break that’s now ignored/monitored only (no new actions)."),
Rebreak("Rebreak", "A previously broken block which new breaks in the same position can compound progression on. Often rebreaking instantly.");
fun getBreakThreshold(breakConfig: BreakConfig) =
when (this) {
Primary -> breakConfig.breakThreshold
else -> 1.0f
}
}
// Post Processing
@Volatile
var broken = false; private set
private var item: ItemEntity? = null
val callbacksCompleted
@Synchronized get() = broken && (request.onItemDrop == null || item != null)
@Synchronized
fun internalOnBreak() {
if (type != BreakType.Rebreak) broken = true
item?.let { item ->
request.onItemDrop?.invoke(item)
}
}
@Synchronized
fun internalOnItemDrop(item: ItemEntity) {
if (type != BreakType.Rebreak) this.item = item
if (broken || type == BreakType.Rebreak) {
request.onItemDrop?.invoke(item)
}
}
fun updateInfo(context: BreakContext, request: BreakRequest? = null) {
updatedThisTick = true
this.context = context
request?.let { this.request = it }
if (type == BreakType.RedundantSecondary) type = BreakType.Secondary
}
fun resetCallbacks() {
broken = false
item = null
}
fun shouldSwap(player: ClientPlayerEntity, world: BlockView): Boolean {
val item = player.inventory.getStack(context.hotbarIndex)
val breakDelta = context.cachedState.calcItemBlockBreakingDelta(player, world, context.blockPos, item)
val breakProgress = breakDelta * (breakingTicks + 1)
return if (couldReBreak == RebreakManager.RebreakPotential.Instant)
breakConfig.swapMode.isEnabled()
else when (breakConfig.swapMode) {
BreakConfig.SwapMode.None -> false
BreakConfig.SwapMode.Start -> !breaking
BreakConfig.SwapMode.End -> breakProgress >= getBreakThreshold()
BreakConfig.SwapMode.StartAndEnd -> !breaking || breakProgress >= getBreakThreshold()
BreakConfig.SwapMode.Constant -> true
}
}
fun setBreakingTextureStage(
player: ClientPlayerEntity,
world: ClientWorld,
stage: Int = getBreakTextureProgress(player, world)
) {
world.setBlockBreakingInfo(player.id, context.blockPos, stage)
}
private fun getBreakTextureProgress(player: PlayerEntity, world: ClientWorld): Int {
val swapMode = breakConfig.swapMode
val item =
if (swapMode.isEnabled() && swapMode != BreakConfig.SwapMode.Start) player.inventory.getStack(context.hotbarIndex) else player.mainHandStack
val breakDelta = context.cachedState.calcItemBlockBreakingDelta(player, world, context.blockPos, item)
val progress = (breakDelta * breakingTicks) / (getBreakThreshold() + (breakDelta * breakConfig.fudgeFactor))
return if (progress > 0.0f) (progress * 10.0f).toInt().coerceAtMost(9) else -1
}
fun getBreakThreshold() = type.getBreakThreshold(breakConfig)
fun startBreakPacket(world: ClientWorld, interaction: ClientPlayerInteractionManager) =
breakPacket(Action.START_DESTROY_BLOCK, world, interaction)
fun stopBreakPacket(world: ClientWorld, interaction: ClientPlayerInteractionManager) =
breakPacket(Action.STOP_DESTROY_BLOCK, world, interaction)
fun abortBreakPacket(world: ClientWorld, interaction: ClientPlayerInteractionManager) =
breakPacket(Action.ABORT_DESTROY_BLOCK, world, interaction)
private fun breakPacket(action: Action, world: ClientWorld, interaction: ClientPlayerInteractionManager) =
interaction.sendSequencedPacket(world) { sequence: Int ->
PlayerActionC2SPacket(
action,
context.blockPos,
context.result.side,
sequence
)
}
}