baritone/src/main/resources/baritone/util/ToolSet.java

101 lines
3.3 KiB
Java
Raw Normal View History

2018-08-01 15:34:35 +00:00
package baritone.util;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraft.block.Block;
2018-08-01 15:42:42 +00:00
import net.minecraft.block.state.IBlockState;
2018-08-01 15:34:35 +00:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.item.Item;
2018-08-01 18:32:21 +00:00
import net.minecraft.item.ItemAir;
2018-08-01 15:34:35 +00:00
import net.minecraft.item.ItemStack;
2018-08-01 18:05:35 +00:00
import net.minecraft.item.ItemTool;
2018-08-01 15:42:42 +00:00
import net.minecraft.util.NonNullList;
2018-08-01 15:34:35 +00:00
import net.minecraft.util.math.BlockPos;
/**
*
* @author avecowa
*/
public class ToolSet {
2018-08-01 15:42:42 +00:00
2018-08-01 18:05:35 +00:00
public ArrayList<ItemTool> tools;
2018-08-01 15:34:35 +00:00
public ArrayList<Byte> slots;
public HashMap<Block, Byte> cache = new HashMap<Block, Byte>();
2018-08-01 15:42:42 +00:00
2018-08-01 18:05:35 +00:00
public ToolSet(ArrayList<ItemTool> tools, ArrayList<Byte> slots) {
2018-08-01 15:34:35 +00:00
this.tools = tools;
this.slots = slots;
}
2018-08-01 15:42:42 +00:00
2018-08-01 15:34:35 +00:00
public ToolSet() {
EntityPlayerSP p = Minecraft.getMinecraft().player;
2018-08-01 15:42:42 +00:00
NonNullList<ItemStack> inv = p.inventory.mainInventory;
2018-08-01 18:05:35 +00:00
tools = new ArrayList<ItemTool>();
2018-08-01 15:34:35 +00:00
slots = new ArrayList<Byte>();
//Out.log("inv: " + Arrays.toString(inv));
boolean fnull = false;
for (byte i = 0; i < 9; i++) {
2018-08-01 18:32:21 +00:00
if (!fnull || ((!(inv.get(i).getItem() instanceof ItemAir)) && inv.get(i).getItem() instanceof ItemTool)) {
tools.add((!(inv.get(i).getItem() instanceof ItemAir)) ? (ItemTool) inv.get(i).getItem() : null);
2018-08-01 15:34:35 +00:00
slots.add(i);
2018-08-01 18:32:21 +00:00
fnull |= (inv.get(i).getItem() instanceof ItemAir) || (!inv.get(i).getItem().isDamageable());
2018-08-01 15:34:35 +00:00
}
}
}
2018-08-01 15:42:42 +00:00
public Item getBestTool(IBlockState b) {
if (cache.get(b.getBlock()) != null) {
return tools.get(cache.get(b.getBlock()));
2018-08-01 15:34:35 +00:00
}
2018-08-01 18:05:35 +00:00
return tools.get(getBestToolIndex(b));
}
private byte getBestToolIndex(IBlockState b) {
2018-08-01 15:34:35 +00:00
byte best = 0;
//Out.log("best: " + best);
2018-08-01 18:05:35 +00:00
float value = -1;
2018-08-01 15:34:35 +00:00
for (byte i = 0; i < tools.size(); i++) {
Item item = tools.get(i);
if (item == null) {
item = Item.getByNameOrId("minecraft:apple");
}
//Out.log(inv[i]);
2018-08-01 17:50:15 +00:00
2018-08-01 18:05:35 +00:00
float v = item.getDestroySpeed(new ItemStack(item), b);
2018-08-01 15:34:35 +00:00
//Out.log("v: " + v);
2018-08-01 18:05:35 +00:00
if (v < value || value == -1) {
2018-08-01 15:34:35 +00:00
value = v;
best = i;
}
}
//Out.log("best: " + best);
2018-08-01 15:42:42 +00:00
cache.put(b.getBlock(), best);
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
public byte getBestSlot(IBlockState b) {
if (cache.get(b.getBlock()) != null) {
return slots.get(cache.get(b.getBlock()));
2018-08-01 15:34:35 +00:00
}
2018-08-01 18:05:35 +00:00
return slots.get(getBestToolIndex(b));
2018-08-01 15:34:35 +00:00
}
2018-08-01 15:42:42 +00:00
public double getStrVsBlock(IBlockState b, BlockPos pos) {
2018-08-01 15:34:35 +00:00
Item item = this.getBestTool(b);
if (item == null) {
item = Item.getByNameOrId("minecraft:apple");
}
float f = b.getBlockHardness(Minecraft.getMinecraft().world, pos);
2018-08-01 18:05:35 +00:00
return f < 0.0F ? 0.0F : (!canHarvest(b, item) ? item.getDestroySpeed(new ItemStack(item), b) / f / 100.0F : item.getDestroySpeed(new ItemStack(item), b) / f / 30.0F);
2018-08-01 15:34:35 +00:00
}
2018-08-01 15:42:42 +00:00
public boolean canHarvest(IBlockState blockIn, Item item) {
2018-08-01 15:34:35 +00:00
if (blockIn.getMaterial().isToolNotRequired()) {
return true;
} else {
return new ItemStack(item).canHarvestBlock(blockIn);
}
}
}