@@ -17,125 +17,6 @@ import kotlin.math.abs
17
17
18
18
object VoidBoundBlockUtils {
19
19
20
- private var lastPos: BlockPos ? = null
21
- private var lastDistance: Double = 0.0
22
-
23
- /* *
24
- * Finds and breaks the furthest log block in a connected set.
25
- */
26
- fun breakFurthestLog (level : Level , pos : BlockPos , blockState : BlockState , player : Player ): Boolean {
27
- lastPos = pos
28
- lastDistance = 0.0
29
-
30
- val reachDistance = if (level.getBlockState(pos).`is `(BlockTags .LOGS )) 2 else 1
31
- searchForBlocks(level, pos, blockState, reachDistance)
32
-
33
- val blockBroken = breakBlock(level, player, lastPos!! )
34
- level.markAndNotifyBlock(pos, level.getChunkAt(pos), blockState, blockState, 3 , 20 )
35
-
36
- if (blockBroken && level.getBlockState(pos).`is `(BlockTags .LOGS )) {
37
- level.markAndNotifyBlock(pos, level.getChunkAt(pos), blockState, blockState, 3 , 20 )
38
-
39
- for (xOffset in - 3 .. 3 ) {
40
- for (yOffset in - 3 .. 3 ) {
41
- for (zOffset in - 3 .. 3 ) {
42
- val offsetPos = lastPos!! .offset(xOffset, yOffset, zOffset)
43
- level.scheduleTick(offsetPos, level.getBlockState(offsetPos).block, 20 )
44
- }
45
- }
46
- }
47
- }
48
-
49
- return blockBroken
50
- }
51
-
52
- /* *
53
- * Handles breaking the block, applying any tool enchantments or player abilities.
54
- */
55
- private fun breakBlock (level : Level , player : Player , pos : BlockPos ): Boolean {
56
- if (player is ServerPlayer ) {
57
- val blockState = level.getBlockState(pos)
58
- val blockEntity = level.getBlockEntity(pos)
59
-
60
- val successfulBreak: Boolean = if (player.abilities.instabuild) {
61
- removeBlock(player, pos)
62
- } else {
63
- val itemStack = player.mainHandItem
64
- val canDestroy = blockState.canEntityDestroy(level, pos, player)
65
- val isRemoved = removeBlock(player, pos, canDestroy)
66
-
67
- if (isRemoved && canDestroy) {
68
- val enchantedStack = applyFortune(itemStack)
69
- blockState.block.playerDestroy(level, player, pos, blockState, blockEntity, enchantedStack)
70
- }
71
-
72
- isRemoved
73
- }
74
-
75
- return successfulBreak
76
- }
77
-
78
- return false
79
- }
80
-
81
- /* *
82
- * Removes the block and applies any block-breaking logic.
83
- */
84
- private fun removeBlock (player : Player , pos : BlockPos , canHarvest : Boolean = false): Boolean {
85
- val blockState = player.level().getBlockState(pos)
86
-
87
- return blockState.onDestroyedByPlayer(player.level(), pos, player, canHarvest, blockState.fluidState)
88
- .also { flag ->
89
- if (flag) {
90
- blockState.block.destroy(player.level(), pos, blockState)
91
- }
92
- }
93
- }
94
-
95
- /* *
96
- * Recursively searches for the furthest block within the given reach.
97
- */
98
- private fun searchForBlocks (level : Level , pos : BlockPos , blockState : BlockState , reach : Int ) {
99
- for (xOffset in - reach.. reach) {
100
- for (yOffset in reach downTo - reach) {
101
- for (zOffset in - reach.. reach) {
102
- if (isOutOfBounds(pos, xOffset, yOffset, zOffset)) return
103
-
104
- val currentPos = lastPos!! .offset(xOffset, yOffset, zOffset)
105
- val currentState = level.getBlockState(currentPos)
106
-
107
- if (currentState.block == blockState.block && currentState.block.properties.destroyTime >= 0.0f ) {
108
- val distance = calculateDistance(pos, xOffset, yOffset, zOffset)
109
- if (distance > lastDistance) {
110
- lastDistance = distance
111
- lastPos = currentPos
112
- searchForBlocks(level, pos, blockState, reach)
113
- return
114
- }
115
- }
116
- }
117
- }
118
- }
119
- }
120
-
121
- /* *
122
- * Calculates the squared distance for optimization purposes.
123
- */
124
- private fun calculateDistance (pos : BlockPos , xOffset : Int , yOffset : Int , zOffset : Int ): Double {
125
- val xd = lastPos!! .x + xOffset - pos.x
126
- val yd = lastPos!! .y + yOffset - pos.y
127
- val zd = lastPos!! .z + zOffset - pos.z
128
- return (xd * xd + yd * yd + zd * zd).toDouble()
129
- }
130
-
131
- /* *
132
- * Checks if the position is out of bounds for searching.
133
- */
134
- private fun isOutOfBounds (pos : BlockPos , xOffset : Int , yOffset : Int , zOffset : Int ): Boolean {
135
- return abs(lastPos!! .x + xOffset - pos.x) > 24 ||
136
- abs(lastPos!! .y + yOffset - pos.y) > 48 ||
137
- abs(lastPos!! .z + zOffset - pos.z) > 24
138
- }
139
20
140
21
/* *
141
22
* Recursively gathers all connected log blocks of the same type.
@@ -181,20 +62,6 @@ object VoidBoundBlockUtils {
181
62
log2.defaultBlockState().`is `(BlockRegistry .EXPOSED_SOULWOOD_LOG .get())
182
63
}
183
64
184
- /* *
185
- * Applies fortune enchantments to the item stack used for block breaking.
186
- */
187
- private fun applyFortune (itemStack : ItemStack ): ItemStack {
188
- val enchantments = EnchantmentHelper .getEnchantments(itemStack).toMutableMap()
189
- val fortuneLevel = enchantments[Enchantments .BLOCK_FORTUNE ] ? : 0
190
-
191
- if (fortuneLevel > 0 ) {
192
- enchantments[Enchantments .BLOCK_FORTUNE ] = fortuneLevel
193
- }
194
-
195
- return itemStack.copy().also { EnchantmentHelper .setEnchantments(enchantments, it) }
196
- }
197
-
198
65
/* *
199
66
* Returns false if the block being broken is warded by any player
200
67
*/
0 commit comments