2019-03-30 04:59:10 +01:00
|
|
|
package com.songoda.ultimatetimber.animation;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
2023-06-29 14:38:33 +02:00
|
|
|
import com.craftaro.core.compatibility.CompatibleMaterial;
|
2019-03-30 22:56:22 +01:00
|
|
|
import com.songoda.ultimatetimber.UltimateTimber;
|
|
|
|
import com.songoda.ultimatetimber.manager.ConfigurationManager;
|
|
|
|
import com.songoda.ultimatetimber.manager.TreeDefinitionManager;
|
2019-04-01 12:22:08 +02:00
|
|
|
import com.songoda.ultimatetimber.tree.DetectedTree;
|
|
|
|
import com.songoda.ultimatetimber.tree.ITreeBlock;
|
|
|
|
import com.songoda.ultimatetimber.tree.TreeBlock;
|
2019-04-30 08:18:45 +02:00
|
|
|
import com.songoda.ultimatetimber.tree.TreeBlockType;
|
2019-04-01 12:22:08 +02:00
|
|
|
import com.songoda.ultimatetimber.tree.TreeDefinition;
|
2020-10-30 21:00:23 +01:00
|
|
|
import com.songoda.ultimatetimber.utils.ParticleUtils;
|
|
|
|
import com.songoda.ultimatetimber.utils.SoundUtils;
|
2019-03-28 07:42:22 +01:00
|
|
|
import org.bukkit.block.Block;
|
2019-04-29 02:58:38 +02:00
|
|
|
import org.bukkit.entity.FallingBlock;
|
2019-03-29 23:52:17 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2019-03-30 22:56:22 +01:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2019-04-29 02:58:38 +02:00
|
|
|
import org.bukkit.util.Vector;
|
2019-03-30 22:56:22 +01:00
|
|
|
|
2019-04-01 12:22:08 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
|
|
|
public class TreeAnimationDisintegrate extends TreeAnimation {
|
2019-03-30 07:46:13 +01:00
|
|
|
public TreeAnimationDisintegrate(DetectedTree detectedTree, Player player) {
|
2019-04-28 04:17:40 +02:00
|
|
|
super(TreeAnimationType.DISINTEGRATE, 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-03-30 22:56:22 +01:00
|
|
|
UltimateTimber ultimateTimber = UltimateTimber.getInstance();
|
|
|
|
TreeDefinitionManager treeDefinitionManager = ultimateTimber.getTreeDefinitionManager();
|
|
|
|
|
|
|
|
boolean useCustomSound = ConfigurationManager.Setting.USE_CUSTOM_SOUNDS.getBoolean();
|
|
|
|
boolean useCustomParticles = ConfigurationManager.Setting.USE_CUSTOM_PARTICLES.getBoolean();
|
|
|
|
|
|
|
|
List<ITreeBlock<Block>> orderedLogBlocks = new ArrayList<>(this.detectedTree.getDetectedTreeBlocks().getLogBlocks());
|
|
|
|
orderedLogBlocks.sort(Comparator.comparingInt(x -> x.getLocation().getBlockY()));
|
|
|
|
|
|
|
|
List<ITreeBlock<Block>> leafBlocks = new ArrayList<>(this.detectedTree.getDetectedTreeBlocks().getLeafBlocks());
|
|
|
|
Collections.shuffle(leafBlocks);
|
|
|
|
|
|
|
|
Player p = this.player;
|
|
|
|
TreeDefinition td = this.detectedTree.getTreeDefinition();
|
2019-03-31 21:48:03 +02:00
|
|
|
boolean hst = this.hasSilkTouch;
|
2019-03-30 22:56:22 +01:00
|
|
|
|
|
|
|
new BukkitRunnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
List<ITreeBlock<Block>> toDestroy = new ArrayList<>();
|
|
|
|
|
|
|
|
if (!orderedLogBlocks.isEmpty()) {
|
2019-04-17 10:11:58 +02:00
|
|
|
ITreeBlock<Block> treeBlock = orderedLogBlocks.remove(0);
|
2019-03-30 22:56:22 +01:00
|
|
|
toDestroy.add(treeBlock);
|
|
|
|
} else if (!leafBlocks.isEmpty()) {
|
2019-04-17 10:11:58 +02:00
|
|
|
ITreeBlock<Block> treeBlock = leafBlocks.remove(0);
|
2019-03-30 22:56:22 +01:00
|
|
|
toDestroy.add(treeBlock);
|
|
|
|
|
|
|
|
if (!leafBlocks.isEmpty()) {
|
2019-04-17 10:11:58 +02:00
|
|
|
treeBlock = leafBlocks.remove(0);
|
2019-03-30 22:56:22 +01:00
|
|
|
toDestroy.add(treeBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 02:58:38 +02:00
|
|
|
for (ITreeBlock<FallingBlock> fallingTreeBlock : TreeAnimationDisintegrate.this.fallingTreeBlocks.getAllTreeBlocks()) {
|
|
|
|
FallingBlock fallingBlock = fallingTreeBlock.getBlock();
|
|
|
|
fallingBlock.setVelocity(fallingBlock.getVelocity().clone().subtract(new Vector(0, 0.05, 0)));
|
|
|
|
}
|
|
|
|
|
2019-03-30 22:56:22 +01:00
|
|
|
if (!toDestroy.isEmpty()) {
|
|
|
|
ITreeBlock<Block> first = toDestroy.get(0);
|
2023-06-29 14:31:22 +02:00
|
|
|
if (useCustomSound) {
|
2020-10-30 21:00:23 +01:00
|
|
|
SoundUtils.playLandingSound(first);
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-03-28 05:22:13 +01:00
|
|
|
|
2019-03-30 22:56:22 +01:00
|
|
|
for (ITreeBlock<Block> treeBlock : toDestroy) {
|
2023-06-29 14:31:22 +02:00
|
|
|
if (treeBlock.getTreeBlockType() == TreeBlockType.LOG) {
|
2023-06-29 14:38:33 +02:00
|
|
|
if (td.getLogMaterial().stream().noneMatch(x -> x == CompatibleMaterial.getMaterial(treeBlock.getBlock().getType()).orElse(null))) {
|
2019-04-30 08:18:45 +02:00
|
|
|
continue;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
|
|
|
} else if (treeBlock.getTreeBlockType() == TreeBlockType.LEAF) {
|
2023-06-29 14:38:33 +02:00
|
|
|
if (td.getLeafMaterial().stream().noneMatch(x -> x == CompatibleMaterial.getMaterial(treeBlock.getBlock().getType()).orElse(null))) {
|
2019-04-30 08:18:45 +02:00
|
|
|
continue;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-04-30 08:18:45 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 14:31:22 +02:00
|
|
|
if (useCustomParticles) {
|
2020-10-30 21:00:23 +01:00
|
|
|
ParticleUtils.playFallingParticles(treeBlock);
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-05-01 12:01:34 +02:00
|
|
|
treeDefinitionManager.dropTreeLoot(td, treeBlock, p, hst, false);
|
2019-04-30 08:18:45 +02:00
|
|
|
TreeAnimationDisintegrate.this.replaceBlock((TreeBlock) treeBlock);
|
2019-03-30 22:56:22 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.cancel();
|
2019-04-01 12:22:08 +02:00
|
|
|
whenFinished.run();
|
2019-03-30 22:56:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTaskTimer(ultimateTimber, 0, 1);
|
2019-03-28 05:22:13 +01:00
|
|
|
}
|
|
|
|
}
|