UltimateTimber/src/main/java/com/craftaro/ultimatetimber/animation/TreeAnimationDisintegrate.java

102 lines
4.6 KiB
Java
Raw Normal View History

package com.craftaro.ultimatetimber.animation;
2019-03-28 05:22:13 +01:00
import com.craftaro.core.compatibility.CompatibleMaterial;
import com.craftaro.ultimatetimber.UltimateTimber;
import com.craftaro.ultimatetimber.manager.TreeDefinitionManager;
import com.craftaro.ultimatetimber.manager.ConfigurationManager;
import com.craftaro.ultimatetimber.tree.DetectedTree;
import com.craftaro.ultimatetimber.tree.ITreeBlock;
import com.craftaro.ultimatetimber.tree.TreeBlock;
import com.craftaro.ultimatetimber.tree.TreeBlockType;
import com.craftaro.ultimatetimber.tree.TreeDefinition;
import com.craftaro.ultimatetimber.utils.ParticleUtils;
import com.craftaro.ultimatetimber.utils.SoundUtils;
import org.bukkit.block.Block;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Player;
2019-03-30 22:56:22 +01:00
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
2019-03-30 22:56:22 +01: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) {
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);
}
}
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);
if (useCustomSound) {
SoundUtils.playLandingSound(first);
}
2019-03-28 05:22:13 +01:00
2019-03-30 22:56:22 +01:00
for (ITreeBlock<Block> treeBlock : toDestroy) {
if (treeBlock.getTreeBlockType() == TreeBlockType.LOG) {
if (td.getLogMaterial().stream().noneMatch(x -> x == CompatibleMaterial.getMaterial(treeBlock.getBlock().getType()).orElse(null))) {
continue;
}
} else if (treeBlock.getTreeBlockType() == TreeBlockType.LEAF) {
if (td.getLeafMaterial().stream().noneMatch(x -> x == CompatibleMaterial.getMaterial(treeBlock.getBlock().getType()).orElse(null))) {
continue;
}
}
if (useCustomParticles) {
ParticleUtils.playFallingParticles(treeBlock);
}
treeDefinitionManager.dropTreeLoot(td, treeBlock, p, hst, false);
TreeAnimationDisintegrate.this.replaceBlock((TreeBlock) treeBlock);
2019-03-30 22:56:22 +01:00
}
} else {
this.cancel();
whenFinished.run();
2019-03-30 22:56:22 +01:00
}
}
}.runTaskTimer(ultimateTimber, 0, 1);
2019-03-28 05:22:13 +01:00
}
}