From 8ff4e315eb5a9a15bf9b08213a3bd2f6326104b7 Mon Sep 17 00:00:00 2001 From: Esophose Date: Wed, 17 Apr 2019 02:11:58 -0600 Subject: [PATCH] Added animation type CRUMBLE --- .../adapter/VersionAdapter.java | 1 - .../animation/TreeAnimation.java | 1 - .../animation/TreeAnimationCrumble.java | 96 +++++++++++++++++++ .../animation/TreeAnimationDisintegrate.java | 9 +- .../animation/TreeAnimationType.java | 2 +- .../manager/TreeAnimationManager.java | 4 + .../src/main/resources/config-current.yml | 6 +- .../src/main/resources/config-legacy.yml | 5 +- 8 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationCrumble.java diff --git a/UltimateTimber/Core/src/main/java/com/songoda/ultimatetimber/adapter/VersionAdapter.java b/UltimateTimber/Core/src/main/java/com/songoda/ultimatetimber/adapter/VersionAdapter.java index c697509..a933be1 100644 --- a/UltimateTimber/Core/src/main/java/com/songoda/ultimatetimber/adapter/VersionAdapter.java +++ b/UltimateTimber/Core/src/main/java/com/songoda/ultimatetimber/adapter/VersionAdapter.java @@ -7,7 +7,6 @@ import org.bukkit.block.Block; import org.bukkit.entity.FallingBlock; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.util.Vector; import java.util.Collection; diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimation.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimation.java index 180c3e3..0a70b3f 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimation.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimation.java @@ -9,7 +9,6 @@ import com.songoda.ultimatetimber.tree.TreeBlock; import com.songoda.ultimatetimber.tree.TreeBlockSet; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.FallingBlock; diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationCrumble.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationCrumble.java new file mode 100644 index 0000000..8c2e34c --- /dev/null +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationCrumble.java @@ -0,0 +1,96 @@ +package com.songoda.ultimatetimber.animation; + +import com.songoda.ultimatetimber.UltimateTimber; +import com.songoda.ultimatetimber.adapter.VersionAdapter; +import com.songoda.ultimatetimber.manager.ConfigurationManager; +import com.songoda.ultimatetimber.tree.DetectedTree; +import com.songoda.ultimatetimber.tree.FallingTreeBlock; +import com.songoda.ultimatetimber.tree.ITreeBlock; +import com.songoda.ultimatetimber.tree.TreeBlock; +import com.songoda.ultimatetimber.tree.TreeBlockSet; +import com.songoda.ultimatetimber.tree.TreeDefinition; +import org.bukkit.block.Block; +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 TreeAnimationCrumble extends TreeAnimation { + + public TreeAnimationCrumble(DetectedTree detectedTree, Player player) { + super(TreeAnimationType.CRUMBLE, detectedTree, player); + } + + @Override + public void playAnimation(Runnable whenFinished) { + UltimateTimber ultimateTimber = UltimateTimber.getInstance(); + VersionAdapter versionAdapter = ultimateTimber.getVersionAdapter(); + + boolean useCustomSound = ConfigurationManager.Setting.USE_CUSTOM_SOUNDS.getBoolean(); + boolean useCustomParticles = ConfigurationManager.Setting.USE_CUSTOM_PARTICLES.getBoolean(); + + // Order blocks by y-axis, lowest first, but shuffled randomly + int currentY = -1; + List>> treeBlocks = new ArrayList<>(); + List> currentPartition = new ArrayList<>(); + List> orderedDetectedTreeBlocks = new ArrayList<>(this.detectedTree.getDetectedTreeBlocks().getAllTreeBlocks()); + orderedDetectedTreeBlocks.sort(Comparator.comparingInt(x -> x.getLocation().getBlockY())); + for (ITreeBlock treeBlock : orderedDetectedTreeBlocks) { + if (currentY != treeBlock.getLocation().getBlockY()) { + Collections.shuffle(currentPartition); + treeBlocks.add(new ArrayList<>(currentPartition)); + currentPartition.clear(); + currentY = treeBlock.getLocation().getBlockY(); + } + currentPartition.add(treeBlock); + } + + Collections.shuffle(currentPartition); + treeBlocks.add(new ArrayList<>(currentPartition)); + + Player p = this.player; + TreeDefinition td = this.detectedTree.getTreeDefinition(); + boolean hst = this.hasSilkTouch; + + new BukkitRunnable() { + @Override + public void run() { + if (!treeBlocks.isEmpty()) { + List> partition = treeBlocks.get(0); + for (int i = 0; i < 3 && !partition.isEmpty(); i++) { + ITreeBlock treeBlock = partition.remove(0); + FallingTreeBlock fallingTreeBlock = TreeAnimationCrumble.this.convertToFallingBlock((TreeBlock)treeBlock); + if (fallingTreeBlock == null) + continue; + + versionAdapter.toggleGravityFallingBlock(fallingTreeBlock.getBlock(), true); + fallingTreeBlock.getBlock().setVelocity(Vector.getRandom().setY(0).subtract(new Vector(0.5, 0, 0.5)).multiply(0.15)); + TreeAnimationCrumble.this.fallingTreeBlocks.add(fallingTreeBlock); + + if (TreeAnimationCrumble.this.fallingTreeBlocks == null) + TreeAnimationCrumble.this.fallingTreeBlocks = new TreeBlockSet<>(fallingTreeBlock); + + if (useCustomSound) + versionAdapter.playLandingSound(treeBlock); + if (useCustomParticles) + versionAdapter.playFallingParticles(td, treeBlock); + } + + if (partition.isEmpty()) { + treeBlocks.remove(0); + } + } + + if (treeBlocks.isEmpty() && TreeAnimationCrumble.this.fallingTreeBlocks.getAllTreeBlocks().isEmpty()) { + whenFinished.run(); + this.cancel(); + } + } + }.runTaskTimer(ultimateTimber, 0, 1); + } +} + diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationDisintegrate.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationDisintegrate.java index ff2fe9a..46daf25 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationDisintegrate.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationDisintegrate.java @@ -48,17 +48,14 @@ public class TreeAnimationDisintegrate extends TreeAnimation { List> toDestroy = new ArrayList<>(); if (!orderedLogBlocks.isEmpty()) { - ITreeBlock treeBlock = orderedLogBlocks.get(0); - orderedLogBlocks.remove(treeBlock); + ITreeBlock treeBlock = orderedLogBlocks.remove(0); toDestroy.add(treeBlock); } else if (!leafBlocks.isEmpty()) { - ITreeBlock treeBlock = leafBlocks.get(0); - leafBlocks.remove(treeBlock); + ITreeBlock treeBlock = leafBlocks.remove(0); toDestroy.add(treeBlock); if (!leafBlocks.isEmpty()) { - treeBlock = leafBlocks.get(0); - leafBlocks.remove(treeBlock); + treeBlock = leafBlocks.remove(0); toDestroy.add(treeBlock); } } diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationType.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationType.java index 3047fce..50b5a55 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationType.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/animation/TreeAnimationType.java @@ -6,6 +6,6 @@ package com.songoda.ultimatetimber.animation; public enum TreeAnimationType { FANCY, DISINTIGRATE, - CHAOS, + CRUMBLE, NONE } diff --git a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/TreeAnimationManager.java b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/TreeAnimationManager.java index 81dda3e..1007520 100644 --- a/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/TreeAnimationManager.java +++ b/UltimateTimber/Plugin/src/main/java/com/songoda/ultimatetimber/manager/TreeAnimationManager.java @@ -3,6 +3,7 @@ package com.songoda.ultimatetimber.manager; import com.songoda.ultimatetimber.UltimateTimber; import com.songoda.ultimatetimber.adapter.VersionAdapter; import com.songoda.ultimatetimber.animation.TreeAnimation; +import com.songoda.ultimatetimber.animation.TreeAnimationCrumble; import com.songoda.ultimatetimber.animation.TreeAnimationDisintegrate; import com.songoda.ultimatetimber.animation.TreeAnimationFancy; import com.songoda.ultimatetimber.animation.TreeAnimationNone; @@ -79,6 +80,9 @@ public class TreeAnimationManager extends Manager implements Listener, Runnable case "DISINTEGRATE": this.registerTreeAnimation(new TreeAnimationDisintegrate(detectedTree, player)); break; + case "CRUMBLE": + this.registerTreeAnimation(new TreeAnimationCrumble(detectedTree, player)); + break; case "NONE": this.registerTreeAnimation(new TreeAnimationNone(detectedTree, player)); break; diff --git a/UltimateTimber/Plugin/src/main/resources/config-current.yml b/UltimateTimber/Plugin/src/main/resources/config-current.yml index 3ecb67b..096936d 100644 --- a/UltimateTimber/Plugin/src/main/resources/config-current.yml +++ b/UltimateTimber/Plugin/src/main/resources/config-current.yml @@ -117,11 +117,11 @@ use-custom-particles: true bonus-loot-multiplier: 2 # The type of animation to use for tree toppling -# Types: FANCY, DISINTEGRATE, NONE +# Types: FANCY, DISINTEGRATE, CRUMBLE, NONE tree-animation-type: FANCY -# If the tree-animation-type is FANCY, make the blocks stick to the ground -# Does nothing if tree-animation-type is not FANCY +# If the tree-animation-type is FANCY or CRUMBLE, make the blocks stick to the ground +# Does nothing if tree-animation-type is not FANCY or CRUMBLE # Default: false scatter-tree-blocks-on-ground: false diff --git a/UltimateTimber/Plugin/src/main/resources/config-legacy.yml b/UltimateTimber/Plugin/src/main/resources/config-legacy.yml index d6efb1f..866952a 100644 --- a/UltimateTimber/Plugin/src/main/resources/config-legacy.yml +++ b/UltimateTimber/Plugin/src/main/resources/config-legacy.yml @@ -118,10 +118,11 @@ use-custom-particles: true bonus-loot-multiplier: 2 # The type of animation to use for tree toppling -# Types: FANCY, DISINTEGRATE, NONE +# Types: FANCY, DISINTEGRATE, CRUMBLE, NONE tree-animation-type: FANCY -# If the tree-animation-type uses falling block entities, make the falling blocks stick to the ground +# If the tree-animation-type is FANCY or CRUMBLE, make the blocks stick to the ground +# Does nothing if tree-animation-type is not FANCY or CRUMBLE # Default: false scatter-tree-blocks-on-ground: false