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

117 lines
3.8 KiB
Java
Raw Normal View History

2018-08-01 15:34:35 +00:00
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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;
import net.minecraft.item.ItemStack;
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 15:34:35 +00:00
public ArrayList<Item> tools;
public ArrayList<Byte> slots;
public HashMap<Block, Byte> cache = new HashMap<Block, Byte>();
2018-08-01 15:42:42 +00:00
2018-08-01 15:34:35 +00:00
public ToolSet(ArrayList<Item> tools, ArrayList<Byte> slots) {
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 15:34:35 +00:00
tools = new ArrayList<Item>();
slots = new ArrayList<Byte>();
//Out.log("inv: " + Arrays.toString(inv));
boolean fnull = false;
for (byte i = 0; i < 9; i++) {
2018-08-01 15:42:42 +00:00
if (!fnull || (inv.get(i) != null && inv.get(i).getItem().isItemTool(null))) {
tools.add(inv.get(i) != null ? inv.get(i).getItem() : null);
2018-08-01 15:34:35 +00:00
slots.add(i);
2018-08-01 15:42:42 +00:00
fnull |= inv.get(i) == null || (!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
}
byte best = 0;
//Out.log("best: " + best);
float value = 0;
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]);
float v = item.getStrVsBlock(new ItemStack(item), b);
//Out.log("v: " + v);
if (v > value) {
value = v;
best = i;
}
}
//Out.log("best: " + best);
2018-08-01 15:42:42 +00:00
cache.put(b.getBlock(), best);
2018-08-01 15:34:35 +00:00
return tools.get(best);
}
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
}
byte best = 0;
//Out.log("best: " + best);
float value = 0;
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]);
float v = item.getStrVsBlock(new ItemStack(item), b);
//Out.log("v: " + v);
if (v > value) {
value = v;
best = i;
}
}
//Out.log("best: " + best);
2018-08-01 15:42:42 +00:00
cache.put(b.getBlock(), best);
2018-08-01 15:34:35 +00:00
return slots.get(best);
}
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);
return f < 0.0F ? 0.0F : (!canHarvest(b, item) ? item.getStrVsBlock(new ItemStack(item), b) / f / 100.0F : item.getStrVsBlock(new ItemStack(item), b) / f / 30.0F);
}
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);
}
}
}