-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeAnimation.kt
More file actions
428 lines (353 loc) · 19 KB
/
TreeAnimation.kt
File metadata and controls
428 lines (353 loc) · 19 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package ru.kainlight.lightcutter.ANIMATIONS
import org.bukkit.*
import org.bukkit.block.Block
import org.bukkit.block.BlockFace
import org.bukkit.block.data.BlockData
import org.bukkit.block.data.type.Leaves
import org.bukkit.entity.BlockDisplay
import org.bukkit.entity.Display
import org.bukkit.entity.FallingBlock
import org.bukkit.entity.Player
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.metadata.FixedMetadataValue
import org.bukkit.util.Transformation
import org.joml.Quaternionf
import org.joml.Vector3f
import ru.kainlight.lightcutter.Main
import ru.kainlight.lightcutter.UTILS.Debug
import ru.kainlight.lightlibrary.LightCommon
import java.util.logging.Level
import kotlin.math.abs
@FancyPhysics("https://github.com/max1mde/FancyPhysics")
class TreeAnimation(private val plugin: Main, private val origin: Block) {
private val lower_1_19_4: Boolean = LightCommon.lower(Bukkit.getVersion(), "1.19.3")
/** Returns true if the tree's properties are characteristic of a naturally generated tree. */
private var isNatural: Boolean
/** The material of the tree's stem. */
private val woodMaterial: Material
/** The material of the tree's leaves. */
private val leaveMaterial: Material
private val woods: MutableList<Block> = mutableListOf()
private val leaves: MutableList<Block> = mutableListOf()
private val scannedBlocks: MutableList<Block> = mutableListOf()
private val oldBlocklist: MutableMap<Location, Material> = mutableMapOf()
private var blockDistanceToLastValid = 0
private var scanAmount = 0
init {
val aboveOrigin = origin.location.clone().add(0.0, 1.0, 0.0).block
this.woodMaterial = aboveOrigin.type
this.leaveMaterial = getLeaveType(this.woodMaterial)
scanTree(aboveOrigin)
woods.add(origin)
this.isNatural = this.woods.size > 3 && this.leaves.size > 5
}
companion object {
// For < 1.19.4
val fallingBlocks: MutableList<FallingBlock> = mutableListOf()
private val displayList = mutableListOf<Display>()
/** Creates a new Tree object and plays a break animation */
fun start(plugin: Main, event: BlockBreakEvent) {
if (event.isCancelled) return
val block = event.block
if(!block.getRelative(BlockFace.UP).type.isWood()) return
val tree = TreeAnimation(plugin, block)
event.player.apply {
if (tree.woods.isNotEmpty()) incrementStatistic(Statistic.MINE_BLOCK, tree.woodMaterial, tree.woods.size)
if (tree.leaves.isNotEmpty()) incrementStatistic(Statistic.MINE_BLOCK, tree.leaveMaterial, tree.leaves.size)
}
tree.breakAndFall(event.player)
val regenerationEnabled = plugin.config.getBoolean("region-settings.regeneration.enable", false)
if(regenerationEnabled) {
val animationEnabled = plugin.config.getBoolean("region-settings.regeneration.animation.enable", false)
val delayBeforeRestore = plugin.config.getInt("region-settings.regeneration.delay")
if(animationEnabled) tree.restoreAnimated(delayBeforeRestore)
else tree.restore(delayBeforeRestore)
}
}
//internal fun getStripedLog(blockName: String): Material = Material.valueOf("STRIPPED_" + blockName.uppercase())
}
/** Breaks the tree with a falling animation if the tree is natural. */
private fun breakAndFall(player: Player) {
if (!isNatural) return
val allowDrop: Boolean = plugin.config.getBoolean("woodcutter-settings.allow-drop", true)
val isAnimated: Boolean = plugin.config.getBoolean("woodcutter-settings.animation", false)
Debug.message("Animation is [$isAnimated]", Level.INFO)
if(!isAnimated) this.treeCapitator(allowDrop)
else {
if (lower_1_19_4) {
Debug.message("Animation «spawnFallingBlocks» started", Level.INFO)
this.woods.forEach { spawnFallingBlocks(it, allowDrop) }
this.leaves.forEach { spawnFallingBlocks(it, allowDrop) }
} else {
Debug.message("Animation «spawnDisplay» started", Level.INFO)
this.woods.forEach { spawnDisplay(it, allowDrop) }
this.leaves.forEach { spawnDisplay(it, allowDrop) }
}
}
}
@Suppress("DEPRECATED")
private fun spawnFallingBlocks(block: Block, allowDrop: Boolean) {
val fallingBlock = block.world.spawnFallingBlock(block.location, block.blockData)
fallingBlock.setHurtEntities(false)
fallingBlock.dropItem = false
fallingBlock.isInvulnerable = true
if (allowDrop) block.breakNaturally()
else block.type = Material.AIR
fallingBlocks.add(fallingBlock)
}
/** Breaks the tree instantly without any animation if the tree is natural. */
private fun treeCapitator(allowDrop: Boolean) {
this.woods.forEach {
if (allowDrop) it.breakNaturally()
else it.type = Material.AIR
}
this.leaves.forEach {
if (allowDrop) it.breakNaturally()
else it.type = Material.AIR
}
}
private fun spawnDisplay(block: Block, allowDrop: Boolean) {
val location = block.location
val blockData = block.type.createBlockData()
// Найти нижний блок дерева
val lowestBlock = woods.minByOrNull { it.location.y } ?: origin
val baseY = lowestBlock.y
location.world.spawn(location, BlockDisplay::class.java) { blockDisplay ->
displayList.add(blockDisplay)
blockDisplay.block = blockData
blockDisplay.addScoreboardTag("lightcutter_tree")
block.type = Material.AIR
val transformationY = -1 + (baseY - block.y).toInt()
val transformationZ = (baseY - block.y + (baseY - block.y) / 0.9).toFloat()
/** Transform display (Falling animation) */
plugin.server.scheduler.scheduleSyncDelayedTask(plugin, Runnable {
val yPlus = (baseY - (block.y + 0.7)).toFloat()
val loc = blockDisplay.location.add(0.0, yPlus + 1.5, (transformationY - 0.5f).toDouble())
var impactLocation = loc.block
if (impactLocation.type.isSolid) {
var tries = 0
while (impactLocation.type.isSolid && tries < 5) {
impactLocation = impactLocation.getRelative(BlockFace.UP)
tries++
}
}
val transformation = Transformation(
// Translation
Vector3f(0f, transformationY + (baseY - (block.y + 0.6)).toFloat() / 2, transformationZ),
// Left rotation
Quaternionf(-1.0f + loc.distance(impactLocation.location).toFloat() / 10, 0f, 0f, 0.1f),
// Scale
Vector3f(1f, 1f, 1f),
// Right rotation
blockDisplay.transformation.rightRotation
)
blockDisplay.interpolationDuration = 30
blockDisplay.interpolationDelay = -1
blockDisplay.transformation = transformation
/** Break tree */
val finalImpactLocation = impactLocation
val dist = (loc.distance(impactLocation.location) * 2).toInt()
plugin.server.scheduler.scheduleSyncDelayedTask(plugin, Runnable {
blockDisplay.location.world.spawnParticle(Particle.BLOCK_CRACK, finalImpactLocation.location, 50, blockData)
removeTree(blockDisplay, transformationY.toFloat(), blockData, allowDrop)
}, 12L - minOf(11, dist).toLong())
}, 2L)
}
}
/**
* Removes the tree after the falling animation is completed.
*
* @param blockDisplay The block display to remove.
* @param transformationY The Y transformation value.
* @param blockData The block data of the block display to get the material.
*/
private fun removeTree(blockDisplay: BlockDisplay, transformationY: Float, blockData: BlockData, allowDrop: Boolean) {
plugin.server.scheduler.scheduleSyncDelayedTask(plugin, Runnable {
val b = blockDisplay.location.add(0.0, (transformationY + 2).toDouble(), transformationY.toDouble()).block
if (b.type == Material.AIR) {
b.type = blockData.material
if (allowDrop) b.breakNaturally()
else b.type = Material.AIR
}
displayList.remove(blockDisplay)
blockDisplay.remove()
}, 4L)
}
private fun restore(delayBeforeRestore: Int?) {
if (delayBeforeRestore == null || delayBeforeRestore <= 0) return
val particlesEnabled = plugin.config.getBoolean("region-settings.regeneration.particle.enable")
val particle = Particle.valueOf(plugin.config.getStringList("region-settings.regeneration.particle.types").random())
val particleCount = plugin.config.getInt("region-settings.regeneration.particle.count")
val soundEnabled = plugin.config.getBoolean("region-settings.regeneration.sound.enable")
val sound = Sound.valueOf(plugin.config.getStringList("region-settings.regeneration.sound.types").random())
val soundVolume: Float = plugin.config.getDouble("region-settings.regeneration.sound.volume").toFloat()
val soundPitch: Float = plugin.config.getDouble("region-settings.regeneration.sound.pitch").toFloat()
plugin.server.scheduler.runTaskLater(plugin, Runnable {
for (location in oldBlocklist.keys) {
val block = location.block
if (block.type != Material.AIR) continue
block.type = oldBlocklist.get(location)!!
val world = block.world
if(particlesEnabled) world.spawnParticle(particle, location, particleCount)
if(soundEnabled) world.playSound(location, sound, soundVolume, soundPitch)
}
origin.type = woodMaterial
}, 20L * delayBeforeRestore)
}
private fun restoreAnimated(delayBeforeRestore: Int?) {
if (delayBeforeRestore == null || delayBeforeRestore <= 0) return
val particlesEnabled = plugin.config.getBoolean("region-settings.regeneration.particle.enable")
val particle = Particle.valueOf(plugin.config.getStringList("region-settings.regeneration.particle.types").random())
val particleCount = plugin.config.getInt("region-settings.regeneration.particle.count")
val soundEnabled = plugin.config.getBoolean("region-settings.regeneration.sound.enable")
val sound = Sound.valueOf(plugin.config.getStringList("region-settings.regeneration.sound.types").random())
val soundVolume: Float = plugin.config.getDouble("region-settings.regeneration.sound.volume").toFloat() * 2
val soundPitch: Float = plugin.config.getDouble("region-settings.regeneration.sound.pitch").toFloat()
// Разделение блоков дерева и листвы
val inverse = plugin.config.getBoolean("region-settings.regeneration.animation.inverse")
var woodBlocks: List<Location> = ArrayList()
var leafBlocks: List<Location> = ArrayList()
if(inverse) {
woodBlocks = oldBlocklist.filterValues { it == woodMaterial }.keys.sortedBy { it.y }
leafBlocks = oldBlocklist.filterValues { it == leaveMaterial }.keys.sortedBy { it.y }
} else {
woodBlocks = oldBlocklist.filterValues { it == woodMaterial }.keys.sortedByDescending { it.y }
leafBlocks = oldBlocklist.filterValues { it == leaveMaterial }.keys.sortedByDescending { it.y }
}
println("Restoring: Old blocklist Size: ${oldBlocklist.keys.size}")
//val totalBlocks = woodBlocks.size + leafBlocks.size
var delayPerBlock = plugin.config.getLong("region-settings.regeneration.animation.delayPerBlock", 1L)
if(delayPerBlock <= 0) delayPerBlock = 1L
var currentDelay = 0L
// Ожидание перед запуском восстановления
plugin.server.scheduler.runTaskLaterAsynchronously(plugin, Runnable {
// Восстанавливаем блоки листвы
for (location in leafBlocks) {
plugin.server.scheduler.runTaskLater(plugin, Runnable {
restoreBlockWithEffects(location, particle, particleCount, sound, soundVolume, soundPitch, particlesEnabled, soundEnabled)
}, currentDelay)
currentDelay += delayPerBlock
}
// Восстанавливаем блоки дерева
for (location in woodBlocks) {
plugin.server.scheduler.runTaskLater(plugin, Runnable {
restoreBlockWithEffects(location, particle, particleCount, sound, soundVolume, soundPitch, particlesEnabled, soundEnabled)
}, currentDelay)
currentDelay += delayPerBlock
}
// Завершающий блок (если есть "origin")
plugin.server.scheduler.runTaskLater(plugin, Runnable {
origin.type = woodMaterial
}, currentDelay)
}, 20L * delayBeforeRestore)
}
/** Recursively scans the tree structure, populating the stem and leaves lists. */
private fun scanTree(scannedBlock: Block) {
scannedBlocks.add(scannedBlock)
scanAmount ++
if (abs(scannedBlock.x - origin.x) > 10 || abs(scannedBlock.z - origin.z) > 10) return
val scanMaxStemSize = 200
val scanMaxLeavesSize = 260
if (scannedBlock.type == woodMaterial) {
if (woods.size < scanMaxStemSize) {
if (this.woods.contains(scannedBlock)) return
this.woods.add(scannedBlock)
this.oldBlocklist.put(scannedBlock.location, scannedBlock.type)
} else {
isNatural = false
return
}
} else if (scannedBlock.type == leaveMaterial) {
if (leaves.size < scanMaxLeavesSize) {
val leaveBlockData = scannedBlock.blockData
if (leaveBlockData is Leaves) {
leaveBlockData.isPersistent = true
scannedBlock.blockData = leaveBlockData
}
if (this.leaves.contains(scannedBlock)) return
this.leaves.add(scannedBlock)
this.oldBlocklist.put(scannedBlock.location, scannedBlock.type)
println("Leaves size: ${leaves.size}")
} else {
isNatural = false
return
}
}
val advancedStemScan = false
val maxInvalidScans = 2700
val maxInvalidBlockDistance = 2
if (listOf(Material.COCOA_BEANS, Material.VINE, Material.SNOW).contains(scannedBlock.type) && advancedStemScan) {
scannedBlock.breakNaturally()
}
listOf(BlockFace.DOWN, BlockFace.UP, BlockFace.SOUTH, BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST).forEach { blockFace ->
val currentBlock = scannedBlock.getRelative(blockFace)
var scan = (currentBlock.type == this.woodMaterial || currentBlock.type == this.leaveMaterial)
///if (blockFace == BlockFace.DOWN && currentBlock.y <= origin.y + 12) scan = false
if (blockFace == BlockFace.DOWN) {
// Проверяем, не вышли ли мы за пределы дерева, ограничение по глубине
val isWithinBounds = currentBlock.y >= origin.y - 12
val isValidMaterial = currentBlock.type == woodMaterial
if (!isWithinBounds || !isValidMaterial) scan = false /*return@forEach*/
}
if (scan) {
scanTree(currentBlock)
blockDistanceToLastValid = 0
return@forEach
}
if (scanAmount < maxInvalidScans && woods.size > 4 && advancedStemScan && blockDistanceToLastValid < maxInvalidBlockDistance) {
blockDistanceToLastValid ++
if (! scannedBlocks.contains(currentBlock)) scanTree(currentBlock)
}
}
}
/**
* Восстанавливает блок с эффектами.
*/
private fun restoreBlockWithEffects(location: Location, particle: Particle, particleCount: Int, sound: Sound, soundVolume: Float, soundPitch: Float, particlesEnabled: Boolean, soundEnabled: Boolean) {
val block = location.block
if (block.type != Material.AIR) return
val material = oldBlocklist.get(location)!!
block.type = material
val world = block.world
if (particlesEnabled) world.spawnParticle(particle, location.add(0.5, 0.5, 0.5), particleCount)
if (soundEnabled) world.playSound(location, sound, soundVolume, soundPitch)
}
private fun getLeaveType(material: Material): Material {
return when (material) {
Material.OAK_LOG, Material.MUD_BRICK_WALL, Material.STRIPPED_OAK_LOG -> Material.OAK_LEAVES
Material.DARK_OAK_LOG, Material.STRIPPED_DARK_OAK_LOG -> Material.DARK_OAK_LEAVES
Material.JUNGLE_LOG, Material.STRIPPED_JUNGLE_LOG -> Material.JUNGLE_LEAVES
Material.ACACIA_LOG, Material.STRIPPED_ACACIA_LOG -> Material.ACACIA_LEAVES
Material.BIRCH_LOG, Material.STRIPPED_BIRCH_LOG -> Material.BIRCH_LEAVES
Material.SPRUCE_LOG, Material.STRIPPED_SPRUCE_LOG -> Material.SPRUCE_LEAVES
Material.CHERRY_LOG, Material.STRIPPED_CHERRY_LOG -> Material.CHERRY_LEAVES
Material.MANGROVE_LOG, Material.STRIPPED_MANGROVE_LOG -> Material.MANGROVE_LEAVES
Material.WARPED_STEM, Material.NETHER_WART_BLOCK -> Material.WARPED_WART_BLOCK
Material.CRIMSON_STEM -> Material.NETHER_WART_BLOCK
else -> Material.AIR
}
}
/**
* Determines the material of the leaves based on the wood material of the tree.
*
* @param material The wood material of the tree.
* @return The material of the leaves.
*/
/*private fun getLeaveType(material: Material): String {
return when (material.name) {
"OAK_LOG", "MUD_BRICK_WALL", "STRIPPED_OAK_LOG" -> "OAK_LEAVES"
"DARK_OAK_LOG", "STRIPPED_DARK_OAK_LOG" -> "DARK_OAK_LEAVES"
"JUNGLE_LOG", "STRIPPED_JUNGLE_LOG" -> "JUNGLE_LEAVES"
"ACACIA_LOG", "STRIPPED_ACACIA_LOG" -> "ACACIA_LEAVES"
"BIRCH_LOG", "STRIPPED_BIRCH_LOG" -> "BIRCH_LEAVES"
"SPRUCE_LOG", "STRIPPED_SPRUCE_LOG" -> "SPRUCE_LEAVES"
"CHERRY_LOG", "STRIPPED_CHERRY_LOG" -> "CHERRY_LEAVES"
"MANGROVE_LOG", "STRIPPED_MANGROVE_LOG" -> "MANGROVE_LEAVES"
"WARPED_STEM", "NETHER_WART_BLOCK" -> "WARPED_WART_BLOCK"
"CRIMSON_STEM" -> "NETHER_WART_BLOCK"
else -> "AIR"
}
}*/
}
fun Material.isWood(): Boolean = this.name.endsWith("LOG") || this.name.endsWith("STEM")
annotation class FancyPhysics(val url: String)