baritone/src/main/java/baritone/utils/ToolSet.java

133 lines
4.5 KiB
Java
Raw Normal View History

2018-08-08 03:16:53 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
2018-08-08 03:16:53 +00:00
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
2018-08-08 03:16:53 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2018-08-08 03:16:53 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-08 03:16:53 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 20:15:56 +00:00
package baritone.utils;
2018-08-01 15:34:35 +00:00
import baritone.Baritone;
2018-08-01 15:34:35 +00:00
import net.minecraft.block.Block;
2018-08-01 15:42:42 +00:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Enchantments;
import net.minecraft.init.MobEffects;
2018-08-01 15:34:35 +00:00
import net.minecraft.item.ItemStack;
2018-08-02 17:49:31 +00:00
import java.util.HashMap;
import java.util.Map;
2018-08-01 15:34:35 +00:00
/**
2018-08-02 17:49:31 +00:00
* A cached list of the best tools on the hotbar for any block
2018-08-01 15:34:35 +00:00
*
* @author avecowa, Brady, leijurv
2018-08-01 15:34:35 +00:00
*/
public class ToolSet implements Helper {
2018-08-07 00:43:20 +00:00
/**
2018-09-04 17:06:48 +00:00
* A cache mapping a {@link Block} to how long it will take to break
* with this toolset, given the optimum tool is used.
*/
2018-09-04 17:06:48 +00:00
private Map<Block, Double> breakStrengthCache = new HashMap<>();
2018-08-01 15:42:42 +00:00
2018-08-02 17:49:31 +00:00
/**
* Calculate which tool on the hotbar is best for mining
*
* @param b the blockstate to be mined
* @return a byte indicating the index in the tools array that worked best
*/
public byte getBestSlot(IBlockState b) {
2018-08-01 15:34:35 +00:00
byte best = 0;
double value = -1;
for (byte i = 0; i < 9; i++) {
double v = calculateStrVsBlock(i, b);
2018-08-13 14:14:11 +00:00
if (v > value || value == -1) {
2018-08-01 15:34:35 +00:00
value = v;
best = i;
}
}
2018-08-01 18:05:35 +00:00
return best;
2018-08-01 15:34:35 +00:00
}
2018-08-01 15:42:42 +00:00
2018-08-02 17:49:31 +00:00
/**
* Using the best tool on the hotbar, how long would it take to mine this block
*
* @param state the blockstate to be mined
2018-08-02 17:49:31 +00:00
* @return how long it would take in ticks
*/
public double getStrVsBlock(IBlockState state) {
2018-10-03 14:48:26 +00:00
Double strength = this.breakStrengthCache.get(state.getBlock());
if (strength != null) {
// the function will take this path >99% of the time
return strength;
}
double str = calculateStrVsBlock(getBestSlot(state), state);
this.breakStrengthCache.put(state.getBlock(), str);
return str;
}
/**
* Calculates how long would it take to mine the specified block given the best tool
* in this toolset is used. A negative value is returned if the specified block is unbreakable.
*
* @param state the blockstate to be mined
* @return how long it would take in ticks
*/
private double calculateStrVsBlock(byte slot, IBlockState state) {
// Calculate the slot with the best item
ItemStack contents = player().inventory.getStackInSlot(slot);
float blockHard = state.getBlockHardness(null, null);
2018-09-08 04:32:25 +00:00
if (blockHard < 0) {
return -1;
2018-09-08 04:32:25 +00:00
}
float speed = contents.getDestroySpeed(state);
if (speed > 1) {
int effLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.EFFICIENCY, contents);
if (effLevel > 0 && !contents.isEmpty()) {
speed += effLevel * effLevel + 1;
}
}
if (Baritone.settings().considerPotionEffects.get()) {
if (player().isPotionActive(MobEffects.HASTE)) {
speed *= 1 + (player().getActivePotionEffect(MobEffects.HASTE).getAmplifier() + 1) * 0.2;
}
if (player().isPotionActive(MobEffects.MINING_FATIGUE)) {
switch (player().getActivePotionEffect(MobEffects.MINING_FATIGUE).getAmplifier()) {
case 0:
speed *= 0.3;
break;
case 1:
speed *= 0.09;
break;
case 2:
speed *= 0.0027;
break;
default:
speed *= 0.00081;
break;
}
}
}
speed /= blockHard;
if (state.getMaterial().isToolNotRequired() || (!contents.isEmpty() && contents.canHarvestBlock(state))) {
return speed / 30;
} else {
return speed / 100;
}
}
2018-08-01 15:34:35 +00:00
}