2019-03-26 02:44:29 +01:00
|
|
|
package com.songoda.ultimatetimber.manager;
|
|
|
|
|
2020-10-30 21:00:23 +01:00
|
|
|
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;
|
2020-10-30 21:00:23 +01:00
|
|
|
import com.songoda.core.utils.ItemUtils;
|
2019-03-28 01:56:39 +01:00
|
|
|
import com.songoda.ultimatetimber.UltimateTimber;
|
2019-03-29 23:52:17 +01:00
|
|
|
import com.songoda.ultimatetimber.events.TreeFallEvent;
|
|
|
|
import com.songoda.ultimatetimber.events.TreeFellEvent;
|
2019-05-01 09:33:21 +02:00
|
|
|
import com.songoda.ultimatetimber.misc.OnlyToppleWhile;
|
2019-03-29 23:52:17 +01:00
|
|
|
import com.songoda.ultimatetimber.tree.DetectedTree;
|
2020-08-29 00:54:47 +02:00
|
|
|
import com.songoda.ultimatetimber.tree.ITreeBlock;
|
2019-05-01 12:01:34 +02:00
|
|
|
import com.songoda.ultimatetimber.tree.TreeBlockSet;
|
2019-03-28 01:56:39 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2019-03-29 23:52:17 +01:00
|
|
|
import org.bukkit.GameMode;
|
2019-04-28 04:04:45 +02:00
|
|
|
import org.bukkit.Material;
|
2019-03-29 23:52:17 +01:00
|
|
|
import org.bukkit.block.Block;
|
2019-05-01 12:01:34 +02:00
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2019-03-29 23:52:17 +01:00
|
|
|
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;
|
2019-03-29 23:52:17 +01:00
|
|
|
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) {
|
2020-01-27 14:16:29 +01:00
|
|
|
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
|
|
|
|
2019-03-29 23:52:17 +01:00
|
|
|
Player player = event.getPlayer();
|
|
|
|
Block block = event.getBlock();
|
2020-10-30 21:00:23 +01:00
|
|
|
ItemStack tool = CompatibleHand.getHand(event).getItem(player);
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
// Protect saplings
|
|
|
|
if (saplingManager.isSaplingProtected(block)) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Condition checks
|
2019-04-05 08:05:43 +02:00
|
|
|
boolean isValid = true;
|
|
|
|
|
2019-03-29 23:52:17 +01:00
|
|
|
if (ConfigurationManager.Setting.DISABLED_WORLDS.getStringList().contains(player.getWorld().getName()))
|
2019-04-05 08:05:43 +02:00
|
|
|
isValid = false;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
if (!ConfigurationManager.Setting.ALLOW_CREATIVE_MODE.getBoolean() && player.getGameMode().equals(GameMode.CREATIVE))
|
2019-04-05 08:05:43 +02:00
|
|
|
isValid = false;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
2019-05-01 09:33:21 +02:00
|
|
|
if (!this.checkToppleWhile(player))
|
2019-04-05 08:05:43 +02:00
|
|
|
isValid = false;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
if (ConfigurationManager.Setting.REQUIRE_CHOP_PERMISSION.getBoolean() && !player.hasPermission("ultimatetimber.chop"))
|
2019-04-05 08:05:43 +02:00
|
|
|
isValid = false;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
if (!choppingManager.isChopping(player))
|
2019-04-05 08:05:43 +02:00
|
|
|
isValid = false;
|
2019-03-29 23:52:17 +01:00
|
|
|
|
2019-05-01 09:33:21 +02:00
|
|
|
if (choppingManager.isInCooldown(player))
|
|
|
|
isValid = false;
|
|
|
|
|
2019-04-29 02:58:38 +02:00
|
|
|
if (treeAnimationManager.isBlockInAnimation(block)) {
|
2019-04-05 08:05:43 +02:00
|
|
|
isValid = false;
|
2019-04-29 02:58:38 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
2019-04-01 12:22:08 +02:00
|
|
|
|
2019-03-29 23:52:17 +01:00
|
|
|
if (!treeDefinitionManager.isToolValidForAnyTreeDefinition(tool))
|
2019-04-05 08:05:43 +02:00
|
|
|
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;
|
|
|
|
|
2019-04-05 08:05:43 +02:00
|
|
|
boolean alwaysReplantSapling = ConfigurationManager.Setting.ALWAYS_REPLANT_SAPLING.getBoolean();
|
|
|
|
if (!isValid && !alwaysReplantSapling)
|
2019-03-29 23:52:17 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
DetectedTree detectedTree = treeDetectionManager.detectTree(block);
|
|
|
|
if (detectedTree == null)
|
|
|
|
return;
|
|
|
|
|
2019-04-05 08:05:43 +02:00
|
|
|
if (alwaysReplantSapling) {
|
2020-01-27 14:16:29 +01:00
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () ->
|
2019-04-05 08:05:43 +02:00
|
|
|
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;
|
|
|
|
|
2019-05-01 12:01:34 +02:00
|
|
|
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))
|
2019-03-29 23:52:17 +01:00
|
|
|
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);
|
|
|
|
|
2019-05-01 09:33:21 +02:00
|
|
|
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)
|
2020-10-30 21:00:23 +01:00
|
|
|
ItemUtils.addDamage(tool, toolDamage);
|
2019-03-29 23:52:17 +01:00
|
|
|
|
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());
|
|
|
|
|
2019-03-29 23:52:17 +01:00
|
|
|
treeAnimationManager.runAnimation(detectedTree, player);
|
2019-05-01 09:33:21 +02:00
|
|
|
treeDefinitionManager.dropTreeLoot(detectedTree.getTreeDefinition(), detectedTree.getDetectedTreeBlocks().getInitialLogBlock(), player, false, true);
|
2019-03-29 23:52:17 +01:00
|
|
|
|
|
|
|
// Trigger fell event
|
|
|
|
TreeFellEvent treeFellEvent = new TreeFellEvent(player, detectedTree);
|
|
|
|
Bukkit.getPluginManager().callEvent(treeFellEvent);
|
2019-03-28 01:56:39 +01:00
|
|
|
}
|
|
|
|
|
2019-05-01 09:33:21 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 12:01:34 +02:00
|
|
|
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
|
|
|
}
|