2023-07-26 17:56:09 +02:00
|
|
|
package com.craftaro.ultimatetimber.manager;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
2023-06-29 14:38:33 +02:00
|
|
|
import com.craftaro.core.compatibility.CompatibleMaterial;
|
|
|
|
import com.craftaro.core.third_party.com.cryptomorin.xseries.XBlock;
|
|
|
|
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.ITreeBlock;
|
|
|
|
import com.craftaro.ultimatetimber.tree.TreeBlockType;
|
|
|
|
import com.craftaro.ultimatetimber.tree.TreeDefinition;
|
2019-03-29 23:52:17 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.block.Block;
|
2019-03-30 07:46:13 +01:00
|
|
|
import org.bukkit.block.BlockFace;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public class SaplingManager extends Manager {
|
2023-06-29 14:31:22 +02:00
|
|
|
private final Random random;
|
|
|
|
private final Set<Location> protectedSaplings;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
2023-06-29 14:31:22 +02:00
|
|
|
public SaplingManager(UltimateTimber plugin) {
|
|
|
|
super(plugin);
|
2019-03-29 23:52:17 +01:00
|
|
|
this.random = new Random();
|
|
|
|
this.protectedSaplings = new HashSet<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reload() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replants a sapling given a TreeDefinition and Location
|
|
|
|
* Takes into account config settings
|
|
|
|
*
|
|
|
|
* @param treeDefinition The TreeDefinition of the sapling
|
2023-06-29 14:31:22 +02:00
|
|
|
* @param treeBlock The ITreeBlock to replant for
|
2019-03-29 23:52:17 +01:00
|
|
|
*/
|
2019-04-01 12:22:08 +02:00
|
|
|
public void replantSapling(TreeDefinition treeDefinition, ITreeBlock treeBlock) {
|
2023-06-29 14:31:22 +02:00
|
|
|
if (!ConfigurationManager.Setting.REPLANT_SAPLINGS.getBoolean()) {
|
2019-03-29 23:52:17 +01:00
|
|
|
return;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-03-29 23:52:17 +01:00
|
|
|
|
2019-04-02 03:07:02 +02:00
|
|
|
Block block = treeBlock.getLocation().getBlock();
|
2023-06-29 14:31:22 +02:00
|
|
|
if (block.getType() != Material.AIR || treeBlock.getTreeBlockType() == TreeBlockType.LEAF) {
|
2019-04-02 03:07:02 +02:00
|
|
|
return;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-04-02 03:07:02 +02:00
|
|
|
|
2020-01-27 14:16:29 +01:00
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> this.internalReplant(treeDefinition, treeBlock), 1L);
|
2019-03-29 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Randomly replants a sapling given a TreeDefinition and Location
|
|
|
|
* Takes into account config settings
|
|
|
|
*
|
|
|
|
* @param treeDefinition The TreeDefinition of the sapling
|
2023-06-29 14:31:22 +02:00
|
|
|
* @param treeBlock The ITreeBlock to replant for
|
2019-03-29 23:52:17 +01:00
|
|
|
*/
|
2019-04-01 12:22:08 +02:00
|
|
|
public void replantSaplingWithChance(TreeDefinition treeDefinition, ITreeBlock treeBlock) {
|
2023-06-29 14:38:33 +02:00
|
|
|
if (!ConfigurationManager.Setting.FALLING_BLOCKS_REPLANT_SAPLINGS.getBoolean() || !CompatibleMaterial.isAir(CompatibleMaterial.getMaterial(treeBlock.getLocation().getBlock().getType()).get())) {
|
2019-03-29 23:52:17 +01:00
|
|
|
return;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
double chance = ConfigurationManager.Setting.FALLING_BLOCKS_REPLANT_SAPLINGS_CHANCE.getDouble();
|
2023-06-29 14:31:22 +02:00
|
|
|
if (this.random.nextDouble() > chance / 100) {
|
2019-03-29 23:52:17 +01:00
|
|
|
return;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-03-29 23:52:17 +01:00
|
|
|
|
2020-01-27 14:16:29 +01:00
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> this.internalReplant(treeDefinition, treeBlock), 1L);
|
2019-03-29 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replants a sapling given a TreeDefinition and Location
|
|
|
|
*
|
|
|
|
* @param treeDefinition The TreeDefinition of the sapling
|
2023-06-29 14:31:22 +02:00
|
|
|
* @param treeBlock The ITreeBlock to replant for
|
2019-03-29 23:52:17 +01:00
|
|
|
*/
|
2019-04-01 12:22:08 +02:00
|
|
|
private void internalReplant(TreeDefinition treeDefinition, ITreeBlock treeBlock) {
|
2020-01-27 14:16:29 +01:00
|
|
|
TreeDefinitionManager treeDefinitionManager = this.plugin.getTreeDefinitionManager();
|
2019-03-30 10:34:27 +01:00
|
|
|
|
2019-04-01 12:22:08 +02:00
|
|
|
Block block = treeBlock.getLocation().getBlock();
|
2019-03-30 07:46:13 +01:00
|
|
|
Block blockBelow = block.getRelative(BlockFace.DOWN);
|
|
|
|
boolean isValidSoil = false;
|
2023-06-29 14:38:33 +02:00
|
|
|
for (XMaterial soilMaterial : treeDefinitionManager.getPlantableSoilMaterial(treeDefinition)) {
|
|
|
|
if (soilMaterial == CompatibleMaterial.getMaterial(blockBelow.getType()).orElse(null)) {
|
2019-03-30 07:46:13 +01:00
|
|
|
isValidSoil = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:31:22 +02:00
|
|
|
if (!isValidSoil) {
|
2019-03-30 07:46:13 +01:00
|
|
|
return;
|
2023-06-29 14:31:22 +02:00
|
|
|
}
|
2019-03-30 07:46:13 +01:00
|
|
|
|
2023-06-29 14:38:33 +02:00
|
|
|
XMaterial material = treeDefinition.getSaplingMaterial();
|
|
|
|
XBlock.setType(block, material);
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
int cooldown = ConfigurationManager.Setting.REPLANT_SAPLINGS_COOLDOWN.getInt();
|
|
|
|
if (cooldown != 0) {
|
2019-04-01 12:22:08 +02:00
|
|
|
this.protectedSaplings.add(block.getLocation());
|
2020-01-27 14:16:29 +01:00
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> this.protectedSaplings.remove(block.getLocation()), cooldown * 20L);
|
2019-03-29 23:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets if a sapling is protected
|
|
|
|
*
|
|
|
|
* @param block The Block to check
|
|
|
|
* @return True if the sapling is protected, otherwise false
|
|
|
|
*/
|
|
|
|
public boolean isSaplingProtected(Block block) {
|
|
|
|
return this.protectedSaplings.contains(block.getLocation());
|
|
|
|
}
|
|
|
|
}
|