2023-07-26 17:56:09 +02:00
|
|
|
package com.craftaro.ultimatetimber.animation;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
2023-07-26 17:56:09 +02:00
|
|
|
import com.craftaro.ultimatetimber.UltimateTimber;
|
|
|
|
import com.craftaro.ultimatetimber.manager.ConfigurationManager;
|
|
|
|
import com.craftaro.ultimatetimber.manager.TreeAnimationManager;
|
|
|
|
import com.craftaro.ultimatetimber.tree.DetectedTree;
|
|
|
|
import com.craftaro.ultimatetimber.tree.FallingTreeBlock;
|
|
|
|
import com.craftaro.ultimatetimber.tree.ITreeBlock;
|
|
|
|
import com.craftaro.ultimatetimber.tree.TreeBlock;
|
|
|
|
import com.craftaro.ultimatetimber.tree.TreeBlockSet;
|
|
|
|
import com.craftaro.ultimatetimber.utils.BlockUtils;
|
|
|
|
import com.craftaro.ultimatetimber.utils.ParticleUtils;
|
|
|
|
import com.craftaro.ultimatetimber.utils.SoundUtils;
|
2019-03-28 07:42:22 +01:00
|
|
|
import org.bukkit.block.Block;
|
2019-04-01 12:22:08 +02:00
|
|
|
import org.bukkit.entity.FallingBlock;
|
2019-03-29 23:52:17 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2019-04-01 12:22:08 +02:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import org.bukkit.util.Vector;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
|
|
|
public class TreeAnimationFancy extends TreeAnimation {
|
2019-03-30 07:46:13 +01:00
|
|
|
public TreeAnimationFancy(DetectedTree detectedTree, Player player) {
|
|
|
|
super(TreeAnimationType.FANCY, detectedTree, player);
|
2019-03-28 05:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-30 07:46:13 +01:00
|
|
|
public void playAnimation(Runnable whenFinished) {
|
2019-04-01 12:22:08 +02:00
|
|
|
UltimateTimber ultimateTimber = UltimateTimber.getInstance();
|
2019-03-28 05:22:13 +01:00
|
|
|
|
2019-04-01 12:22:08 +02:00
|
|
|
boolean useCustomSound = ConfigurationManager.Setting.USE_CUSTOM_SOUNDS.getBoolean();
|
|
|
|
boolean useCustomParticles = ConfigurationManager.Setting.USE_CUSTOM_PARTICLES.getBoolean();
|
|
|
|
|
|
|
|
ITreeBlock<Block> initialTreeBlock = this.detectedTree.getDetectedTreeBlocks().getInitialLogBlock();
|
2023-06-29 14:31:22 +02:00
|
|
|
FallingTreeBlock initialFallingBlock = this.convertToFallingBlock((TreeBlock) this.detectedTree.getDetectedTreeBlocks().getInitialLogBlock());
|
2019-04-01 12:22:08 +02:00
|
|
|
|
2023-06-29 14:31:22 +02:00
|
|
|
if (useCustomSound) {
|
2020-10-30 21:00:23 +01:00
|
|
|
SoundUtils.playFallingSound(initialTreeBlock);
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-04-01 12:22:08 +02:00
|
|
|
|
|
|
|
Vector velocityVector = initialTreeBlock.getLocation().clone().subtract(this.player.getLocation().clone()).toVector().normalize().setY(0);
|
|
|
|
|
|
|
|
this.fallingTreeBlocks = new TreeBlockSet<>(initialFallingBlock);
|
|
|
|
for (ITreeBlock<Block> treeBlock : this.detectedTree.getDetectedTreeBlocks().getAllTreeBlocks()) {
|
2023-06-29 14:31:22 +02:00
|
|
|
FallingTreeBlock fallingTreeBlock = this.convertToFallingBlock((TreeBlock) treeBlock);
|
|
|
|
if (fallingTreeBlock == null) {
|
2019-04-01 12:22:08 +02:00
|
|
|
continue;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-04-01 12:22:08 +02:00
|
|
|
|
|
|
|
FallingBlock fallingBlock = fallingTreeBlock.getBlock();
|
|
|
|
this.fallingTreeBlocks.add(fallingTreeBlock);
|
|
|
|
|
2023-06-29 14:31:22 +02:00
|
|
|
if (useCustomParticles) {
|
|
|
|
ParticleUtils.playFallingParticles(treeBlock);
|
|
|
|
}
|
2019-04-01 12:22:08 +02:00
|
|
|
|
|
|
|
double multiplier = (treeBlock.getLocation().getY() - this.player.getLocation().getY()) * 0.05;
|
|
|
|
fallingBlock.setVelocity(velocityVector.clone().multiply(multiplier));
|
|
|
|
fallingBlock.setVelocity(fallingBlock.getVelocity().multiply(0.3));
|
|
|
|
}
|
|
|
|
|
|
|
|
new BukkitRunnable() {
|
|
|
|
int timer = 0;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (this.timer == 0) {
|
|
|
|
for (ITreeBlock<FallingBlock> fallingTreeBlock : TreeAnimationFancy.this.fallingTreeBlocks.getAllTreeBlocks()) {
|
|
|
|
FallingBlock fallingBlock = fallingTreeBlock.getBlock();
|
2020-10-30 21:00:23 +01:00
|
|
|
BlockUtils.toggleGravityFallingBlock(fallingBlock, true);
|
2019-04-01 12:22:08 +02:00
|
|
|
fallingBlock.setVelocity(fallingBlock.getVelocity().multiply(1.5));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TreeAnimationFancy.this.fallingTreeBlocks.getAllTreeBlocks().isEmpty()) {
|
|
|
|
whenFinished.run();
|
|
|
|
this.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ITreeBlock<FallingBlock> fallingTreeBlock : TreeAnimationFancy.this.fallingTreeBlocks.getAllTreeBlocks()) {
|
|
|
|
FallingBlock fallingBlock = fallingTreeBlock.getBlock();
|
2019-04-01 21:50:19 +02:00
|
|
|
fallingBlock.setVelocity(fallingBlock.getVelocity().clone().subtract(new Vector(0, 0.05, 0)));
|
2019-04-01 12:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.timer++;
|
|
|
|
|
|
|
|
if (this.timer > 4 * 20) {
|
|
|
|
TreeAnimationManager treeAnimationManager = ultimateTimber.getTreeAnimationManager();
|
2023-06-29 14:31:22 +02:00
|
|
|
for (ITreeBlock<FallingBlock> fallingTreeBlock : TreeAnimationFancy.this.fallingTreeBlocks.getAllTreeBlocks()) {
|
2019-04-01 12:22:08 +02:00
|
|
|
treeAnimationManager.runFallingBlockImpact(TreeAnimationFancy.this, fallingTreeBlock);
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-04-01 12:22:08 +02:00
|
|
|
whenFinished.run();
|
|
|
|
this.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTaskTimer(ultimateTimber, 20L, 1L);
|
2019-03-28 05:22:13 +01:00
|
|
|
}
|
|
|
|
}
|