2013-03-01 06:52:01 +01:00
|
|
|
package com.gmail.nossr50.skills.woodcutting;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.config.AdvancedConfig;
|
|
|
|
import com.gmail.nossr50.config.Config;
|
2013-08-23 10:16:22 +02:00
|
|
|
import com.gmail.nossr50.config.experience.ExperienceConfig;
|
2017-06-10 19:47:20 +02:00
|
|
|
import com.gmail.nossr50.datatypes.skills.SkillType;
|
2018-07-24 04:13:57 +02:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-03-06 22:23:19 +01:00
|
|
|
import com.gmail.nossr50.util.BlockUtils;
|
2013-03-01 06:52:01 +01:00
|
|
|
import com.gmail.nossr50.util.Misc;
|
2013-10-28 20:12:24 +01:00
|
|
|
import com.gmail.nossr50.util.skills.SkillUtils;
|
2018-07-24 04:13:57 +02:00
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.TreeSpecies;
|
|
|
|
import org.bukkit.block.BlockFace;
|
|
|
|
import org.bukkit.block.BlockState;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.material.Tree;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2013-03-01 06:52:01 +01:00
|
|
|
|
|
|
|
public final class Woodcutting {
|
2013-03-06 22:23:19 +01:00
|
|
|
public static int leafBlowerUnlockLevel = AdvancedConfig.getInstance().getLeafBlowUnlockLevel();
|
|
|
|
public static int treeFellerThreshold = Config.getInstance().getTreeFellerThreshold();
|
|
|
|
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
protected static boolean treeFellerReachedThreshold = false;
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
protected enum ExperienceGainMethod {
|
|
|
|
DEFAULT,
|
|
|
|
TREE_FELLER,
|
2018-07-24 04:13:57 +02:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
|
|
|
|
private Woodcutting() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the experience reward from a log
|
|
|
|
*
|
|
|
|
* @param blockState Log being broken
|
|
|
|
* @param experienceGainMethod How the log is being broken
|
|
|
|
* @return Amount of experience
|
|
|
|
*/
|
|
|
|
protected static int getExperienceFromLog(BlockState blockState, ExperienceGainMethod experienceGainMethod) {
|
2014-02-03 20:48:43 +01:00
|
|
|
if (mcMMO.getModManager().isCustomLog(blockState)) {
|
|
|
|
return mcMMO.getModManager().getBlock(blockState).getXpGain();
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 01:53:29 +02:00
|
|
|
return ExperienceConfig.getInstance().getXp(SkillType.WOODCUTTING, blockState.getBlockData());
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for double drops
|
|
|
|
*
|
|
|
|
* @param blockState Block being broken
|
|
|
|
*/
|
2013-03-06 22:23:19 +01:00
|
|
|
protected static void checkForDoubleDrop(BlockState blockState) {
|
2014-02-03 20:48:43 +01:00
|
|
|
if (mcMMO.getModManager().isCustomLog(blockState) && mcMMO.getModManager().getBlock(blockState).isDoubleDropEnabled()) {
|
2016-03-16 17:47:40 +01:00
|
|
|
Misc.dropItems(Misc.getBlockCenter(blockState), blockState.getBlock().getDrops());
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
2013-12-30 23:45:56 +01:00
|
|
|
else {
|
2018-07-24 04:13:57 +02:00
|
|
|
TreeSpecies species = new Tree(blockState.getType()).getSpecies();
|
2013-12-30 23:45:56 +01:00
|
|
|
|
|
|
|
if (Config.getInstance().getWoodcuttingDoubleDropsEnabled(species)) {
|
2016-03-16 17:47:40 +01:00
|
|
|
Misc.dropItems(Misc.getBlockCenter(blockState), blockState.getBlock().getDrops());
|
2013-12-30 23:45:56 +01:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-06 22:23:19 +01:00
|
|
|
|
|
|
|
/**
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
* The x/y differences to the blocks in a flat cylinder around the center
|
|
|
|
* block, which is excluded.
|
2013-03-06 22:23:19 +01:00
|
|
|
*/
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
private static final int[][] directions = {
|
|
|
|
new int[] {-2, -1}, new int[] {-2, 0}, new int[] {-2, 1},
|
|
|
|
new int[] {-1, -2}, new int[] {-1, -1}, new int[] {-1, 0}, new int[] {-1, 1}, new int[] {-1, 2},
|
|
|
|
new int[] { 0, -2}, new int[] { 0, -1}, new int[] { 0, 1}, new int[] { 0, 2},
|
|
|
|
new int[] { 1, -2}, new int[] { 1, -1}, new int[] { 1, 0}, new int[] { 1, 1}, new int[] { 1, 2},
|
|
|
|
new int[] { 2, -1}, new int[] { 2, 0}, new int[] { 2, 1},
|
|
|
|
};
|
2013-03-06 22:23:19 +01:00
|
|
|
|
|
|
|
/**
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
* Processes Tree Feller in a recursive manner
|
2013-03-06 22:23:19 +01:00
|
|
|
*
|
|
|
|
* @param blockState Block being checked
|
|
|
|
* @param treeFellerBlocks List of blocks to be removed
|
|
|
|
*/
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
/*
|
|
|
|
* Algorithm: An int[][] of X/Z directions is created on static class
|
|
|
|
* initialization, representing a cylinder with radius of about 2 - the
|
|
|
|
* (0,0) center and all (+-2, +-2) corners are omitted.
|
|
|
|
*
|
|
|
|
* handleBlock() returns a boolean, which is used for the sole purpose of
|
|
|
|
* switching between these two behaviors:
|
|
|
|
*
|
|
|
|
* (Call blockState "this log" for the below explanation.)
|
|
|
|
*
|
|
|
|
* [A] There is another log above this log (TRUNK)
|
|
|
|
* Only the flat cylinder in the directions array is searched.
|
|
|
|
* [B] There is not another log above this log (BRANCH AND TOP)
|
|
|
|
* The cylinder in the directions array is extended up and down by 1
|
|
|
|
* block in the Y-axis, and the block below this log is checked as
|
|
|
|
* well. Due to the fact that the directions array will catch all
|
|
|
|
* blocks on a red mushroom, the special method for it is eliminated.
|
|
|
|
*
|
|
|
|
* This algorithm has been shown to achieve a performance of 2-5
|
|
|
|
* milliseconds on regular trees and 10-15 milliseconds on jungle trees
|
|
|
|
* once the JIT has optimized the function (use the ability about 4 times
|
|
|
|
* before taking measurements).
|
|
|
|
*/
|
2013-12-07 08:55:26 +01:00
|
|
|
protected static void processTree(BlockState blockState, Set<BlockState> treeFellerBlocks) {
|
2013-03-06 22:23:19 +01:00
|
|
|
List<BlockState> futureCenterBlocks = new ArrayList<BlockState>();
|
|
|
|
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
// Check the block up and take different behavior (smaller search) if it's a log
|
|
|
|
if (handleBlock(blockState.getBlock().getRelative(BlockFace.UP).getState(), futureCenterBlocks, treeFellerBlocks)) {
|
|
|
|
for (int[] dir : directions) {
|
|
|
|
handleBlock(blockState.getBlock().getRelative(dir[0], 0, dir[1]).getState(), futureCenterBlocks, treeFellerBlocks);
|
2013-03-06 22:23:19 +01:00
|
|
|
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
if (treeFellerReachedThreshold) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Cover DOWN
|
|
|
|
handleBlock(blockState.getBlock().getRelative(BlockFace.DOWN).getState(), futureCenterBlocks, treeFellerBlocks);
|
|
|
|
// Search in a cube
|
|
|
|
for (int y = -1; y <= 1; y++) {
|
|
|
|
for (int[] dir : directions) {
|
|
|
|
handleBlock(blockState.getBlock().getRelative(dir[0], y, dir[1]).getState(), futureCenterBlocks, treeFellerBlocks);
|
|
|
|
|
|
|
|
if (treeFellerReachedThreshold) {
|
2013-03-06 22:23:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursive call for each log found
|
|
|
|
for (BlockState futureCenterBlock : futureCenterBlocks) {
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
if (treeFellerReachedThreshold) {
|
2013-03-06 22:23:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
processTree(futureCenterBlock, treeFellerBlocks);
|
2013-03-06 22:23:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the durability loss
|
|
|
|
*
|
|
|
|
* @param treeFellerBlocks List of blocks to be removed
|
|
|
|
* @param inHand tool being used
|
|
|
|
* @return True if the tool can sustain the durability loss
|
|
|
|
*/
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
protected static boolean handleDurabilityLoss(Set<BlockState> treeFellerBlocks, ItemStack inHand) {
|
2013-03-06 22:23:19 +01:00
|
|
|
short durabilityLoss = 0;
|
2013-11-25 15:29:41 +01:00
|
|
|
Material type = inHand.getType();
|
2013-03-06 22:23:19 +01:00
|
|
|
|
|
|
|
for (BlockState blockState : treeFellerBlocks) {
|
2013-11-06 14:45:20 +01:00
|
|
|
if (BlockUtils.isLog(blockState)) {
|
2013-03-06 22:23:19 +01:00
|
|
|
durabilityLoss += Config.getInstance().getAbilityToolDamage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 20:12:24 +01:00
|
|
|
SkillUtils.handleDurabilityChange(inHand, durabilityLoss);
|
2013-11-25 15:29:41 +01:00
|
|
|
return (inHand.getDurability() < (mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability()));
|
2013-03-06 22:23:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
* Handle a block addition to the list of blocks to be removed and to the
|
|
|
|
* list of blocks used for future recursive calls of
|
|
|
|
* 'processTree()'
|
2013-03-06 22:23:19 +01:00
|
|
|
*
|
|
|
|
* @param blockState Block to be added
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
* @param futureCenterBlocks List of blocks that will be used to call
|
|
|
|
* 'processTree()'
|
2013-03-06 22:23:19 +01:00
|
|
|
* @param treeFellerBlocks List of blocks to be removed
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
* @return true if and only if the given blockState was a Log not already
|
|
|
|
* in treeFellerBlocks.
|
2013-03-06 22:23:19 +01:00
|
|
|
*/
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
private static boolean handleBlock(BlockState blockState, List<BlockState> futureCenterBlocks, Set<BlockState> treeFellerBlocks) {
|
|
|
|
if (treeFellerBlocks.contains(blockState) || mcMMO.getPlaceStore().isTrue(blockState)) {
|
|
|
|
return false;
|
2013-03-06 22:23:19 +01:00
|
|
|
}
|
|
|
|
|
2013-12-07 08:55:26 +01:00
|
|
|
// Without this check Tree Feller propagates through leaves until the threshold is hit
|
2013-03-06 22:23:19 +01:00
|
|
|
if (treeFellerBlocks.size() > treeFellerThreshold) {
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
treeFellerReachedThreshold = true;
|
2013-03-06 22:23:19 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 16:48:55 +01:00
|
|
|
if (BlockUtils.isLog(blockState)) {
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
treeFellerBlocks.add(blockState);
|
2013-03-16 16:48:55 +01:00
|
|
|
futureCenterBlocks.add(blockState);
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (BlockUtils.isLeaves(blockState)) {
|
|
|
|
treeFellerBlocks.add(blockState);
|
|
|
|
return false;
|
2013-03-16 16:48:55 +01:00
|
|
|
}
|
Get all logs in Tree Feller, and optimize performance
Tree Feller has been shown, both anecdotally and with timings, to put a strain on the server, and therefore is worthy of the effort of optimization.
Prior to this change, on jungle trees, Tree Feller would take around 20-40 milliseconds to process a Jungle Tree after the JIT kicked in, and around 15-25 milliseconds for a normal tree.
Additionally, logs would be left up in the air for jungle trees.
After this change, Tree Feller takes 2-5 milliseconds on normal trees, and 10-15 milliseconds on jungle trees, and no logs are left up in the air.
2013-10-14 21:32:12 +02:00
|
|
|
return false;
|
2013-03-06 22:23:19 +01:00
|
|
|
}
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|