2023-07-26 17:56:09 +02:00
|
|
|
package com.craftaro.ultimatetimber.animation;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
2023-06-29 14:38:33 +02:00
|
|
|
import com.craftaro.core.compatibility.CompatibleHand;
|
|
|
|
import com.craftaro.core.compatibility.CompatibleMaterial;
|
|
|
|
import com.craftaro.core.third_party.com.cryptomorin.xseries.XMaterial;
|
2023-07-26 17:56:09 +02:00
|
|
|
import com.craftaro.ultimatetimber.UltimateTimber;
|
|
|
|
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;
|
2019-04-01 12:22:08 +02:00
|
|
|
import org.bukkit.Location;
|
2019-03-30 07:46:13 +01:00
|
|
|
import org.bukkit.Material;
|
2019-03-28 05:22:13 +01:00
|
|
|
import org.bukkit.block.Block;
|
2019-03-31 21:48:03 +02:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
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-03-31 21:48:03 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
|
|
|
public abstract class TreeAnimation {
|
|
|
|
protected final TreeAnimationType treeAnimationType;
|
2019-03-30 07:46:13 +01:00
|
|
|
protected final DetectedTree detectedTree;
|
|
|
|
protected final Player player;
|
2019-03-31 21:48:03 +02:00
|
|
|
protected final boolean hasSilkTouch;
|
2019-04-01 12:22:08 +02:00
|
|
|
protected TreeBlockSet<FallingBlock> fallingTreeBlocks;
|
2019-03-28 05:22:13 +01:00
|
|
|
|
2019-03-30 07:46:13 +01:00
|
|
|
TreeAnimation(TreeAnimationType treeAnimationType, DetectedTree detectedTree, Player player) {
|
2019-03-28 05:22:13 +01:00
|
|
|
this.treeAnimationType = treeAnimationType;
|
2019-03-30 07:46:13 +01:00
|
|
|
this.detectedTree = detectedTree;
|
|
|
|
this.player = player;
|
2019-03-31 21:48:03 +02:00
|
|
|
|
2020-10-30 21:00:23 +01:00
|
|
|
ItemStack itemInHand = CompatibleHand.getHand(CompatibleHand.MAIN_HAND).getItem(player);
|
2019-03-31 21:48:03 +02:00
|
|
|
this.hasSilkTouch = itemInHand != null && itemInHand.hasItemMeta() && itemInHand.getItemMeta().hasEnchant(Enchantment.SILK_TOUCH);
|
2019-04-01 12:22:08 +02:00
|
|
|
|
2019-04-05 08:05:43 +02:00
|
|
|
this.fallingTreeBlocks = new TreeBlockSet<>(); // Should be overridden in any subclasses that need to use it
|
2019-03-28 05:22:13 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 23:52:17 +01:00
|
|
|
/**
|
|
|
|
* Plays this tree topple animation
|
|
|
|
*
|
2019-03-30 07:46:13 +01:00
|
|
|
* @param whenFinished The runnable to run when the animation is done
|
2019-03-29 23:52:17 +01:00
|
|
|
*/
|
2019-03-30 07:46:13 +01:00
|
|
|
public abstract void playAnimation(Runnable whenFinished);
|
|
|
|
|
2019-04-01 12:22:08 +02:00
|
|
|
/**
|
|
|
|
* Gets the type of tree animation that this is
|
|
|
|
*
|
|
|
|
* @return The TreeAnimationType
|
|
|
|
*/
|
|
|
|
public TreeAnimationType getTreeAnimationType() {
|
|
|
|
return this.treeAnimationType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the detected tree
|
|
|
|
*
|
|
|
|
* @return The detected tree
|
|
|
|
*/
|
|
|
|
public DetectedTree getDetectedTree() {
|
|
|
|
return this.detectedTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the player who started this tree animation
|
|
|
|
*
|
|
|
|
* @return The player who started this tree animation
|
|
|
|
*/
|
|
|
|
public Player getPlayer() {
|
|
|
|
return this.player;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this tree animation has silk touch
|
|
|
|
*
|
|
|
|
* @return True if this animation has silk touch, otherwise false
|
|
|
|
*/
|
|
|
|
public boolean hasSilkTouch() {
|
|
|
|
return this.hasSilkTouch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a TreeBlockSet of the active falling tree blocks
|
|
|
|
* May return null if the animation type does not use falling blocks
|
|
|
|
*
|
|
|
|
* @return A tree block set
|
|
|
|
*/
|
|
|
|
public TreeBlockSet<FallingBlock> getFallingTreeBlocks() {
|
|
|
|
return this.fallingTreeBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a TreeBlock into a FallingTreeBlock
|
|
|
|
*
|
|
|
|
* @param treeBlock The TreeBlock to convert
|
|
|
|
* @return A FallingTreeBlock that has been converted from a TreeBlock
|
|
|
|
*/
|
|
|
|
protected FallingTreeBlock convertToFallingBlock(TreeBlock treeBlock) {
|
|
|
|
Location location = treeBlock.getLocation().clone().add(0.5, 0, 0.5);
|
|
|
|
Block block = treeBlock.getBlock();
|
2023-06-29 14:38:33 +02:00
|
|
|
XMaterial material = CompatibleMaterial.getMaterial(block.getType()).get();
|
|
|
|
if (CompatibleMaterial.isAir(material)) {
|
2019-04-02 03:07:02 +02:00
|
|
|
this.replaceBlock(treeBlock);
|
2019-04-01 12:22:08 +02:00
|
|
|
return null;
|
2019-04-02 03:07:02 +02:00
|
|
|
}
|
2019-04-01 12:22:08 +02:00
|
|
|
|
2020-10-30 21:00:23 +01:00
|
|
|
FallingBlock fallingBlock = BlockUtils.spawnFallingBlock(location, material);
|
|
|
|
BlockUtils.configureFallingBlock(fallingBlock);
|
2019-04-01 12:22:08 +02:00
|
|
|
|
|
|
|
FallingTreeBlock fallingTreeBlock = new FallingTreeBlock(fallingBlock, treeBlock.getTreeBlockType());
|
|
|
|
this.replaceBlock(treeBlock);
|
|
|
|
return fallingTreeBlock;
|
|
|
|
}
|
|
|
|
|
2019-03-30 07:46:13 +01:00
|
|
|
/**
|
|
|
|
* Replaces a given block with a new one
|
|
|
|
*
|
2019-04-01 12:22:08 +02:00
|
|
|
* @param treeBlock The tree block to replace
|
|
|
|
*/
|
|
|
|
public void replaceBlock(TreeBlock treeBlock) {
|
|
|
|
treeBlock.getBlock().setType(Material.AIR);
|
|
|
|
UltimateTimber.getInstance().getSaplingManager().replantSapling(this.detectedTree.getTreeDefinition(), treeBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a falling block from the animation
|
|
|
|
*
|
|
|
|
* @param fallingBlock The FallingBlock to remove
|
2019-03-30 07:46:13 +01:00
|
|
|
*/
|
2019-04-01 12:22:08 +02:00
|
|
|
public void removeFallingBlock(FallingBlock fallingBlock) {
|
|
|
|
for (ITreeBlock<FallingBlock> fallingTreeBlock : this.fallingTreeBlocks.getAllTreeBlocks()) {
|
2019-04-02 03:07:02 +02:00
|
|
|
if (fallingTreeBlock.getBlock().equals(fallingBlock)) {
|
2019-04-01 12:22:08 +02:00
|
|
|
this.fallingTreeBlocks.remove(fallingTreeBlock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 07:46:13 +01:00
|
|
|
}
|
2019-03-28 05:22:13 +01:00
|
|
|
|
|
|
|
}
|