package com.craftaro.ultimatetimber.animation; 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; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class TreeAnimationDisintegrate extends TreeAnimation { public TreeAnimationDisintegrate(DetectedTree detectedTree, Player player) { super(TreeAnimationType.DISINTEGRATE, detectedTree, player); } @Override public void playAnimation(Runnable whenFinished) { 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> orderedLogBlocks = new ArrayList<>(this.detectedTree.getDetectedTreeBlocks().getLogBlocks()); orderedLogBlocks.sort(Comparator.comparingInt(x -> x.getLocation().getBlockY())); List> leafBlocks = new ArrayList<>(this.detectedTree.getDetectedTreeBlocks().getLeafBlocks()); Collections.shuffle(leafBlocks); Player p = this.player; TreeDefinition td = this.detectedTree.getTreeDefinition(); boolean hst = this.hasSilkTouch; new BukkitRunnable() { @Override public void run() { List> toDestroy = new ArrayList<>(); if (!orderedLogBlocks.isEmpty()) { ITreeBlock treeBlock = orderedLogBlocks.remove(0); toDestroy.add(treeBlock); } else if (!leafBlocks.isEmpty()) { ITreeBlock treeBlock = leafBlocks.remove(0); toDestroy.add(treeBlock); if (!leafBlocks.isEmpty()) { treeBlock = leafBlocks.remove(0); toDestroy.add(treeBlock); } } for (ITreeBlock fallingTreeBlock : TreeAnimationDisintegrate.this.fallingTreeBlocks.getAllTreeBlocks()) { FallingBlock fallingBlock = fallingTreeBlock.getBlock(); fallingBlock.setVelocity(fallingBlock.getVelocity().clone().subtract(new Vector(0, 0.05, 0))); } if (!toDestroy.isEmpty()) { ITreeBlock first = toDestroy.get(0); if (useCustomSound) { SoundUtils.playLandingSound(first); } for (ITreeBlock 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); } } else { this.cancel(); whenFinished.run(); } } }.runTaskTimer(ultimateTimber, 0, 1); } }