public boolean switchTools(ToolType tool) {
    MainPlayerEntity player = world.getBot().getPlayer();
    if (player == null) return false;
    PlayerInventory inventory = player.getInventory();

    ItemStack bestTool = null;
    int bestToolSlot = -1, bestToolValue = -1;
    for (int i = 0; i < 36; i++) {
      ItemStack item = inventory.getItemAt(i);
      if (tool == null) {
        if (i > 8) break;
        if (item == null || ToolType.getById(item.getId()) == null) {
          bestTool = item;
          bestToolSlot = i;
          break;
        }
        continue;
      }
      if (item == null) continue;
      ToolType toolType = ToolType.getById(item.getId());
      if (toolType == null || toolType != tool) continue;
      int toolValue = getToolPriority(item.getId());
      if (bestTool == null || toolValue > bestToolValue) {
        bestTool = item;
        bestToolSlot = i;
        bestToolValue = toolValue;
      }
    }
    if (bestToolSlot == -1) return false;
    return switchHeldItems(bestToolSlot);
  }
 public boolean switchHeldItems(int newSlot) {
   if (inventory.getCurrentHeldSlot() == newSlot) return true;
   if (newSlot > 8) {
     int hotbarSpace = 9;
     for (int hotbarIndex = 0; hotbarIndex < 9; hotbarIndex++) {
       ItemStack item = inventory.getItemAt(hotbarIndex);
       if (item == null) {
         hotbarSpace = hotbarIndex;
         break;
       } else if (ToolType.getById(item.getId()) == null && hotbarIndex < hotbarSpace)
         hotbarSpace = hotbarIndex;
     }
     if (hotbarSpace == 9) hotbarSpace = 0;
     inventory.selectItemAt(newSlot);
     inventory.selectItemAt(hotbarSpace);
     if (inventory.getSelectedItem() != null) inventory.selectItemAt(newSlot);
     inventory.close();
     newSlot = hotbarSpace;
   }
   inventory.setCurrentHeldSlot(newSlot);
   return true;
 }