UltimateTimber/UltimateTimber/Plugin/src/com/songoda/ultimatetimber/manager/TreeFallManager.java

189 lines
7.1 KiB
Java
Raw Normal View History

2019-03-26 02:44:29 +01:00
package com.songoda.ultimatetimber.manager;
import com.songoda.core.compatibility.CompatibleHand;
2020-08-29 00:54:47 +02:00
import com.songoda.core.hooks.JobsHook;
import com.songoda.core.hooks.LogManager;
import com.songoda.core.hooks.McMMOHook;
import com.songoda.core.utils.ItemUtils;
2019-03-28 01:56:39 +01:00
import com.songoda.ultimatetimber.UltimateTimber;
import com.songoda.ultimatetimber.events.TreeFallEvent;
import com.songoda.ultimatetimber.events.TreeFellEvent;
import com.songoda.ultimatetimber.misc.OnlyToppleWhile;
import com.songoda.ultimatetimber.tree.DetectedTree;
2020-08-29 00:54:47 +02:00
import com.songoda.ultimatetimber.tree.ITreeBlock;
import com.songoda.ultimatetimber.tree.TreeBlockSet;
2019-03-28 01:56:39 +01:00
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
2019-04-28 04:04:45 +02:00
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
2019-03-28 01:56:39 +01:00
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
2019-03-28 01:56:39 +01:00
2020-08-29 00:54:47 +02:00
import java.util.stream.Collectors;
2019-03-28 01:56:39 +01:00
public class TreeFallManager extends Manager implements Listener {
public TreeFallManager(UltimateTimber ultimateTimber) {
super(ultimateTimber);
Bukkit.getPluginManager().registerEvents(this, ultimateTimber);
}
@Override
public void reload() {
}
@Override
public void disable() {
}
2019-04-28 04:04:45 +02:00
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
2019-03-28 01:56:39 +01:00
public void onBlockBreak(BlockBreakEvent event) {
TreeDefinitionManager treeDefinitionManager = this.plugin.getTreeDefinitionManager();
TreeDetectionManager treeDetectionManager = this.plugin.getTreeDetectionManager();
TreeAnimationManager treeAnimationManager = this.plugin.getTreeAnimationManager();
ChoppingManager choppingManager = this.plugin.getChoppingManager();
SaplingManager saplingManager = this.plugin.getSaplingManager();
2019-03-28 01:56:39 +01:00
Player player = event.getPlayer();
Block block = event.getBlock();
ItemStack tool = CompatibleHand.getHand(event).getItem(player);
// Protect saplings
if (saplingManager.isSaplingProtected(block)) {
event.setCancelled(true);
return;
}
// Condition checks
boolean isValid = true;
if (ConfigurationManager.Setting.DISABLED_WORLDS.getStringList().contains(player.getWorld().getName()))
isValid = false;
if (!ConfigurationManager.Setting.ALLOW_CREATIVE_MODE.getBoolean() && player.getGameMode().equals(GameMode.CREATIVE))
isValid = false;
if (!this.checkToppleWhile(player))
isValid = false;
if (ConfigurationManager.Setting.REQUIRE_CHOP_PERMISSION.getBoolean() && !player.hasPermission("ultimatetimber.chop"))
isValid = false;
if (!choppingManager.isChopping(player))
isValid = false;
if (choppingManager.isInCooldown(player))
isValid = false;
if (treeAnimationManager.isBlockInAnimation(block)) {
isValid = false;
event.setCancelled(true);
}
if (!treeDefinitionManager.isToolValidForAnyTreeDefinition(tool))
isValid = false;
2020-08-29 00:54:47 +02:00
if (ConfigurationManager.Setting.HOOKS_REQUIRE_ABILITY_ACTIVE.getBoolean()
&& McMMOHook.isUsingTreeFeller(player))
2019-04-28 04:04:45 +02:00
isValid = false;
boolean alwaysReplantSapling = ConfigurationManager.Setting.ALWAYS_REPLANT_SAPLING.getBoolean();
if (!isValid && !alwaysReplantSapling)
return;
DetectedTree detectedTree = treeDetectionManager.detectTree(block);
if (detectedTree == null)
return;
if (alwaysReplantSapling) {
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () ->
saplingManager.replantSapling(detectedTree.getTreeDefinition(), detectedTree.getDetectedTreeBlocks().getInitialLogBlock()));
if (!isValid)
return;
}
2019-03-31 00:22:45 +01:00
if (!treeDefinitionManager.isToolValidForTreeDefinition(detectedTree.getTreeDefinition(), tool))
return;
int toolDamage = this.getToolDamage(detectedTree.getDetectedTreeBlocks(), tool.containsEnchantment(Enchantment.SILK_TOUCH));
2020-11-03 23:37:30 +01:00
if (!ConfigurationManager.Setting.PROTECT_TOOL.getBoolean() && !ItemUtils.hasEnoughDurability(tool, toolDamage))
return;
// Trigger fall event
TreeFallEvent treeFallEvent = new TreeFallEvent(player, detectedTree);
Bukkit.getPluginManager().callEvent(treeFallEvent);
if (treeFallEvent.isCancelled())
return;
// Valid tree and meets all conditions past this point
event.setCancelled(true);
choppingManager.cooldownPlayer(player);
2019-04-28 04:04:45 +02:00
// Destroy initiated block if enabled
if (ConfigurationManager.Setting.DESTROY_INITIATED_BLOCK.getBoolean()) {
detectedTree.getDetectedTreeBlocks().getInitialLogBlock().getBlock().setType(Material.AIR);
detectedTree.getDetectedTreeBlocks().remove(detectedTree.getDetectedTreeBlocks().getInitialLogBlock());
}
2020-11-24 22:49:26 +01:00
boolean isCreative = player.getGameMode().equals(GameMode.CREATIVE);
if (!isCreative)
ItemUtils.addDamage(tool, toolDamage);
2020-08-29 00:54:47 +02:00
McMMOHook.addWoodcutting(player, detectedTree.getDetectedTreeBlocks().getAllTreeBlocks().stream()
.map(ITreeBlock::getBlock).collect(Collectors.toList()));
2020-11-24 22:49:26 +01:00
if (!isCreative && JobsHook.isEnabled())
for (ITreeBlock<Block> treeBlock : detectedTree.getDetectedTreeBlocks().getLogBlocks())
JobsHook.breakBlock(player, treeBlock.getBlock());
for (ITreeBlock<Block> treeBlock : detectedTree.getDetectedTreeBlocks().getAllTreeBlocks())
2020-08-29 00:54:47 +02:00
LogManager.logRemoval(player, treeBlock.getBlock());
treeAnimationManager.runAnimation(detectedTree, player);
treeDefinitionManager.dropTreeLoot(detectedTree.getTreeDefinition(), detectedTree.getDetectedTreeBlocks().getInitialLogBlock(), player, false, true);
// Trigger fell event
TreeFellEvent treeFellEvent = new TreeFellEvent(player, detectedTree);
Bukkit.getPluginManager().callEvent(treeFellEvent);
2019-03-28 01:56:39 +01:00
}
/**
* Checks if a player is doing a certain action required to topple a tree
*
* @param player The player to check
* @return True if the check passes, otherwise false
*/
private boolean checkToppleWhile(Player player) {
switch (OnlyToppleWhile.fromString(ConfigurationManager.Setting.ONLY_TOPPLE_WHILE.getString())) {
case SNEAKING:
return player.isSneaking();
case NOT_SNEAKING:
return !player.isSneaking();
default:
return true;
}
}
private int getToolDamage(TreeBlockSet<Block> treeBlocks, boolean hasSilkTouch) {
if (!ConfigurationManager.Setting.REALISTIC_TOOL_DAMAGE.getBoolean())
return 1;
if (ConfigurationManager.Setting.APPLY_SILK_TOUCH_TOOL_DAMAGE.getBoolean() && hasSilkTouch) {
return treeBlocks.size();
} else {
return treeBlocks.getLogBlocks().size();
}
}
2019-03-26 02:44:29 +01:00
}